@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,113 @@
|
|
|
1
|
+
import { Toast as Control } from "@base-ui/react/toast";
|
|
2
|
+
import { TOAST_GLYPHS } from "@slotform/ui-core/banner";
|
|
3
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
4
|
+
import type { LinkAct } from "@slotform/ui-core/descriptors";
|
|
5
|
+
import { useWords } from "@slotform/ui-core/react/words";
|
|
6
|
+
import type { Tone } from "@slotform/ui-core/tokens";
|
|
7
|
+
import {
|
|
8
|
+
LAYER_TOAST,
|
|
9
|
+
TOAST,
|
|
10
|
+
text,
|
|
11
|
+
toastState,
|
|
12
|
+
YIELDS_FIRST,
|
|
13
|
+
} from "@slotform/ui-core/variants";
|
|
14
|
+
import { use, useRef } from "react";
|
|
15
|
+
import { IconButtonBase } from "../../atoms/button/glyph.tsx";
|
|
16
|
+
import { Icon } from "../../atoms/icon/index.tsx";
|
|
17
|
+
import type { Act } from "../../lib/act.ts";
|
|
18
|
+
import type { Closed } from "../../lib/closed.ts";
|
|
19
|
+
import { useFollow } from "../../lib/follow.ts";
|
|
20
|
+
import { ToastEntry } from "../../lib/toast.ts";
|
|
21
|
+
import { ActFace } from "../button/face.tsx";
|
|
22
|
+
|
|
23
|
+
// A swipe along the line is the toast's; the page keeps the vertical pan.
|
|
24
|
+
const BOX = "flex items-center max-w-full pointer-events-auto touch-pan-y";
|
|
25
|
+
// On touch the toast is as wide as its words (a short report is a pill, never a
|
|
26
|
+
// full-width card over a page that is mostly empty), centred in the layer.
|
|
27
|
+
const HUG = "touch:w-fit touch:left-0 touch:mx-auto";
|
|
28
|
+
// Each toast stands over the ones in front of it, Base UI's offset (their
|
|
29
|
+
// heights) plus a pair per toast in front (`translate`); `LAYER_TOAST` enters,
|
|
30
|
+
// leaves and closes it up.
|
|
31
|
+
const STACKED =
|
|
32
|
+
"absolute bottom-0 right-0 -translate-y-[calc(var(--toast-offset-y)_+_var(--toast-index)_*_var(--spacing-gap))]";
|
|
33
|
+
const GLYPH = "flex shrink-0";
|
|
34
|
+
const SENTENCE = `min-w-0 grow ${YIELDS_FIRST}`;
|
|
35
|
+
// The act gives way only once the sentence has (its shrink weight is 10^7 against
|
|
36
|
+
// the act's 1).
|
|
37
|
+
const ACT = "flex min-w-0 max-w-full";
|
|
38
|
+
|
|
39
|
+
/** A short report of how an act went. */
|
|
40
|
+
interface ToastProps extends Closed {
|
|
41
|
+
/** What happened (a sentence; wraps). */
|
|
42
|
+
sentence: string;
|
|
43
|
+
/** How the act it reports ended (`done`, the default): its glyph in the tone's ink. */
|
|
44
|
+
tone?: Tone;
|
|
45
|
+
/** A route (`{ label, href }`, an anchor) or a retry; never an undo. */
|
|
46
|
+
act?: Act | LinkAct;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A raised toast: the tone's glyph, the sentence, its act and the dismiss act. It stands in the toasts' layer the Shell holds, which `toast()` queues it to; Base UI's toast pauses its timer under the pointer or focus, and a failed one is announced at once. On touch it is as wide as its words (a short report is a pill, a long one fills the layer) and centred, and follows a swipe toward the inline end 1:1 and leaves past the reach or flung, else settles back. */
|
|
50
|
+
export function Toast({ sentence, tone = "done", act }: ToastProps) {
|
|
51
|
+
const words = useWords();
|
|
52
|
+
const entry = use(ToastEntry);
|
|
53
|
+
const { close } = Control.useToastManager();
|
|
54
|
+
const root = useRef<HTMLDivElement>(null);
|
|
55
|
+
const swipe = useFollow({
|
|
56
|
+
target: root,
|
|
57
|
+
axis: "x",
|
|
58
|
+
away: "inline-end",
|
|
59
|
+
enabled: true,
|
|
60
|
+
onAway: () => entry && close(entry.id),
|
|
61
|
+
});
|
|
62
|
+
if (!entry)
|
|
63
|
+
throw new Error(
|
|
64
|
+
"a Toast stands in the Shell's toasts layer, queued by toast()",
|
|
65
|
+
);
|
|
66
|
+
// A failed (high priority) toast is announced by its own alert, and Base UI
|
|
67
|
+
// hides the toast itself until it is focused so a polite region does not read
|
|
68
|
+
// it twice. Its acts must stay reachable (a hidden subtree holding a focusable
|
|
69
|
+
// act fails `aria-hidden-focus`), so the toast stays in the tree and turns
|
|
70
|
+
// its own live region off instead: the alert is the one announcement.
|
|
71
|
+
const alerted = entry.priority === "high";
|
|
72
|
+
return (
|
|
73
|
+
<Control.Root
|
|
74
|
+
ref={root}
|
|
75
|
+
toast={entry}
|
|
76
|
+
// The swipe is `useFollow`'s, with the reach and the fling the phone has.
|
|
77
|
+
swipeDirection={[]}
|
|
78
|
+
onPointerDown={swipe.onPointerDown}
|
|
79
|
+
onPointerMove={swipe.onPointerMove}
|
|
80
|
+
onPointerUp={swipe.onPointerUp}
|
|
81
|
+
onPointerCancel={swipe.onPointerCancel}
|
|
82
|
+
aria-hidden={alerted ? false : undefined}
|
|
83
|
+
aria-live={alerted ? "off" : undefined}
|
|
84
|
+
// The Shell's page makes room for it (`PAGE_BODY_UNDER_TOAST`).
|
|
85
|
+
data-toast=""
|
|
86
|
+
className={cn(TOAST, BOX, STACKED, HUG, LAYER_TOAST)}
|
|
87
|
+
>
|
|
88
|
+
<span className={cn(toastState({ tone }), GLYPH)}>
|
|
89
|
+
<Icon name={TOAST_GLYPHS[tone]} />
|
|
90
|
+
</span>
|
|
91
|
+
<Control.Title
|
|
92
|
+
render={<span />}
|
|
93
|
+
className={cn(text({ role: "body" }), SENTENCE)}
|
|
94
|
+
>
|
|
95
|
+
{sentence}
|
|
96
|
+
</Control.Title>
|
|
97
|
+
{act ? (
|
|
98
|
+
<span className={ACT}>
|
|
99
|
+
<ActFace
|
|
100
|
+
act={act}
|
|
101
|
+
kind="secondary"
|
|
102
|
+
blocked={"blocked" in act ? act.blocked : undefined}
|
|
103
|
+
/>
|
|
104
|
+
</span>
|
|
105
|
+
) : null}
|
|
106
|
+
{/* Base UI hides the close act while the stack is collapsed; ours is always drawn, so it stays in the tree. */}
|
|
107
|
+
<Control.Close
|
|
108
|
+
aria-hidden={false}
|
|
109
|
+
render={<IconButtonBase icon="X" label={words.dismiss} />}
|
|
110
|
+
/>
|
|
111
|
+
</Control.Root>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Toast as Control } from "@base-ui/react/toast";
|
|
2
|
+
import { type ToastData, ToastEntry } from "../../lib/toast.ts";
|
|
3
|
+
|
|
4
|
+
// The stack's anchor at the layer's corner, a toast wide: each toast stands
|
|
5
|
+
// on its foot, raised by Base UI's offset.
|
|
6
|
+
const ANCHOR = "relative w-toast max-w-full";
|
|
7
|
+
|
|
8
|
+
import { Toast } from "./index.tsx";
|
|
9
|
+
|
|
10
|
+
/** The queued toasts, oldest first, each drawn as a `Toast`. Outside the package's exports: the toasts' layer (the Shell's) stands them inside Base UI's viewport. */
|
|
11
|
+
export function ToastList() {
|
|
12
|
+
const { toasts } = Control.useToastManager<ToastData>();
|
|
13
|
+
return (
|
|
14
|
+
<div className={ANCHOR}>
|
|
15
|
+
{toasts.toReversed().map((entry) => (
|
|
16
|
+
<ToastEntry key={entry.id} value={entry}>
|
|
17
|
+
<Toast
|
|
18
|
+
sentence={entry.data?.sentence ?? ""}
|
|
19
|
+
tone={entry.data?.tone}
|
|
20
|
+
act={entry.data?.act}
|
|
21
|
+
/>
|
|
22
|
+
</ToastEntry>
|
|
23
|
+
))}
|
|
24
|
+
</div>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
2
|
+
import { Popover } from "@base-ui/react/popover";
|
|
3
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
4
|
+
import type { MultiPick, OptionPick } from "@slotform/ui-core/descriptors";
|
|
5
|
+
import { filterChip, filterEmpty } from "@slotform/ui-core/list-state";
|
|
6
|
+
import { useOptions } from "@slotform/ui-core/react/read";
|
|
7
|
+
import { useWords } from "@slotform/ui-core/react/words";
|
|
8
|
+
import { type Fielded, paints } from "@slotform/ui-core/read";
|
|
9
|
+
import type { OptionText } from "@slotform/ui-core/reason";
|
|
10
|
+
import {
|
|
11
|
+
LAYER_POPOVER,
|
|
12
|
+
POPOVER,
|
|
13
|
+
TOOLBAR_FILTERS,
|
|
14
|
+
TOOLBAR_FILTERS_POPOVER,
|
|
15
|
+
} from "@slotform/ui-core/variants";
|
|
16
|
+
import { use, useRef, useState } from "react";
|
|
17
|
+
import { IconButtonBase } from "../../atoms/button/glyph.tsx";
|
|
18
|
+
import { ChipBase } from "../../atoms/chip/base.tsx";
|
|
19
|
+
import { useAct } from "../../lib/act.ts";
|
|
20
|
+
import { useQuietGuards } from "../../lib/guards.ts";
|
|
21
|
+
import { spacing } from "../../lib/measure.ts";
|
|
22
|
+
import { useTouch } from "../../lib/media.ts";
|
|
23
|
+
import { PortalContainer } from "../../lib/portal.ts";
|
|
24
|
+
import { ButtonBase } from "../button/base.tsx";
|
|
25
|
+
import { FormField } from "../form-field/index.tsx";
|
|
26
|
+
import { SheetBase } from "../sheet/base.tsx";
|
|
27
|
+
|
|
28
|
+
// A filter as a toolbar takes it: a `Picker`'s own props, its `options` a list
|
|
29
|
+
// or a read of one, as the picker's are.
|
|
30
|
+
type Readable<F> = F extends { options: infer O }
|
|
31
|
+
? Omit<F, "options"> & { options: Fielded<O, OptionText> }
|
|
32
|
+
: never;
|
|
33
|
+
export type FilterProp =
|
|
34
|
+
| Readable<OptionPick<string | null>>
|
|
35
|
+
| Readable<MultiPick<string> & { several: true }>;
|
|
36
|
+
|
|
37
|
+
// The popover stops at the room Base UI measures under the act, its fields
|
|
38
|
+
// scrolling inside it; a sheet's body scrolls itself.
|
|
39
|
+
const POPUP = "flex flex-col min-w-0 max-h-(--available-height)";
|
|
40
|
+
const POSITIONER = "z-(--layer-popover)";
|
|
41
|
+
const PANEL = "flex flex-col min-h-0 overflow-y-auto overscroll-contain";
|
|
42
|
+
// The clear act stands at the panel's start edge, under the fields.
|
|
43
|
+
const CLEAR = "flex items-start";
|
|
44
|
+
|
|
45
|
+
// What setting a filter to its empty value is, run in the holder's toast: the
|
|
46
|
+
// consumer's `onChange` takes `null`, or `[]` for several.
|
|
47
|
+
function clear(filter: FilterProp) {
|
|
48
|
+
return (filter.onChange as (value: null | []) => unknown)(
|
|
49
|
+
filterEmpty(filter),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One set filter's removable chip, "Work type: Change", over its options as they stand: painted while the read waits, and its value as given when the read ended, so nothing set is hidden. Removing it sets the filter to its empty value, a rejection toasting. Outside the package's exports. */
|
|
54
|
+
export function FilterChip({ filter }: { filter: FilterProp }) {
|
|
55
|
+
const { drawn } = useOptions(filter.options);
|
|
56
|
+
const acting = useAct("toast");
|
|
57
|
+
if (paints(drawn)) return <ChipBase label="" family="neutral" painted />;
|
|
58
|
+
return (
|
|
59
|
+
<ChipBase
|
|
60
|
+
family="neutral"
|
|
61
|
+
label={filterChip(filter, drawn.painted ? [] : drawn.value)}
|
|
62
|
+
onRemove={() => acting.attempt(true, { onAct: () => clear(filter) })}
|
|
63
|
+
/>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The labelled picks, each applying at once as the viewer picks, and the act
|
|
68
|
+
// that sets every one to its empty value while any is set. Clearing hands focus
|
|
69
|
+
// to the first pick, since the act it was on is gone.
|
|
70
|
+
function Fields(props: { filters: readonly FilterProp[]; set: number }) {
|
|
71
|
+
const words = useWords();
|
|
72
|
+
const panel = useRef<HTMLDivElement>(null);
|
|
73
|
+
const clearAll = () => {
|
|
74
|
+
panel.current?.querySelector("button")?.focus();
|
|
75
|
+
return Promise.all(props.filters.map(clear));
|
|
76
|
+
};
|
|
77
|
+
return (
|
|
78
|
+
<div ref={panel} className={cn(TOOLBAR_FILTERS, PANEL)}>
|
|
79
|
+
{props.filters.map(({ label, ...picker }) => (
|
|
80
|
+
<FormField
|
|
81
|
+
key={label}
|
|
82
|
+
label={label}
|
|
83
|
+
// A pick of several holds strings, whose `string[]` change the
|
|
84
|
+
// field's `(string | null)[]` one cannot take; it never gives null.
|
|
85
|
+
picker={picker as never}
|
|
86
|
+
/>
|
|
87
|
+
))}
|
|
88
|
+
{props.set > 0 ? (
|
|
89
|
+
<div className={CLEAR}>
|
|
90
|
+
<ButtonBase act="quiet" label={words.clearFilters} onAct={clearAll} />
|
|
91
|
+
</div>
|
|
92
|
+
) : null}
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The one act a toolbar draws for its filters where they fold (three or more on the desktop, any on touch), "Filters" and the count of those set: it opens the filters as labelled picks, in a popover under it on the desktop and a sheet on touch. Escape and a press outside close it and focus returns to the act. Outside the package's exports. */
|
|
98
|
+
export function FiltersAct(props: {
|
|
99
|
+
filters: readonly FilterProp[];
|
|
100
|
+
set: number;
|
|
101
|
+
}) {
|
|
102
|
+
const words = useWords();
|
|
103
|
+
const touch = useTouch();
|
|
104
|
+
const container = use(PortalContainer);
|
|
105
|
+
useQuietGuards();
|
|
106
|
+
const [open, setOpen] = useState(false);
|
|
107
|
+
const [sheet] = useState(() => Dialog.createHandle<unknown>());
|
|
108
|
+
// On touch the act is its glyph alone at the search's end; the set filters
|
|
109
|
+
// show as the chips under the row. On the desktop it is the labelled button.
|
|
110
|
+
const act = touch ? (
|
|
111
|
+
<IconButtonBase icon="ListFilter" label={words.filters} pending={false} />
|
|
112
|
+
) : (
|
|
113
|
+
<ButtonBase
|
|
114
|
+
act="secondary"
|
|
115
|
+
icon="ListFilter"
|
|
116
|
+
label={words.filters}
|
|
117
|
+
count={props.set > 0 ? props.set : undefined}
|
|
118
|
+
pending={false}
|
|
119
|
+
/>
|
|
120
|
+
);
|
|
121
|
+
const fields = <Fields filters={props.filters} set={props.set} />;
|
|
122
|
+
if (touch)
|
|
123
|
+
return (
|
|
124
|
+
<>
|
|
125
|
+
<Dialog.Trigger handle={sheet} render={act} />
|
|
126
|
+
<SheetBase
|
|
127
|
+
handle={sheet}
|
|
128
|
+
open={open}
|
|
129
|
+
onOpen={() => setOpen(true)}
|
|
130
|
+
onClose={() => setOpen(false)}
|
|
131
|
+
title={words.filters}
|
|
132
|
+
>
|
|
133
|
+
{fields}
|
|
134
|
+
</SheetBase>
|
|
135
|
+
</>
|
|
136
|
+
);
|
|
137
|
+
return (
|
|
138
|
+
<Popover.Root modal={false} open={open} onOpenChange={setOpen}>
|
|
139
|
+
<Popover.Trigger render={act} />
|
|
140
|
+
<Popover.Portal container={container}>
|
|
141
|
+
<Popover.Positioner
|
|
142
|
+
className={POSITIONER}
|
|
143
|
+
align="start"
|
|
144
|
+
sideOffset={() => spacing("gap")}
|
|
145
|
+
>
|
|
146
|
+
<Popover.Popup
|
|
147
|
+
aria-label={words.filters}
|
|
148
|
+
className={cn(
|
|
149
|
+
POPOVER,
|
|
150
|
+
LAYER_POPOVER,
|
|
151
|
+
TOOLBAR_FILTERS_POPOVER,
|
|
152
|
+
POPUP,
|
|
153
|
+
)}
|
|
154
|
+
>
|
|
155
|
+
{fields}
|
|
156
|
+
</Popover.Popup>
|
|
157
|
+
</Popover.Positioner>
|
|
158
|
+
</Popover.Portal>
|
|
159
|
+
</Popover.Root>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import type {
|
|
3
|
+
LinkSegment,
|
|
4
|
+
ToolbarChip,
|
|
5
|
+
ToolbarSearch,
|
|
6
|
+
} from "@slotform/ui-core/descriptors";
|
|
7
|
+
import { filterSet, filtersFold } from "@slotform/ui-core/list-state";
|
|
8
|
+
import { FieldOwned } from "@slotform/ui-core/react/field";
|
|
9
|
+
import { useWords } from "@slotform/ui-core/react/words";
|
|
10
|
+
import {
|
|
11
|
+
TOOLBAR_CHIPS,
|
|
12
|
+
TOOLBAR_LINE,
|
|
13
|
+
TOOLBAR_ROW,
|
|
14
|
+
TOOLBAR_SEARCH,
|
|
15
|
+
TOOLBAR_TABS,
|
|
16
|
+
} from "@slotform/ui-core/variants";
|
|
17
|
+
import type { ReactNode } from "react";
|
|
18
|
+
import { CHIP_LINES } from "../../lib/chip-lines.ts";
|
|
19
|
+
import type { Closed } from "../../lib/closed.ts";
|
|
20
|
+
import { useTouch } from "../../lib/media.ts";
|
|
21
|
+
import { Chip } from "../chip/index.tsx";
|
|
22
|
+
import { Input } from "../input/index.tsx";
|
|
23
|
+
import { HeldPicker } from "../picker/base.tsx";
|
|
24
|
+
import { Picker, type PickerMenuProps } from "../picker/index.tsx";
|
|
25
|
+
import { FilterChip, type FilterProp, FiltersAct } from "./filters.tsx";
|
|
26
|
+
|
|
27
|
+
const STRIP = "flex flex-col";
|
|
28
|
+
const ROW = "flex flex-wrap items-center";
|
|
29
|
+
// The acts row yields inside the strip's row, so a field trigger wider than the
|
|
30
|
+
// line truncates its value instead of holding the row at its full label, and
|
|
31
|
+
// keeps the row's end whatever the search leaves.
|
|
32
|
+
const ACTS = "min-w-0 ms-auto";
|
|
33
|
+
// The tabs yield inside the row and scroll sideways in their own box.
|
|
34
|
+
const TABS = "flex";
|
|
35
|
+
// The chips line under the row carries the room above the hairline below it.
|
|
36
|
+
const CHIPS = "pb-gap";
|
|
37
|
+
|
|
38
|
+
/** A strip of controls over a list. */
|
|
39
|
+
export interface ToolbarProps extends Closed {
|
|
40
|
+
/** The views of the list, each a label and the `href` it opens, drawn first in the row, before the filters and acts, as a `Picker` in `fit: segments` draws links (pill tabs at the `control` height on no track ground): the one the location selects is marked, and a press opens its href in place. On touch, where the row wraps, they take the first line whole and scroll sideways, and the search its own line. */
|
|
41
|
+
tabs?: readonly LinkSegment[];
|
|
42
|
+
/** The search box: its value, what it hears and its hint. Absent, the strip has none. */
|
|
43
|
+
search?: ToolbarSearch;
|
|
44
|
+
/** The filters, each a `Picker`'s own props (its `label`, which names it and no other filter, `options` as a list or a read, `value` and `onChange`; a `value` that is an array picks several). On touch every filter folds; on the desktop two or fewer stand beside the acts as picks and three or more fold, all of them, behind one Filters act that opens them as labelled picks (a popover, a sheet on touch) and shows the count of those set, a removable chip for each one set in the chips row. */
|
|
45
|
+
filters?: readonly FilterProp[];
|
|
46
|
+
/** The applied filters of the app's own, each a removable `Chip`, in a row under the search and the acts, after the filters' chips. */
|
|
47
|
+
chips?: readonly ToolbarChip[];
|
|
48
|
+
/** The acts at the bar fit (a filter's `Button` with its count, an icon `Button`, a `Picker` at its default fit: `bar` fills its column and takes a line of its own here). Never a filled act: the create act is the page's. */
|
|
49
|
+
children?: ReactNode;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** One bar row with no hairline of its own: the `tabs` first, the search after them growing up to the `column` width, then the filters and acts at the row's end, at the control height (on touch the tabs take a line of their own, then the search leads the row and takes the width the acts leave, the Filters act its glyph alone), the applied filters' chips in a line under it, drawn only while any is applied; below `tablet` it leaves with its list while a record stands alone. */
|
|
53
|
+
export function Toolbar({
|
|
54
|
+
tabs,
|
|
55
|
+
search,
|
|
56
|
+
filters = [],
|
|
57
|
+
chips,
|
|
58
|
+
children,
|
|
59
|
+
}: ToolbarProps) {
|
|
60
|
+
const words = useWords();
|
|
61
|
+
const touch = useTouch();
|
|
62
|
+
const folded = filtersFold(filters.length, touch);
|
|
63
|
+
const set = filters.filter(filterSet);
|
|
64
|
+
// The chips row is a block of the page: the viewer's filter bringing the first
|
|
65
|
+
// chip or removing the last moves what follows.
|
|
66
|
+
const applied = (folded ? set.length : 0) + (chips?.length ?? 0);
|
|
67
|
+
return (
|
|
68
|
+
<div className={STRIP}>
|
|
69
|
+
<div className={cn(TOOLBAR_ROW, TOOLBAR_LINE, ROW)}>
|
|
70
|
+
{tabs ? (
|
|
71
|
+
<nav aria-label={words.tabs} className={cn(TOOLBAR_TABS, TABS)}>
|
|
72
|
+
<Picker fit="segments" options={tabs} />
|
|
73
|
+
</nav>
|
|
74
|
+
) : null}
|
|
75
|
+
{search ? (
|
|
76
|
+
<div className={TOOLBAR_SEARCH}>
|
|
77
|
+
<Input
|
|
78
|
+
label={words.search}
|
|
79
|
+
kind="search"
|
|
80
|
+
value={search.value}
|
|
81
|
+
onChange={search.onChange}
|
|
82
|
+
placeholder={search.placeholder}
|
|
83
|
+
/>
|
|
84
|
+
</div>
|
|
85
|
+
) : null}
|
|
86
|
+
{filters.length > 0 || children ? (
|
|
87
|
+
<div className={cn(TOOLBAR_ROW, ROW, ACTS)}>
|
|
88
|
+
{folded ? (
|
|
89
|
+
<FiltersAct filters={filters} set={set.length} />
|
|
90
|
+
) : (
|
|
91
|
+
filters.map((filter) => (
|
|
92
|
+
<HeldPicker
|
|
93
|
+
key={filter.label}
|
|
94
|
+
// A pick of several holds strings, whose `string[]` change the
|
|
95
|
+
// picker's `(string | null)[]` one cannot take; it never gives null.
|
|
96
|
+
{...(filter as PickerMenuProps<string | null>)}
|
|
97
|
+
fit="bar"
|
|
98
|
+
/>
|
|
99
|
+
))
|
|
100
|
+
)}
|
|
101
|
+
{/* The strip names the controls in its acts: each reads its label as its name. */}
|
|
102
|
+
<FieldOwned value>{children}</FieldOwned>
|
|
103
|
+
</div>
|
|
104
|
+
) : null}
|
|
105
|
+
</div>
|
|
106
|
+
{applied > 0 ? (
|
|
107
|
+
<div className={cn(TOOLBAR_CHIPS, CHIPS, ROW, CHIP_LINES)}>
|
|
108
|
+
{folded
|
|
109
|
+
? set.map((filter) => (
|
|
110
|
+
<FilterChip key={filter.label} filter={filter} />
|
|
111
|
+
))
|
|
112
|
+
: null}
|
|
113
|
+
{chips?.map((chip) => (
|
|
114
|
+
<Chip
|
|
115
|
+
key={`${chip.family}:${chip.label}`}
|
|
116
|
+
family={chip.family}
|
|
117
|
+
label={chip.label}
|
|
118
|
+
onRemove={chip.onRemove}
|
|
119
|
+
/>
|
|
120
|
+
))}
|
|
121
|
+
</div>
|
|
122
|
+
) : null}
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { trackTime } from "@slotform/ui-core/clock";
|
|
2
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
3
|
+
import type { Peaks } from "@slotform/ui-core/descriptors";
|
|
4
|
+
import { WAVEFORM_BAR, WAVEFORM_RECORD } from "@slotform/ui-core/variants";
|
|
5
|
+
import {
|
|
6
|
+
COLUMN,
|
|
7
|
+
type Columns,
|
|
8
|
+
columnBoxes,
|
|
9
|
+
columnCount,
|
|
10
|
+
columns,
|
|
11
|
+
headAt,
|
|
12
|
+
momentAt,
|
|
13
|
+
seekKey,
|
|
14
|
+
shareOf,
|
|
15
|
+
type WaveformFit,
|
|
16
|
+
} from "@slotform/ui-core/waveform";
|
|
17
|
+
import {
|
|
18
|
+
type KeyboardEvent,
|
|
19
|
+
type PointerEvent,
|
|
20
|
+
useCallback,
|
|
21
|
+
useEffect,
|
|
22
|
+
useLayoutEffect,
|
|
23
|
+
useRef,
|
|
24
|
+
useState,
|
|
25
|
+
} from "react";
|
|
26
|
+
import { useHeldDraft } from "../../lib/act.ts";
|
|
27
|
+
import type { Closed } from "../../lib/closed.ts";
|
|
28
|
+
import { inkOf, inkVersion, onInkChange } from "../../lib/ink.ts";
|
|
29
|
+
|
|
30
|
+
/** A track's peaks drawn as a slider along its length. */
|
|
31
|
+
export interface WaveformProps extends Closed {
|
|
32
|
+
/** What the waveform seeks: the slider's accessible name (a short phrase). */
|
|
33
|
+
label: string;
|
|
34
|
+
/** The track's amplitude, from its start to its end; any resolution, reduced to the width. */
|
|
35
|
+
peaks: Peaks;
|
|
36
|
+
/** The track's length in seconds. */
|
|
37
|
+
duration: number;
|
|
38
|
+
/** The playhead's place in seconds: the played part up to it stands in the accent, the rest in the edge. */
|
|
39
|
+
at: number;
|
|
40
|
+
/** Hears the second the viewer lets the playhead go at. A drag holds the pointer as the playhead and seeks once, on release (a plain press is a drag that does not move); Left and Down move it back five seconds, Right and Up forward (Shift: one), Page Up and Page Down thirty, Home and End to the ends, each a seek at once. Return the seek's promise and the playhead holds while it pends; a refusal puts it back, with its reason. The playhead only moves when `at` does. */
|
|
41
|
+
onSeek: (seconds: number) => unknown;
|
|
42
|
+
/** `bar` stands at the control's height (a bar, the player dock); `record`, the default, at the waveform size. */
|
|
43
|
+
fit?: WaveformFit;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const ROOT =
|
|
47
|
+
"relative w-full cursor-pointer touch-none select-none in-data-[modality=keyboard]:focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring";
|
|
48
|
+
const CANVAS = "absolute inset-0 size-full";
|
|
49
|
+
|
|
50
|
+
interface Measured {
|
|
51
|
+
width: number;
|
|
52
|
+
height: number;
|
|
53
|
+
ratio: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The peaks of a track drawn on a canvas as columns, the played part in the accent and the rest in the edge, with a playhead; a slider the pointer and the keys seek along. Standing in a bar it is the player dock's seek; at record height it stands alone in a `Pane` or a `Card`. */
|
|
57
|
+
export function Waveform(props: WaveformProps) {
|
|
58
|
+
const { label, duration, onSeek, fit = "record" } = props;
|
|
59
|
+
// The pointer holds the playhead while it drags; the seek lands on release.
|
|
60
|
+
const [pointer, hold] = useState<number>();
|
|
61
|
+
const draft = useHeldDraft(props.at);
|
|
62
|
+
const at = pointer ?? draft.value;
|
|
63
|
+
const root = useRef<HTMLDivElement>(null);
|
|
64
|
+
const canvas = useRef<HTMLCanvasElement>(null);
|
|
65
|
+
const latest = useRef(props);
|
|
66
|
+
const size = useRef<Measured>({ width: 0, height: 0, ratio: 1 });
|
|
67
|
+
const reduced = useRef<{ peaks: Peaks; count: number; value: Columns }>(null);
|
|
68
|
+
const frame = useRef(0);
|
|
69
|
+
const ink = useRef<{ version: number; roles: Map<string, string> }>({
|
|
70
|
+
version: -1,
|
|
71
|
+
roles: new Map(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const draw = useCallback(() => {
|
|
75
|
+
frame.current = 0;
|
|
76
|
+
const element = canvas.current;
|
|
77
|
+
const context = element?.getContext("2d");
|
|
78
|
+
if (!element || !context) return;
|
|
79
|
+
const now = latest.current;
|
|
80
|
+
const { width, height } = size.current;
|
|
81
|
+
const ratio = window.devicePixelRatio || 1;
|
|
82
|
+
size.current.ratio = ratio;
|
|
83
|
+
const pixelWidth = Math.max(1, Math.round(width * ratio));
|
|
84
|
+
const pixelHeight = Math.max(1, Math.round(height * ratio));
|
|
85
|
+
if (element.width !== pixelWidth) element.width = pixelWidth;
|
|
86
|
+
if (element.height !== pixelHeight) element.height = pixelHeight;
|
|
87
|
+
context.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
88
|
+
context.clearRect(0, 0, width, height);
|
|
89
|
+
if (width <= 0 || height <= 0) return;
|
|
90
|
+
|
|
91
|
+
// The roles' values are read off the element once per mode, not per frame.
|
|
92
|
+
if (ink.current.version !== inkVersion()) {
|
|
93
|
+
ink.current = { version: inkVersion(), roles: new Map() };
|
|
94
|
+
}
|
|
95
|
+
const paint = (role: string) => {
|
|
96
|
+
let value = ink.current.roles.get(role);
|
|
97
|
+
if (value === undefined) {
|
|
98
|
+
value = inkOf(element, role);
|
|
99
|
+
ink.current.roles.set(role, value);
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const count = columnCount(width);
|
|
105
|
+
if (reduced.current?.peaks !== now.peaks || reduced.current.count !== count)
|
|
106
|
+
reduced.current = {
|
|
107
|
+
peaks: now.peaks,
|
|
108
|
+
count,
|
|
109
|
+
value: columns(now.peaks, count),
|
|
110
|
+
};
|
|
111
|
+
const share = shareOf(now.at, now.duration);
|
|
112
|
+
const played = paint("toggle-on");
|
|
113
|
+
const rest = paint("edge-strong");
|
|
114
|
+
columnBoxes(reduced.current.value, width, height, ratio).forEach(
|
|
115
|
+
(box, index) => {
|
|
116
|
+
context.fillStyle = (index + 0.5) / count <= share ? played : rest;
|
|
117
|
+
context.fillRect(box.left, box.top, box.width, box.height);
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
context.fillStyle = paint("ink-body");
|
|
122
|
+
context.fillRect(headAt(share, width, ratio), 0, COLUMN.head, height);
|
|
123
|
+
}, []);
|
|
124
|
+
|
|
125
|
+
const schedule = useCallback(() => {
|
|
126
|
+
if (!frame.current) frame.current = requestAnimationFrame(draw);
|
|
127
|
+
}, [draw]);
|
|
128
|
+
|
|
129
|
+
// Every render hands the draw its latest props; the draw itself is one frame
|
|
130
|
+
// later, however many renders came.
|
|
131
|
+
useLayoutEffect(() => {
|
|
132
|
+
latest.current = { ...props, at };
|
|
133
|
+
schedule();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
const element = root.current;
|
|
138
|
+
if (!element) return;
|
|
139
|
+
// The canvas's backing store is its box in device pixels, which only the
|
|
140
|
+
// layout knows: CSS cannot size a bitmap.
|
|
141
|
+
const measure = () => {
|
|
142
|
+
const box = element.getBoundingClientRect();
|
|
143
|
+
size.current = { ...size.current, width: box.width, height: box.height };
|
|
144
|
+
schedule();
|
|
145
|
+
};
|
|
146
|
+
measure();
|
|
147
|
+
const watch = new ResizeObserver(measure);
|
|
148
|
+
watch.observe(element);
|
|
149
|
+
const stop = onInkChange(schedule);
|
|
150
|
+
return () => {
|
|
151
|
+
watch.disconnect();
|
|
152
|
+
stop();
|
|
153
|
+
cancelAnimationFrame(frame.current);
|
|
154
|
+
frame.current = 0;
|
|
155
|
+
};
|
|
156
|
+
}, [schedule]);
|
|
157
|
+
|
|
158
|
+
const momentOf = (event: PointerEvent<HTMLDivElement>) => {
|
|
159
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
160
|
+
return momentAt(event.clientX - box.left, box.width, duration);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const seek = (to: number) => draft.change(to, () => onSeek(to));
|
|
164
|
+
|
|
165
|
+
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
|
|
166
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
167
|
+
const to = seekKey(event.key, event.shiftKey, at, duration);
|
|
168
|
+
if (to === undefined) return;
|
|
169
|
+
event.preventDefault();
|
|
170
|
+
seek(to);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
return (
|
|
174
|
+
<div
|
|
175
|
+
ref={root}
|
|
176
|
+
role="slider"
|
|
177
|
+
tabIndex={0}
|
|
178
|
+
aria-label={label}
|
|
179
|
+
aria-valuemin={0}
|
|
180
|
+
aria-valuemax={Math.round(duration)}
|
|
181
|
+
aria-valuenow={Math.round(at)}
|
|
182
|
+
aria-valuetext={trackTime(at)}
|
|
183
|
+
className={cn(fit === "bar" ? WAVEFORM_BAR : WAVEFORM_RECORD, ROOT)}
|
|
184
|
+
onPointerDown={(event) => {
|
|
185
|
+
if (event.button !== 0) return;
|
|
186
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
187
|
+
hold(momentOf(event));
|
|
188
|
+
}}
|
|
189
|
+
onPointerMove={(event) => {
|
|
190
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId))
|
|
191
|
+
hold(momentOf(event));
|
|
192
|
+
}}
|
|
193
|
+
onPointerUp={(event) => {
|
|
194
|
+
if (!event.currentTarget.hasPointerCapture(event.pointerId)) return;
|
|
195
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
196
|
+
hold(undefined);
|
|
197
|
+
seek(momentOf(event));
|
|
198
|
+
}}
|
|
199
|
+
onPointerCancel={() => hold(undefined)}
|
|
200
|
+
onKeyDown={onKeyDown}
|
|
201
|
+
>
|
|
202
|
+
<canvas ref={canvas} aria-hidden className={CANVAS} />
|
|
203
|
+
</div>
|
|
204
|
+
);
|
|
205
|
+
}
|