The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Previews: a card can rest on its peak, wait for a hover, or pause
Adarshaeebf8d1cb73
diff --git a/src/components/preset-gallery.tsx b/src/components/preset-gallery.tsxindex 7620a1f5..328e9f32 100644--- a/src/components/preset-gallery.tsx+++ b/src/components/preset-gallery.tsx@@ -35,6 +35,7 @@ export const PresetCard = memo(function PresetCard({preset,href,compact,+ bare,engine,gpu,gpuName,@@ -42,6 +43,8 @@ export const PresetCard = memo(function PresetCard({preset: PresetCardData;href: string;compact?: boolean;+ /** The picture, the name and the description only: no category icon or engine chips (Home's rows). */+ bare?: boolean;/** Settings → Engine mode: with one engine locked in, the other's chip would suggest a route the user turned off. */engine?: "cloud" | "open";/** Its state on the viewer's GPU (a badge as one string, badgeKey), or none. */@@ -61,14 +64,18 @@ export const PresetCard = memo(function PresetCard({><div className="relative aspect-[4/3] overflow-hidden rounded-lg bg-muted"><PresetPreview preview={preset.preview} hue={preset.hue} playing={playing} flip={previewFlip(preset)} />- <span className="pointer-events-none absolute inset-x-0 bottom-0 h-12 bg-gradient-to-t from-black/40 to-transparent" />- <span className="pointer-events-none absolute bottom-1.5 left-1.5 grid size-6 place-items-center rounded-md bg-black/30 text-white">- <Icon className="size-3.5" />- </span>- <span className="pointer-events-none absolute right-1.5 bottom-1.5 flex gap-1">- {preset.cloud && engine !== "open" && <EngineChip engine="cloud" />}- {preset.open && engine !== "cloud" && <EngineChip engine="open" />}- </span>+ {!bare && (+ <>+ <span className="pointer-events-none absolute inset-x-0 bottom-0 h-12 bg-gradient-to-t from-black/40 to-transparent" />+ <span className="pointer-events-none absolute bottom-1.5 left-1.5 grid size-6 place-items-center rounded-md bg-black/30 text-white">+ <Icon className="size-3.5" />+ </span>+ <span className="pointer-events-none absolute right-1.5 bottom-1.5 flex gap-1">+ {preset.cloud && engine !== "open" && <EngineChip engine="cloud" />}+ {preset.open && engine !== "cloud" && <EngineChip engine="open" />}+ </span>+ </>+ )}{preset.top && (<span className="absolute top-1.5 left-1.5 rounded-[4px] bg-highlight px-1 text-[10px] font-semibold text-highlight-foreground">Popular</span>)}@@ -161,7 +168,7 @@ function RunsOnControl({ value, onChange, locked, owner }: { value: RunsOn; onCh}/** Keeps data-fade-start / data-fade-end on a horizontal scroller in sync with its scroll position. */-function useFadeEdges<T extends HTMLElement>() {+export function useFadeEdges<T extends HTMLElement>() {const ref = useRef<T>(null);useEffect(() => {const el = ref.current;diff --git a/src/components/preset-preview.tsx b/src/components/preset-preview.tsxindex d62384e8..3105eb73 100644--- a/src/components/preset-preview.tsx+++ b/src/components/preset-preview.tsx@@ -17,19 +17,41 @@ import {} from "@/lib/preset-cards";import { releaseVideo } from "@/lib/media-release";import { glyphFor } from "@/lib/move-glyphs";-import { autoplays, browserPlaybackEnv, createPlaybackStore, playsWhenAsked, previewKind } from "@/lib/preview-playback";+import { autoplays, browserPlaybackEnv, createPlaybackStore, playsWhenAsked, previewKind, type AutoPlay } from "@/lib/preview-playback";import { cn } from "@/lib/utils";/** Which previews play, shared by every card and the preset header (rules in lib/preview-playback.ts). */const store = createPlaybackStore(browserPlaybackEnv);+/** Home's "Pause previews", remembered in this browser: it holds on every page. */+const PAUSE_KEY = "fuobe:previews";+try {+ if (typeof window !== "undefined" && localStorage.getItem(PAUSE_KEY) === "paused") store.setPaused(true);+} catch {}++/** Whether the viewer paused previews (false until hydrated, as on the server). */+export const usePreviewsPaused = () => useSyncExternalStore(store.subscribe, store.isPaused, () => false);++export function setPreviewsPaused(paused: boolean) {+ store.setPaused(paused);+ try {+ if (paused) localStorage.setItem(PAUSE_KEY, "paused");+ else localStorage.removeItem(PAUSE_KEY);+ } catch {}+}+/*** Wire a card element to the playback store. Returns whether its preview should play, the handlers* to spread on the card (hover and keyboard focus, after a short rest) and `onTap`, which plays or* stops it from a touch (for an element a tap doesn't otherwise use). A clip plays by itself while- * the card is in view; with `always`, any preview that moves does (the preset header).+ * the card is in view (with `auto: "touch"`, only where nothing can hover); with `always`, any+ * preview that moves does (the preset header).*/-export function usePreviewPlayback<T extends HTMLElement>(id: string, preview: Pick<Preview, "video" | "move" | "fx">, { always = false } = {}) {+export function usePreviewPlayback<T extends HTMLElement>(+ id: string,+ preview: Pick<Preview, "video" | "move" | "fx">,+ { always = false, auto = "always" }: { always?: boolean; auto?: AutoPlay } = {},+) {const ref = useRef<T | null>(null);const playing = useSyncExternalStore(store.subscribe,@@ -39,9 +61,9 @@ export function usePreviewPlayback<T extends HTMLElement>(id: string, preview: Pconst kind = previewKind(preview);useEffect(() => {const el = ref.current;- if (!el || !(always ? kind !== "still" : autoplays(kind, store.canHover()))) return;+ if (!el || !(always ? kind !== "still" : autoplays(kind, store.canHover(), auto))) return;return store.watch(el, id);- }, [id, kind, always]);+ }, [id, kind, always, auto]);useEffect(() => () => store.stop(id), [id]);const on = () => playsWhenAsked(kind, store.reducedMotion()) && store.play(id);const off = () => store.stop(id);@@ -253,9 +275,10 @@ function Fx({ fx, scene }: { fx: NonNullable<Preview["fx"]>; scene: PreviewScene/*** A real cover's clip, mounted only while it plays (a browser allows a page only so many live* players, a phone fewer), over the poster until its first frame is ready. The poster is that- * frame, so starting and stopping never flashes.+ * frame, so starting and stopping never flashes. With `startAt` (a card resting on the clip's peak+ * frame) it starts from that frame instead, and shows once the seek there is done, with a short fade.*/-function Clip({ src }: { src: string }) {+function Clip({ src, startAt }: { src: string; startAt?: number }) {const ref = useRef<HTMLVideoElement>(null);const [ready, setReady] = useState(false);// A layout effect: a page kept hidden after navigating away (Activity) keeps its elements, and a@@ -277,8 +300,10 @@ function Clip({ src }: { src: string }) {loopplaysInlinepreload="auto"- onLoadedData={() => setReady(true)}- className={cn("absolute inset-0 size-full object-cover", !ready && "opacity-0")}+ onLoadedMetadata={startAt ? (e) => void (e.currentTarget.currentTime = startAt) : undefined}+ onLoadedData={startAt ? undefined : () => setReady(true)}+ onSeeked={startAt ? () => setReady(true) : undefined}+ className={cn("absolute inset-0 size-full object-cover", startAt && "transition-opacity duration-150", !ready && "opacity-0")}aria-hidden/>);@@ -295,12 +320,18 @@ export const PresetPreview = memo(function PresetPreview({hue,playing,flip,+ loading = "lazy",+ fetchPriority,}: {preview: Preview;hue: number;playing: boolean;/** Mirror the still (see previewFlip). */flip?: boolean;+ /** A real cover's still: "eager" above the fold (Home's wall), lazy everywhere else. */+ loading?: "eager" | "lazy";+ /** "high" for the page's largest picture (Next's image guide: instead of preloading it). */+ fetchPriority?: "high";}) {const [armed, setArmed] = useState(false);if (playing && !armed) setArmed(true);@@ -311,8 +342,19 @@ export const PresetPreview = memo(function PresetPreview({return (<>{/* eslint-disable-next-line @next/next/no-img-element */}- <img src={preview.poster} alt="" draggable={false} loading="lazy" decoding="async" className="absolute inset-0 size-full object-cover" aria-hidden />- {preview.video && playing && <Clip src={preview.video} />}+ <img+ src={preview.rest?.src ?? preview.poster}+ alt=""+ width={512}+ height={384}+ draggable={false}+ loading={loading}+ fetchPriority={fetchPriority}+ decoding={fetchPriority ? undefined : "async"}+ className="absolute inset-0 size-full object-cover"+ aria-hidden+ />+ {preview.video && playing && <Clip src={preview.video} startAt={preview.rest?.at} />}</>);diff --git a/src/lib/preset-cards.ts b/src/lib/preset-cards.tsindex d7f8c5dd..dfcdf47b 100644--- a/src/lib/preset-cards.ts+++ b/src/lib/preset-cards.ts@@ -179,6 +179,11 @@ export interface PresetPreview {poster: string;/** A real cover's clip (preset-previews.generated.ts), played instead of the procedural preview. */video?: string;+ /**+ * Shown at rest instead of the poster: the clip's peak frame (preset-peaks.generated.ts), where the clip starts+ * (`at`, in seconds) when it plays, so nothing jumps. Home's cards only; the galleries keep their posters.+ */+ rest?: { src: string; at: number };scene: PreviewScene;move?: CameraMove;fx?: PreviewFx;diff --git a/src/lib/preview-playback.ts b/src/lib/preview-playback.tsindex 225404ad..6238dcf9 100644--- a/src/lib/preview-playback.ts+++ b/src/lib/preview-playback.ts@@ -5,7 +5,8 @@* download them all. Procedural previews (no clip) play by themselves only where nothing can hover* (phones); a still never moves. Nothing plays by itself when the user prefers reduced motion, asks* for less data (Save-Data, a 2G connection) or the tab is hidden; reduced motion turned on while the- * page is open stops what plays by itself at once.+ * page is open stops what plays by itself at once. The viewer can pause previews too (Home's "Pause previews"): then+ * nothing plays by itself, anywhere.** The hovered or focused card plays too, on top of those, once the pointer has rested on it for a* moment (a pointer crossing the grid doesn't start every card it passes); asked for, it plays even@@ -56,8 +57,18 @@ export type PreviewKind = "clip" | "procedural" | "still";export const previewKind = (p: { video?: string; move?: string; fx?: string }): PreviewKind => (p.video ? "clip" : p.move || p.fx ? "procedural" : "still");-/** Whether a preview of this kind plays by itself while it's in view: a clip anywhere, a procedural one only where nothing can hover. */-export const autoplays = (kind: PreviewKind, canHover: boolean) => kind === "clip" || (kind === "procedural" && !canHover);+/**+ * When a card plays by itself: "always" (while it's in view), or "touch", only where nothing can hover (Home's studio+ * cards: with a mouse they wait for it, so the cards above them keep the screen's few clips).+ */+export type AutoPlay = "always" | "touch";++/**+ * Whether a preview of this kind plays by itself while it's in view: a clip anywhere, a procedural one only where+ * nothing can hover; with "touch", nothing does where a pointer can hover.+ */+export const autoplays = (kind: PreviewKind, canHover: boolean, auto: AutoPlay = "always") =>+ (auto === "always" || !canHover) && (kind === "clip" || (kind === "procedural" && !canHover));/*** Whether a hover, keyboard focus or tap plays a preview of this kind. With reduced motion only a clip@@ -101,6 +112,8 @@ export function createPlaybackStore(env: PlaybackEnv) {/** The card a hover is about to play, and what calls that off. */let pending: { id: string; cancel: () => void } | null = null;let settle: (() => void) | null = null;+ /** The viewer paused previews: nothing plays by itself; a hover, focus or tap still plays. */+ let paused = false;const notify = () => listeners.forEach((l) => l());const setHovered = (id: string | null) => {@@ -125,7 +138,7 @@ export function createPlaybackStore(env: PlaybackEnv) {const pick = () => {settle = null;- if (reducedMotion() || env.saveData()) return setAuto([]);+ if (paused || reducedMotion() || env.saveData()) return setAuto([]);const cx = env.viewportWidth() / 2;const cy = env.viewportHeight() / 2;const cards: Candidate[] = [];@@ -156,7 +169,7 @@ export function createPlaybackStore(env: PlaybackEnv) {notify();};- const isPlaying = (id: string) => !env.hidden() && (hovered === id || (auto.has(id) && !reducedMotion()));+ const isPlaying = (id: string) => !env.hidden() && (hovered === id || (auto.has(id) && !paused && !reducedMotion()));/** Hover / focus: play this one once it has had the pointer for HOVER_INTENT. */const play = (id: string) => {cancelPending();@@ -190,6 +203,17 @@ export function createPlaybackStore(env: PlaybackEnv) {/** Every card playing now, by id (for tests). */playing: () => [...new Set([...auto, ...(hovered ? [hovered] : [])])].filter(isPlaying).sort(),canHover: () => env.matches("(hover: hover) and (pointer: fine)"),+ isPaused: () => paused,+ /** Pause previews, or play them again: paused, what plays by itself stops at once; played again, the cards in view are picked. */+ setPaused(b: boolean) {+ if (paused === b) return;+ paused = b;+ if (b) {+ cancelSettle();+ auto = new Set();+ } else if (watched.size) schedule();+ notify();+ },reducedMotion,saveData: () => env.saveData(),play,diff --git a/tests/shell-logic.test.ts b/tests/shell-logic.test.tsindex 0cdd86d1..d428b3b3 100644--- a/tests/shell-logic.test.ts+++ b/tests/shell-logic.test.ts@@ -433,6 +433,44 @@ describe("preview playback store", () => {expect([true, false].map((hover) => autoplays("still", hover))).toEqual([false, false]);});+ it('with auto: "touch" (Home\'s studio cards) nothing plays by itself where a pointer can hover', () => {+ for (const kind of ["clip", "procedural", "still"] as const) expect(autoplays(kind, true, "touch"), kind).toBe(false);+ // Where nothing can hover (a phone) it plays as any card does.+ expect((["clip", "procedural", "still"] as const).map((kind) => autoplays(kind, false, "touch"))).toEqual([true, true, false]);+ expect((["clip", "procedural", "still"] as const).map((kind) => autoplays(kind, false, "touch"))).toEqual((["clip", "procedural", "still"] as const).map((kind) => autoplays(kind, false)));+ });++ it("paused previews: nothing plays by itself, at once and on every pick, yet a hover still plays; played again, the cards in view are picked", () => {+ const fake = fakeEnv({ hover: true, width: 1280, height: 800 });+ const store = createPlaybackStore(fake.env);+ const seen: string[][] = [];+ store.subscribe(() => seen.push(store.playing()));+ watchGrid(store, fake, 1, 3);+ fake.advance(SCROLL_SETTLE);+ expect(store.playing()).toEqual(["r0c0", "r0c1", "r0c2"]);+ store.setPaused(true);+ expect(store.isPaused()).toBe(true);+ expect(store.playing()).toEqual([]);+ // Scrolling again picks nothing.+ fake.scrolled();+ fake.advance(SCROLL_SETTLE);+ expect(store.playing()).toEqual([]);+ // The card the pointer rests on still plays.+ store.play("r0c1");+ fake.advance(HOVER_INTENT);+ expect(store.playing()).toEqual(["r0c1"]);+ store.stop("r0c1");+ expect(store.playing()).toEqual([]);+ // Pausing twice changes nothing; playing again waits for the page to be still, then picks what's in view.+ const before = seen.length;+ store.setPaused(true);+ expect(seen.length).toBe(before);+ store.setPaused(false);+ expect(store.playing()).toEqual([]);+ fake.advance(SCROLL_SETTLE);+ expect(store.playing()).toEqual(["r0c0", "r0c1", "r0c2"]);+ });+it("with reduced motion a hover or tap plays a clip, but a procedural preview keeps its poster", () => {expect([false, true].map((reduced) => playsWhenAsked("clip", reduced))).toEqual([true, true]);expect([false, true].map((reduced) => playsWhenAsked("procedural", reduced))).toEqual([true, false]);