feat: add collaborative shopping lists with items and invitations
Some checks failed
CI / Verify (push) Has been cancelled
Some checks failed
CI / Verify (push) Has been cancelled
API: lists, items and invitations modules (list, create, get, update, set-primary, items create/list, invitation create/accept/list-pending), BetterAuth session helper, list membership, mailer and Prisma schema plus migrations. Web: lists overview, list detail (items) and list management (settings) views, item and invitation forms/hooks, sign-up and invitation accept pages. Shared: shopping list DTOs. Tooling: ignore the generated TanStack Router cache (apps/web/.tanstack).
This commit is contained in:
@@ -1,11 +1,84 @@
|
||||
import { ThemeProvider as NextThemesProvider } from 'next-themes'
|
||||
import type { ComponentProps } from 'react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
|
||||
|
||||
/** Wraps `next-themes` so the theme is toggled by a class on `<html>`. */
|
||||
export function ThemeProvider({ children, ...props }: ComponentProps<typeof NextThemesProvider>) {
|
||||
return (
|
||||
<NextThemesProvider attribute="class" enableSystem disableTransitionOnChange {...props}>
|
||||
{children}
|
||||
</NextThemesProvider>
|
||||
)
|
||||
export type Theme = 'light' | 'dark' | 'system'
|
||||
|
||||
type ResolvedTheme = Exclude<Theme, 'system'>
|
||||
|
||||
type ThemeContextValue = {
|
||||
/** The preference the user picked, `system` included. */
|
||||
theme: Theme
|
||||
/** The preference already resolved against the OS setting. */
|
||||
resolvedTheme: ResolvedTheme
|
||||
setTheme: (theme: Theme) => void
|
||||
}
|
||||
|
||||
const STORAGE_KEY = 'superlist-theme'
|
||||
const DARK_QUERY = '(prefers-color-scheme: dark)'
|
||||
|
||||
/**
|
||||
* This theme handling is hand-rolled on purpose. Libraries such as `next-themes`
|
||||
* avoid the pre-paint flash by rendering a `<script>` from React, but React 19
|
||||
* never executes scripts rendered by components (it even logs a warning). Since
|
||||
* this app is client-rendered, the flash is prevented by the static script in
|
||||
* `index.html`, which runs before React mounts and uses this same storage key
|
||||
* and `.dark` class.
|
||||
*/
|
||||
const ThemeContext = createContext<ThemeContextValue>({
|
||||
theme: 'system',
|
||||
resolvedTheme: 'light',
|
||||
setTheme: () => {},
|
||||
})
|
||||
|
||||
function readStoredTheme(): Theme {
|
||||
try {
|
||||
const stored = window.localStorage.getItem(STORAGE_KEY)
|
||||
if (stored === 'light' || stored === 'dark' || stored === 'system') return stored
|
||||
} catch {
|
||||
// Storage can be unavailable (private mode, blocked cookies); fall back to system.
|
||||
}
|
||||
return 'system'
|
||||
}
|
||||
|
||||
function systemTheme(): ResolvedTheme {
|
||||
return window.matchMedia(DARK_QUERY).matches ? 'dark' : 'light'
|
||||
}
|
||||
|
||||
export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||
const [theme, setThemeState] = useState<Theme>(readStoredTheme)
|
||||
const [systemDark, setSystemDark] = useState(() => systemTheme() === 'dark')
|
||||
const resolvedTheme: ResolvedTheme = theme === 'system' ? (systemDark ? 'dark' : 'light') : theme
|
||||
|
||||
useEffect(() => {
|
||||
const media = window.matchMedia(DARK_QUERY)
|
||||
const onChange = (event: MediaQueryListEvent) => setSystemDark(event.matches)
|
||||
media.addEventListener('change', onChange)
|
||||
return () => media.removeEventListener('change', onChange)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const root = document.documentElement
|
||||
root.classList.toggle('dark', resolvedTheme === 'dark')
|
||||
root.style.colorScheme = resolvedTheme
|
||||
}, [resolvedTheme])
|
||||
|
||||
const setTheme = useCallback((next: Theme) => {
|
||||
setThemeState(next)
|
||||
try {
|
||||
window.localStorage.setItem(STORAGE_KEY, next)
|
||||
} catch {
|
||||
// Ignore storage failures: the theme still applies for this session.
|
||||
}
|
||||
}, [])
|
||||
|
||||
const value = useMemo(
|
||||
() => ({ theme, resolvedTheme, setTheme }),
|
||||
[theme, resolvedTheme, setTheme],
|
||||
)
|
||||
|
||||
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
return useContext(ThemeContext)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user