Skip to content

Commit 3ec26b5

Browse files
feat(studio): shared menu, popover and value-control primitives (#4191)
* feat(studio): shared Menu, ContextMenu and Popover primitives One menu chrome and one popover chrome, on Base UI, wearing the theme tokens. Dismissal is plain Base UI with no capture-phase wrapper: its outside-press listeners are registered on document with capture set, so the canvas overlay's bubble-phase stopPropagation cannot starve them. Menu items carry an optional mono shortcut hint, a disabled state and a danger tone. ContextMenu opens at the pointer. Popover shares the surface, radius and open motion but owns no item semantics, so a text field inside it keeps its own keys. Also adds [role='menuitemradio'] to the playback-shortcut ignore list. The hand-rolled speed menu spelled its rows as buttons, which that list already matched; the shared radio item is a div with the same role, and nothing else in the list matched it, so an open speed menu would have leaked arrow keys and Space to the player. * feat(studio): shared Input, NumberField, Select, Slider and Toggle primitives The five value controls exist once, on Base UI, wearing the theme tokens. Each is boxed with a visible border and a focus ring so an inspector value reads as an input rather than as its own label. Value writes happen at a commit boundary, never per keystroke or per pointer move: one drag is one onCommit and one telemetry call. Right-click and Escape abort a drag in flight and put the pre-drag value back. The switch role joins the typing-target selector list, so a Toggle keeps the keys the native checkbox it replaces owned. No consumer moves onto these yet. * refactor(studio): shorten the primitives' comments and stop a default string reading as a class Main's comment gate rejects long comment blocks, and the class gate read the MenuItem tone default as a Tailwind class. The prop is optional instead. * feat(studio): export the shared primitives from the package entry A host mounting Studio pieces in its own tree imports them from the package: cn, Menu family, Popover, Input, NumberField, Select, Slider and Toggle.
1 parent e505dcc commit 3ec26b5

13 files changed

Lines changed: 1789 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Input: a boxed text field. Typing edits a local draft; Enter and blur commit it,
3+
* Escape abandons it. `onTrack` fires once per commit, never per keystroke.
4+
*/
5+
6+
import { Input as BaseInput } from "@base-ui/react/input";
7+
import { useEffect, useRef, useState, type ComponentPropsWithoutRef } from "react";
8+
import { cn } from "./cn";
9+
import type { PreviewState } from "./Button";
10+
11+
/**
12+
* Shared field look (also used by NumberField and Select). Each interactive state is written
13+
* twice, real and `data-[preview-state=...]`; `valueControls.test.tsx` asserts the lists match.
14+
*/
15+
export const fieldBase = cn(
16+
"flex min-w-0 items-center gap-1.5 h-ctl px-2 rounded-md",
17+
"bg-input border border-border-input text-step-11 font-medium text-text-1",
18+
"transition-[border-color,background-color] ease-standard duration-focus",
19+
"hover:border-border-strong data-[preview-state=hover]:border-border-strong",
20+
"outline-hidden",
21+
"focus-within:outline-solid focus-within:outline-2 focus-within:outline-offset-1 focus-within:outline-accent",
22+
"data-[preview-state=focus]:outline-solid data-[preview-state=focus]:outline-2 data-[preview-state=focus]:outline-offset-1 data-[preview-state=focus]:outline-accent",
23+
// `aria-invalid` is not one of Tailwind's built-in aria variants, so the
24+
// arbitrary form is the one that compiles. The token gate would have caught
25+
// the bare `aria-invalid:` spelling as a class that resolves to nothing.
26+
"aria-[invalid]:border-danger",
27+
"has-[:disabled]:opacity-40 has-[:disabled]:cursor-not-allowed",
28+
);
29+
30+
/** The text itself, inside the box. The box owns the border and the ring. */
31+
export const fieldText = cn(
32+
"min-w-0 w-full bg-transparent text-inherit outline-hidden",
33+
"placeholder:text-text-5 disabled:cursor-not-allowed",
34+
);
35+
36+
export interface InputProps extends Omit<
37+
ComponentPropsWithoutRef<"input">,
38+
"value" | "onChange" | "className"
39+
> {
40+
/** The committed value. A draft lives inside until Enter or blur. */
41+
value: string;
42+
/** Called with the draft on Enter and on blur, only when it really changed. */
43+
onCommit: (next: string) => void;
44+
/** Called once per committed change, for design-input telemetry. */
45+
onTrack?: () => void;
46+
/** Marks the field invalid: red boundary and `aria-invalid`. */
47+
invalid?: boolean;
48+
className?: string;
49+
"data-preview-state"?: PreviewState;
50+
}
51+
52+
export function Input({
53+
value,
54+
onCommit,
55+
onTrack,
56+
invalid,
57+
disabled,
58+
className,
59+
"data-preview-state": previewState,
60+
...props
61+
}: InputProps) {
62+
const [draft, setDraft] = useState(value);
63+
const valueRef = useRef(value);
64+
valueRef.current = value;
65+
const dirtyRef = useRef(false);
66+
67+
// An external write (a seek, an undo, another panel) is authoritative, but it
68+
// must not eat what the user is halfway through typing.
69+
useEffect(() => {
70+
if (!dirtyRef.current) setDraft(value);
71+
}, [value]);
72+
73+
const commit = (next: string) => {
74+
dirtyRef.current = false;
75+
if (next === valueRef.current) return;
76+
onCommit(next);
77+
onTrack?.();
78+
};
79+
80+
return (
81+
<div
82+
className={cn(fieldBase, className)}
83+
aria-invalid={invalid || undefined}
84+
data-preview-state={previewState}
85+
>
86+
<BaseInput
87+
value={draft}
88+
disabled={disabled}
89+
aria-invalid={invalid || undefined}
90+
className={fieldText}
91+
onValueChange={(next) => {
92+
dirtyRef.current = true;
93+
setDraft(next);
94+
}}
95+
onBlur={() => commit(draft)}
96+
onKeyDown={(event) => {
97+
if (event.key === "Enter") {
98+
event.preventDefault();
99+
commit(draft);
100+
return;
101+
}
102+
if (event.key === "Escape") {
103+
// Abandon the draft rather than leave it for the blur commit.
104+
event.preventDefault();
105+
event.stopPropagation();
106+
dirtyRef.current = false;
107+
setDraft(valueRef.current);
108+
event.currentTarget.blur();
109+
}
110+
}}
111+
{...props}
112+
/>
113+
</div>
114+
);
115+
}

0 commit comments

Comments
 (0)