@slotform/react-ui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +44 -2
- package/dist/density.d.ts +4 -0
- package/dist/density.d.ts.map +1 -0
- package/dist/density.js +8 -0
- package/dist/density.js.map +1 -0
- package/dist/index.d.ts +400 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +388 -0
- package/dist/index.js.map +1 -0
- package/dist/node/canvas.d.ts +3 -0
- package/dist/node/canvas.d.ts.map +1 -0
- package/dist/node/canvas.js +13 -0
- package/dist/node/canvas.js.map +1 -0
- package/dist/node/codegen.d.ts +6 -0
- package/dist/node/codegen.d.ts.map +1 -0
- package/dist/node/codegen.js +70 -0
- package/dist/node/codegen.js.map +1 -0
- package/dist/node/css-escape.d.ts +8 -0
- package/dist/node/css-escape.d.ts.map +1 -0
- package/dist/node/css-escape.js +12 -0
- package/dist/node/css-escape.js.map +1 -0
- package/dist/node/fonts.d.ts +6 -0
- package/dist/node/fonts.d.ts.map +1 -0
- package/dist/node/fonts.js +219 -0
- package/dist/node/fonts.js.map +1 -0
- package/dist/node/lint.d.ts +3 -0
- package/dist/node/lint.d.ts.map +1 -0
- package/dist/node/lint.js +23 -0
- package/dist/node/lint.js.map +1 -0
- package/dist/node/mode.d.ts +9 -0
- package/dist/node/mode.d.ts.map +1 -0
- package/dist/node/mode.js +38 -0
- package/dist/node/mode.js.map +1 -0
- package/dist/node/theme.d.ts +13 -0
- package/dist/node/theme.d.ts.map +1 -0
- package/dist/node/theme.js +160 -0
- package/dist/node/theme.js.map +1 -0
- package/dist/types.d.ts +250 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +80 -0
- package/dist/types.js.map +1 -0
- package/guide/acts.md +61 -0
- package/guide/audio.md +77 -0
- package/guide/blocks.md +82 -0
- package/guide/canvas-choosing.md +34 -0
- package/guide/canvas-editing.md +36 -0
- package/guide/canvas-layout.md +46 -0
- package/guide/canvas-opening.md +47 -0
- package/guide/canvas-overview.md +24 -0
- package/guide/canvas-states.md +37 -0
- package/guide/canvas-view.md +46 -0
- package/guide/canvas.md +94 -0
- package/guide/containers.md +60 -0
- package/guide/content.md +40 -0
- package/guide/controls.md +89 -0
- package/guide/density.md +48 -0
- package/guide/dock.md +42 -0
- package/guide/drag.md +38 -0
- package/guide/figures.md +25 -0
- package/guide/forms.md +82 -0
- package/guide/pane-bar.md +11 -0
- package/guide/pane.md +50 -0
- package/guide/reference.md +74 -0
- package/guide/rules.md +109 -0
- package/guide/section.md +29 -0
- package/guide/shell.md +26 -0
- package/guide/shots.md +37 -0
- package/guide/sidebar.md +30 -0
- package/guide/table.md +119 -0
- package/lint/no-arbitrary-value.grit +9 -0
- package/lint/no-class-on-component.grit +12 -0
- package/lint/no-density.grit +8 -0
- package/lint/no-host-look.grit +38 -0
- package/lint/no-img.grit +10 -0
- package/lint/no-navigate-act.grit +22 -0
- package/lint/no-raw-style.grit +8 -0
- package/package.json +150 -3
- package/src/density.ts +9 -0
- package/src/index.ts +503 -0
- package/src/node/canvas.ts +14 -0
- package/src/node/codegen.ts +100 -0
- package/src/node/css-escape.ts +27 -0
- package/src/node/fonts.ts +260 -0
- package/src/node/lint.ts +25 -0
- package/src/node/mode.ts +64 -0
- package/src/node/theme.ts +232 -0
- package/src/types.ts +120 -0
- package/src/ui/atoms/avatar/base.tsx +42 -0
- package/src/ui/atoms/button/glyph.tsx +244 -0
- package/src/ui/atoms/button/link.tsx +81 -0
- package/src/ui/atoms/button/look.ts +32 -0
- package/src/ui/atoms/button/reason.tsx +69 -0
- package/src/ui/atoms/chip/base.tsx +85 -0
- package/src/ui/atoms/count/base.tsx +27 -0
- package/src/ui/atoms/icon/index.tsx +51 -0
- package/src/ui/atoms/message/waiting.tsx +19 -0
- package/src/ui/atoms/prose/painted.tsx +35 -0
- package/src/ui/atoms/row/badges.tsx +226 -0
- package/src/ui/atoms/row/leading.tsx +166 -0
- package/src/ui/atoms/row/lock.tsx +17 -0
- package/src/ui/atoms/row/marks.tsx +79 -0
- package/src/ui/atoms/section/card.tsx +27 -0
- package/src/ui/atoms/spinner/index.tsx +42 -0
- package/src/ui/atoms/status/base.tsx +51 -0
- package/src/ui/atoms/status/change.tsx +29 -0
- package/src/ui/atoms/status/dot.tsx +45 -0
- package/src/ui/atoms/switch/checkbox.tsx +121 -0
- package/src/ui/atoms/turn.tsx +28 -0
- package/src/ui/components/action-bar/base.tsx +276 -0
- package/src/ui/components/action-bar/index.tsx +20 -0
- package/src/ui/components/avatar/index.tsx +21 -0
- package/src/ui/components/banner/index.tsx +207 -0
- package/src/ui/components/banner/pending.tsx +109 -0
- package/src/ui/components/bar-chart/index.tsx +701 -0
- package/src/ui/components/button/base.tsx +214 -0
- package/src/ui/components/button/face.tsx +84 -0
- package/src/ui/components/button/index.tsx +206 -0
- package/src/ui/components/canvas/connection.tsx +26 -0
- package/src/ui/components/canvas/edges.tsx +116 -0
- package/src/ui/components/canvas/elk.ts +260 -0
- package/src/ui/components/canvas/floor.ts +173 -0
- package/src/ui/components/canvas/geometry.ts +772 -0
- package/src/ui/components/canvas/ground.tsx +97 -0
- package/src/ui/components/canvas/group.tsx +156 -0
- package/src/ui/components/canvas/hit.ts +40 -0
- package/src/ui/components/canvas/index.tsx +497 -0
- package/src/ui/components/canvas/label.tsx +53 -0
- package/src/ui/components/canvas/layout.ts +543 -0
- package/src/ui/components/canvas/lift.ts +272 -0
- package/src/ui/components/canvas/look.ts +129 -0
- package/src/ui/components/canvas/node.tsx +689 -0
- package/src/ui/components/canvas/open.ts +243 -0
- package/src/ui/components/canvas/opening.ts +240 -0
- package/src/ui/components/canvas/view.ts +621 -0
- package/src/ui/components/canvas/viewport.ts +417 -0
- package/src/ui/components/canvas/zoom.tsx +113 -0
- package/src/ui/components/card/index.tsx +20 -0
- package/src/ui/components/chip/index.tsx +24 -0
- package/src/ui/components/code/frame.ts +2 -0
- package/src/ui/components/code/index.tsx +234 -0
- package/src/ui/components/columns/index.tsx +51 -0
- package/src/ui/components/count/index.tsx +17 -0
- package/src/ui/components/diff/index.tsx +249 -0
- package/src/ui/components/diff/word.tsx +104 -0
- package/src/ui/components/dock/index.tsx +262 -0
- package/src/ui/components/file-row/index.tsx +213 -0
- package/src/ui/components/form/base.tsx +280 -0
- package/src/ui/components/form/derive.tsx +269 -0
- package/src/ui/components/form/index.tsx +49 -0
- package/src/ui/components/form/saved.tsx +39 -0
- package/src/ui/components/form-field/index.tsx +658 -0
- package/src/ui/components/gallery/index.tsx +83 -0
- package/src/ui/components/gate/index.tsx +183 -0
- package/src/ui/components/image/base.tsx +245 -0
- package/src/ui/components/image/index.tsx +58 -0
- package/src/ui/components/input/base.tsx +246 -0
- package/src/ui/components/input/code.tsx +108 -0
- package/src/ui/components/input/drop.ts +37 -0
- package/src/ui/components/input/file.tsx +154 -0
- package/src/ui/components/input/index.tsx +132 -0
- package/src/ui/components/input/prose.tsx +107 -0
- package/src/ui/components/input/refusal.tsx +30 -0
- package/src/ui/components/item-header/base.tsx +410 -0
- package/src/ui/components/menu/base.tsx +319 -0
- package/src/ui/components/message/attachments.tsx +101 -0
- package/src/ui/components/message/base.tsx +266 -0
- package/src/ui/components/message-input/index.tsx +377 -0
- package/src/ui/components/missing/base.tsx +45 -0
- package/src/ui/components/pane/head.tsx +399 -0
- package/src/ui/components/pane/index.tsx +325 -0
- package/src/ui/components/picker/base.tsx +1551 -0
- package/src/ui/components/picker/index.tsx +224 -0
- package/src/ui/components/picker/list.tsx +423 -0
- package/src/ui/components/picker/segments.tsx +183 -0
- package/src/ui/components/prose/index.tsx +259 -0
- package/src/ui/components/qr-code/index.tsx +65 -0
- package/src/ui/components/row/acts.tsx +126 -0
- package/src/ui/components/row/fact.tsx +54 -0
- package/src/ui/components/row/index.tsx +1370 -0
- package/src/ui/components/row/progress.tsx +38 -0
- package/src/ui/components/row/status.tsx +54 -0
- package/src/ui/components/row/title.ts +7 -0
- package/src/ui/components/rules/index.tsx +290 -0
- package/src/ui/components/section/band.tsx +39 -0
- package/src/ui/components/section/head.tsx +267 -0
- package/src/ui/components/section/index.tsx +409 -0
- package/src/ui/components/section/rows.tsx +356 -0
- package/src/ui/components/sheet/base.tsx +272 -0
- package/src/ui/components/sheet/confirm.tsx +79 -0
- package/src/ui/components/sheet/docked.tsx +218 -0
- package/src/ui/components/sheet/form.tsx +76 -0
- package/src/ui/components/sheet/head.tsx +120 -0
- package/src/ui/components/sheet/index.tsx +45 -0
- package/src/ui/components/sheet/layer.tsx +168 -0
- package/src/ui/components/sheet/outlet.tsx +58 -0
- package/src/ui/components/sheet/task.tsx +41 -0
- package/src/ui/components/shell/cut.tsx +116 -0
- package/src/ui/components/shell/drag.tsx +45 -0
- package/src/ui/components/shell/host.tsx +71 -0
- package/src/ui/components/shell/index.tsx +737 -0
- package/src/ui/components/shell/panes.tsx +76 -0
- package/src/ui/components/shell/preview.ts +230 -0
- package/src/ui/components/shell/rail.tsx +163 -0
- package/src/ui/components/shell/row.ts +11 -0
- package/src/ui/components/shell/switcher.tsx +143 -0
- package/src/ui/components/shell/tree.tsx +252 -0
- package/src/ui/components/slider/base.tsx +139 -0
- package/src/ui/components/slider/index.tsx +57 -0
- package/src/ui/components/stages/index.tsx +198 -0
- package/src/ui/components/stats/count-links.tsx +47 -0
- package/src/ui/components/stats/index.tsx +203 -0
- package/src/ui/components/stats/meter.tsx +213 -0
- package/src/ui/components/stats/parts.ts +10 -0
- package/src/ui/components/step-count/index.tsx +45 -0
- package/src/ui/components/switch/base.tsx +86 -0
- package/src/ui/components/switch/index.tsx +38 -0
- package/src/ui/components/table/columns.tsx +131 -0
- package/src/ui/components/table/index.tsx +2215 -0
- package/src/ui/components/text/index.tsx +37 -0
- package/src/ui/components/thread/index.tsx +503 -0
- package/src/ui/components/thread/latest.tsx +59 -0
- package/src/ui/components/toast/index.tsx +113 -0
- package/src/ui/components/toast/layer.tsx +26 -0
- package/src/ui/components/toolbar/filters.tsx +161 -0
- package/src/ui/components/toolbar/index.tsx +125 -0
- package/src/ui/components/waveform/index.tsx +205 -0
- package/src/ui/globals.css +427 -0
- package/src/ui/lib/act.ts +60 -0
- package/src/ui/lib/age.ts +49 -0
- package/src/ui/lib/bound.ts +16 -0
- package/src/ui/lib/canvas-layout.ts +11 -0
- package/src/ui/lib/caret.ts +9 -0
- package/src/ui/lib/chip-lines.ts +3 -0
- package/src/ui/lib/closed.ts +8 -0
- package/src/ui/lib/code.tsx +184 -0
- package/src/ui/lib/columns.ts +39 -0
- package/src/ui/lib/commit.ts +24 -0
- package/src/ui/lib/container.ts +112 -0
- package/src/ui/lib/copy.ts +29 -0
- package/src/ui/lib/drag.ts +575 -0
- package/src/ui/lib/empty.tsx +85 -0
- package/src/ui/lib/field.ts +39 -0
- package/src/ui/lib/focus.ts +175 -0
- package/src/ui/lib/folds.ts +67 -0
- package/src/ui/lib/follow.ts +220 -0
- package/src/ui/lib/form.ts +19 -0
- package/src/ui/lib/guards.ts +38 -0
- package/src/ui/lib/heading.ts +14 -0
- package/src/ui/lib/ink.ts +26 -0
- package/src/ui/lib/lang.ts +7 -0
- package/src/ui/lib/leave.ts +14 -0
- package/src/ui/lib/lockup.tsx +145 -0
- package/src/ui/lib/measure.ts +22 -0
- package/src/ui/lib/media.ts +63 -0
- package/src/ui/lib/modality.ts +69 -0
- package/src/ui/lib/moment.ts +13 -0
- package/src/ui/lib/more.tsx +76 -0
- package/src/ui/lib/navigate.ts +210 -0
- package/src/ui/lib/noop.ts +3 -0
- package/src/ui/lib/not-found.tsx +40 -0
- package/src/ui/lib/paint.tsx +55 -0
- package/src/ui/lib/parts-line.tsx +54 -0
- package/src/ui/lib/picked.ts +18 -0
- package/src/ui/lib/player-store.ts +38 -0
- package/src/ui/lib/player.ts +139 -0
- package/src/ui/lib/portal.ts +12 -0
- package/src/ui/lib/read.tsx +269 -0
- package/src/ui/lib/region.tsx +49 -0
- package/src/ui/lib/reorder.ts +154 -0
- package/src/ui/lib/rest.ts +16 -0
- package/src/ui/lib/saved.ts +29 -0
- package/src/ui/lib/scramble.tsx +147 -0
- package/src/ui/lib/scrolls.ts +70 -0
- package/src/ui/lib/style.ts +6 -0
- package/src/ui/lib/swap.tsx +107 -0
- package/src/ui/lib/toast.ts +39 -0
- package/src/ui/lib/walk-classes.ts +16 -0
- package/src/ui/lib/window.tsx +376 -0
- package/src/ui/worker.d.ts +4 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { type RefObject, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
// What a viewer's Tab reaches in a region: anything rendered whose effective
|
|
4
|
+
// tab index is 0 or more, so a button, a field, a roving radio group's one tab
|
|
5
|
+
// stop and a scroller that took a stop all count, in document order.
|
|
6
|
+
export function isTabbable(element: HTMLElement): boolean {
|
|
7
|
+
return (
|
|
8
|
+
element.tabIndex >= 0 &&
|
|
9
|
+
!("disabled" in element && element.disabled) &&
|
|
10
|
+
element.closest('[hidden], [inert], [aria-hidden="true"]') === null &&
|
|
11
|
+
element.checkVisibility()
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// The first tabbable element in `root`; `only` narrows it to those matching a
|
|
16
|
+
// selector (a foot hands focus to its typing control, not to the attach act
|
|
17
|
+
// ahead of it).
|
|
18
|
+
export function firstTabbable(
|
|
19
|
+
root: HTMLElement | null,
|
|
20
|
+
only?: string,
|
|
21
|
+
): HTMLElement | undefined {
|
|
22
|
+
const all = root?.querySelectorAll<HTMLElement>("*") ?? [];
|
|
23
|
+
return Array.from(all).find(
|
|
24
|
+
(element) => isTabbable(element) && (!only || element.matches(only)),
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Focuses the first tabbable element in `root`, narrowed as `firstTabbable` is.
|
|
29
|
+
export function focusFirst(root: HTMLElement | null, only?: string): boolean {
|
|
30
|
+
const first = firstTabbable(root, only);
|
|
31
|
+
first?.focus();
|
|
32
|
+
return first !== undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// What a form's first field is: a typing control, a native select, a pick's
|
|
36
|
+
// trigger, or the tab stop of a pick drawn as its options (a radio, a box, an
|
|
37
|
+
// option of a listbox) or of a switch. A pick of options standing first is the
|
|
38
|
+
// first field, so a sheet opens on it, at its top, and not on a later field
|
|
39
|
+
// it would scroll down to.
|
|
40
|
+
export const FIELDS =
|
|
41
|
+
"input, textarea, select, [role='combobox'], [role='radio'], [role='checkbox'], [role='option'], [role='switch']";
|
|
42
|
+
|
|
43
|
+
// Whether focus is inside a region, as the page last moved it. A focus or a
|
|
44
|
+
// press landing outside the region ends the hold; one inside begins it. The
|
|
45
|
+
// removal of the focused element fires neither, so the hold survives it: that
|
|
46
|
+
// is how a region learns its focus left with its own page.
|
|
47
|
+
function trackHold(
|
|
48
|
+
region: () => HTMLElement | null,
|
|
49
|
+
hold: { current: boolean },
|
|
50
|
+
): () => void {
|
|
51
|
+
const follow = (event: Event) => {
|
|
52
|
+
hold.current =
|
|
53
|
+
event.target instanceof Node &&
|
|
54
|
+
(region()?.contains(event.target) ?? false);
|
|
55
|
+
};
|
|
56
|
+
document.addEventListener("focusin", follow, true);
|
|
57
|
+
document.addEventListener("pointerdown", follow, true);
|
|
58
|
+
return () => {
|
|
59
|
+
document.removeEventListener("focusin", follow, true);
|
|
60
|
+
document.removeEventListener("pointerdown", follow, true);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// A foot region hands focus to its first field: docs/architecture/plugins/react-ui.md, "Focus".
|
|
65
|
+
export function useFootFocus(region: RefObject<HTMLElement | null>) {
|
|
66
|
+
const held = useRef(false);
|
|
67
|
+
useEffect(() => trackHold(() => region.current, held), [region]);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (held.current && document.activeElement === document.body) {
|
|
70
|
+
held.current = false;
|
|
71
|
+
focusFirst(region.current, "input, textarea");
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The opener of a sheet opened from a popup's act: docs/architecture/plugins/react-ui.md, "Focus".
|
|
77
|
+
const OPENER_WINDOW = 1000;
|
|
78
|
+
let opener: { element: HTMLElement | null; at: number } | undefined;
|
|
79
|
+
|
|
80
|
+
// The trigger of the popup that is open: a pick's or a menu's trigger, or a
|
|
81
|
+
// touch sheet's, which carry `aria-expanded` while it stands.
|
|
82
|
+
export function expandedTrigger(): HTMLElement | null {
|
|
83
|
+
const all = document.querySelectorAll<HTMLElement>(
|
|
84
|
+
'[aria-expanded="true"]:is([aria-haspopup], [role="combobox"])',
|
|
85
|
+
);
|
|
86
|
+
return all.item(all.length - 1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// An act in a popup calls this before it closes the popup and runs.
|
|
90
|
+
export function rememberOpener(element: HTMLElement | null) {
|
|
91
|
+
opener = { element, at: Date.now() };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// The sheet that opens takes the remembered trigger once, if the act that
|
|
95
|
+
// named it ran a moment ago.
|
|
96
|
+
function claimOpener(): HTMLElement | null {
|
|
97
|
+
const held = opener;
|
|
98
|
+
opener = undefined;
|
|
99
|
+
return held && Date.now() - held.at <= OPENER_WINDOW ? held.element : null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Focus on the page a sheet left standing: docs/architecture/plugins/react-ui.md, "Focus".
|
|
103
|
+
function focusPage(): void {
|
|
104
|
+
// Our own focus lands here too, which ends the chain on success.
|
|
105
|
+
let moved = false;
|
|
106
|
+
const note = () => {
|
|
107
|
+
moved = true;
|
|
108
|
+
};
|
|
109
|
+
document.addEventListener("focusin", note, true);
|
|
110
|
+
const attempt = (left: number) => {
|
|
111
|
+
if (!moved) {
|
|
112
|
+
for (const page of document.querySelectorAll<HTMLElement>("[data-page]"))
|
|
113
|
+
if (focusFirst(page)) break;
|
|
114
|
+
if (!moved && left > 0) {
|
|
115
|
+
setTimeout(() => attempt(left - 1), 100);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
document.removeEventListener("focusin", note, true);
|
|
120
|
+
};
|
|
121
|
+
attempt(5);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// A closing sheet hands focus back: docs/architecture/plugins/react-ui.md, "Focus".
|
|
125
|
+
export function useReturnFocus(
|
|
126
|
+
open: boolean,
|
|
127
|
+
region: RefObject<HTMLElement | null>,
|
|
128
|
+
) {
|
|
129
|
+
const held = useRef(false);
|
|
130
|
+
const turn = useRef(0);
|
|
131
|
+
useEffect(() => trackHold(() => region.current, held), [region]);
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (!open) return;
|
|
134
|
+
const mine = ++turn.current;
|
|
135
|
+
const active = document.activeElement;
|
|
136
|
+
// A sheet opened with focus nowhere holds it from the start: its own
|
|
137
|
+
// first focus lands a frame later, and an Escape before then still
|
|
138
|
+
// leaves the page to take focus, not the body.
|
|
139
|
+
held.current =
|
|
140
|
+
active === document.body || (region.current?.contains(active) ?? false);
|
|
141
|
+
const from =
|
|
142
|
+
claimOpener() ??
|
|
143
|
+
(active instanceof HTMLElement &&
|
|
144
|
+
active !== document.body &&
|
|
145
|
+
!region.current?.contains(active)
|
|
146
|
+
? active
|
|
147
|
+
: null);
|
|
148
|
+
// Acts once the sheet is gone: its leave played, or its route unmounted it.
|
|
149
|
+
let frames = 0;
|
|
150
|
+
const settle = () => {
|
|
151
|
+
if (turn.current !== mine) return;
|
|
152
|
+
if (region.current && frames++ < 120) {
|
|
153
|
+
requestAnimationFrame(settle);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (!held.current || document.activeElement !== document.body) return;
|
|
157
|
+
held.current = false;
|
|
158
|
+
if (from?.isConnected && isTabbable(from)) from.focus();
|
|
159
|
+
else focusPage();
|
|
160
|
+
};
|
|
161
|
+
return () => {
|
|
162
|
+
setTimeout(settle, 0);
|
|
163
|
+
};
|
|
164
|
+
}, [open, region]);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// A Pane opened from a press takes the keyboard: docs/architecture/plugins/react-ui.md, "Focus".
|
|
168
|
+
export function usePaneFocus(pane: HTMLElement | null) {
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
if (!pane) return;
|
|
171
|
+
const from = document.activeElement?.closest("[data-page]");
|
|
172
|
+
if (!from || from === pane || pane.contains(from)) return;
|
|
173
|
+
focusFirst(pane);
|
|
174
|
+
}, [pane]);
|
|
175
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useCallback, useSyncExternalStore } from "react";
|
|
2
|
+
import { saveChoice, savedChoice, subscribeSaved } from "./saved.ts";
|
|
3
|
+
|
|
4
|
+
// The viewer's open and folded branches of the sidebar are a saved choice
|
|
5
|
+
// (`./saved.ts`), one for every tree of the page. A choice is by the
|
|
6
|
+
// branch's route; a branch never chosen is left to the Shell (open on the
|
|
7
|
+
// current place's path, else folded). The sidebar's column folds away the same
|
|
8
|
+
// way, under a key no route can be.
|
|
9
|
+
const KEY = "slotform:places:open";
|
|
10
|
+
|
|
11
|
+
// The parse of the last raw choice read, so the snapshot stays the same object
|
|
12
|
+
// until the choice changes.
|
|
13
|
+
let last: { raw: string | null; open: Readonly<Record<string, boolean>> } = {
|
|
14
|
+
raw: null,
|
|
15
|
+
open: {},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function read(): Readonly<Record<string, boolean>> {
|
|
19
|
+
const raw = savedChoice(KEY);
|
|
20
|
+
if (raw !== last.raw) last = { raw, open: parse(raw) };
|
|
21
|
+
return last.open;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function parse(raw: string | null): Readonly<Record<string, boolean>> {
|
|
25
|
+
try {
|
|
26
|
+
const parsed: unknown = JSON.parse(raw ?? "{}");
|
|
27
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
28
|
+
return Object.fromEntries(
|
|
29
|
+
Object.entries(parsed).filter(
|
|
30
|
+
(entry): entry is [string, boolean] => typeof entry[1] === "boolean",
|
|
31
|
+
),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
} catch {}
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function write(route: string, open: boolean): void {
|
|
39
|
+
saveChoice(KEY, JSON.stringify({ ...read(), [route]: open }));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const NONE: Readonly<Record<string, boolean>> = {};
|
|
43
|
+
|
|
44
|
+
/** The branches the viewer opened or folded (by route) and the act that records a choice. Outside the package's exports. */
|
|
45
|
+
export function useFolds(): {
|
|
46
|
+
chosen: Readonly<Record<string, boolean>>;
|
|
47
|
+
choose: (route: string, open: boolean) => void;
|
|
48
|
+
} {
|
|
49
|
+
const chosen = useSyncExternalStore(subscribeSaved, read, () => NONE);
|
|
50
|
+
const choose = useCallback(
|
|
51
|
+
(route: string, open: boolean) => write(route, open),
|
|
52
|
+
[],
|
|
53
|
+
);
|
|
54
|
+
return { chosen, choose };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A route starts with a slash, so this key is never a route's.
|
|
58
|
+
const COLUMN = "column";
|
|
59
|
+
|
|
60
|
+
/** Whether the sidebar's column stands docked beside the rail (docked until the viewer folds it to the rail) and the act that records the choice. Outside the package's exports. */
|
|
61
|
+
export function useDocked(): [
|
|
62
|
+
docked: boolean,
|
|
63
|
+
dock: (docked: boolean) => void,
|
|
64
|
+
] {
|
|
65
|
+
const { chosen, choose } = useFolds();
|
|
66
|
+
return [chosen[COLUMN] ?? true, (docked) => choose(COLUMN, docked)];
|
|
67
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { DRAG_DELAY_SLOP } from "@slotform/ui-core/drag";
|
|
2
|
+
import {
|
|
3
|
+
eased,
|
|
4
|
+
mix,
|
|
5
|
+
position,
|
|
6
|
+
release,
|
|
7
|
+
type Sample,
|
|
8
|
+
velocity,
|
|
9
|
+
} from "@slotform/ui-core/follow";
|
|
10
|
+
import {
|
|
11
|
+
type PointerEvent,
|
|
12
|
+
type RefObject,
|
|
13
|
+
useLayoutEffect,
|
|
14
|
+
useRef,
|
|
15
|
+
} from "react";
|
|
16
|
+
import { NOOP } from "./noop.ts";
|
|
17
|
+
|
|
18
|
+
// The `base` rung in ms as the page has it now: the root's custom property,
|
|
19
|
+
// which the tuning page writes and reduced motion zeroes.
|
|
20
|
+
function base(): number {
|
|
21
|
+
return Number.parseFloat(
|
|
22
|
+
getComputedStyle(document.documentElement).getPropertyValue(
|
|
23
|
+
"--transition-duration-base",
|
|
24
|
+
),
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A move the script carries because CSS cannot: `from` to `to` on `base`
|
|
30
|
+
* `in-out`, each frame handed to `apply` (the canvas viewport, whose grid and
|
|
31
|
+
* layer both follow). Reduced motion lands on `to` at once. Returns the
|
|
32
|
+
* cancel, which a pointer or a wheel taking over calls; a cancelled move stays
|
|
33
|
+
* where it got to.
|
|
34
|
+
*/
|
|
35
|
+
export function travel<T extends Record<string, number>>(
|
|
36
|
+
from: T,
|
|
37
|
+
to: T,
|
|
38
|
+
apply: (view: T) => void,
|
|
39
|
+
): () => void {
|
|
40
|
+
const duration = base();
|
|
41
|
+
if (!(duration > 0)) {
|
|
42
|
+
apply(to);
|
|
43
|
+
return NOOP;
|
|
44
|
+
}
|
|
45
|
+
let start: number | undefined;
|
|
46
|
+
let frame = 0;
|
|
47
|
+
const step = (now: number) => {
|
|
48
|
+
start ??= now;
|
|
49
|
+
const share = Math.min(1, (now - start) / duration);
|
|
50
|
+
if (share >= 1) {
|
|
51
|
+
apply(to);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const along = eased("in-out", share);
|
|
55
|
+
apply(
|
|
56
|
+
// `Object.fromEntries` widens its keys to `string`; they are `from`'s own.
|
|
57
|
+
Object.fromEntries(
|
|
58
|
+
Object.keys(from).map((key) => [
|
|
59
|
+
key,
|
|
60
|
+
mix(from[key] ?? 0, to[key] ?? 0, along),
|
|
61
|
+
]),
|
|
62
|
+
) as T,
|
|
63
|
+
);
|
|
64
|
+
frame = requestAnimationFrame(step);
|
|
65
|
+
};
|
|
66
|
+
frame = requestAnimationFrame(step);
|
|
67
|
+
return () => cancelAnimationFrame(frame);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A press on one of these is the part's own, never the start of a drag. A
|
|
71
|
+
// button is not among them, so a toast or a sheet head swipes from any spot.
|
|
72
|
+
const OWN = "input,textarea,select,[contenteditable='true']";
|
|
73
|
+
|
|
74
|
+
/** What a drag along one axis is for: the element it carries, and the way it leaves. */
|
|
75
|
+
interface FollowArgs {
|
|
76
|
+
/** The element the drag carries; its edge to edge along the axis is how far it has to go to be gone. */
|
|
77
|
+
target: RefObject<HTMLElement | null>;
|
|
78
|
+
axis: "x" | "y";
|
|
79
|
+
/** The way it leaves along the axis: 1 toward the end, -1 toward the start, or `inline-end` (the text direction's). */
|
|
80
|
+
away: 1 | -1 | "inline-end";
|
|
81
|
+
/** Off, the handlers do nothing: a mouse never drags (a touch or a pen does), and a held dismissal stays. */
|
|
82
|
+
enabled: boolean;
|
|
83
|
+
/** The drag was released past the reach or flung: the owner closes it, and the leave takes it on from where it was let go. */
|
|
84
|
+
onAway: () => void;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
interface Drag {
|
|
88
|
+
pointer: number;
|
|
89
|
+
from: { x: number; y: number };
|
|
90
|
+
samples: Sample[];
|
|
91
|
+
// Past the slop along the axis and toward away: the element follows.
|
|
92
|
+
held: boolean;
|
|
93
|
+
// Went the wrong way, or across, first: the drag is not ours.
|
|
94
|
+
dead: boolean;
|
|
95
|
+
pulled: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const sign = (away: FollowArgs["away"], element: Element): 1 | -1 => {
|
|
99
|
+
if (away !== "inline-end") return away;
|
|
100
|
+
return getComputedStyle(element).direction === "rtl" ? -1 : 1;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const shift = (axis: "x" | "y", by: number) =>
|
|
104
|
+
axis === "x" ? `translateX(${by}px)` : `translateY(${by}px)`;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A drag with the finger (docs/architecture/ui-core/drag.md, "Follow"): the element moves 1:1 with
|
|
108
|
+
* the pointer toward `away` and never against it, with no easing; on release it
|
|
109
|
+
* either settles back on `base` `out` or, past the reach or flung, leaves on
|
|
110
|
+
* the layer's own leave. Spread the returned handlers on the part that is
|
|
111
|
+
* grabbed (a sheet's head, a toast), whose `touch-action` must keep the
|
|
112
|
+
* browser from taking the axis. Only a touch or a pen takes part.
|
|
113
|
+
*/
|
|
114
|
+
export function useFollow(args: FollowArgs) {
|
|
115
|
+
const drag = useRef<Drag | null>(null);
|
|
116
|
+
const latest = useRef(args);
|
|
117
|
+
useLayoutEffect(() => {
|
|
118
|
+
latest.current = args;
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const down = (event: PointerEvent<HTMLElement>) => {
|
|
122
|
+
const { enabled, target } = latest.current;
|
|
123
|
+
if (
|
|
124
|
+
!enabled ||
|
|
125
|
+
event.pointerType === "mouse" ||
|
|
126
|
+
event.button !== 0 ||
|
|
127
|
+
drag.current ||
|
|
128
|
+
!target.current ||
|
|
129
|
+
(event.target instanceof Element && event.target.closest(OWN))
|
|
130
|
+
)
|
|
131
|
+
return;
|
|
132
|
+
drag.current = {
|
|
133
|
+
pointer: event.pointerId,
|
|
134
|
+
from: { x: event.clientX, y: event.clientY },
|
|
135
|
+
samples: [],
|
|
136
|
+
held: false,
|
|
137
|
+
dead: false,
|
|
138
|
+
pulled: 0,
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
const move = (event: PointerEvent<HTMLElement>) => {
|
|
142
|
+
const state = drag.current;
|
|
143
|
+
const { target, axis } = latest.current;
|
|
144
|
+
const element = target.current;
|
|
145
|
+
if (!state || state.dead || event.pointerId !== state.pointer || !element)
|
|
146
|
+
return;
|
|
147
|
+
const away = sign(latest.current.away, element);
|
|
148
|
+
const along =
|
|
149
|
+
axis === "x"
|
|
150
|
+
? event.clientX - state.from.x
|
|
151
|
+
: event.clientY - state.from.y;
|
|
152
|
+
const across =
|
|
153
|
+
axis === "x"
|
|
154
|
+
? event.clientY - state.from.y
|
|
155
|
+
: event.clientX - state.from.x;
|
|
156
|
+
if (!state.held) {
|
|
157
|
+
if (Math.hypot(along, across) <= DRAG_DELAY_SLOP) return;
|
|
158
|
+
// Across first, or against the way it leaves: a scroll or a tap's wobble.
|
|
159
|
+
if (Math.abs(across) >= Math.abs(along) || position(along, away) === 0) {
|
|
160
|
+
state.dead = true;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
state.held = true;
|
|
164
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
165
|
+
}
|
|
166
|
+
state.pulled = position(along, away);
|
|
167
|
+
state.samples.push({
|
|
168
|
+
at: event.timeStamp,
|
|
169
|
+
value: axis === "x" ? event.clientX : event.clientY,
|
|
170
|
+
});
|
|
171
|
+
// The element follows the pointer with no transition, whatever its class gives.
|
|
172
|
+
element.style.transition = "none";
|
|
173
|
+
element.style.transform = shift(axis, state.pulled * away);
|
|
174
|
+
};
|
|
175
|
+
const end = (event: PointerEvent<HTMLElement>) => {
|
|
176
|
+
const state = drag.current;
|
|
177
|
+
if (!state || event.pointerId !== state.pointer) return;
|
|
178
|
+
drag.current = null;
|
|
179
|
+
const { target, axis, onAway } = latest.current;
|
|
180
|
+
const element = target.current;
|
|
181
|
+
if (!state.held || !element) return;
|
|
182
|
+
const away = sign(latest.current.away, element);
|
|
183
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId))
|
|
184
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
185
|
+
const extent = axis === "x" ? element.offsetWidth : element.offsetHeight;
|
|
186
|
+
const cancelled = event.type === "pointercancel";
|
|
187
|
+
const goes =
|
|
188
|
+
!cancelled &&
|
|
189
|
+
release(
|
|
190
|
+
state.pulled,
|
|
191
|
+
velocity(state.samples, away, event.timeStamp),
|
|
192
|
+
extent,
|
|
193
|
+
) === "away";
|
|
194
|
+
if (goes) {
|
|
195
|
+
// The layer's own leave takes it on from here: its transition, back,
|
|
196
|
+
// and the way off the edge.
|
|
197
|
+
element.style.transition = "";
|
|
198
|
+
element.style.transform = shift(axis, extent * away);
|
|
199
|
+
onAway();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
// Back to rest on `base` `out`, then the layer's own transition again.
|
|
203
|
+
element.style.transition =
|
|
204
|
+
"transform var(--transition-duration-base) var(--ease-out)";
|
|
205
|
+
element.style.transform = "";
|
|
206
|
+
const done = () => {
|
|
207
|
+
element.style.transition = "";
|
|
208
|
+
};
|
|
209
|
+
element.addEventListener("transitionend", done, { once: true });
|
|
210
|
+
element.addEventListener("transitioncancel", done, { once: true });
|
|
211
|
+
// A rest that needed no travel (reduced motion, or already at rest) ends no transition.
|
|
212
|
+
if (base() === 0 || state.pulled === 0) done();
|
|
213
|
+
};
|
|
214
|
+
return {
|
|
215
|
+
onPointerDown: down,
|
|
216
|
+
onPointerMove: move,
|
|
217
|
+
onPointerUp: end,
|
|
218
|
+
onPointerCancel: end,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
|
|
3
|
+
// Set by an `ActionBar` whose form it is, around its filled act: the `Button`
|
|
4
|
+
// it draws is the form's submit button, so Enter in a field presses it.
|
|
5
|
+
export const SubmitContext = createContext(false);
|
|
6
|
+
|
|
7
|
+
// An act that cannot run where it stands: docs/architecture/ui-core/acts.md, "Act model".
|
|
8
|
+
export const ActInert = createContext(false);
|
|
9
|
+
|
|
10
|
+
// A submit button's press runs the act itself, so the press ends the native
|
|
11
|
+
// submit it would start: a step the act swaps in leaves the form detached,
|
|
12
|
+
// and a submit still pending then logs "the form is not connected". Enter in a
|
|
13
|
+
// field presses the same button, so click and Enter share this one path.
|
|
14
|
+
export function endSubmit(
|
|
15
|
+
submits: boolean,
|
|
16
|
+
event: { preventDefault: () => void },
|
|
17
|
+
): void {
|
|
18
|
+
if (submits) event.preventDefault();
|
|
19
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
|
|
3
|
+
// Base UI's focus guards without `aria-hidden`: docs/architecture/plugins/react-ui.md, "Accessibility".
|
|
4
|
+
const GUARD = "[data-base-ui-focus-guard]";
|
|
5
|
+
|
|
6
|
+
function quiet(root: ParentNode) {
|
|
7
|
+
if (root instanceof Element && root.matches(GUARD))
|
|
8
|
+
root.removeAttribute("aria-hidden");
|
|
9
|
+
for (const guard of root.querySelectorAll(GUARD))
|
|
10
|
+
guard.removeAttribute("aria-hidden");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// One observer for every popup on the page, counted by the popups that ask.
|
|
14
|
+
let asked = 0;
|
|
15
|
+
let watch: MutationObserver | undefined;
|
|
16
|
+
|
|
17
|
+
/** Keeps Base UI's focus guards, which a popup draws as it opens, out of `aria-hidden` while this component is mounted. Outside the package's exports. */
|
|
18
|
+
export function useQuietGuards(): void {
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
asked += 1;
|
|
21
|
+
if (asked === 1) {
|
|
22
|
+
quiet(document);
|
|
23
|
+
watch = new MutationObserver((records) => {
|
|
24
|
+
for (const record of records)
|
|
25
|
+
for (const node of record.addedNodes)
|
|
26
|
+
if (node instanceof Element) quiet(node);
|
|
27
|
+
});
|
|
28
|
+
watch.observe(document.body, { childList: true, subtree: true });
|
|
29
|
+
}
|
|
30
|
+
return () => {
|
|
31
|
+
asked -= 1;
|
|
32
|
+
if (asked === 0) {
|
|
33
|
+
watch?.disconnect();
|
|
34
|
+
watch = undefined;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}, []);
|
|
38
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
|
|
3
|
+
// A Section's heading level: docs/architecture/ui-core/layout.md, "Headings".
|
|
4
|
+
type HeadingLevel = 2 | 3 | 4 | 5 | 6;
|
|
5
|
+
|
|
6
|
+
export const HeadingContext = createContext<HeadingLevel>(2);
|
|
7
|
+
|
|
8
|
+
export const DEEPER = {
|
|
9
|
+
2: 3,
|
|
10
|
+
3: 4,
|
|
11
|
+
4: 5,
|
|
12
|
+
5: 6,
|
|
13
|
+
6: 6,
|
|
14
|
+
} as const satisfies Record<HeadingLevel, HeadingLevel>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createStore } from "@slotform/ui-core/store";
|
|
2
|
+
|
|
3
|
+
// The colour tokens a canvas draws with: docs/architecture/plugins/react-ui.md, "Canvas".
|
|
4
|
+
const version = createStore(0, {
|
|
5
|
+
source: (store) => {
|
|
6
|
+
const watch = new MutationObserver(() => store.set(store.get() + 1));
|
|
7
|
+
watch.observe(document.documentElement, {
|
|
8
|
+
attributeFilter: ["class", "data-density"],
|
|
9
|
+
});
|
|
10
|
+
return () => watch.disconnect();
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/** Follows the mode: `listener` is called when the root's mode or density changes, and the returned function stops it. */
|
|
15
|
+
export const onInkChange = version.subscribe;
|
|
16
|
+
|
|
17
|
+
/** The version of the ink: a cached colour is valid while it holds. */
|
|
18
|
+
export const inkVersion = version.get;
|
|
19
|
+
|
|
20
|
+
/** A colour role's resolved value on `element` (`ink-body`, `chip-amber`), a CSS colour a canvas takes. */
|
|
21
|
+
export function inkOf(element: Element, role: string): string {
|
|
22
|
+
const value = getComputedStyle(element)
|
|
23
|
+
.getPropertyValue(`--color-${role}`)
|
|
24
|
+
.trim();
|
|
25
|
+
return value === "" ? "currentColor" : value;
|
|
26
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Leave } from "@slotform/ui-core/leave";
|
|
2
|
+
import { askDiscard } from "@slotform/ui-core/react/confirm";
|
|
3
|
+
import { useWords } from "@slotform/ui-core/react/words";
|
|
4
|
+
import { useEffect } from "react";
|
|
5
|
+
import { blockLeave } from "./navigate.ts";
|
|
6
|
+
|
|
7
|
+
// A `Form` that stands edited asks before it is left: docs/architecture/ui-core/acts.md, "Leave".
|
|
8
|
+
export function useLeaveGuard(leave: Leave, guarded: boolean): void {
|
|
9
|
+
const words = useWords();
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (!guarded) return;
|
|
12
|
+
return blockLeave(leave, () => askDiscard(words));
|
|
13
|
+
}, [leave, guarded, words]);
|
|
14
|
+
}
|