Skip to content

Commit

Permalink
playground: support dark theme (#1552)
Browse files Browse the repository at this point in the history
at least you don't get a flashbang anymore

![image](https://github.com/user-attachments/assets/a08b01b3-6771-4b23-a917-8cd5a696c33f)
  • Loading branch information
miguel-nascimento authored Nov 18, 2024
1 parent c50f944 commit 1a6eea7
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/playground/src/components/blocks/timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ const Reaction = ({
<button
type="button"
className={cn(
'flex h-8 w-full items-center justify-center gap-2 rounded-sm border border-neutral-200 bg-neutral-100 px-2',
isMyReaction && 'border-lime-200 bg-lime-100',
'flex h-8 w-full items-center justify-center gap-2 rounded-sm border border-neutral-200 bg-neutral-100 px-2 dark:border-neutral-800 dark:bg-neutral-900',
isMyReaction && 'border-lime-200 bg-lime-100 dark:border-lime-800 dark:bg-lime-900',
)}
onClick={() => {
if (isMyReaction) {
Expand Down
6 changes: 3 additions & 3 deletions packages/playground/src/components/layout/grid-side-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export const GridSidePanel = ({
side?: React.ReactNode
}) => {
return (
<main className="grid flex-1 grid-cols-[1fr_3fr] bg-zinc-50">
<aside className="flex max-h-[calc(100dvh-64px)] w-full flex-col gap-4 overflow-y-auto border-r border-zinc-200 bg-zinc-50 px-4 py-2">
<main className="grid flex-1 grid-cols-[1fr_3fr] bg-zinc-50 dark:bg-zinc-900">
<aside className="flex max-h-[calc(100dvh-64px)] w-full flex-col gap-4 overflow-y-auto border-r border-zinc-200 bg-zinc-50 px-4 py-2 dark:border-zinc-800 dark:bg-zinc-900">
{side}
</aside>
<section className="flex max-h-[calc(100dvh-64px)] w-full flex-col gap-4 overflow-y-auto bg-zinc-50 px-4 py-2">
<section className="flex max-h-[calc(100dvh-64px)] w-full flex-col gap-4 overflow-y-auto bg-zinc-50 px-4 py-2 dark:bg-zinc-900">
{main}
</section>
</main>
Expand Down
5 changes: 3 additions & 2 deletions packages/playground/src/components/layout/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import { RiverBeaver } from '@/components/river-beaver'
import { RiverEnvSwitcher } from '../dialog/env-switcher'
import { Button } from '../ui/button'
import { UpdateMetadata } from '../form/metadata/update'
import { Avatar } from '../ui/avatar'

export const LayoutHeader = () => {
const location = useLocation()
const isAuthRoute = location.pathname.startsWith('/auth')
const { isAgentConnected } = useAgentConnection()

return (
<header className="flex justify-between border-b border-zinc-200 px-4 py-3">
<header className="flex justify-between border-b border-zinc-200 px-4 py-3 dark:border-zinc-800">
<div className="flex items-center gap-2">
<RiverBeaver />
<h1 className="text-2xl font-bold">River Playground</h1>
Expand All @@ -41,7 +42,7 @@ const Profile = () => {
return (
<Sheet>
<SheetTrigger>
<img src="/pp1.png" alt="profile" className="h-8 w-8 rounded-full" />
<Avatar userId={user.id} className="h-8 w-8" />
</SheetTrigger>
<SheetContent side="right" className="flex flex-col justify-between">
<div className="flex flex-col gap-2">
Expand Down
73 changes: 73 additions & 0 deletions packages/playground/src/components/theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { createContext, useContext, useEffect, useState } from 'react'

type Theme = 'dark' | 'light' | 'system'

type ThemeProviderProps = {
children: React.ReactNode
defaultTheme?: Theme
storageKey?: string
}

type ThemeProviderState = {
theme: Theme
setTheme: (theme: Theme) => void
}

const initialState: ThemeProviderState = {
theme: 'system',
setTheme: () => null,
}

const ThemeProviderContext = createContext<ThemeProviderState>(initialState)

export function ThemeProvider({
children,
defaultTheme = 'system',
storageKey = 'ui-theme',
...props
}: ThemeProviderProps) {
const [theme, setTheme] = useState<Theme>(
() => (localStorage.getItem(storageKey) as Theme) || defaultTheme,
)

useEffect(() => {
const root = window.document.documentElement

root.classList.remove('light', 'dark')

if (theme === 'system') {
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light'

root.classList.add(systemTheme)
return
}

root.classList.add(theme)
}, [theme])

const value = {
theme,
setTheme: (theme: Theme) => {
localStorage.setItem(storageKey, theme)
setTheme(theme)
},
}

return (
<ThemeProviderContext.Provider {...props} value={value}>
{children}
</ThemeProviderContext.Provider>
)
}

export const useTheme = () => {
const context = useContext(ThemeProviderContext)

if (context === undefined) {
throw new Error('useTheme must be used within a ThemeProvider')
}

return context
}
10 changes: 7 additions & 3 deletions packages/playground/src/components/ui/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export type BlockProps = {
export const Block = ({ children, title, variant = 'primary', className }: BlockProps) => {
return (
<div className={blockVariants({ variant, className })}>
{title && <h2 className="text-sm font-semibold text-primary text-zinc-700">{title}</h2>}
{title && (
<h2 className="text-sm font-semibold text-primary text-zinc-700 dark:text-zinc-300">
{title}
</h2>
)}
{children}
</div>
)
Expand All @@ -19,8 +23,8 @@ export const Block = ({ children, title, variant = 'primary', className }: Block
const blockVariants = cva('flex flex-col gap-2 rounded-sm border p-4', {
variants: {
variant: {
primary: 'border-zinc-300 bg-zinc-50',
secondary: 'border-zinc-200 bg-zinc-100',
primary: 'border-zinc-300 bg-zinc-50 dark:border-zinc-700 dark:bg-zinc-900',
secondary: 'border-zinc-200 bg-zinc-100 dark:border-zinc-800 dark:bg-zinc-900',
},
},
defaultVariants: {
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/routes/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const AuthRoute = () => {
<div className="flex flex-col items-center gap-6">
<div className="max-w-lg space-y-4">
<h1 className="text-center text-2xl font-bold">Connect to River</h1>
<p className="text-center text-sm text-zinc-500">
<p className="text-center text-sm text-zinc-500 dark:text-zinc-400">
You can use a bearer token, or connect straight to the network using your
wallet.
</p>
Expand Down
15 changes: 9 additions & 6 deletions packages/playground/src/routes/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Outlet } from 'react-router-dom'
import { Suspense } from 'react'
import { LayoutHeader } from '@/components/layout/header'
import { ThemeProvider } from '@/components/theme-provider'

export const RootLayout = () => {
return (
<div className="flex h-[100dvh] flex-col">
<LayoutHeader />
<Suspense>
<Outlet />
</Suspense>
</div>
<ThemeProvider defaultTheme="system">
<div className="flex h-[100dvh] flex-col">
<LayoutHeader />
<Suspense>
<Outlet />
</Suspense>
</div>
</ThemeProvider>
)
}

0 comments on commit 1a6eea7

Please sign in to comment.