mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-21 04:12:08 +03:00
feat(app): build Knobs feature pages from knobs controls (#58896)
This commit is contained in:
committed by
GitHub
Unverified
parent
6a495480e8
commit
30311ae6a7
@@ -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.15",
|
||||
"knobs": "npm:@kitlangton/knobs@0.4.0-next.22",
|
||||
"tw-animate-css": "1.4.0",
|
||||
"vite": "8.2.2",
|
||||
"vite-plugin-pwa": "1.3.0",
|
||||
@@ -420,6 +420,7 @@
|
||||
"effect": "catalog:",
|
||||
"fuzzysort": "catalog:",
|
||||
"ghostty-web": "github:anomalyco/ghostty-web#e91e38a9ceb13c566ddfa15185a9393849f712a9",
|
||||
"knobs": "npm:@kitlangton/knobs@0.4.0-next.22",
|
||||
"remeda": "catalog:",
|
||||
"solid-js": "catalog:",
|
||||
"solid-presence": "0.2.0",
|
||||
@@ -889,7 +890,7 @@
|
||||
"@solid-primitives/resize-observer": "catalog:",
|
||||
"fuzzysort": "catalog:",
|
||||
"katex": "0.16.47",
|
||||
"knobs": "npm:@kitlangton/knobs@0.4.0-next.15",
|
||||
"knobs": "npm:@kitlangton/knobs@0.4.0-next.22",
|
||||
"marked": "catalog:",
|
||||
"motion": "12.34.5",
|
||||
"remeda": "catalog:",
|
||||
@@ -3949,7 +3950,7 @@
|
||||
|
||||
"klona": ["klona@2.0.6", "", {}, "sha512-dhG34DXATL5hSxJbIexCft8FChFXtmskoZYnoPWjXQuebWYCNkVeV3KkGegCK9CP1oswI/vQibS2GY7Em/sJJA=="],
|
||||
|
||||
"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=="],
|
||||
"knobs": ["@kitlangton/knobs@0.4.0-next.22", "", { "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-YuEogbCIT1vMamHVa9VNt2xcwEdgvjcSMzOc6be6f4c69IRrofHX/2aQG74SCR/y+QBtvY3dZWAnKfcVPdj3rg=="],
|
||||
|
||||
"lang-map": ["lang-map@0.4.0", "", { "dependencies": { "language-map": "^1.1.0" } }, "sha512-oiSqZIEUnWdFeDNsp4HId4tAxdFbx5iMBOwA3666Fn2L8Khj8NiD9xRvMsGmKXopPVkaDFtSv3CJOmXFUB0Hcg=="],
|
||||
|
||||
|
||||
@@ -78,9 +78,7 @@ story("a feature page's Enabled switch turns that feature on after another featu
|
||||
document.querySelector<HTMLButtonElement>(`[data-motion-feature="Switches · ${name}"]`)
|
||||
|
||||
const enabled = (name: string) =>
|
||||
[...document.querySelectorAll("h3")]
|
||||
.find((heading) => heading.textContent === name)
|
||||
?.parentElement?.querySelector<HTMLInputElement>('[data-component="switch"] input')
|
||||
document.querySelector<HTMLElement>(`[data-knobs-mount="opencode-motion-Switches · ${name}"] [role="switch"]`)
|
||||
|
||||
await until(() => row("First") !== null)
|
||||
row("First")?.click()
|
||||
@@ -99,3 +97,110 @@ story("a feature page's Enabled switch turns that feature on after another featu
|
||||
|
||||
expect(result).toEqual({ first: false, second: true })
|
||||
})
|
||||
|
||||
story("a feature page's preset chips go through the feature model, modes included", async ({ page }) => {
|
||||
const result = page.evaluate(
|
||||
async (modules) => {
|
||||
localStorage.setItem("opencode.motion.panel", JSON.stringify({ x: 0, y: 0, width: 680 }))
|
||||
const { motionTuning } = await import(modules.tuning)
|
||||
const meta = { group: "Chips", subsection: "Feature" }
|
||||
|
||||
const alt = motionTuning.mode("chips.alt", { label: "Alt", ...meta })
|
||||
|
||||
// A mode brings rows of its own, as James does.
|
||||
motionTuning.number("chips.alt.shift", {
|
||||
label: "Shift",
|
||||
...meta,
|
||||
when: alt,
|
||||
value: 10,
|
||||
min: 0,
|
||||
max: 20,
|
||||
step: 1,
|
||||
unit: "px",
|
||||
})
|
||||
|
||||
const spring = motionTuning.spring("chips.spring", {
|
||||
label: "Spring",
|
||||
...meta,
|
||||
when: () => !alt(),
|
||||
presets: {
|
||||
snappy: { duration: 0.2, bounce: 0 },
|
||||
smooth: { duration: 0.3, bounce: 0 },
|
||||
calm: { duration: 0.45, bounce: 0 },
|
||||
},
|
||||
})
|
||||
|
||||
const shipped = spring().duration
|
||||
const { mountExtensions, until } = await import(modules.fixture)
|
||||
const debug = await import(modules.debug)
|
||||
const definition = { ...debug.default, renderer: () => import(modules.renderer) }
|
||||
|
||||
const host = mountExtensions({
|
||||
definitions: [definition],
|
||||
stored: { "extension.debug.bar": { visible: true, motion: true } },
|
||||
})
|
||||
|
||||
host.release()
|
||||
|
||||
const mount = '[data-knobs-mount="opencode-motion-Chips · Feature"]'
|
||||
|
||||
const chip = (name: string) =>
|
||||
[...document.querySelectorAll<HTMLElement>(`${mount} .knobs-preset-chip`)].find(
|
||||
(item) => item.textContent?.trim() === name,
|
||||
)
|
||||
|
||||
const reset = () => document.querySelector<HTMLButtonElement>(`${mount} button[aria-label^="Reset "]`)
|
||||
|
||||
await until(() => !!document.querySelector('[data-motion-feature="Chips · Feature"]'))
|
||||
document.querySelector<HTMLButtonElement>('[data-motion-feature="Chips · Feature"]')?.click()
|
||||
await until(() => !!chip("Calm"))
|
||||
chip("Calm")?.click()
|
||||
await until(() => spring().duration === 0.45)
|
||||
chip("Alt")?.click()
|
||||
await until(() => alt())
|
||||
await until(() => !!chip("Snappy"))
|
||||
chip("Snappy")?.click()
|
||||
await until(() => !alt() && spring().duration === 0.2)
|
||||
await until(() => !!chip("Alt"))
|
||||
chip("Alt")?.click()
|
||||
await until(() => alt())
|
||||
await until(() => !!chip("Shipped"))
|
||||
chip("Shipped")?.click()
|
||||
await until(() => !alt() && spring().duration === shipped)
|
||||
|
||||
// The group's Reset after a preset goes back to Shipped, modes included.
|
||||
await until(() => !!chip("Calm"))
|
||||
chip("Calm")?.click()
|
||||
await until(() => spring().duration === 0.45 && !!reset())
|
||||
reset()?.click()
|
||||
await until(() => spring().duration === shipped)
|
||||
await until(() => !!chip("Alt"))
|
||||
chip("Alt")?.click()
|
||||
await until(() => alt())
|
||||
|
||||
// The test presses a key on Shift and Reset twice: back to Alt, then to Shipped.
|
||||
const shift = () => document.querySelector<HTMLElement>(`${mount} [role="slider"][aria-label="Shift"]`)
|
||||
await until(() => !!shift())
|
||||
shift()?.focus()
|
||||
await until(() => !alt())
|
||||
const values = { alt: alt(), shipped: spring().duration === shipped }
|
||||
host.unmount()
|
||||
|
||||
return values
|
||||
},
|
||||
{ ...modules, tuning: sourceURL(new URL("../../ui/src/motion-tuning.ts", import.meta.url)) },
|
||||
)
|
||||
|
||||
const panel = page.locator('[data-knobs-mount="opencode-motion-Chips · Feature"]')
|
||||
const shift = panel.getByRole("slider", { name: "Shift" })
|
||||
|
||||
await shift.and(page.locator(":focus")).waitFor()
|
||||
await page.keyboard.press("ArrowRight")
|
||||
await expect(panel.getByRole("radio", { name: "Alt, modified" })).toBeChecked()
|
||||
await panel.getByRole("button", { name: "Reset Feature" }).click()
|
||||
await expect(panel.getByRole("radio", { name: "Alt", exact: true })).toBeChecked()
|
||||
await panel.getByRole("button", { name: "Reset Feature" }).click()
|
||||
await expect(shift).toHaveCount(0)
|
||||
|
||||
expect(await result).toEqual({ alt: false, shipped: true })
|
||||
})
|
||||
|
||||
@@ -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.15",
|
||||
"knobs": "npm:@kitlangton/knobs@0.4.0-next.22",
|
||||
"tw-animate-css": "1.4.0",
|
||||
"vite": "8.2.2",
|
||||
"vite-plugin-pwa": "1.3.0",
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
"effect": "catalog:",
|
||||
"fuzzysort": "catalog:",
|
||||
"ghostty-web": "github:anomalyco/ghostty-web#e91e38a9ceb13c566ddfa15185a9393849f712a9",
|
||||
"knobs": "npm:@kitlangton/knobs@0.4.0-next.22",
|
||||
"remeda": "catalog:",
|
||||
"solid-js": "catalog:",
|
||||
"solid-presence": "0.2.0",
|
||||
|
||||
@@ -48,14 +48,9 @@ export default {
|
||||
"motion.close": "Close Knobs",
|
||||
"motion.off": "Off",
|
||||
"motion.shipped": "Shipped",
|
||||
"motion.custom": "Custom",
|
||||
"motion.presets": "Presets",
|
||||
"motion.replay": "Replay",
|
||||
"motion.focus": "Focus",
|
||||
"motion.reset": "Reset to Shipped",
|
||||
"motion.enabled": "Enabled",
|
||||
"motion.duration": "Duration",
|
||||
"motion.bounce": "Bounce",
|
||||
"components.label": "COMPONENTS",
|
||||
"components.tip": "Open the components lab: every story, with live controls and its states",
|
||||
"speed.label": "SPEED",
|
||||
|
||||
@@ -1,360 +0,0 @@
|
||||
import { For, Show, type Accessor } from "solid-js"
|
||||
import { sliderFraction, sliderStep, sliderValue, Spring, type DurationOptions } from "@opencode/ui/motion"
|
||||
import { motionTuning, type Bezier, type MotionKnob } from "@opencode/ui/motion-tuning"
|
||||
import { changed, reset } from "@opencode/ui/motion-tuning-features"
|
||||
import { SegmentedControl, SegmentedControlItem } from "@opencode/ui/segmented-control"
|
||||
import { Switch } from "@opencode/ui/switch"
|
||||
import { Tooltip } from "@opencode/ui/tooltip"
|
||||
import { useExtension } from "../sdk"
|
||||
|
||||
type Of<Kind extends MotionKnob["kind"]> = Extract<MotionKnob, { kind: Kind }>
|
||||
|
||||
// One grid for every row on a feature's page: the label, the control, the value right-aligned in its own column, and
|
||||
// the dot that resets a changed knob.
|
||||
const row = "grid min-h-11 grid-cols-[104px_minmax(0,1fr)_64px_16px] items-center gap-x-2 px-4"
|
||||
|
||||
const sub = "grid min-h-8 grid-cols-[104px_minmax(0,1fr)_64px_16px] items-center gap-x-2 px-4"
|
||||
|
||||
/**
|
||||
* A knob's row on its feature's page. `live` changes whenever any knob does, since the registry isn't reactive. A row
|
||||
* keeps its knob for its lifetime, so the control under a drag is never replaced.
|
||||
*/
|
||||
export function ControlRow(props: { knob: MotionKnob; live: Accessor<number>; label?: string }) {
|
||||
const knob = props.knob
|
||||
const label = () => props.label ?? knob.label
|
||||
|
||||
if (knob.kind === "number") return <NumberRow knob={knob} live={props.live} label={label()} />
|
||||
|
||||
if (knob.kind === "toggle" || knob.kind === "mode") return <ToggleRow knob={knob} live={props.live} label={label()} />
|
||||
|
||||
if (knob.kind === "choice") return <ChoiceRow knob={knob} live={props.live} label={label()} />
|
||||
|
||||
if (knob.kind === "spring") return <SpringRow knob={knob} live={props.live} label={label()} />
|
||||
|
||||
return <EasingRow knob={knob} live={props.live} label={label()} />
|
||||
}
|
||||
|
||||
function NumberRow(props: { knob: Of<"number">; live: Accessor<number>; label: string }) {
|
||||
const value = read(props.knob, props.live)
|
||||
|
||||
return (
|
||||
<div class={row}>
|
||||
<Label text={props.label} />
|
||||
<Track
|
||||
label={props.label}
|
||||
value={value()}
|
||||
shipped={props.knob.initial}
|
||||
min={props.knob.min}
|
||||
max={props.knob.max}
|
||||
step={props.knob.step}
|
||||
onChange={(next) => motionTuning.set(props.knob, next)}
|
||||
/>
|
||||
<Value text={format(value(), props.knob.step, props.knob.unit)} />
|
||||
<Dot knob={props.knob} live={props.live} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function ToggleRow(props: { knob: Of<"toggle"> | Of<"mode">; live: Accessor<number>; label: string }) {
|
||||
const value = read(props.knob, props.live)
|
||||
|
||||
return (
|
||||
<div class={row}>
|
||||
<Label text={props.label} />
|
||||
<span />
|
||||
<Switch class="justify-self-end" checked={value()} onChange={(next) => motionTuning.set(props.knob, next)} hideLabel>
|
||||
{props.label}
|
||||
</Switch>
|
||||
<Dot knob={props.knob} live={props.live} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function ChoiceRow(props: { knob: Of<"choice">; live: Accessor<number>; label: string }) {
|
||||
const value = read(props.knob, props.live)
|
||||
|
||||
return (
|
||||
<div class={row}>
|
||||
<Label text={props.label} />
|
||||
<SegmentedControl
|
||||
class="segmented-control-v2--full-width col-span-2"
|
||||
aria-label={props.label}
|
||||
value={value()}
|
||||
onChange={(next) => {
|
||||
if (next) motionTuning.set(props.knob, next)
|
||||
}}
|
||||
>
|
||||
<For each={props.knob.options}>
|
||||
{(option) => (
|
||||
<SegmentedControlItem class="text-[12px]" style={{ padding: "0 4px" }} value={option}>
|
||||
<span class="truncate">{props.knob.labels[option] ?? option}</span>
|
||||
</SegmentedControlItem>
|
||||
)}
|
||||
</For>
|
||||
</SegmentedControl>
|
||||
<Dot knob={props.knob} live={props.live} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function SpringRow(props: { knob: Of<"spring">; live: Accessor<number>; label: string }) {
|
||||
const ctx = useExtension()
|
||||
const value = read(props.knob, props.live)
|
||||
const set = (patch: Partial<DurationOptions>) => motionTuning.set(props.knob, { ...value(), ...patch })
|
||||
const span = () => Math.max(value().duration, props.knob.initial.duration) * 1.6
|
||||
|
||||
return (
|
||||
<div class="py-1">
|
||||
<div class={row}>
|
||||
<Label text={props.label} />
|
||||
<Graph ghost={springPath(props.knob.initial, span())} path={springPath(value(), span())} />
|
||||
<Dot knob={props.knob} live={props.live} />
|
||||
</div>
|
||||
<div class={sub}>
|
||||
<Label text={ctx.t("motion.duration")} sub />
|
||||
<Track
|
||||
label={`${props.label} ${ctx.t("motion.duration")}`}
|
||||
value={value().duration}
|
||||
shipped={props.knob.initial.duration}
|
||||
min={0.05}
|
||||
max={1}
|
||||
step={0.01}
|
||||
onChange={(duration) => set({ duration })}
|
||||
/>
|
||||
<Value text={format(value().duration, 0.01, "s")} />
|
||||
</div>
|
||||
<div class={sub}>
|
||||
<Label text={ctx.t("motion.bounce")} sub />
|
||||
<Track
|
||||
label={`${props.label} ${ctx.t("motion.bounce")}`}
|
||||
value={value().bounce}
|
||||
shipped={props.knob.initial.bounce}
|
||||
min={0}
|
||||
max={0.6}
|
||||
step={0.01}
|
||||
onChange={(bounce) => set({ bounce })}
|
||||
/>
|
||||
<Value text={format(value().bounce, 0.01)} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const handles = [
|
||||
{ label: "x1", min: 0, max: 1 },
|
||||
{ label: "y1", min: -0.5, max: 1.5 },
|
||||
{ label: "x2", min: 0, max: 1 },
|
||||
{ label: "y2", min: -0.5, max: 1.5 },
|
||||
]
|
||||
|
||||
function EasingRow(props: { knob: Of<"easing">; live: Accessor<number>; label: string }) {
|
||||
const value = read(props.knob, props.live)
|
||||
|
||||
const set = (index: number, next: number) => {
|
||||
const curve: Bezier = [...value()]
|
||||
curve[index] = next
|
||||
motionTuning.set(props.knob, curve)
|
||||
}
|
||||
|
||||
return (
|
||||
<div class="py-1">
|
||||
<div class={row}>
|
||||
<Label text={props.label} />
|
||||
<Graph ghost={bezierPath(props.knob.initial)} path={bezierPath(value())} />
|
||||
<Dot knob={props.knob} live={props.live} />
|
||||
</div>
|
||||
<For each={handles}>
|
||||
{(handle, index) => (
|
||||
<div class={sub}>
|
||||
<Label text={handle.label} sub />
|
||||
<Track
|
||||
label={`${props.label} ${handle.label}`}
|
||||
value={value()[index()] ?? 0}
|
||||
shipped={props.knob.initial[index()] ?? 0}
|
||||
min={handle.min}
|
||||
max={handle.max}
|
||||
step={0.01}
|
||||
onChange={(next) => set(index(), next)}
|
||||
/>
|
||||
<Value text={format(value()[index()] ?? 0, 0.01)} />
|
||||
</div>
|
||||
)}
|
||||
</For>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function Label(props: { text: string; sub?: boolean }) {
|
||||
return (
|
||||
<span
|
||||
title={props.text}
|
||||
classList={{
|
||||
"truncate leading-[18px]": true,
|
||||
"text-[13px] text-v2-text-text-base": !props.sub,
|
||||
"ps-3 text-[12px] text-v2-text-text-faint": !!props.sub,
|
||||
}}
|
||||
>
|
||||
{props.text}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function Value(props: { text: string }) {
|
||||
return (
|
||||
<span dir="ltr" class="justify-self-end whitespace-nowrap font-mono text-[12px] tabular-nums text-v2-text-text-base">
|
||||
{props.text}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
// A changed knob's dot resets it to what ships.
|
||||
function Dot(props: { knob: MotionKnob; live: Accessor<number> }) {
|
||||
const ctx = useExtension()
|
||||
|
||||
const shown = () => {
|
||||
props.live()
|
||||
|
||||
return changed(props.knob)
|
||||
}
|
||||
|
||||
return (
|
||||
<Show when={shown()} fallback={<span />}>
|
||||
<Tooltip appearance="compact" value={ctx.t("motion.reset")} placement="top">
|
||||
<button
|
||||
type="button"
|
||||
class="grid size-4 place-items-center rounded-full focus-visible:outline focus-visible:outline-2 focus-visible:outline-v2-border-border-focus"
|
||||
aria-label={ctx.t("motion.reset")}
|
||||
onClick={() => reset(props.knob)}
|
||||
>
|
||||
<span class="size-1.5 rounded-full bg-v2-icon-icon-accent" />
|
||||
</button>
|
||||
</Tooltip>
|
||||
</Show>
|
||||
)
|
||||
}
|
||||
|
||||
// A slider whose track scrubs: press anywhere on it and drag. The amber tick marks what ships.
|
||||
function Track(props: {
|
||||
label: string
|
||||
value: number
|
||||
shipped: number
|
||||
min: number
|
||||
max: number
|
||||
step: number
|
||||
onChange: (value: number) => void
|
||||
}) {
|
||||
let track!: HTMLDivElement
|
||||
const spec = () => ({ min: props.min, max: props.max, step: props.step })
|
||||
const percent = (value: number) => `${sliderFraction(spec(), value) * 100}%`
|
||||
|
||||
const at = (x: number) => {
|
||||
const rect = track.getBoundingClientRect()
|
||||
|
||||
return sliderValue(spec(), props.min + ((x - rect.left) / Math.max(1, rect.width)) * (props.max - props.min))
|
||||
}
|
||||
|
||||
const press = (event: PointerEvent) => {
|
||||
if (event.button !== 0) return
|
||||
event.preventDefault()
|
||||
track.setPointerCapture(event.pointerId)
|
||||
track.focus()
|
||||
props.onChange(at(event.clientX))
|
||||
}
|
||||
|
||||
const drag = (event: PointerEvent) => {
|
||||
if (track.hasPointerCapture(event.pointerId)) props.onChange(at(event.clientX))
|
||||
}
|
||||
|
||||
const key = (event: KeyboardEvent) => {
|
||||
const direction = { ArrowRight: 1, ArrowUp: 1, ArrowLeft: -1, ArrowDown: -1 }[event.key]
|
||||
|
||||
if (direction !== 1 && direction !== -1) return
|
||||
event.preventDefault()
|
||||
props.onChange(sliderStep(spec(), props.value, direction, event.shiftKey ? 10 : 1))
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={track}
|
||||
role="slider"
|
||||
tabIndex={0}
|
||||
dir="ltr"
|
||||
aria-label={props.label}
|
||||
aria-valuemin={props.min}
|
||||
aria-valuemax={props.max}
|
||||
aria-valuenow={props.value}
|
||||
class="group/track relative h-7 min-w-0 cursor-ew-resize touch-none rounded outline-none focus-visible:outline focus-visible:outline-2 focus-visible:outline-v2-border-border-focus"
|
||||
onPointerDown={press}
|
||||
onPointerMove={drag}
|
||||
onKeyDown={key}
|
||||
>
|
||||
<span class="absolute inset-x-0 top-[13px] h-0.5 rounded-full bg-v2-background-bg-layer-03" />
|
||||
<span
|
||||
class="absolute left-0 top-[13px] h-0.5 rounded-full bg-v2-icon-icon-muted group-hover/track:bg-v2-icon-icon-base"
|
||||
style={{ width: percent(props.value) }}
|
||||
/>
|
||||
<span
|
||||
class="absolute top-[9px] -ml-px h-2.5 w-0.5 rounded-full bg-v2-icon-icon-accent opacity-60"
|
||||
style={{ left: percent(props.shipped) }}
|
||||
/>
|
||||
<span
|
||||
class="absolute top-[7px] -ml-[7px] size-3.5 rounded-full bg-v2-text-text-base shadow-[var(--v2-elevation-elements)]"
|
||||
style={{ left: percent(props.value) }}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// The curve a spring or easing draws, over the shipped one dashed.
|
||||
function Graph(props: { ghost: string; path: string }) {
|
||||
return (
|
||||
<svg
|
||||
class="col-span-2 h-12 w-full rounded-md bg-v2-background-bg-layer-01"
|
||||
viewBox="0 0 240 48"
|
||||
preserveAspectRatio="none"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d={props.ghost} fill="none" class="stroke-v2-icon-icon-faint" stroke-width="1.5" stroke-dasharray="3 3" />
|
||||
<path d={props.path} fill="none" class="stroke-v2-text-text-base" stroke-width="2" />
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
function read<K extends MotionKnob>(knob: K, live: Accessor<number>): Accessor<K["value"]> {
|
||||
return () => {
|
||||
live()
|
||||
|
||||
return knob.value
|
||||
}
|
||||
}
|
||||
|
||||
function format(value: number, step: number, unit?: string) {
|
||||
const text = value.toFixed(step < 0.01 ? 3 : step < 0.1 ? 2 : step < 1 ? 1 : 0)
|
||||
|
||||
if (!unit) return text
|
||||
|
||||
return unit === "%" ? `${text}%` : `${text} ${unit}`
|
||||
}
|
||||
|
||||
// Height runs from 0 at the bottom to 1 near the top, with room above for overshoot.
|
||||
const y = (value: number) => 42 - value * 32
|
||||
|
||||
function springPath(options: DurationOptions, span: number) {
|
||||
const spring = Spring.fromDuration(0, { duration: Math.max(0.03, options.duration), bounce: Math.min(0.95, options.bounce) })
|
||||
spring.set(1)
|
||||
const steps = 96
|
||||
|
||||
return Array.from({ length: steps + 1 }, (_, index) => {
|
||||
const point = `${index === 0 ? "M" : "L"}${((index / steps) * 240).toFixed(1)},${y(spring.value).toFixed(1)}`
|
||||
spring.step(span / steps)
|
||||
|
||||
return point
|
||||
}).join("")
|
||||
}
|
||||
|
||||
function bezierPath(curve: Bezier) {
|
||||
const [x1, y1, x2, y2] = curve
|
||||
const x = (value: number) => 8 + value * 224
|
||||
|
||||
return `M${x(0)},${y(0)} C${x(x1)},${y(y1)} ${x(x2)},${y(y2)} ${x(1)},${y(1)}`
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import { createMemo, createSignal, For, Index, onCleanup, onMount, Show } from "solid-js"
|
||||
import { createMemo, createSignal, Index, onCleanup, onMount, Show } from "solid-js"
|
||||
import { Portal } from "solid-js/web"
|
||||
import { createRemoteControls, type ControlDescriptor, type State, type Value } from "knobs"
|
||||
import { KnobsPanel } from "knobs/solid"
|
||||
import { Icon } from "@opencode/ui/icon"
|
||||
import { IconButton } from "@opencode/ui/icon-button"
|
||||
import { springStyle } from "@opencode/ui/motion/solid"
|
||||
import { motionPresets, motionTuning } from "@opencode/ui/motion-tuning"
|
||||
import { motionPresets, motionTuning, type MotionKnob } from "@opencode/ui/motion-tuning"
|
||||
import {
|
||||
activeFeaturePreset,
|
||||
applyFeaturePreset,
|
||||
@@ -11,12 +13,12 @@ import {
|
||||
featureControls,
|
||||
featurePresets,
|
||||
motionFeatures,
|
||||
reset,
|
||||
resetFeature,
|
||||
type MotionFeature,
|
||||
} from "@opencode/ui/motion-tuning-features"
|
||||
import { SegmentedControl, SegmentedControlItem } from "@opencode/ui/segmented-control"
|
||||
import { applyMotion } from "@opencode/ui/motion-tuning-fields"
|
||||
import { useExtension } from "../sdk"
|
||||
import { ControlRow } from "./motion-rows"
|
||||
|
||||
// Where the panel was dragged to, as an offset from its bottom-right home, its width, and the feature it last showed;
|
||||
// kept per device.
|
||||
@@ -250,14 +252,7 @@ export default function Motion(props: { title: string; onClose: () => void }) {
|
||||
|
||||
const page = () => (
|
||||
<Show when={feature()}>
|
||||
{(item) => (
|
||||
<FeaturePage
|
||||
feature={item()}
|
||||
live={live}
|
||||
back={wide() ? undefined : back}
|
||||
onFocus={focus}
|
||||
/>
|
||||
)}
|
||||
{(item) => <FeaturePage feature={item()} live={live} back={wide() ? undefined : back} onFocus={focus} />}
|
||||
</Show>
|
||||
)
|
||||
|
||||
@@ -332,30 +327,32 @@ function FeaturePage(props: {
|
||||
back?: () => void
|
||||
onFocus: (target: string) => void
|
||||
}) {
|
||||
const ctx = useExtension()
|
||||
// knobs fixes a store's presets when it's made, so a new store is made whenever the rows, the mode or the replays
|
||||
// change.
|
||||
const rows = createMemo(
|
||||
() => {
|
||||
props.live()
|
||||
|
||||
const state = createMemo(() => {
|
||||
props.live()
|
||||
|
||||
return {
|
||||
controls: featureControls(props.feature),
|
||||
presets: featurePresets(props.feature),
|
||||
active: activeFeaturePreset(props.feature),
|
||||
changed: featureChanged(props.feature),
|
||||
on: props.feature.switch?.value !== false,
|
||||
}
|
||||
})
|
||||
|
||||
const presetLabel = (preset: string) => {
|
||||
if (preset === "shipped") return ctx.t("motion.shipped")
|
||||
|
||||
if (preset === "snappy" || preset === "smooth" || preset === "calm") return motionPresets[preset]
|
||||
|
||||
return props.feature.modes.find((item) => item.id === preset)?.label ?? preset
|
||||
}
|
||||
return {
|
||||
feature: props.feature,
|
||||
mode: props.feature.modes.find((item) => item.value),
|
||||
controls: featureControls(props.feature),
|
||||
}
|
||||
},
|
||||
undefined,
|
||||
{
|
||||
equals: (a, b) =>
|
||||
!!a &&
|
||||
a.feature.key === b.feature.key &&
|
||||
a.feature.switch === b.feature.switch &&
|
||||
a.mode === b.mode &&
|
||||
same(a.controls, b.controls) &&
|
||||
same(a.feature.actions, b.feature.actions),
|
||||
},
|
||||
)
|
||||
|
||||
return (
|
||||
<div class="pb-6">
|
||||
<div class="pb-3">
|
||||
<Show when={props.back}>
|
||||
{(back) => (
|
||||
<button
|
||||
@@ -368,86 +365,194 @@ function FeaturePage(props: {
|
||||
</button>
|
||||
)}
|
||||
</Show>
|
||||
<h3 classList={{ "px-4 text-[17px] font-semibold leading-6": true, "pt-4": !props.back }}>{props.feature.name}</h3>
|
||||
<h3 classList={{ "px-4 text-[15px] font-semibold leading-5": true, "pt-3": !props.back }}>
|
||||
{props.feature.name}
|
||||
</h3>
|
||||
<Show when={props.feature.description}>
|
||||
{(description) => <p class="mt-1 px-4 text-[13px] leading-[18px] text-v2-text-text-muted">{description()}</p>}
|
||||
{(description) => <p class="px-4 text-[12px] leading-[18px] text-v2-text-text-muted">{description()}</p>}
|
||||
</Show>
|
||||
<div class="flex flex-wrap items-center gap-2 px-4 pt-4">
|
||||
{/* One trigger is Replay; several replay on their own, each named for what it moves. */}
|
||||
<For each={props.feature.actions}>
|
||||
{(item) => (
|
||||
<PageButton
|
||||
icon="outline-rotate-clockwise"
|
||||
label={props.feature.actions.length === 1 ? ctx.t("motion.replay") : item.label}
|
||||
onClick={() => item.run()}
|
||||
/>
|
||||
)}
|
||||
</For>
|
||||
<Show when={props.feature.target}>
|
||||
{(target) => (
|
||||
<PageButton icon="select-element" label={ctx.t("motion.focus")} onClick={() => props.onFocus(target())} />
|
||||
)}
|
||||
</Show>
|
||||
<span class="ms-auto">
|
||||
<PageButton
|
||||
icon="outline-reset"
|
||||
label={ctx.t("motion.reset")}
|
||||
disabled={!state().changed}
|
||||
onClick={() => resetFeature(props.feature)}
|
||||
<Show when={rows()} keyed>
|
||||
{(shown) => (
|
||||
<FeatureKnobs
|
||||
feature={shown.feature}
|
||||
mode={shown.mode}
|
||||
controls={shown.controls}
|
||||
live={props.live}
|
||||
onFocus={props.onFocus}
|
||||
/>
|
||||
</span>
|
||||
</div>
|
||||
<Show when={state().on && state().presets.length > 0}>
|
||||
<div class="px-4 pt-4">
|
||||
<SegmentedControl
|
||||
class="segmented-control-v2--full-width"
|
||||
aria-label={ctx.t("motion.presets")}
|
||||
value={state().active ?? null}
|
||||
onChange={(preset) => {
|
||||
if (preset) applyFeaturePreset(props.feature, preset)
|
||||
}}
|
||||
>
|
||||
<For each={state().presets}>
|
||||
{(preset) => (
|
||||
<SegmentedControlItem class="text-[12px]" style={{ padding: "0 4px" }} value={preset}>
|
||||
<span class="truncate">{presetLabel(preset)}</span>
|
||||
</SegmentedControlItem>
|
||||
)}
|
||||
</For>
|
||||
</SegmentedControl>
|
||||
<Show when={!state().active}>
|
||||
<p class="mt-1.5 text-[12px] leading-4 text-v2-text-text-faint">{ctx.t("motion.custom")}</p>
|
||||
</Show>
|
||||
</div>
|
||||
)}
|
||||
</Show>
|
||||
<div class="mt-4 divide-y divide-v2-border-border-muted border-t border-v2-border-border-muted">
|
||||
{/* Keyed: a row keeps the knob it mounted with, so another feature's switch needs a row of its own. */}
|
||||
<Show when={props.feature.switch} keyed>
|
||||
{(item) => <ControlRow knob={item} live={props.live} label={ctx.t("motion.enabled")} />}
|
||||
</Show>
|
||||
<For each={state().controls}>{(item) => <ControlRow knob={item} live={props.live} />}</For>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function PageButton(props: {
|
||||
icon: "outline-rotate-clockwise" | "select-element" | "outline-reset"
|
||||
label: string
|
||||
disabled?: boolean
|
||||
onClick: () => void
|
||||
/**
|
||||
* A feature's knobs as one embedded knobs group bound to the registry: Replay and Focus beside its reset and die, its
|
||||
* presets, and its rows. Choosing a preset goes through the feature model, so a mode turns on as it does from
|
||||
* anywhere else.
|
||||
*/
|
||||
function FeatureKnobs(props: {
|
||||
feature: MotionFeature
|
||||
mode?: MotionKnob
|
||||
controls: MotionKnob[]
|
||||
live: () => number
|
||||
onFocus: (target: string) => void
|
||||
}) {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
disabled={props.disabled}
|
||||
class="flex h-8 items-center gap-1.5 rounded-lg bg-v2-background-bg-layer-01 ps-2.5 pe-3 text-[13px] font-medium text-v2-text-text-base shadow-[0_0_0_0.5px_var(--v2-border-border-base)] hover:bg-v2-background-bg-layer-02 focus-visible:outline focus-visible:outline-2 focus-visible:outline-v2-border-border-focus disabled:pointer-events-none disabled:text-v2-text-text-faint"
|
||||
onClick={() => props.onClick()}
|
||||
>
|
||||
<Icon name={props.icon} size="small" class="text-v2-icon-icon-muted" />
|
||||
<span>{props.label}</span>
|
||||
</button>
|
||||
const ctx = useExtension()
|
||||
const feature = props.feature
|
||||
const group = feature.key
|
||||
const toggle = feature.switch
|
||||
const knobs = [...(toggle ? [toggle] : []), ...props.controls]
|
||||
const byId = new Map(knobs.map((item) => [item.id, item]))
|
||||
// While a mode is on, the spring presets don't move its springs; choosing one turns the mode off first.
|
||||
const springs = props.mode ? [] : props.controls.flatMap((item) => (item.kind === "spring" ? [item] : []))
|
||||
const presets = featurePresets(feature).filter((preset) => preset !== "shipped")
|
||||
|
||||
const preset = (id: string): State => {
|
||||
if (id !== "snappy" && id !== "smooth" && id !== "calm") return {}
|
||||
|
||||
return Object.fromEntries(springs.map((item) => [item.id, { ...item.presets[id] }]))
|
||||
}
|
||||
|
||||
const target = feature.target
|
||||
|
||||
const groupActions = () => [
|
||||
// One trigger is Replay; several replay on their own, each named for what it moves.
|
||||
...feature.actions.map((item) => ({
|
||||
label: feature.actions.length === 1 ? ctx.t("motion.replay") : item.label,
|
||||
icon: "retrigger",
|
||||
run: () => item.run(),
|
||||
})),
|
||||
...(target ? [{ label: ctx.t("motion.focus"), icon: "target", run: () => props.onFocus(target) }] : []),
|
||||
]
|
||||
|
||||
// Everything but the switch dims while it's off.
|
||||
const off = toggle ? (values: State) => values[toggle.id] === false : undefined
|
||||
|
||||
const descriptors = knobs.map((item) =>
|
||||
item === toggle ? descriptor(item, group, ctx.t("motion.enabled")) : descriptor(item, group, item.label, off),
|
||||
)
|
||||
|
||||
const values = () => {
|
||||
props.live()
|
||||
|
||||
return Object.fromEntries(knobs.map((item) => [item.id, item.value]))
|
||||
}
|
||||
|
||||
const set = (key: string, value: Value | null) => {
|
||||
const item = byId.get(key)
|
||||
|
||||
// With nullDefaults off, null never arrives; resets reach onReset.
|
||||
if (!item || value === null) return
|
||||
|
||||
// SAFETY: each key's descriptor was made from its knob, so knobs sends that knob's kind of value.
|
||||
applyMotion(item, value as MotionKnob["value"])
|
||||
}
|
||||
|
||||
const controls = createRemoteControls(`opencode-motion-${group}`, descriptors, {
|
||||
values: values(),
|
||||
// Beta is a production build, where knobs resolves its production entry: the panel mounts only when shipped.
|
||||
ship: () => import("knobs/panel"),
|
||||
nullDefaults: false,
|
||||
echo: 0,
|
||||
persist: "none",
|
||||
storage: null,
|
||||
defaultLabel: ctx.t("motion.shipped"),
|
||||
groupLabels: { [group]: feature.name },
|
||||
presetLabels: { ...motionPresets, ...Object.fromEntries(feature.modes.map((item) => [item.id, item.label])) },
|
||||
scopedPresets:
|
||||
presets.length > 0 ? { [group]: Object.fromEntries(presets.map((id) => [id, preset(id)])) } : undefined,
|
||||
onSet: set,
|
||||
onSetBatch: (changes) => changes.forEach((change) => set(change.key, change.value)),
|
||||
// A group's reset is the feature's, modes included.
|
||||
onReset: (scope) => {
|
||||
if (!scope.key) return resetFeature(feature)
|
||||
|
||||
const item = byId.get(scope.key)
|
||||
|
||||
if (item) reset(item)
|
||||
},
|
||||
})
|
||||
|
||||
// Shows the preset the registry is on: a mode, or what its values match.
|
||||
const current = activeFeaturePreset(feature)
|
||||
|
||||
if (current && current !== "shipped" && presets.includes(current)) controls.preset(current, { scope: group })
|
||||
|
||||
let chosen = controls.activePreset(group)?.id
|
||||
|
||||
// A chip chosen in the strip applies through the feature model, which turns modes on and off.
|
||||
onCleanup(
|
||||
controls.subscribe(() => {
|
||||
const next = controls.activePreset(group)?.id
|
||||
|
||||
if (next === chosen) return
|
||||
|
||||
chosen = next
|
||||
|
||||
// The Shipped chip clears the scope's active preset.
|
||||
applyFeaturePreset(feature, next ?? "shipped")
|
||||
}),
|
||||
)
|
||||
|
||||
return (
|
||||
// Inset so the rows' labels line up with the page's title.
|
||||
<div class="px-2 pt-1">
|
||||
<KnobsPanel
|
||||
controls={controls}
|
||||
values={values()}
|
||||
theme={document.documentElement.dataset.colorScheme === "light" ? "light" : "dark"}
|
||||
embed
|
||||
surface="flat"
|
||||
groups={[group]}
|
||||
groupActions={groupActions}
|
||||
hints="panel"
|
||||
session={false}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function descriptor(
|
||||
item: MotionKnob,
|
||||
group: string,
|
||||
label = item.label,
|
||||
disabled?: (values: State) => boolean,
|
||||
): ControlDescriptor {
|
||||
// Experiments are the design under test, not tuning: presets leave them be.
|
||||
const meta = {
|
||||
key: item.id,
|
||||
label,
|
||||
group,
|
||||
description: item.description,
|
||||
inPresets: item.experiment ? (false as const) : undefined,
|
||||
disabled,
|
||||
}
|
||||
|
||||
if (item.kind === "number")
|
||||
return {
|
||||
...meta,
|
||||
kind: "number",
|
||||
default: item.initial,
|
||||
min: item.min,
|
||||
max: item.max,
|
||||
step: item.step,
|
||||
unit: item.unit,
|
||||
}
|
||||
|
||||
if (item.kind === "choice")
|
||||
return {
|
||||
...meta,
|
||||
kind: "choice",
|
||||
default: item.initial,
|
||||
choices: item.options.map((option) => ({ id: option, label: item.labels[option] ?? option })),
|
||||
}
|
||||
|
||||
if (item.kind === "toggle" || item.kind === "mode") return { ...meta, kind: "toggle", default: item.initial }
|
||||
|
||||
if (item.kind === "spring") return { ...meta, kind: "spring", default: { ...item.initial } }
|
||||
|
||||
return { ...meta, kind: "easing", default: [...item.initial] }
|
||||
}
|
||||
|
||||
// Frames the part of the app a feature changes: a pointer-transparent outline that fades in and out. Its rects are read
|
||||
@@ -504,3 +609,7 @@ function save(state: { x: number; y: number; width: number; feature: string | un
|
||||
localStorage.setItem(key, JSON.stringify(state))
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function same<T>(a: T[], b: T[]) {
|
||||
return a.length === b.length && a.every((item, index) => item === b[index])
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@
|
||||
"@solid-primitives/resize-observer": "catalog:",
|
||||
"fuzzysort": "catalog:",
|
||||
"katex": "0.16.47",
|
||||
"knobs": "npm:@kitlangton/knobs@0.4.0-next.15",
|
||||
"knobs": "npm:@kitlangton/knobs@0.4.0-next.22",
|
||||
"marked": "catalog:",
|
||||
"motion": "12.34.5",
|
||||
"remeda": "catalog:",
|
||||
|
||||
Reference in New Issue
Block a user