diff --git a/bun.lock b/bun.lock index c12184a35b6..8bf86ca2c22 100644 --- a/bun.lock +++ b/bun.lock @@ -106,7 +106,7 @@ "diff": "catalog:", "ghostty-web": "github:anomalyco/ghostty-web#e91e38a9ceb13c566ddfa15185a9393849f712a9", "happy-dom": "20.11.1", - "knobs": "npm:@kitlangton/knobs@0.4.0-next.12", + "knobs": "npm:@kitlangton/knobs@0.4.0-next.15", "tw-animate-css": "1.4.0", "vite": "8.2.2", "vite-plugin-pwa": "1.3.0", @@ -887,7 +887,7 @@ "@solid-primitives/resize-observer": "catalog:", "fuzzysort": "catalog:", "katex": "0.16.47", - "knobs": "npm:@kitlangton/knobs@0.4.0-next.12", + "knobs": "npm:@kitlangton/knobs@0.4.0-next.15", "marked": "catalog:", "motion": "12.34.5", "remeda": "catalog:", @@ -3953,7 +3953,7 @@ "klona": ["klona@2.0.6", "", {}, "sha512-dhG34DXATL5hSxJbIexCft8FChFXtmskoZYnoPWjXQuebWYCNkVeV3KkGegCK9CP1oswI/vQibS2GY7Em/sJJA=="], - "knobs": ["@kitlangton/knobs@0.4.0-next.12", "", { "dependencies": { "@kitlangton/rolling-number": "^0.4.1" }, "peerDependencies": { "@pierre/diffs": "^1.5.2", "react": ">=18", "solid-js": ">=1.8" }, "optionalPeers": ["@pierre/diffs", "react", "solid-js"] }, "sha512-ImGtRxPKbuqi25YzQYpiAuLELtxt9XJM75VXp2gjWopwsTP+KkqzBuWl62qccLnHuCiJ/t9VEcqrSsqT1zvmHg=="], + "knobs": ["@kitlangton/knobs@0.4.0-next.15", "", { "dependencies": { "@kitlangton/rolling-number": "^0.4.1" }, "peerDependencies": { "@pierre/diffs": "^1.5.2", "react": ">=18", "solid-js": ">=1.8" }, "optionalPeers": ["@pierre/diffs", "react", "solid-js"] }, "sha512-DdBzlwpLUR42jdwcJBS4hDc7jTuU4uG1H7ZJO5RToejfmcAb1pQJ48hHwBbeK4wyRO8XRrs3T7BdDdPRtuRZxw=="], "lang-map": ["lang-map@0.4.0", "", { "dependencies": { "language-map": "^1.1.0" } }, "sha512-oiSqZIEUnWdFeDNsp4HId4tAxdFbx5iMBOwA3666Fn2L8Khj8NiD9xRvMsGmKXopPVkaDFtSv3CJOmXFUB0Hcg=="], diff --git a/packages/app/package.json b/packages/app/package.json index 73db2738a8e..d07b0efdf86 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -49,7 +49,7 @@ "diff": "catalog:", "ghostty-web": "github:anomalyco/ghostty-web#e91e38a9ceb13c566ddfa15185a9393849f712a9", "happy-dom": "20.11.1", - "knobs": "npm:@kitlangton/knobs@0.4.0-next.12", + "knobs": "npm:@kitlangton/knobs@0.4.0-next.15", "tw-animate-css": "1.4.0", "vite": "8.2.2", "vite-plugin-pwa": "1.3.0", diff --git a/packages/app/src/components-lab/lab.tsx b/packages/app/src/components-lab/lab.tsx index 71c4d825a9f..567adb6198a 100644 --- a/packages/app/src/components-lab/lab.tsx +++ b/packages/app/src/components-lab/lab.tsx @@ -46,7 +46,7 @@ import { type StoryArgs, type StoryModule, } from "./catalog" -import { applyMotionPatch, motionFields, motionValues, sameMotion, storyMotion } from "./motion" +import { applyMotionPatch, motionFields, motionScopes, motionValues, sameMotion, storyMotion } from "./motion" // How long Play holds each state at full speed. Slow motion doubles it while the motion itself runs 4× longer, so // a transition still finishes and Play keeps moving. @@ -354,11 +354,7 @@ function Stage(props: { entry: LabEntry; inspector: HTMLElement; scheme: "dark" first === undefined || rest.length === 0 ? {} : { - state: choice(first, [first, ...rest], { - variants: true, - // The digit that picks each of the first nine. - labels: Object.fromEntries(names.slice(0, 9).map((name, offset) => [name, `${offset + 1} ${name}`])), - }), + state: choice(first, [first, ...rest], { variants: true }), play: toolbar( [ { @@ -386,24 +382,25 @@ function Stage(props: { entry: LabEntry; inspector: HTMLElement; scheme: "dark" const [shown, setShown] = createSignal(motion, { equals: sameMotion }) const inspect = () => { - const own = motionFields(motion, (values) => store.set(values)) + const scopes = motionScopes(motion) return defineControls( "components-lab", - { ...states, ...fields, ...own }, + { ...states, ...fields, ...motionFields(motion) }, { storage: null, persist: "none", // Beta is a production build, where knobs resolves its production entry: the panel mounts only when shipped. ship: () => import("knobs/panel"), - defaultLabel: first && rest.length > 0 ? `1 ${first}` : first, + defaultLabel: { ...(first !== undefined && { "": first }), ...scopes.defaultLabels }, presets: Object.fromEntries( entry.states.slice(1).map((state, offset) => [state.name, knobArgs(fields, stateArgs(entry, offset + 1))]), ), + // A state leaves the Knobs where they are: they're the story's tuning, not part of a state. + inPresets: scopes.inPresets, + scopedPresets: scopes.scopedPresets, onChange: (patch, values, meta) => { - // A state leaves the Knobs where they are: they're the story's tuning, not part of a state. - if (meta.source === "preset") queueMicrotask(() => store.sync(motionValues(motion))) - else applyMotionPatch(motion, patch) + applyMotionPatch(motion, patch) if (!("state" in patch)) { if (meta.source !== "preset") pause() @@ -441,10 +438,11 @@ function Stage(props: { entry: LabEntry; inspector: HTMLElement; scheme: "dark" variants: false, session: false, empty: language.t("lab.noControls"), + hints: { digits: "always" }, }), ) - // Applied rather than matched, so the state's chip stays lit while the Knobs, which no state sets, move. + // A rebuild stays on the state it was showing. if (rest.length > 0) untrack(() => preset(index())) }) diff --git a/packages/app/src/components-lab/motion.ts b/packages/app/src/components-lab/motion.ts index e5010abcb6e..3fee9397f9c 100644 --- a/packages/app/src/components-lab/motion.ts +++ b/packages/app/src/components-lab/motion.ts @@ -1,7 +1,6 @@ import { motionGlobals, motionPresets, motionTuning, type MotionKnob } from "@opencode/ui/motion-tuning" import { applyMotion, motionField } from "@opencode/ui/motion-tuning-fields" -import type { DurationOptions } from "@opencode/ui/motion" -import { action, toolbar, type Field, type Values } from "knobs" +import { action, type Field, type Values } from "knobs" /** * The Knobs a story names in `parameters.motion` (ids, groups or cards) and their replay buttons. Speed and slow motion @@ -31,26 +30,10 @@ export function sameMotion(a: StoryMotion, b: StoryMotion) { return left.length === right.length && left.every((item, index) => item === right[index]) } -/** - * One card per group (or card): buttons for its spring presets, its knobs and its replay buttons, then speed and slow - * motion. `apply` sets the values a preset button names. - */ -export function motionFields( - motion: StoryMotion, - apply: (values: { [key: string]: DurationOptions }) => void, -): { [key: string]: Field } { +/** One card per group (or card): its knobs and its replay buttons, then speed and slow motion. */ +export function motionFields(motion: StoryMotion): { [key: string]: Field } { return Object.fromEntries([ - ...Object.entries(springPresets(motion)).map(([group, list]) => [ - `presets.${group}`, - toolbar( - list.map((item) => ({ id: item.name, label: item.name, run: () => apply(item.values) })), - { group, labels: true }, - ), - ]), - ...motion.knobs.map((item) => [ - key(item), - motionField(item, { label: item.label, group: item.card ?? item.group }), - ]), + ...motion.knobs.map((item) => [key(item), motionField(item, { label: item.label, group: group(item) })]), ...motion.actions.map((item) => [ `replay.${item.id}`, action(item.label, { group: item.group, subsection: "Replay", run: item.run }), @@ -59,6 +42,34 @@ export function motionFields( ]) } +/** + * The motion cards' place among the story's presets: the states leave every card alone, and each card with springs + * gets its own strip of Shipped, Snappy, Smooth and Calm. + */ +export function motionScopes(motion: StoryMotion) { + const springs = motion.knobs.flatMap((item) => (item.kind === "spring" ? [item] : [])) + const scopes = [...new Set(springs.map(group))] + const groups = [...motion.knobs.map(group), ...motion.actions.map((item) => item.group), ...motion.globals.map(group)] + + return { + inPresets: Object.fromEntries(groups.map((name) => [name, false])), + scopedPresets: Object.fromEntries( + scopes.map((scope) => [ + scope, + Object.fromEntries( + presets.map((preset) => [ + motionPresets[preset], + Object.fromEntries( + springs.flatMap((item) => (group(item) === scope ? [[key(item), item.presets[preset]]] : [])), + ), + ]), + ), + ]), + ), + defaultLabels: Object.fromEntries(scopes.map((scope) => [scope, "Shipped"])), + } +} + export function motionValues(motion: StoryMotion) { return Object.fromEntries([...motion.knobs, ...motion.globals].map((item) => [key(item), item.value])) } @@ -80,24 +91,6 @@ function key(item: MotionKnob) { return `motion.${item.id}` } -// The Shipped, Snappy, Smooth and Calm values of each card's springs. -function springPresets(motion: StoryMotion) { - const springs = motion.knobs.flatMap((item) => (item.kind === "spring" ? [item] : [])) - - return Object.fromEntries( - [...new Set(springs.map((item) => item.card ?? item.group))].map((group) => { - const own = springs.filter((item) => (item.card ?? item.group) === group) - - return [ - group, - [ - { name: "Shipped", values: Object.fromEntries(own.map((item) => [key(item), item.initial])) }, - ...presets.map((preset) => ({ - name: motionPresets[preset], - values: Object.fromEntries(own.map((item) => [key(item), item.presets[preset]])), - })), - ], - ] - }), - ) +function group(item: MotionKnob) { + return item.card ?? item.group } diff --git a/packages/ui/package.json b/packages/ui/package.json index 54aa0366b1d..5af811d966a 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -128,7 +128,7 @@ "@solid-primitives/resize-observer": "catalog:", "fuzzysort": "catalog:", "katex": "0.16.47", - "knobs": "npm:@kitlangton/knobs@0.4.0-next.12", + "knobs": "npm:@kitlangton/knobs@0.4.0-next.15", "marked": "catalog:", "motion": "12.34.5", "remeda": "catalog:",