@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,154 @@
|
|
|
1
|
+
import { afterRun } from "@slotform/ui-core/acts";
|
|
2
|
+
import type { Moved, Reorder } from "@slotform/ui-core/descriptors";
|
|
3
|
+
import { moveEntry, type Step, stepOf } from "@slotform/ui-core/drag";
|
|
4
|
+
import type { Words } from "@slotform/ui-core/tokens";
|
|
5
|
+
import {
|
|
6
|
+
type RefObject,
|
|
7
|
+
use,
|
|
8
|
+
useCallback,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
useState,
|
|
12
|
+
} from "react";
|
|
13
|
+
import { type Act, useAct } from "./act.ts";
|
|
14
|
+
import { Scroller } from "./window.tsx";
|
|
15
|
+
|
|
16
|
+
/** A collection's order, as its rows read and move it: handed to each row, one object for the collection's life. */
|
|
17
|
+
export interface Movable {
|
|
18
|
+
/** The collection's box, which the rows stand in. */
|
|
19
|
+
root: () => HTMLElement | null;
|
|
20
|
+
/** The ids in the order they stand now. */
|
|
21
|
+
ids: () => readonly (string | undefined)[];
|
|
22
|
+
/** Moves a row: it stands there at once, and `onMove` hears it. */
|
|
23
|
+
move: (id: string, to: Moved) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Held {
|
|
27
|
+
source: unknown;
|
|
28
|
+
moves: readonly { id: string; index: number }[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const NONE: Held["moves"] = [];
|
|
32
|
+
|
|
33
|
+
/** A collection that orders its rows (`reorder`): its `rows` in the order the viewer has moved them to, and the `Movable` its rows read and move it by. A move shows at once and stands until the data answers another (`source`, the data the rows are read from, changes) or the move is refused; `onMove` runs as an act does, its `done` toasted, a rejection toasted with Retry. Off (`on` false: a sorted table, a tree), the rows are as given and there is nothing to move. */
|
|
34
|
+
export function useMovable<T>(args: {
|
|
35
|
+
reorder: Reorder | undefined;
|
|
36
|
+
on: boolean;
|
|
37
|
+
source: unknown;
|
|
38
|
+
rows: readonly T[];
|
|
39
|
+
idOf: (row: T) => string | undefined;
|
|
40
|
+
box: RefObject<HTMLElement | null>;
|
|
41
|
+
}): { rows: readonly T[]; movable: Movable | undefined } {
|
|
42
|
+
const { reorder, source, idOf } = args;
|
|
43
|
+
const scroll = use(Scroller);
|
|
44
|
+
const acting = useAct<true>("toast");
|
|
45
|
+
const [held, hold] = useState<Held>();
|
|
46
|
+
const moves = held && held.source === source ? held.moves : NONE;
|
|
47
|
+
const on = args.on && reorder !== undefined;
|
|
48
|
+
const rows = useMemo(() => {
|
|
49
|
+
if (!on || moves.length === 0) return args.rows;
|
|
50
|
+
return moves.reduce<readonly T[]>((order, move) => {
|
|
51
|
+
const from = order.findIndex((row) => idOf(row) === move.id);
|
|
52
|
+
return from < 0 ? order : moveEntry(order, from, move.index);
|
|
53
|
+
}, args.rows);
|
|
54
|
+
}, [on, moves, args.rows, idOf]);
|
|
55
|
+
const latest = useRef({ reorder, acting, rows, source, idOf, scroll });
|
|
56
|
+
latest.current = { reorder, acting, rows, source, idOf, scroll };
|
|
57
|
+
const move = useCallback((id: string, to: Moved) => {
|
|
58
|
+
const now = latest.current;
|
|
59
|
+
const spec = now.reorder;
|
|
60
|
+
if (!spec) return;
|
|
61
|
+
hold((was) => ({
|
|
62
|
+
source: now.source,
|
|
63
|
+
moves: [
|
|
64
|
+
...(was && was.source === now.source ? was.moves : NONE),
|
|
65
|
+
{ id, index: to.index },
|
|
66
|
+
],
|
|
67
|
+
}));
|
|
68
|
+
now.acting.attempt(true, {
|
|
69
|
+
done: spec.done,
|
|
70
|
+
onAct: () =>
|
|
71
|
+
afterRun(
|
|
72
|
+
() => spec.onMove(id, to),
|
|
73
|
+
(failed) => {
|
|
74
|
+
if (failed) hold(undefined);
|
|
75
|
+
},
|
|
76
|
+
),
|
|
77
|
+
});
|
|
78
|
+
}, []);
|
|
79
|
+
const movable = useMemo<Movable>(
|
|
80
|
+
() => ({
|
|
81
|
+
root: () => args.box.current,
|
|
82
|
+
ids: () => latest.current.rows.map((row) => latest.current.idOf(row)),
|
|
83
|
+
move,
|
|
84
|
+
}),
|
|
85
|
+
[args.box, move],
|
|
86
|
+
);
|
|
87
|
+
return { rows, movable: on ? movable : undefined };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Where a row stands in an order that moves, for the moves its menu offers: the first row cannot go up, the last cannot go down. */
|
|
91
|
+
export type Edge = "first" | "last" | "only";
|
|
92
|
+
|
|
93
|
+
/** The edge of the row at `index` of `count`. */
|
|
94
|
+
export function edgeOf(index: number, count: number): Edge | undefined {
|
|
95
|
+
if (count <= 1) return "only";
|
|
96
|
+
if (index === 0) return "first";
|
|
97
|
+
return index === count - 1 ? "last" : undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const STEPS: readonly {
|
|
101
|
+
step: Step;
|
|
102
|
+
word: "moveUp" | "moveDown" | "moveFirst" | "moveLast";
|
|
103
|
+
icon: "ArrowUp" | "ArrowDown" | "ChevronsUp" | "ChevronsDown";
|
|
104
|
+
from: Edge;
|
|
105
|
+
}[] = [
|
|
106
|
+
{ step: "up", word: "moveUp", icon: "ArrowUp", from: "first" },
|
|
107
|
+
{ step: "down", word: "moveDown", icon: "ArrowDown", from: "last" },
|
|
108
|
+
{ step: "first", word: "moveFirst", icon: "ChevronsUp", from: "first" },
|
|
109
|
+
{ step: "last", word: "moveLast", icon: "ChevronsDown", from: "last" },
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
/** The entries that move a row by menu, so an order is never the pointer's alone: up, down, to the top and to the bottom, each left out where the row already stands. They read the order when pressed. */
|
|
113
|
+
export function movesOf(
|
|
114
|
+
words: Words,
|
|
115
|
+
movable: Movable,
|
|
116
|
+
id: string,
|
|
117
|
+
edge: Edge | undefined,
|
|
118
|
+
): Act[] {
|
|
119
|
+
return STEPS.filter((one) => edge !== "only" && edge !== one.from).map(
|
|
120
|
+
(one) => ({
|
|
121
|
+
label: words[one.word],
|
|
122
|
+
icon: one.icon,
|
|
123
|
+
onAct: () => {
|
|
124
|
+
const to = stepOf(movable.ids(), id, one.step);
|
|
125
|
+
if (to) movable.move(id, to);
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The step a key makes of the row it is pressed on: Alt with Up, Down, Home or End. */
|
|
132
|
+
export function stepKey(event: {
|
|
133
|
+
key: string;
|
|
134
|
+
altKey: boolean;
|
|
135
|
+
ctrlKey: boolean;
|
|
136
|
+
metaKey: boolean;
|
|
137
|
+
shiftKey: boolean;
|
|
138
|
+
}): Step | undefined {
|
|
139
|
+
if (!event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
switch (event.key) {
|
|
143
|
+
case "ArrowUp":
|
|
144
|
+
return "up";
|
|
145
|
+
case "ArrowDown":
|
|
146
|
+
return "down";
|
|
147
|
+
case "Home":
|
|
148
|
+
return "first";
|
|
149
|
+
case "End":
|
|
150
|
+
return "last";
|
|
151
|
+
default:
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// A tooltip shows after the pointer's rest, and a second one within `RECENT`
|
|
2
|
+
// of the last's going shows with no rest and no fade (the reader is already
|
|
3
|
+
// scanning names). Base UI's provider keeps that window for the icon acts'
|
|
4
|
+
// names; the cut-text tooltip mounts and unmounts its own, so it reads the
|
|
5
|
+
// moment either one went from here.
|
|
6
|
+
export const RECENT = 300;
|
|
7
|
+
|
|
8
|
+
let gone = Number.NEGATIVE_INFINITY;
|
|
9
|
+
|
|
10
|
+
export function hid(): void {
|
|
11
|
+
gone = performance.now();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function recently(): boolean {
|
|
15
|
+
return performance.now() - gone < RECENT;
|
|
16
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createStore } from "@slotform/ui-core/store";
|
|
2
|
+
|
|
3
|
+
// A viewer's choice is theirs from one visit to the next: a convenience, so a
|
|
4
|
+
// store that is blocked or empty leaves the choice for the visit alone. What
|
|
5
|
+
// was read or written stands here, the one source every part of the page
|
|
6
|
+
// reads.
|
|
7
|
+
const held = createStore(new Map<string, string | null>());
|
|
8
|
+
|
|
9
|
+
export const subscribeSaved = held.subscribe;
|
|
10
|
+
|
|
11
|
+
export function savedChoice(key: string): string | null {
|
|
12
|
+
const saved = held.get();
|
|
13
|
+
if (!saved.has(key)) {
|
|
14
|
+
try {
|
|
15
|
+
saved.set(key, localStorage.getItem(key));
|
|
16
|
+
} catch {
|
|
17
|
+
saved.set(key, null);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return saved.get(key) ?? null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function saveChoice(key: string, value: string): void {
|
|
24
|
+
held.get().set(key, value);
|
|
25
|
+
try {
|
|
26
|
+
localStorage.setItem(key, value);
|
|
27
|
+
} catch {}
|
|
28
|
+
held.emit();
|
|
29
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import {
|
|
3
|
+
kindOf,
|
|
4
|
+
loading,
|
|
5
|
+
type PaintRole,
|
|
6
|
+
paintShape,
|
|
7
|
+
setLoading,
|
|
8
|
+
ticker,
|
|
9
|
+
tuned,
|
|
10
|
+
} from "@slotform/ui-core/paint";
|
|
11
|
+
import {
|
|
12
|
+
BUSY_STILL,
|
|
13
|
+
busy,
|
|
14
|
+
decode,
|
|
15
|
+
frame,
|
|
16
|
+
lockedAt,
|
|
17
|
+
seedOf,
|
|
18
|
+
} from "@slotform/ui-core/scramble";
|
|
19
|
+
import type { LoadingRegion } from "@slotform/ui-core/tokens";
|
|
20
|
+
import { paint, SCRAMBLE } from "@slotform/ui-core/variants";
|
|
21
|
+
import { useId, useLayoutEffect, useRef } from "react";
|
|
22
|
+
import { pageLang } from "./lang.ts";
|
|
23
|
+
import { reducedNow } from "./media.ts";
|
|
24
|
+
import { NOOP } from "./noop.ts";
|
|
25
|
+
|
|
26
|
+
// The `loading` record read from the root: docs/architecture/plugins/react-ui.md, "Mode script and CSP".
|
|
27
|
+
const ATTRIBUTES: Record<LoadingRegion, string> = {
|
|
28
|
+
text: "loadingText",
|
|
29
|
+
figure: "loadingFigure",
|
|
30
|
+
id: "loadingId",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
if (typeof document !== "undefined") {
|
|
34
|
+
const set = document.documentElement.dataset;
|
|
35
|
+
setLoading(
|
|
36
|
+
// `Object.keys` widens to `string[]`; `ATTRIBUTES` is keyed by every region.
|
|
37
|
+
Object.fromEntries(
|
|
38
|
+
(Object.keys(ATTRIBUTES) as LoadingRegion[])
|
|
39
|
+
.filter((region) => set[ATTRIBUTES[region]] === "scramble")
|
|
40
|
+
.map((region) => [region, "scramble"]),
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Whether the viewer asked for reduced motion, read when a scramble mounts: a
|
|
46
|
+
// scramble then stays a still bar (the stylesheet draws the bar and hides the
|
|
47
|
+
// glyphs, so the page and the script agree).
|
|
48
|
+
const stilled = (node: Element): boolean =>
|
|
49
|
+
reducedNow() || node.closest("[data-still]") !== null;
|
|
50
|
+
|
|
51
|
+
// The glyphs the ticker writes, in the region's own face and ink. They are laid
|
|
52
|
+
// over the filler (`Painted` sizes the region with it), clipped to its box, one
|
|
53
|
+
// line, so a letter's width never moves what stands after the region.
|
|
54
|
+
const GLYPHS =
|
|
55
|
+
"absolute inset-0 overflow-hidden whitespace-nowrap motion-reduce:hidden";
|
|
56
|
+
|
|
57
|
+
/** The text a scrambled region draws: the page's ticker writes the node directly, so a tick renders nothing. */
|
|
58
|
+
function Scramble(props: { kind: LoadingRegion; shape: string }) {
|
|
59
|
+
const { kind, shape } = props;
|
|
60
|
+
const node = useRef<HTMLSpanElement>(null);
|
|
61
|
+
const seed = seedOf(useId());
|
|
62
|
+
useLayoutEffect(() => {
|
|
63
|
+
const el = node.current;
|
|
64
|
+
if (!el) return;
|
|
65
|
+
el.textContent = frame(kind, shape, ticker.now(), seed);
|
|
66
|
+
if (stilled(el)) return;
|
|
67
|
+
return ticker.subscribe((tick) => {
|
|
68
|
+
el.textContent = frame(kind, shape, tick, seed);
|
|
69
|
+
});
|
|
70
|
+
}, [kind, shape, seed]);
|
|
71
|
+
return <span ref={node} aria-hidden data-scrambling="" className={GLYPHS} />;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A glyph region painted by the scramble: the filler sizes the box (transparent), the generated characters stand over it in the region's own ink, and the fill returns under reduced motion and in a still frame (`globals.css`). */
|
|
75
|
+
export function ScrambledGlyphs(props: { role: PaintRole; at: number }) {
|
|
76
|
+
const { role, at } = props;
|
|
77
|
+
const kind = kindOf(role);
|
|
78
|
+
const shape = paintShape(role, at, pageLang());
|
|
79
|
+
return (
|
|
80
|
+
<span
|
|
81
|
+
aria-hidden
|
|
82
|
+
data-painted=""
|
|
83
|
+
data-scramble=""
|
|
84
|
+
className={cn(
|
|
85
|
+
paint({ kind: "box" }),
|
|
86
|
+
SCRAMBLE,
|
|
87
|
+
"box-decoration-clone select-none",
|
|
88
|
+
)}
|
|
89
|
+
>
|
|
90
|
+
<span className="text-transparent">{shape}</span>
|
|
91
|
+
<Scramble kind={kind} shape={shape} />
|
|
92
|
+
</span>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A figure or an id arriving, its characters locking from the reading start across `slow` while the layout is the loaded one: tabular digits and the mono face give every glyph one advance. */
|
|
97
|
+
export function Decoding(props: { text: string; onDone: () => void }) {
|
|
98
|
+
const { text, onDone } = props;
|
|
99
|
+
const node = useRef<HTMLSpanElement>(null);
|
|
100
|
+
const seed = seedOf(useId());
|
|
101
|
+
useLayoutEffect(() => {
|
|
102
|
+
const el = node.current;
|
|
103
|
+
if (!el) return;
|
|
104
|
+
const rtl = el.closest("[dir]")?.getAttribute("dir") === "rtl";
|
|
105
|
+
const length = Array.from(text).length;
|
|
106
|
+
const start = performance.now();
|
|
107
|
+
let frameId = 0;
|
|
108
|
+
// Keeps the page's clock running for the tail's characters.
|
|
109
|
+
const off = ticker.subscribe(NOOP);
|
|
110
|
+
// The locks follow the clock frame by frame; the characters not yet locked
|
|
111
|
+
// change at the page's tick.
|
|
112
|
+
const draw = () => {
|
|
113
|
+
const locked = lockedAt(performance.now() - start, length, tuned.slow);
|
|
114
|
+
el.textContent = decode(text, locked, ticker.now(), seed, rtl);
|
|
115
|
+
if (locked >= length) onDone();
|
|
116
|
+
else frameId = requestAnimationFrame(draw);
|
|
117
|
+
};
|
|
118
|
+
draw();
|
|
119
|
+
return () => {
|
|
120
|
+
cancelAnimationFrame(frameId);
|
|
121
|
+
off();
|
|
122
|
+
};
|
|
123
|
+
}, [text, seed, onDone]);
|
|
124
|
+
return <span ref={node} aria-hidden />;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The busy glyph's text: three mono glyphs the page's ticker cycles, `···` under reduced motion. */
|
|
128
|
+
export function BusyGlyphs() {
|
|
129
|
+
const node = useRef<HTMLSpanElement>(null);
|
|
130
|
+
const seed = seedOf(useId());
|
|
131
|
+
useLayoutEffect(() => {
|
|
132
|
+
const el = node.current;
|
|
133
|
+
if (!el) return;
|
|
134
|
+
el.textContent = busy(ticker.now(), seed);
|
|
135
|
+
if (stilled(el)) {
|
|
136
|
+
el.textContent = BUSY_STILL;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
return ticker.subscribe((tick) => {
|
|
140
|
+
el.textContent = busy(tick, seed);
|
|
141
|
+
});
|
|
142
|
+
}, [seed]);
|
|
143
|
+
return <span ref={node} aria-hidden />;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Whether the page's busy glyph is the scramble's three glyphs: the theme scrambles its text. */
|
|
147
|
+
export const busyScrambles = (): boolean => loading.text === "scramble";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { isTabbable } from "./focus.ts";
|
|
3
|
+
|
|
4
|
+
/** Whether a region takes a tab stop of its own: it runs past its box vertically and holds nothing a keyboard reaches, so no other stop scrolls it. */
|
|
5
|
+
function takesStop(
|
|
6
|
+
box: { scrollHeight: number; clientHeight: number },
|
|
7
|
+
tabbable: boolean,
|
|
8
|
+
): boolean {
|
|
9
|
+
return box.scrollHeight > box.clientHeight && !tabbable;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Whether a mutation changed `node`'s own children, the ones its size is watched on; a change deeper in the subtree only needs a re-measure. */
|
|
13
|
+
function changesChildren(
|
|
14
|
+
records: ReadonlyArray<{ type: string; target: unknown }>,
|
|
15
|
+
node: unknown,
|
|
16
|
+
): boolean {
|
|
17
|
+
return records.some(
|
|
18
|
+
(record) => record.type === "childList" && record.target === node,
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// The attributes that make an element reachable by Tab, or not (`isTabbable`).
|
|
23
|
+
const TABBABLE_ATTRIBUTES = [
|
|
24
|
+
"tabindex",
|
|
25
|
+
"disabled",
|
|
26
|
+
"hidden",
|
|
27
|
+
"inert",
|
|
28
|
+
"aria-hidden",
|
|
29
|
+
"href",
|
|
30
|
+
"contenteditable",
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/** Gives a scrolling region its tab stop (`takesStop`), measured as it resizes. Its children are watched too, since new content resizes them and not the box; a change to its own children rebinds them. Content swapping anywhere inside it, or an element becoming or ceasing to be tabbable, re-measures it, since a loaded body holds its waiting size and resizes nothing. The stop is set on the node in the observer's own callback, before the frame paints: a state would land a frame later, and a commit inside the callback resizes what it observes. The node takes no `tabIndex` prop, so a render never undoes it. */
|
|
34
|
+
export function useScrolls(node: HTMLElement | null): void {
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!node) return;
|
|
37
|
+
const measure = () => {
|
|
38
|
+
const stop = takesStop(
|
|
39
|
+
node,
|
|
40
|
+
Array.from(node.querySelectorAll<HTMLElement>("*")).some(isTabbable),
|
|
41
|
+
);
|
|
42
|
+
// Only a change writes: the write is itself a mutation it watches.
|
|
43
|
+
if (stop === node.hasAttribute("tabindex")) return;
|
|
44
|
+
if (stop) node.tabIndex = 0;
|
|
45
|
+
else node.removeAttribute("tabindex");
|
|
46
|
+
};
|
|
47
|
+
const resize = new ResizeObserver(measure);
|
|
48
|
+
const watch = () => {
|
|
49
|
+
resize.disconnect();
|
|
50
|
+
resize.observe(node);
|
|
51
|
+
for (const child of node.children) resize.observe(child);
|
|
52
|
+
measure();
|
|
53
|
+
};
|
|
54
|
+
const content = new MutationObserver((records) => {
|
|
55
|
+
if (changesChildren(records, node)) watch();
|
|
56
|
+
else measure();
|
|
57
|
+
});
|
|
58
|
+
content.observe(node, {
|
|
59
|
+
childList: true,
|
|
60
|
+
subtree: true,
|
|
61
|
+
attributes: true,
|
|
62
|
+
attributeFilter: TABBABLE_ATTRIBUTES,
|
|
63
|
+
});
|
|
64
|
+
watch();
|
|
65
|
+
return () => {
|
|
66
|
+
resize.disconnect();
|
|
67
|
+
content.disconnect();
|
|
68
|
+
};
|
|
69
|
+
}, [node]);
|
|
70
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CSSProperties } from "react";
|
|
2
|
+
|
|
3
|
+
/** A style that sets custom properties beside the standard ones, which `CSSProperties` has no key for. Outside the package's exports. */
|
|
4
|
+
export type VarStyle = CSSProperties & {
|
|
5
|
+
[custom: `--${string}`]: number | string | undefined;
|
|
6
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import { tuned } from "@slotform/ui-core/paint";
|
|
3
|
+
import { rollOf, rollShift } from "@slotform/ui-core/roll";
|
|
4
|
+
import { SWAP_IN, SWAP_OUT } from "@slotform/ui-core/variants";
|
|
5
|
+
import {
|
|
6
|
+
type ReactNode,
|
|
7
|
+
useEffect,
|
|
8
|
+
useLayoutEffect,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
} from "react";
|
|
12
|
+
import type { VarStyle } from "./style.ts";
|
|
13
|
+
|
|
14
|
+
// The mark's size the new one grows from and the old one leaves toward.
|
|
15
|
+
const MARK_FROM = 0.6;
|
|
16
|
+
|
|
17
|
+
interface Leaving {
|
|
18
|
+
id: string;
|
|
19
|
+
node: ReactNode;
|
|
20
|
+
shift: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A value that changes while shown (a mark, a figure, a label): when `id` changes the new one enters and the old one, held for the rung, leaves over it (`SWAP_IN`, `SWAP_OUT`). With `of`, its number, the old leaves and the new rises by the sign of the change (a figure rolls, in a clipped box); with `scale`, a mark swaps: the old one leaves over `fast` and the new one then grows from 0.6, so the two are never drawn together; with neither it is a cross-fade. It mounts at rest: a value that arrives (a waiting shape's switch to its loaded one) is the settle's, never a swap. */
|
|
24
|
+
export function Swap(props: {
|
|
25
|
+
id: string;
|
|
26
|
+
of?: number;
|
|
27
|
+
scale?: boolean;
|
|
28
|
+
children: ReactNode;
|
|
29
|
+
}) {
|
|
30
|
+
const { id, of, scale, children } = props;
|
|
31
|
+
const [at, setAt] = useState({ id, of });
|
|
32
|
+
const [leaving, setLeaving] = useState<Leaving>();
|
|
33
|
+
// What the last commit drew, which is what leaves.
|
|
34
|
+
const drawn = useRef<ReactNode>(children);
|
|
35
|
+
useLayoutEffect(() => {
|
|
36
|
+
drawn.current = children;
|
|
37
|
+
});
|
|
38
|
+
if (at.id !== id) {
|
|
39
|
+
setAt({ id, of });
|
|
40
|
+
setLeaving({
|
|
41
|
+
id: at.id,
|
|
42
|
+
node: drawn.current,
|
|
43
|
+
shift:
|
|
44
|
+
at.of !== undefined && of !== undefined
|
|
45
|
+
? rollShift(rollOf(at.of, of))
|
|
46
|
+
: 0,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!leaving) return;
|
|
51
|
+
// No `animationend` ends the leave: an animation that never runs (under a
|
|
52
|
+
// `display: none` ancestor) or is cut fires none, and the old one would
|
|
53
|
+
// stand with the box taking no press; a mark's leave is two animations.
|
|
54
|
+
const timer = setTimeout(
|
|
55
|
+
() => setLeaving(undefined),
|
|
56
|
+
scale ? 2 * tuned.fast : tuned.fast,
|
|
57
|
+
);
|
|
58
|
+
return () => clearTimeout(timer);
|
|
59
|
+
}, [leaving, scale]);
|
|
60
|
+
const style = (shift: number, enters = false): VarStyle => ({
|
|
61
|
+
...(shift === 0 ? {} : { "--swap-y": shift }),
|
|
62
|
+
...(scale ? { "--swap-scale": MARK_FROM } : {}),
|
|
63
|
+
// A mark's new one waits for the old one to be gone (`fast`; 0 under
|
|
64
|
+
// reduced motion, so it stands at once).
|
|
65
|
+
...(scale && enters
|
|
66
|
+
? { animationDelay: "var(--transition-duration-fast)" }
|
|
67
|
+
: {}),
|
|
68
|
+
});
|
|
69
|
+
return (
|
|
70
|
+
<span
|
|
71
|
+
className={cn(
|
|
72
|
+
"inline-flex",
|
|
73
|
+
// Positioned only while the old one leaves: at rest it must not stand
|
|
74
|
+
// over a row's hit, which it would take the press from.
|
|
75
|
+
leaving && "relative pointer-events-none",
|
|
76
|
+
of !== undefined && "overflow-clip",
|
|
77
|
+
)}
|
|
78
|
+
>
|
|
79
|
+
{leaving ? (
|
|
80
|
+
<span
|
|
81
|
+
key={`out-${leaving.id}`}
|
|
82
|
+
aria-hidden
|
|
83
|
+
style={style(leaving.shift)}
|
|
84
|
+
className={cn("absolute inset-0 inline-flex", SWAP_OUT)}
|
|
85
|
+
>
|
|
86
|
+
{leaving.node}
|
|
87
|
+
</span>
|
|
88
|
+
) : null}
|
|
89
|
+
<span
|
|
90
|
+
key={id}
|
|
91
|
+
style={style(leaving?.shift ?? 0, true)}
|
|
92
|
+
className={cn("inline-flex", leaving && SWAP_IN)}
|
|
93
|
+
>
|
|
94
|
+
{children}
|
|
95
|
+
</span>
|
|
96
|
+
</span>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** A figure that rolls when its number changes while shown (`rollOf`): `text` is what it reads, `of` the number it is. A clock's tick is no `Roll`: it switches at once. */
|
|
101
|
+
export function Roll(props: { text: string; of: number }) {
|
|
102
|
+
return (
|
|
103
|
+
<Swap id={props.text} of={props.of}>
|
|
104
|
+
{props.text}
|
|
105
|
+
</Swap>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Toast } from "@base-ui/react/toast";
|
|
2
|
+
import type { LinkAct } from "@slotform/ui-core/descriptors";
|
|
3
|
+
import { TOAST_MS, type Tone } from "@slotform/ui-core/tokens";
|
|
4
|
+
import { createContext } from "react";
|
|
5
|
+
import type { Act } from "./act.ts";
|
|
6
|
+
|
|
7
|
+
// How the act a toast reports ended (its glyph in the tone's ink, `done` by
|
|
8
|
+
// default), and an
|
|
9
|
+
// act it offers (a route or a retry; client-owned, so never an undo).
|
|
10
|
+
export interface ToastOptions {
|
|
11
|
+
tone?: Tone;
|
|
12
|
+
act?: Act | LinkAct;
|
|
13
|
+
/** Called once the toast has left, however it went. */
|
|
14
|
+
onGone?: () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// The app's toast queue: `toast()` is called from anywhere, and the Shell
|
|
18
|
+
// stands the queue in its toasts' layer. A failed one is announced at once.
|
|
19
|
+
export interface ToastData extends ToastOptions {
|
|
20
|
+
sentence: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const toasts = Toast.createToastManager<ToastData>();
|
|
24
|
+
|
|
25
|
+
/** Queues a toast: `sentence` is what happened (a sentence; wraps). */
|
|
26
|
+
export function toast(sentence: string, options: ToastOptions = {}): void {
|
|
27
|
+
toasts.add({
|
|
28
|
+
description: sentence,
|
|
29
|
+
data: { sentence, ...options },
|
|
30
|
+
onRemove: options.onGone,
|
|
31
|
+
timeout: TOAST_MS,
|
|
32
|
+
priority: options.tone === "failed" ? "high" : "low",
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The queued toast a `Toast` draws: the layer hands each its entry.
|
|
37
|
+
export const ToastEntry = createContext<
|
|
38
|
+
Toast.Root.ToastObject<ToastData> | undefined
|
|
39
|
+
>(undefined);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// The class lists the walk's exceptions name: docs/architecture/plugins/screens.md, "Render walk exceptions".
|
|
2
|
+
|
|
3
|
+
/** An image that opens: a button sized by its picture. */
|
|
4
|
+
export const IMAGE_BOX = "block relative shrink-0 overflow-hidden";
|
|
5
|
+
|
|
6
|
+
/** A modal Sheet's overlay: the viewport, over the scrim. */
|
|
7
|
+
export const SHEET_BACKDROP = "fixed inset-0 z-(--layer-sheet)";
|
|
8
|
+
|
|
9
|
+
/** A BarChart's body: the plot starts half a tick label's line below its top. */
|
|
10
|
+
export const BAR_CHART_BODY = "flex items-start pt-[calc(1lh/2)]";
|
|
11
|
+
|
|
12
|
+
/** The Gate's column, centred by an auto margin. */
|
|
13
|
+
export const GATE_CENTRED = "flex flex-col my-auto";
|
|
14
|
+
|
|
15
|
+
/** A canvas group's head band: it stands over the edges that cross it into the frame, so no edge strikes through its words. */
|
|
16
|
+
export const GROUP_HEAD = "relative z-1 flex items-center";
|