Skip to content

Isometric line figures that answer the pointer. For React and for anything with a DOM.

Package: a play option walks the figure's tour until a hand or focus arrives

Lucas Marques919ea062ec06
diff --git a/packages/hairline/src/mount.ts b/packages/hairline/src/mount.ts
index 9446c3e..1414ffe 100644
--- a/packages/hairline/src/mount.ts
+++ b/packages/hairline/src/mount.ts
@@ -1,5 +1,5 @@
import { inject } from "./core/styles";
-import type { FigureMount, Readout } from "./core/stage";
+import { LAP, tour, type FigureMount, type Readout, type Tour, type TourHandle } from "./core/stage";
import { parameter, type FigureId } from "./intensity";
/**
@@ -21,6 +21,12 @@ export type HairlineOptions = {
label?: string;
/** The figure's caption, each time it changes. Called once at mount with the rest caption. */
onRead?: (text: string) => void;
+ /**
+ * Walks the figure through its answer on its own, in a loop, until the
+ * pointer or focus arrives; it resumes after they leave. Only `true` plays.
+ * Under prefers-reduced-motion the figure rests. Default false.
+ */
+ play?: boolean;
};
export type Figure = {
@@ -40,6 +46,8 @@ export type Spec = {
engine: FigureMount;
/** Operable from the keyboard: a focusable group with a live region, not an image. */
focusable?: boolean;
+ /** Where play stops, in viewBox units; null leaves the stage. LAP when unset. */
+ tour?: Tour;
};
const NS = "http://www.w3.org/2000/svg";
@@ -108,6 +116,8 @@ export function create(spec: Spec, el: HTMLElement, options?: HairlineOptions):
};
let value = parameter(spec.id, opts.intensity);
+ /* the tour registers before the engine, so its board ticks first in a frame: the ghost moves, then the engine draws the answer */
+ let playing: TourHandle | null = opts.play === true ? tour(el, spec.tour ?? LAP) : null;
const engine = spec.engine({ stage: el, svg, read }, value);
if (text === null) read.textContent = spec.rest;
@@ -116,6 +126,8 @@ export function create(spec: Spec, el: HTMLElement, options?: HairlineOptions):
if (dead) return;
dead = true;
if (mounted.get(el) === destroy) mounted.delete(el);
+ playing?.stop();
+ playing = null;
engine.destroy();
svg.remove();
live?.remove();
@@ -135,6 +147,11 @@ export function create(spec: Spec, el: HTMLElement, options?: HairlineOptions):
}
const v = parameter(spec.id, opts.intensity);
if (v !== value) { value = v; engine.set(v); }
+ const play = opts.play === true;
+ if (play !== !!playing) {
+ playing?.stop();
+ playing = play ? tour(el, spec.tour ?? LAP) : null;
+ }
dress();
},
destroy,
diff --git a/packages/hairline/src/react.tsx b/packages/hairline/src/react.tsx
index c749624..52b148d 100644
--- a/packages/hairline/src/react.tsx
+++ b/packages/hairline/src/react.tsx
@@ -21,7 +21,7 @@ const useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayout
function make(name: string, mount: (el: HTMLElement, options?: HairlineOptions) => Figure): HairlineComponent {
const Component = forwardRef<HTMLDivElement, HairlineProps>(function Hairline(props, ref) {
- const { intensity, theme, label, onRead, style, ...attrs } = props;
+ const { intensity, theme, label, onRead, play, style, ...attrs } = props;
/* aria-label stays on the div and is the figure's label too, so the two never disagree about the name */
const named = label ?? props["aria-label"];
@@ -37,7 +37,7 @@ function make(name: string, mount: (el: HTMLElement, options?: HairlineOptions)
useIsoLayoutEffect(() => { read.current = onRead; });
useIsoLayoutEffect(() => {
- const f = mount(el.current!, { intensity, theme, label: named, onRead: (text) => read.current?.(text) });
+ const f = mount(el.current!, { intensity, theme, label: named, play, onRead: (text) => read.current?.(text) });
figure.current = f;
return () => { f.destroy(); figure.current = null; };
// mounts once; options reach the figure through the effect below
@@ -45,7 +45,7 @@ function make(name: string, mount: (el: HTMLElement, options?: HairlineOptions)
}, []);
/* every key is sent, so a prop that was removed goes back to its default */
- useIsoLayoutEffect(() => { figure.current?.update({ intensity, theme, label: named }); }, [intensity, theme, named]);
+ useIsoLayoutEffect(() => { figure.current?.update({ intensity, theme, label: named, play }); }, [intensity, theme, named, play]);
return <div {...attrs} ref={set} style={{ aspectRatio: "5 / 4", ...style }} />;
});
diff --git a/packages/hairline/test/mount.test.ts b/packages/hairline/test/mount.test.ts
index 0df936d..803bbc0 100644
--- a/packages/hairline/test/mount.test.ts
+++ b/packages/hairline/test/mount.test.ts
@@ -654,3 +654,43 @@ describe("basket", () => {
});
});
+describe("play", () => {
+ it("walks the figure on its own: the read-out leaves rest, and update({ play: false }) brings it back", () => {
+ const el = host(), reads: string[] = [];
+ const f = terrain(el, { play: true, onRead: (t) => reads.push(t) });
+ let n = 0;
+ while (reads.at(-1) === "rest" && n++ < 800) frames();
+ expect(reads.at(-1)).not.toBe("rest");
+ f.update({ play: false });
+ frames(60);
+ expect(reads.at(-1)).toBe("rest");
+ f.destroy();
+ });
+
+ it("update with the same play value does not restart the tour", () => {
+ const el = host(), reads: string[] = [];
+ const f = terrain(el, { play: true, onRead: (t) => reads.push(t) });
+ let n = 0;
+ while (reads.at(-1) === "rest" && n++ < 800) frames();
+ f.update({ play: true, intensity: 0.7 });
+ frames(5);
+ expect(reads.at(-1)).not.toBe("rest");
+ f.destroy();
+ });
+
+ it("plays only for true", () => {
+ const el = host(), reads: string[] = [];
+ const f = terrain(el, { play: 1 as unknown as boolean, onRead: (t) => reads.push(t) });
+ frames(800);
+ expect(reads).toEqual(["rest"]);
+ f.destroy();
+ });
+
+ it("destroy stops the tour with the figure", () => {
+ const f = terrain(host(), { play: true });
+ frames(10);
+ f.destroy();
+ expect(pending()).toBe(0);
+ });
+});
+
diff --git a/packages/hairline/test/react.test.tsx b/packages/hairline/test/react.test.tsx
index 13fc382..606af73 100644
--- a/packages/hairline/test/react.test.tsx
+++ b/packages/hairline/test/react.test.tsx
@@ -16,6 +16,18 @@ const sized = (el: Element) => {
const svg = (el: Element) => el.querySelector("svg")!.innerHTML.replace(/hl-fd\d+/g, "hl-fd");
describe("components", () => {
+ it("play walks the figure, and taking it away brings the read-out back to rest", () => {
+ const reads: string[] = [];
+ const { container, rerender } = render(<Terrain play onRead={(t) => reads.push(t)} />);
+ sized(container.firstElementChild as HTMLElement);
+ let n = 0;
+ while (reads.at(-1) === "rest" && n++ < 800) frames();
+ expect(reads.at(-1)).not.toBe("rest");
+ rerender(<Terrain onRead={(t) => reads.push(t)} />);
+ frames(60);
+ expect(reads.at(-1)).toBe("rest");
+ });
+
it("renders each figure into one div", () => {
const { container } = render(<><Riffle /><Terrain /><Exploded /><Phosphor /><Slow /><Turntable /><Keyboard /><Elevator /><Phone /><Laptop /><Terminal /><Cabinet /><Branches /><Vault /><Lockers /><Padlock /><Patch /><Dish /><Router /><Loupe /><Sieve /><Rail /><Plug /><Query /><Drawer /><Basket /><Plot /></>);
const ids = [...container.children].map((el) => el.getAttribute("data-hairline"));
diff --git a/packages/hairline/test/ssr.test.tsx b/packages/hairline/test/ssr.test.tsx
index 593e79c..176de0f 100644
--- a/packages/hairline/test/ssr.test.tsx
+++ b/packages/hairline/test/ssr.test.tsx
@@ -13,7 +13,7 @@ describe("on the server", () => {
it("renders a component as an empty box of the right shape", async () => {
const { Riffle } = await import("../src/react");
- const html = renderToString(createElement(Riffle, { className: "w-80", intensity: 0.8, theme: "dark", label: "Cards", onRead() {} }));
+ const html = renderToString(createElement(Riffle, { className: "w-80", intensity: 0.8, theme: "dark", label: "Cards", play: true, onRead() {} }));
expect(html).toBe('<div class="w-80" style="aspect-ratio:5 / 4"></div>');
});
diff --git a/packages/hairline/test/types.test-d.tsx b/packages/hairline/test/types.test-d.tsx
index 00f2377..bbf9528 100644
--- a/packages/hairline/test/types.test-d.tsx
+++ b/packages/hairline/test/types.test-d.tsx
@@ -33,7 +33,9 @@ slow(el, { intensity: "0.8" });
exploded(el, { theme: "sepia" });
// @ts-expect-error the element is required
riffle();
-yes<Equal<keyof HairlineOptions, "intensity" | "theme" | "label" | "onRead">>();
+yes<Equal<keyof HairlineOptions, "intensity" | "theme" | "label" | "onRead" | "play">>();
+terrain(document.createElement("div"), { play: true });
+<Terrain play />;
/* the handle */
const f = riffle(el);