Skip to content

Commit 01aa446

Browse files
feat(studio): shared Button, IconButton, Tooltip and Tabs primitives (#3622)
* feat(studio): shared Button, IconButton, Tooltip and Tabs primitives Four primitives that consume design tokens only, plus the configured class merger they all share. - ui/cn.ts: one `cn` instance whose config knows the two class families theme.css invents, the `text-step-*` type scale and the `ctl` control heights, so a caller's class replaces a primitive's instead of stacking with it and letting stylesheet order decide. - Button and IconButton: native buttons on the three control heights, the radius scale, the semantic palette and the motion tokens. Four classes that resolved to nothing (rounded-button, shadow-btn-primary, bg-accent-red, ease-standard) are gone, which is why the Renders Export now has a radius. - Tooltip: Base UI's tooltip, so the portal, the viewport flip and the collision shift stop being hand-rolled. role="tooltip", the id wiring and aria-describedby stay explicit here because the library does not add them. - Tabs: Base UI's tabs, with roving tabindex, arrow keys, Home and End, and a data-tab-id the sidebar's focus restore looks up. Every interactive look is also reachable through a data-preview-state attribute that CSS alone reads, so a gallery can show hover, active and focus without a pointer. A test pairs the two lists in both directions. The class-resolution test compiles Studio's real stylesheet with every class the primitives emit and fails on any that produces no selector. Tailwind treats an undefined class as silence; that silence is what let four dead classes sit in this file. * feat(studio): export Button, IconButton, Tabs and Tooltip from the package root * docs(studio): trim comment blocks to pass the no-essay-comments gate * refactor(studio): use native CSS.escape in the button class-resolution test * refactor(studio): share the ui test mount helper and flatten the Button leading slot * fix(studio): keep the tooltip open on a disabled control * fix(studio): give the tooltip trigger no layout box and anchor to the child * fix(studio): anchor the tooltip through a ref so it adds no render * fix(studio): button children lay out with the consumer gap * test(studio): keep the no-icon label and shortcut layout assertion from the standalone fix * docs(studio): shorten the Tooltip header comment to fit the comment gate * test(studio): the Button vacuity guard names tokens that stay undefined
1 parent fd020ca commit 01aa446

15 files changed

Lines changed: 871 additions & 295 deletions

‎.fallowrc.jsonc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/fallow-rs/fallow/main/config-schema.json",
33
"entry": [
4+
// IconButton, Tabs and cn are exported before their first consumer exists.
5+
"packages/studio/src/components/ui/index.ts",
46
"packages/producer/src/**/*.test.ts",
57
"packages/aws-lambda/src/**/*.test.ts",
68
"packages/gcp-cloud-run/src/**/*.test.ts",

‎bun.lock‎

Lines changed: 113 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/studio/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"test:timeline-default": "bun run test:timeline-virtualization"
5959
},
6060
"dependencies": {
61+
"@base-ui/react": "1.7.0",
6162
"@codemirror/autocomplete": "^6.20.1",
6263
"@codemirror/commands": "^6.10.3",
6364
"@codemirror/lang-css": "^6.3.1",
@@ -78,6 +79,7 @@
7879
"@phosphor-icons/react": "^2.1.10",
7980
"@tanstack/react-virtual": "^3.14.6",
8081
"bpm-detective": "^2.0.5",
82+
"cn": "0.2.4",
8183
"dockview-react": "8.3.1",
8284
"gsap": "^3.13.0",
8385
"mediabunny": "^1.45.3"

‎packages/studio/src/components/ui/Button.test.tsx‎

Lines changed: 222 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,249 @@
11
// @vitest-environment happy-dom
22

3-
// happy-dom has no CSS engine, so this pins the structure that makes the gap
4-
// work (children are direct flex items of the gap-bearing button), not the
5-
// computed pixels. The measured gap is verified in a real browser.
3+
/**
4+
* Button/IconButton: renders every variant and size, then compiles Studio's
5+
* real stylesheet against the emitted classes so an unstyled class fails.
6+
*/
7+
import { createRequire } from "node:module";
8+
import { readFileSync } from "node:fs";
9+
import path from "node:path";
10+
import React, { act } from "react";
11+
import { compile } from "tailwindcss";
12+
import { afterEach, beforeAll, describe, expect, it } from "vitest";
13+
import { Button, type ButtonSize, type ButtonVariant } from "./Button";
14+
import { IconButton } from "./IconButton";
15+
import { Tab, Tabs, TabsList } from "./Tabs";
16+
import { cleanupMounted, mountHost } from "./mountHost.testHelpers";
17+
import { isTypingTarget } from "../../utils/typingTarget";
18+
import { shouldIgnorePlaybackShortcutTarget } from "../../player/lib/playbackShortcuts";
619

7-
import { act } from "react";
8-
import { createRoot, type Root } from "react-dom/client";
9-
import { afterEach, describe, expect, it } from "vitest";
10-
import { Button } from "./Button";
20+
const VARIANTS: ButtonVariant[] = ["primary", "secondary", "danger", "ghost"];
21+
const SIZES: ButtonSize[] = ["sm", "md", "lg"];
1122

12-
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
23+
afterEach(cleanupMounted);
1324

14-
let root: Root | null = null;
25+
function classesOf(host: HTMLElement, selector = "button"): string[] {
26+
const el = host.querySelector(selector);
27+
if (!el) throw new Error(`nothing matched ${selector}`);
28+
return [...el.classList];
29+
}
30+
31+
// -- the compiled stylesheet, built once for the whole file --
1532

16-
afterEach(() => {
17-
if (root) act(() => root?.unmount());
18-
root = null;
19-
document.body.innerHTML = "";
33+
const STYLES_DIR = path.resolve(__dirname, "../../styles");
34+
const require = createRequire(import.meta.url);
35+
const TAILWIND_DIR = path.dirname(require.resolve("tailwindcss/package.json"));
36+
37+
async function loadStylesheet(id: string, base: string) {
38+
const file = id === "tailwindcss" ? path.join(TAILWIND_DIR, "index.css") : path.resolve(base, id);
39+
return { path: file, base: path.dirname(file), content: readFileSync(file, "utf8") };
40+
}
41+
42+
let compileStudioCss: (candidates: string[]) => string;
43+
44+
beforeAll(async () => {
45+
const compiled = await compile(readFileSync(path.join(STYLES_DIR, "studio.css"), "utf8"), {
46+
base: STYLES_DIR,
47+
loadStylesheet,
48+
});
49+
compileStudioCss = (candidates) => compiled.build(candidates);
2050
});
2151

22-
function mount(element: React.ReactNode): HTMLButtonElement {
23-
const host = document.createElement("div");
24-
document.body.append(host);
25-
root = createRoot(host);
26-
act(() => root?.render(element));
27-
const button = host.querySelector("button");
28-
if (!button) throw new Error("button did not render");
29-
return button;
52+
function unresolved(candidates: string[]): string[] {
53+
const css = compileStudioCss(candidates);
54+
return candidates.filter((candidate) => !css.includes(CSS.escape(candidate)));
3055
}
3156

32-
describe("Button children layout", () => {
33-
it("lays label and kbd out as sibling flex items of the gap-bearing button", () => {
34-
const button = mount(
57+
describe("Button classes", () => {
58+
it("emits only classes Studio's stylesheet defines", () => {
59+
const emitted = new Set<string>();
60+
for (const variant of VARIANTS) {
61+
for (const size of SIZES) {
62+
const host = mountHost(
63+
<>
64+
<Button variant={variant} size={size}>
65+
Export
66+
</Button>
67+
<IconButton variant={variant} size={size} icon={null} aria-label="Zoom out" />
68+
</>,
69+
);
70+
for (const el of host.querySelectorAll("button")) {
71+
for (const cls of el.classList) emitted.add(cls);
72+
}
73+
cleanupMounted();
74+
}
75+
}
76+
77+
expect(emitted.size).toBeGreaterThan(20);
78+
expect(unresolved([...emitted])).toEqual([]);
79+
});
80+
81+
it("fails an undefined class, so the check above is not vacuous", () => {
82+
// Names in a real namespace with no token behind them; the theme file now defines the
83+
// `rounded-button` pair this used to name.
84+
expect(unresolved(["bg-not-a-token", "text-step-999"])).toEqual([
85+
"bg-not-a-token",
86+
"text-step-999",
87+
]);
88+
});
89+
90+
it("lets a caller's type size beat the size's own", () => {
91+
// The Renders Export case: `size="md"` brings `text-step-12`, the caller
92+
// wants 11px, and with plain concatenation both survived into the class
93+
// list and the stylesheet's order decided the winner.
94+
const classes = classesOf(mountHost(<Button size="md" className="text-step-11" />));
95+
96+
expect(classes).toContain("text-step-11");
97+
expect(classes).not.toContain("text-step-12");
98+
});
99+
100+
it("keeps every interactive look reachable without a pointer", () => {
101+
// `data-preview-state` exists so a gallery can show the hover, active and
102+
// focus looks. It is worth nothing if it drifts from the real state, so
103+
// each pair is matched here in both directions.
104+
const realPrefix = {
105+
hover: ["enabled:hover:", "hover:"],
106+
active: ["enabled:active:", "active:"],
107+
focus: ["focus-visible:"],
108+
};
109+
110+
for (const variant of VARIANTS) {
111+
const classes = classesOf(mountHost(<Button variant={variant}>Export</Button>));
112+
cleanupMounted();
113+
114+
for (const [state, prefixes] of Object.entries(realPrefix)) {
115+
const previewPrefix = `data-[preview-state=${state}]:`;
116+
const preview = classes
117+
.filter((cls) => cls.startsWith(previewPrefix))
118+
.map((cls) => cls.slice(previewPrefix.length));
119+
const real = classes
120+
.filter((cls) => prefixes.some((prefix) => cls.startsWith(prefix)))
121+
.map((cls) => cls.slice(cls.lastIndexOf(":") + 1));
122+
123+
expect([...preview].sort(), `${variant} ${state}`).toEqual([...real].sort());
124+
expect(preview.length, `${variant} ${state}`).toBeGreaterThan(0);
125+
}
126+
}
127+
});
128+
129+
it("uses a motion token that zeroes itself under reduced motion", () => {
130+
// AE4. The zero-duration case lives in the `duration-press` utility itself
131+
// (theme.css), not in a `motion-reduce:` class beside it, so a caller
132+
// cannot use the token and forget the reduced-motion half. The button's
133+
// job is to name the token; the stylesheet's job is the media query.
134+
expect(classesOf(mountHost(<Button>Export</Button>))).toContain("duration-press");
135+
136+
expect(compileStudioCss(["duration-press"])).toMatch(
137+
/\.duration-press \{[\s\S]*?prefers-reduced-motion: reduce[\s\S]*?transition-duration: 0ms/,
138+
);
139+
});
140+
});
141+
142+
describe("Button behaviour", () => {
143+
it("does not fire a disabled click, and says it is disabled", () => {
144+
let clicks = 0;
145+
const host = mountHost(
146+
<Button disabled onClick={() => (clicks += 1)}>
147+
Export
148+
</Button>,
149+
);
150+
const button = host.querySelector("button")!;
151+
152+
act(() => button.click());
153+
154+
expect(clicks).toBe(0);
155+
expect(button.getAttribute("aria-disabled")).toBe("true");
156+
});
157+
158+
it("still fires a click while a preview state is forced", () => {
159+
let clicks = 0;
160+
const host = mountHost(
161+
<Button data-preview-state="hover" onClick={() => (clicks += 1)}>
162+
Export
163+
</Button>,
164+
);
165+
const button = host.querySelector("button")!;
166+
167+
act(() => button.click());
168+
169+
expect(clicks).toBe(1);
170+
expect(button.getAttribute("data-preview-state")).toBe("hover");
171+
});
172+
173+
it("is classified by the hotkey selectors the way a plain button is", () => {
174+
// KTD13. Both selector lists gate on roles and element names. A primitive
175+
// that landed on a different role would leak or swallow global hotkeys with
176+
// nothing to notice it.
177+
const host = mountHost(
178+
<>
179+
<button type="button" data-testid="plain">
180+
Export
181+
</button>
182+
<Button data-testid="primitive">Export</Button>
183+
<Tabs defaultValue="code">
184+
<TabsList aria-label="Sidebar panels">
185+
<Tab value="code">Code</Tab>
186+
</TabsList>
187+
</Tabs>
188+
</>,
189+
);
190+
const plain = host.querySelector('[data-testid="plain"]')!;
191+
const button = host.querySelector('[data-testid="primitive"]')!;
192+
const tab = host.querySelector('[role="tab"]')!;
193+
194+
for (const el of [plain, button, tab]) {
195+
expect(isTypingTarget(el)).toBe(false);
196+
expect(shouldIgnorePlaybackShortcutTarget(el)).toBe(true);
197+
}
198+
});
199+
});
200+
201+
describe("disabled states", () => {
202+
it("disables a loading Button", () => {
203+
const host = mountHost(<Button loading>Save</Button>);
204+
const button = host.querySelector("button");
205+
206+
expect(button?.disabled).toBe(true);
207+
expect(button?.getAttribute("aria-disabled")).toBe("true");
208+
});
209+
210+
it("marks a disabled IconButton for assistive tech", () => {
211+
const host = mountHost(<IconButton aria-label="Undo" icon={<svg />} disabled />);
212+
const button = host.querySelector("button");
213+
214+
expect(button?.disabled).toBe(true);
215+
expect(button?.getAttribute("aria-disabled")).toBe("true");
216+
});
217+
});
218+
219+
describe("children layout", () => {
220+
it("keeps label and shortcut direct flex items of the gap-bearing button without an icon", () => {
221+
const host = mountHost(
35222
<Button size="md">
36223
<span>Export</span>
37224
<kbd>⌘E</kbd>
38225
</Button>,
39226
);
227+
const button = host.querySelector("button");
40228

41-
expect(button.classList.contains("inline-flex")).toBe(true);
42-
expect(button.classList.contains("gap-1.5")).toBe(true);
43-
expect(Array.from(button.children).map((child) => child.tagName)).toEqual(["SPAN", "KBD"]);
229+
expect(button?.classList.contains("inline-flex")).toBe(true);
230+
expect(button?.classList.contains("gap-1.5")).toBe(true);
231+
expect(Array.from(button?.children ?? []).map((child) => child.tagName)).toEqual([
232+
"SPAN",
233+
"KBD",
234+
]);
44235
});
45236

46-
it("puts children next to the icon as direct flex items too", () => {
47-
const button = mount(
237+
it("makes label and shortcut direct flex items so the button gap applies between them", () => {
238+
const host = mountHost(
48239
<Button icon={<svg />}>
49240
<span>Export</span>
50241
<kbd>⌘E</kbd>
51242
</Button>,
52243
);
244+
const button = host.querySelector("button");
53245

54-
expect(Array.from(button.children).map((child) => child.tagName)).toEqual([
246+
expect(Array.from(button?.children ?? []).map((child) => child.tagName)).toEqual([
55247
"SPAN",
56248
"SPAN",
57249
"KBD",

0 commit comments

Comments
 (0)