diff --git a/apps/www/DESIGN.md b/apps/www/DESIGN.md index 8282d8d03..fadd0a868 100644 --- a/apps/www/DESIGN.md +++ b/apps/www/DESIGN.md @@ -118,6 +118,6 @@ Everything above describes the tech mode. Pages under `/hobby` (films, series, a - **The sentence is the navigation.** The page opens with one sentence whose nouns (films, series, anime, games) are links that narrow the list to that kind, and widen it again when current. - **A work opens as a sheet.** Choosing a line opens the work over the list in a sheet that covers the screen but for a margin. Its key art fills the whole sheet and stays put while the content scrolls; over it lies the page colour as a wash that is thin at the top and never opaque, thickened only on the side the text stands on, so the art reads through all of it. On that sit the cover, the title at display size, a short comment, then the facts as labelled values (status, a rating out of ten drawn as five stars, dates; for a game, hours played, achievements with a thin meter, last played). A work with a cover but no key art uses the cover, blurred, for its colours. The sheet has its own URL; opened directly, the same content is a page whose art fills the viewport behind the wordmark. The cover is capped by the height of its surface, so the work reads without scrolling in a short window. - **The cover is a lit card.** It tilts towards the pointer under studio light: a band of light sweeps across as it turns, a glare follows the pointer and the bevelled edge catches both. It sweeps once when the sheet opens, draws only while something moves, and holds still under reduced motion. -- **Chrome stays out of the way.** The wordmark is plain text at the top, beside the portrait that switches mode; the chat launcher and command menu float in the bottom-left corner; theme, language and settings close the page as one row. -- **Where the shader cannot run** (no WebGPU), the key art itself stands in behind the page at low opacity, and the colour still follows the work. With reduced motion the field holds still. +- **Chrome stays out of the way.** The wordmark is plain text at the top, beside the portrait that switches mode; the command menu floats in the bottom-left corner; theme, language and settings close the page as one row. +- **Where the shader cannot run** (no WebGPU), the key art itself stands in behind the page at low opacity, and the colour still follows the work. With reduced motion the field holds still, and so do the field and the card when the reader reduces effects in settings, which is the default on touch devices. - **Reader palette does not apply.** The palette settings recolour the tech mode only. diff --git a/apps/www/__tests__/hooks/use-reduced-effects.test.ts b/apps/www/__tests__/hooks/use-reduced-effects.test.ts new file mode 100644 index 000000000..138dc1fd4 --- /dev/null +++ b/apps/www/__tests__/hooks/use-reduced-effects.test.ts @@ -0,0 +1,47 @@ +import { renderHook } from "@testing-library/react"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { useReducedEffects } from "@/hooks/use-reduced-effects"; +import { useSettingsStore } from "@/stores/settings/store"; + +const stubHover = (canHover: boolean) => { + vi.stubGlobal("matchMedia", (query: string) => ({ + matches: canHover, + media: query, + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + })); +}; + +describe("useReducedEffects", () => { + beforeEach(() => { + useSettingsStore.setState({ reducedEffects: null }); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it("is off by default where the pointer hovers", () => { + stubHover(true); + expect(renderHook(() => useReducedEffects()).result.current).toBe(false); + }); + + it("is on by default on a touch device", () => { + stubHover(false); + expect(renderHook(() => useReducedEffects()).result.current).toBe(true); + }); + + it.each([ + [true, true], + [false, false], + ])("the reader's choice of %s wins on either device", (choice, expected) => { + useSettingsStore.getState().setReducedEffects(choice); + for (const canHover of [true, false]) { + stubHover(canHover); + expect(renderHook(() => useReducedEffects()).result.current).toBe( + expected + ); + } + }); +}); diff --git a/apps/www/src/components/agent/chat-dock.tsx b/apps/www/src/components/agent/chat-dock.tsx index 8de2d8983..ae73d5e93 100644 --- a/apps/www/src/components/agent/chat-dock.tsx +++ b/apps/www/src/components/agent/chat-dock.tsx @@ -12,6 +12,8 @@ import { DockActions, DockMode, DockShell } from "@chia/ui/dock"; import { cn } from "@chia/ui/utils/cn.util"; import { CHBot } from "@/components/commons/ch-bot"; +import { usePathname } from "@/libs/i18n/navigation"; +import { SiteMode, siteModeOf } from "@/shared/site-mode"; import { useChatDockStore } from "@/stores/chat-dock/store"; import { useSettingsStore } from "@/stores/settings/store"; @@ -37,6 +39,10 @@ const DEFAULT_WIDTH = 384; export const ChatDock = () => { const t = useTranslations("chbot"); const aiEnabled = useSettingsStore((state) => state.aiEnabled); + const pathname = usePathname(); + // The agent is not offered in the hobby mode: no launcher there, and a dock left open in the + // tech mode steps aside until the reader is back. + const isOffered = aiEnabled && siteModeOf(pathname) !== SiteMode.Hobby; const mode = useChatDockStore((state) => state.mode); const setMode = useChatDockStore((state) => state.setMode); const toggle = useChatDockStore((state) => state.toggle); @@ -48,9 +54,9 @@ export const ChatDock = () => { void useChatDockStore.persist.rehydrate(); }, []); - useHotkey("Mod+I", toggle, { enabled: aiEnabled }); + useHotkey("Mod+I", toggle, { enabled: isOffered }); - if (!aiEnabled) { + if (!isOffered) { return null; } diff --git a/apps/www/src/components/commons/footer.tsx b/apps/www/src/components/commons/footer.tsx index 0f31aae32..f0d9c5bb3 100644 --- a/apps/www/src/components/commons/footer.tsx +++ b/apps/www/src/components/commons/footer.tsx @@ -117,7 +117,7 @@ const Footer: FC<{ locale?: Locale }> = ({ locale: _locale }) => { }} /> - + diff --git a/apps/www/src/components/commons/settings.tsx b/apps/www/src/components/commons/settings.tsx index 62c76955c..3f36498f2 100644 --- a/apps/www/src/components/commons/settings.tsx +++ b/apps/www/src/components/commons/settings.tsx @@ -1,25 +1,90 @@ "use client"; import { startTransition } from "react"; +import type { ReactNode } from "react"; import { Button, Description, Label, Modal, Switch } from "@heroui/react"; import { useTranslations } from "next-intl"; import { PaletteSettings } from "@/components/commons/palette-settings"; +import { useReducedEffects } from "@/hooks/use-reduced-effects"; +import { SiteMode } from "@/shared/site-mode"; import { useSettingsStore } from "@/stores/settings/store"; -/** `palette` is off in a mode that brings its own colours, where the reader palette does not apply. */ -export const Settings = ({ palette = true }: { palette?: boolean }) => { +const Toggle = ({ + label, + description, + isSelected, + onChange, +}: { + label: string; + description: ReactNode; + isSelected: boolean; + onChange: (isSelected: boolean) => void; +}) => ( + startTransition(() => onChange(next))} + aria-label={label}> + + + + +
+ + {description} +
+
+
+); + +const TechSettings = () => { const t = useTranslations("settings"); const aiEnabled = useSettingsStore((s) => s.aiEnabled); const setAiEnabled = useSettingsStore((s) => s.setAiEnabled); + return ( + <> + +
+ +
+ + ); +}; + +const HobbySettings = () => { + const t = useTranslations("settings"); + const reducedEffects = useReducedEffects(); + const setReducedEffects = useSettingsStore((s) => s.setReducedEffects); + return ( + + ); +}; + +/** + * Each mode lists what applies to it: the agent and the reader palette exist only in the tech + * mode, the effects only in the hobby mode. + */ +export const Settings = ({ mode }: { mode: SiteMode }) => { + const t = useTranslations("settings"); return ( - {/* The page stays visible so a colour change can be judged against it. */} + {/* The page stays visible so a change can be judged against it. */} @@ -28,28 +93,7 @@ export const Settings = ({ palette = true }: { palette?: boolean }) => { {t("title")} - - startTransition(() => setAiEnabled(isSelected)) - } - aria-label={t("aiEnabled")}> - - - - -
- - {t("aiEnabledDescription")} -
-
-
- {palette ? ( -
- -
- ) : null} + {mode === SiteMode.Tech ? : }
diff --git a/apps/www/src/components/hobby/hobby-footer.tsx b/apps/www/src/components/hobby/hobby-footer.tsx index 19f8bfde1..1664b3029 100644 --- a/apps/www/src/components/hobby/hobby-footer.tsx +++ b/apps/www/src/components/hobby/hobby-footer.tsx @@ -11,6 +11,7 @@ import { Copyright } from "@/components/commons/copyright"; import { LoadingSkeleton } from "@/components/commons/current-playing"; import LocaleSelector from "@/components/commons/locale-selector"; import { Settings } from "@/components/commons/settings"; +import { SiteMode } from "@/shared/site-mode"; const CurrentPlaying = dynamic( () => @@ -56,7 +57,7 @@ export const HobbyFooter = () => { }} /> - + ); diff --git a/apps/www/src/components/hobby/hobby-frame.tsx b/apps/www/src/components/hobby/hobby-frame.tsx index 650ad6439..3a3c8e9c7 100644 --- a/apps/www/src/components/hobby/hobby-frame.tsx +++ b/apps/www/src/components/hobby/hobby-frame.tsx @@ -2,7 +2,6 @@ import type { ReactNode } from "react"; import type { Locale } from "next-intl"; -import { ChatLauncher } from "@/components/agent/chat-launcher"; import { CommandMenu } from "@/components/commons/command-menu"; import { ModeSwitch } from "@/components/commons/mode-switch"; import { HobbyFooter } from "@/components/hobby/hobby-footer"; @@ -42,8 +41,7 @@ const HobbyFrame = ({ {/* What a header would hold, kept out of the way. No backdrop blur: the field redraws under it. */}
- + className="bg-background/85 fixed bottom-4 left-(--hobby-gutter) z-50 flex items-center gap-1 rounded-xl p-1">
diff --git a/apps/www/src/components/hobby/hobby-stage.tsx b/apps/www/src/components/hobby/hobby-stage.tsx index 952f9e45e..fd87afc91 100644 --- a/apps/www/src/components/hobby/hobby-stage.tsx +++ b/apps/www/src/components/hobby/hobby-stage.tsx @@ -8,6 +8,7 @@ import { AsciiField, AsciiFieldStatus } from "@chia/shaders/ascii-field"; import { cn } from "@chia/ui/utils/cn.util"; import useTheme, { Theme } from "@chia/ui/utils/use-theme"; +import { useReducedEffects } from "@/hooks/use-reduced-effects"; import { readableImageSourceOf } from "@/libs/image-source"; import { HOBBY_IDLE_COLOR } from "@/shared/media"; import { useHobbyStore } from "@/stores/hobby/store"; @@ -94,11 +95,13 @@ export const HobbyStage = () => { const focus = useSettledFocus(); const pinned = useHobbyStore((state) => state.pinned); const color = (pinned ?? focus)?.color ?? null; - const backdrop = useBackdrop(pinned ? null : (focus?.backdropUrl ?? null)); + // Kept while a work is open: dropping it would revoke a blob the shader may still be decoding. + const backdrop = useBackdrop(focus?.backdropUrl ?? null); const [status, setStatus] = useState( AsciiFieldStatus.Pending ); const isDark = resolvedTheme === Theme.Dark; + const reducedEffects = useReducedEffects(); useEffect(() => { const { style } = document.documentElement; @@ -131,6 +134,7 @@ export const HobbyStage = () => { color={toneOf(color ?? HOBBY_IDLE_COLOR, isDark)} cellSize={10} paused={pinned !== null} + reducedMotion={reducedEffects} onStatusChange={setStatus} className={cn( "absolute inset-0 transition-opacity duration-700", diff --git a/apps/www/src/components/hobby/media-cover.tsx b/apps/www/src/components/hobby/media-cover.tsx new file mode 100644 index 000000000..ec2132bf2 --- /dev/null +++ b/apps/www/src/components/hobby/media-cover.tsx @@ -0,0 +1,33 @@ +"use client"; + +import Image from "next/image"; + +import { ReflectiveCard } from "@chia/shaders/reflective-card"; + +import { useReducedEffects } from "@/hooks/use-reduced-effects"; + +/** A work's cover under studio light, which holds still when the reader's effects are reduced. */ +export const MediaCover = ({ + src, + className, +}: { + src: string; + className?: string; +}) => { + const reducedEffects = useReducedEffects(); + return ( + + + + ); +}; diff --git a/apps/www/src/components/hobby/media-detail.tsx b/apps/www/src/components/hobby/media-detail.tsx index cb6e1b7f5..fd25089f7 100644 --- a/apps/www/src/components/hobby/media-detail.tsx +++ b/apps/www/src/components/hobby/media-detail.tsx @@ -4,9 +4,9 @@ import type { ReactNode } from "react"; import { getFormatter, getLocale, getTranslations } from "next-intl/server"; import { MediaKind } from "@chia/db/types"; -import { ReflectiveCard } from "@chia/shaders/reflective-card"; import { cn } from "@chia/ui/utils/cn.util"; +import { MediaCover } from "@/components/hobby/media-cover"; import { MediaPin } from "@/components/hobby/media-pin"; import { StarRating } from "@/components/hobby/star-rating"; import { dbLocaleResolver } from "@/libs/utils/i18n"; @@ -127,19 +127,11 @@ export const MediaDetail = async ({ : "pt-6 pb-10" )}> {entry.posterUrl ? ( - - - + className="aspect-2/3 h-[min(13.5rem,40cqh)] shadow-lg md:h-[min(21rem,42cqh)]" + /> ) : null}
diff --git a/apps/www/src/components/hobby/media-list.tsx b/apps/www/src/components/hobby/media-list.tsx index 52ad76b4e..5fd0da036 100644 --- a/apps/www/src/components/hobby/media-list.tsx +++ b/apps/www/src/components/hobby/media-list.tsx @@ -1,19 +1,12 @@ "use client"; -import { useEffect, useRef, useSyncExternalStore } from "react"; +import { useEffect, useRef } from "react"; +import { useCanHover } from "@/hooks/use-can-hover"; import { Link } from "@/libs/i18n/navigation"; import type { MediaRow } from "@/shared/media"; import { useHobbyStore } from "@/stores/hobby/store"; -const HOVER_QUERY = "(hover: hover)"; - -const subscribeHover = (onChange: () => void) => { - const query = window.matchMedia(HOVER_QUERY); - query.addEventListener("change", onChange); - return () => query.removeEventListener("change", onChange); -}; - /** A row is in focus on a touch screen while it crosses this band in the middle of the viewport. */ const CENTRE_BAND = "-45% 0px -45% 0px"; @@ -27,11 +20,7 @@ const ROW_CLASS_NAME = export const MediaList = ({ rows }: { rows: readonly MediaRow[] }) => { const focusId = useHobbyStore((state) => state.focus?.id ?? null); const setFocus = useHobbyStore((state) => state.setFocus); - const canHover = useSyncExternalStore( - subscribeHover, - () => window.matchMedia(HOVER_QUERY).matches, - () => true - ); + const canHover = useCanHover(); const list = useRef(null); useEffect(() => { diff --git a/apps/www/src/hooks/use-can-hover.ts b/apps/www/src/hooks/use-can-hover.ts new file mode 100644 index 000000000..b4e91876f --- /dev/null +++ b/apps/www/src/hooks/use-can-hover.ts @@ -0,0 +1,19 @@ +"use client"; + +import { useSyncExternalStore } from "react"; + +const HOVER_QUERY = "(hover: hover)"; + +const subscribe = (onChange: () => void) => { + const query = window.matchMedia(HOVER_QUERY); + query.addEventListener("change", onChange); + return () => query.removeEventListener("change", onChange); +}; + +/** Whether the primary input can hover: false on phones and tablets. The server assumes it can. */ +export const useCanHover = () => + useSyncExternalStore( + subscribe, + () => window.matchMedia(HOVER_QUERY).matches, + () => true + ); diff --git a/apps/www/src/hooks/use-reduced-effects.ts b/apps/www/src/hooks/use-reduced-effects.ts new file mode 100644 index 000000000..ccd7b12b1 --- /dev/null +++ b/apps/www/src/hooks/use-reduced-effects.ts @@ -0,0 +1,14 @@ +"use client"; + +import { useCanHover } from "@/hooks/use-can-hover"; +import { useSettingsStore } from "@/stores/settings/store"; + +/** + * Whether the hobby mode's effects hold still: the reader's choice, and until they make one, on + * for touch devices, where a full-screen shader costs the most and has no pointer to follow. + */ +export const useReducedEffects = () => { + const preference = useSettingsStore((state) => state.reducedEffects); + const canHover = useCanHover(); + return preference ?? !canHover; +}; diff --git a/apps/www/src/stores/settings/store.ts b/apps/www/src/stores/settings/store.ts index f4d7aac1d..6f3d3313f 100644 --- a/apps/www/src/stores/settings/store.ts +++ b/apps/www/src/stores/settings/store.ts @@ -15,6 +15,11 @@ export interface SettingsState { agentSessionId: string | null; /** Reader overrides of the theme's colours, one set per mode. */ palette: Palette; + /** + * Holds the hobby mode's effects still. `null` until the reader chooses, which follows the + * device: see `useReducedEffects`. + */ + reducedEffects: boolean | null; } /** A palette that does not parse is dropped on its own, keeping the rest of the settings. */ @@ -31,6 +36,7 @@ const persistedSettingsSchema = z [PaletteMode.Light]: modePaletteSchema, [PaletteMode.Dark]: modePaletteSchema, }), + reducedEffects: z.boolean().nullable(), }) .partial(); @@ -44,6 +50,7 @@ export interface SettingsActions { color: string | null ) => void; resetPalette: (mode: PaletteMode) => void; + setReducedEffects: (reduced: boolean) => void; } export type SettingsStore = SettingsState & SettingsActions; @@ -55,6 +62,7 @@ const defaultState: SettingsState = { [PaletteMode.Light]: {}, [PaletteMode.Dark]: {}, }, + reducedEffects: null, }; export const useSettingsStore = create()( @@ -76,6 +84,7 @@ export const useSettingsStore = create()( }), resetPalette: (mode) => set((state) => ({ palette: { ...state.palette, [mode]: {} } })), + setReducedEffects: (reduced) => set({ reducedEffects: reduced }), }), { name: SETTINGS_STORAGE_KEY, @@ -93,6 +102,7 @@ export const useSettingsStore = create()( aiEnabled: state.aiEnabled, agentSessionId: state.agentSessionId, palette: state.palette, + reducedEffects: state.reducedEffects, }), } ) diff --git a/packages/i18n/www/en-US.json b/packages/i18n/www/en-US.json index 117f4468a..4fc01cbdb 100644 --- a/packages/i18n/www/en-US.json +++ b/packages/i18n/www/en-US.json @@ -325,6 +325,8 @@ "open": "Open settings", "aiEnabled": "Enable AI features", "aiEnabledDescription": "Allow AI-powered features on this site.", + "reducedEffects": "Reduce motion", + "reducedEffectsDescription": "Holds the backdrop and cover effects still, which is lighter on a phone. On by default on touch devices.", "done": "Done", "palette": { "title": "Colors", diff --git a/packages/i18n/www/zh-TW.json b/packages/i18n/www/zh-TW.json index 01612acc7..98dbc5451 100644 --- a/packages/i18n/www/zh-TW.json +++ b/packages/i18n/www/zh-TW.json @@ -325,6 +325,8 @@ "open": "開啟設定", "aiEnabled": "啟用 AI 功能", "aiEnabledDescription": "允許在本站使用 AI 相關功能。", + "reducedEffects": "減少動畫", + "reducedEffectsDescription": "讓背景與封面的特效保持靜止,在行動裝置上更省資源。觸控裝置預設開啟。", "done": "完成", "palette": { "title": "顏色", diff --git a/packages/shaders/src/ascii-field.tsx b/packages/shaders/src/ascii-field.tsx index 127fb2c93..dd436f217 100644 --- a/packages/shaders/src/ascii-field.tsx +++ b/packages/shaders/src/ascii-field.tsx @@ -43,6 +43,8 @@ interface AsciiFieldProps extends ComponentPropsWithoutRef<"div"> { maxDensity?: number; /** Stops the field's loop and hides its trail; a new `image` or `color` still redraws it once. */ paused?: boolean; + /** Renders as it does under the system's reduced-motion setting: no drift, no trail, no fades. */ + reducedMotion?: boolean; /** Told once the shader either paints or cannot run here, so the consumer can show a fallback. */ onStatusChange?: (status: AsciiFieldStatus) => void; } @@ -59,6 +61,7 @@ export const AsciiField = ({ cellSize = 14, maxDensity = 1.5, paused = false, + reducedMotion = false, onStatusChange, className, ...props @@ -85,7 +88,7 @@ export const AsciiField = ({ characters={characters} cellSize={cellSize} maxDensity={maxDensity} - animating={!reduceMotion} + animating={!reduceMotion && !reducedMotion} paused={paused} onReady={() => report(AsciiFieldStatus.Ready)} onUnavailable={() => report(AsciiFieldStatus.Unavailable)} diff --git a/packages/shaders/src/reflective-card.tsx b/packages/shaders/src/reflective-card.tsx index 9796409ba..9426c55e2 100644 --- a/packages/shaders/src/reflective-card.tsx +++ b/packages/shaders/src/reflective-card.tsx @@ -304,6 +304,8 @@ interface ReflectiveCardProps { children: ReactNode; /** Corner radius in CSS pixels; the bevel follows it. */ radius?: number; + /** Renders as it does under the system's reduced-motion setting: no tilt, no opening sweep. */ + reducedMotion?: boolean; className?: string; } @@ -315,6 +317,7 @@ interface ReflectiveCardProps { export const ReflectiveCard = ({ children, radius = 12, + reducedMotion = false, className, }: ReflectiveCardProps) => { const root = useRef(null); @@ -332,9 +335,9 @@ export const ReflectiveCard = ({ const renderer = createRenderer(canvasElement); if (!renderer) return; - const reduced = window.matchMedia( - "(prefers-reduced-motion: reduce)" - ).matches; + const reduced = + reducedMotion || + window.matchMedia("(prefers-reduced-motion: reduce)").matches; const tilt = reduced ? 0 : TILT_DEGREES; const x: Spring = { position: 0, velocity: 0, target: 0 }; const y: Spring = { position: 0, velocity: 0, target: 0 }; @@ -526,7 +529,7 @@ export const ReflectiveCard = ({ bodyElement.style.transform = ""; renderer.dispose(); }; - }, [generation, radius]); + }, [generation, radius, reducedMotion]); return (