@slotform/react-ui 0.0.0-stage → 0.1.1
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,376 @@
|
|
|
1
|
+
import { dev } from "@slotform/ui-core/read";
|
|
2
|
+
import {
|
|
3
|
+
OVERSCAN,
|
|
4
|
+
ranged,
|
|
5
|
+
VIRTUAL_FROM,
|
|
6
|
+
windowEntries,
|
|
7
|
+
} from "@slotform/ui-core/window";
|
|
8
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
9
|
+
import {
|
|
10
|
+
createContext,
|
|
11
|
+
type Key,
|
|
12
|
+
type ReactElement,
|
|
13
|
+
type ReactNode,
|
|
14
|
+
type RefObject,
|
|
15
|
+
use,
|
|
16
|
+
useCallback,
|
|
17
|
+
useEffect,
|
|
18
|
+
useLayoutEffect,
|
|
19
|
+
useMemo,
|
|
20
|
+
useRef,
|
|
21
|
+
useState,
|
|
22
|
+
} from "react";
|
|
23
|
+
import { spacing } from "./measure.ts";
|
|
24
|
+
|
|
25
|
+
/** The nearest element that scrolls vertically, which a long collection windows against; none draws every row. A Pane's body, a Sheet's body, a Gate's page, a Thread's log, a sheet's listbox of options, a popover's list of options and a long BarChart's plot (which scrolls sideways) provide it, the nearest one winning, so a Section's sheet in a portaled Sheet never windows against the Pane under it. The `holder` is the same from the first render, its element attached with the commit, so a collection mounting with the Pane windows in its first draw; `node` is the element as state, so what measures against it renders once it has arrived. */
|
|
26
|
+
export const Scroller = createContext<{
|
|
27
|
+
holder: RefObject<HTMLElement | null>;
|
|
28
|
+
node: HTMLElement | null;
|
|
29
|
+
} | null>(null);
|
|
30
|
+
|
|
31
|
+
/** A scrolling body's `Scroller` value (its `holder` and `node`) and the ref that fills it (and `own`, the host's ref to the same element). The ref is one function for the body's life, so a render never detaches the holder from under a Window that measures in the same commit; the value changes only as the element does. */
|
|
32
|
+
export function useScroller<T extends HTMLElement = HTMLElement>(
|
|
33
|
+
own?: RefObject<T | null>,
|
|
34
|
+
) {
|
|
35
|
+
const holder = useRef<HTMLElement | null>(null);
|
|
36
|
+
const [node, setNode] = useState<HTMLElement | null>(null);
|
|
37
|
+
const ref = useCallback(
|
|
38
|
+
(element: T | null) => {
|
|
39
|
+
holder.current = element;
|
|
40
|
+
if (own) own.current = element;
|
|
41
|
+
setNode(element);
|
|
42
|
+
},
|
|
43
|
+
[own],
|
|
44
|
+
);
|
|
45
|
+
return useMemo(() => ({ holder, node, ref }), [node, ref]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The height a row is taken to have until the first one is measured: the
|
|
49
|
+
// `row` size at the density and size in force.
|
|
50
|
+
const estimate = () => spacing("row");
|
|
51
|
+
// How long a scroll is taken to run after its last event (ms). Not
|
|
52
|
+
// `scrollend`: it waits for a held finger's release, so a still finger would
|
|
53
|
+
// read as scrolling, and it is Baseline only since December 2025.
|
|
54
|
+
const SETTLED = 150;
|
|
55
|
+
|
|
56
|
+
interface WindowProps {
|
|
57
|
+
/** How many rows there are. */
|
|
58
|
+
count: number;
|
|
59
|
+
/** The key of the row at an index, which the virtualizer keeps its measures by. */
|
|
60
|
+
keyOf: (index: number) => Key;
|
|
61
|
+
/** The row at an index, exactly one element carrying that key: built only for the indices the window mounts, so a long collection never builds (or renders) the rest. */
|
|
62
|
+
row: (index: number) => ReactElement;
|
|
63
|
+
/** The element the rows stand in as its first children, each row exactly one element. */
|
|
64
|
+
box: RefObject<HTMLElement | null>;
|
|
65
|
+
/** A row mounted whatever the scroll (a Table's cursor). */
|
|
66
|
+
keep?: number;
|
|
67
|
+
/** The window opens at the last rows (a Thread). */
|
|
68
|
+
end?: boolean;
|
|
69
|
+
/** The element a spacer is: a `tr` in a tbody. */
|
|
70
|
+
spacer?: "div" | "tr";
|
|
71
|
+
/** The rows stand side by side in a box that scrolls sideways (a chart's bars), so the window runs along x: widths for heights, the scroller's left for its top. */
|
|
72
|
+
horizontal?: boolean;
|
|
73
|
+
/** Hears the indices of the rows this window mounts after each commit (every row while it draws whole): the view, its overscan and the kept rows. A collection whose rows load by where the viewer scrolls (a window read) asks for the pages they stand in. */
|
|
74
|
+
onRange?: (indices: readonly number[]) => void;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The scroller's top over the box's top (its left, along x): how far the
|
|
78
|
+
// scroller's view has gone into the box, so a collection's place in the page
|
|
79
|
+
// is nobody's business.
|
|
80
|
+
function into(
|
|
81
|
+
scroller: HTMLElement,
|
|
82
|
+
box: HTMLElement | null,
|
|
83
|
+
horizontal: boolean,
|
|
84
|
+
): number {
|
|
85
|
+
if (!box) return 0;
|
|
86
|
+
const view = scroller.getBoundingClientRect();
|
|
87
|
+
const at = box.getBoundingClientRect();
|
|
88
|
+
return horizontal
|
|
89
|
+
? view.left + scroller.clientLeft - at.left
|
|
90
|
+
: view.top + scroller.clientTop - at.top;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// The box's gap between rows, which every spacer gives back to its neighbours.
|
|
94
|
+
function gapOf(box: HTMLElement, horizontal: boolean): number {
|
|
95
|
+
const style = getComputedStyle(box);
|
|
96
|
+
return Number.parseFloat(horizontal ? style.columnGap : style.rowGap) || 0;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The rows of a long collection, drawn only near the scroller's view: TanStack Virtual places and ranges them, the mounted rows stand in the host's box between spacers that hold the room of the rest. Fewer than `VIRTUAL_FROM` rows, or no `Scroller`, draw every row unchanged, so crossing the count (a Show more) mounts nothing again. The rows are measured by their place among the box's children, so a row is exactly one element and carries nothing of this. The focused row and `keep` stay mounted. */
|
|
100
|
+
export function Window({
|
|
101
|
+
count,
|
|
102
|
+
keyOf,
|
|
103
|
+
row,
|
|
104
|
+
box,
|
|
105
|
+
keep,
|
|
106
|
+
end,
|
|
107
|
+
spacer = "div",
|
|
108
|
+
horizontal = false,
|
|
109
|
+
onRange,
|
|
110
|
+
}: WindowProps): ReactNode {
|
|
111
|
+
const scroll = use(Scroller);
|
|
112
|
+
// A Window mounting with its scroller (a Pane's first render) renders
|
|
113
|
+
// before the element is attached, from the viewport's size; the element
|
|
114
|
+
// arriving (`node`) renders it once more, to measure against it.
|
|
115
|
+
const scroller = scroll?.node ?? null;
|
|
116
|
+
const on = scroll !== null && count >= VIRTUAL_FROM;
|
|
117
|
+
const view = {
|
|
118
|
+
width: scroller?.clientWidth ?? window.innerWidth,
|
|
119
|
+
height: scroller?.clientHeight ?? window.innerHeight,
|
|
120
|
+
};
|
|
121
|
+
// The row holding focus, which the extractor reads at its moment, not at a
|
|
122
|
+
// render.
|
|
123
|
+
const focused = useRef<number>(undefined);
|
|
124
|
+
// The box's gap and a row's height, read when the window opens. They are
|
|
125
|
+
// state so the virtualizer holds them before it measures again.
|
|
126
|
+
const [shape, setShape] = useState<{ gap: number; size: number }>();
|
|
127
|
+
const { gap, size } = shape ?? { gap: 0, size: estimate() };
|
|
128
|
+
// How much of the virtualizer's adjustments since its last offset the
|
|
129
|
+
// scroller has already taken.
|
|
130
|
+
const applied = useRef(0);
|
|
131
|
+
// Reports the scroller's offset when the virtualizer holds another one.
|
|
132
|
+
const sync = useRef<() => void>(undefined);
|
|
133
|
+
// Whether the scroller's offset has been reported to the virtualizer since it
|
|
134
|
+
// took the scroller.
|
|
135
|
+
const heard = useRef(false);
|
|
136
|
+
// A mounted row's size along the window's axis.
|
|
137
|
+
const extentOf = (node: Element | undefined) => {
|
|
138
|
+
const rect = node?.getBoundingClientRect();
|
|
139
|
+
return (horizontal ? rect?.width : rect?.height) ?? 0;
|
|
140
|
+
};
|
|
141
|
+
const extract = useCallback(
|
|
142
|
+
(range: Parameters<typeof ranged>[0]) =>
|
|
143
|
+
ranged(range, [focused.current, keep]),
|
|
144
|
+
// A new `keep` is a new extractor, which ranges again.
|
|
145
|
+
[keep],
|
|
146
|
+
);
|
|
147
|
+
const virtualizer = useVirtualizer<HTMLElement, Element>({
|
|
148
|
+
count,
|
|
149
|
+
horizontal,
|
|
150
|
+
enabled: on,
|
|
151
|
+
getScrollElement: () => scroll?.holder.current ?? null,
|
|
152
|
+
estimateSize: () => size,
|
|
153
|
+
getItemKey: keyOf,
|
|
154
|
+
gap,
|
|
155
|
+
overscan: OVERSCAN,
|
|
156
|
+
rangeExtractor: extract,
|
|
157
|
+
useFlushSync: false,
|
|
158
|
+
initialRect: view,
|
|
159
|
+
initialOffset: () =>
|
|
160
|
+
end
|
|
161
|
+
? Math.max(0, count * size - (horizontal ? view.width : view.height))
|
|
162
|
+
: 0,
|
|
163
|
+
observeElementOffset: (instance, report) => {
|
|
164
|
+
const element = instance.scrollElement;
|
|
165
|
+
if (!element) return;
|
|
166
|
+
let running = false;
|
|
167
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
168
|
+
heard.current = false;
|
|
169
|
+
const send = () => {
|
|
170
|
+
heard.current = true;
|
|
171
|
+
applied.current = 0;
|
|
172
|
+
report(into(element, box.current, horizontal), running);
|
|
173
|
+
};
|
|
174
|
+
const onScroll = () => {
|
|
175
|
+
running = true;
|
|
176
|
+
send();
|
|
177
|
+
clearTimeout(timer);
|
|
178
|
+
timer = setTimeout(() => {
|
|
179
|
+
running = false;
|
|
180
|
+
send();
|
|
181
|
+
}, SETTLED);
|
|
182
|
+
};
|
|
183
|
+
element.addEventListener("scroll", onScroll, { passive: true });
|
|
184
|
+
sync.current = () => {
|
|
185
|
+
if (into(element, box.current, horizontal) !== instance.scrollOffset) {
|
|
186
|
+
send();
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
// Content above the box changing height moves the box without a
|
|
190
|
+
// scroll; the observer also reports once on observing, which gives
|
|
191
|
+
// the first true offset once the box stands.
|
|
192
|
+
const resize = new ResizeObserver(send);
|
|
193
|
+
resize.observe(element);
|
|
194
|
+
for (const child of element.children) resize.observe(child);
|
|
195
|
+
return () => {
|
|
196
|
+
sync.current = undefined;
|
|
197
|
+
element.removeEventListener("scroll", onScroll);
|
|
198
|
+
clearTimeout(timer);
|
|
199
|
+
resize.disconnect();
|
|
200
|
+
};
|
|
201
|
+
},
|
|
202
|
+
scrollToFn: (offset, { adjustments, behavior }, instance) => {
|
|
203
|
+
const element = instance.scrollElement;
|
|
204
|
+
if (!element) return;
|
|
205
|
+
// The virtualizer's first move on a scroller is its initial offset,
|
|
206
|
+
// which is box-relative: honoured, it would scroll the box's top to the
|
|
207
|
+
// view's top, past whatever stands above it (a toolbar) and over a scroll
|
|
208
|
+
// the scroller already held. A window opens where the page stands (a
|
|
209
|
+
// Thread's, at its end), so it hears where that is.
|
|
210
|
+
if (!heard.current && adjustments === undefined && !end) {
|
|
211
|
+
sync.current?.();
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
// The virtualizer speaks box-relative, the scroller its own. An
|
|
215
|
+
// adjustment (rows above the view measured) moves the scroller from
|
|
216
|
+
// where it is now, not from the offset the virtualizer last heard: a
|
|
217
|
+
// scroll the browser made since (a focus) is not undone.
|
|
218
|
+
const now = horizontal ? element.scrollLeft : element.scrollTop;
|
|
219
|
+
const to =
|
|
220
|
+
adjustments === undefined
|
|
221
|
+
? now - into(element, box.current, horizontal) + offset
|
|
222
|
+
: now + adjustments - applied.current;
|
|
223
|
+
applied.current = adjustments ?? 0;
|
|
224
|
+
element.scrollTo(
|
|
225
|
+
horizontal ? { left: to, behavior } : { top: to, behavior },
|
|
226
|
+
);
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
const placed = on
|
|
230
|
+
? virtualizer.getVirtualItems().map(({ index, start, end: stop }) => ({
|
|
231
|
+
index,
|
|
232
|
+
start,
|
|
233
|
+
end: stop,
|
|
234
|
+
}))
|
|
235
|
+
: [];
|
|
236
|
+
const entries = on
|
|
237
|
+
? windowEntries(placed, count, virtualizer.getTotalSize(), gap)
|
|
238
|
+
: [];
|
|
239
|
+
// What the box holds after this commit, which the effects measure it by.
|
|
240
|
+
const drawn = useRef(entries);
|
|
241
|
+
useLayoutEffect(() => {
|
|
242
|
+
drawn.current = entries;
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// The rows mounted are the pages the collection needs.
|
|
246
|
+
useEffect(() => {
|
|
247
|
+
onRange?.(
|
|
248
|
+
on
|
|
249
|
+
? placed.map((one) => one.index)
|
|
250
|
+
: Array.from({ length: count }, (_, i) => i),
|
|
251
|
+
);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
// A scroll the host made in the same commit (Latest) is heard as the scroll
|
|
255
|
+
// event arrives, a frame later; the window renders against the offset the
|
|
256
|
+
// scroller has now, so the rows it jumped to are mounted when it lands.
|
|
257
|
+
useLayoutEffect(() => sync.current?.());
|
|
258
|
+
|
|
259
|
+
// The first row's height stands in for the rest of the rows, which nothing
|
|
260
|
+
// has measured yet, so the virtualizer starts over once it holds the shape.
|
|
261
|
+
useLayoutEffect(() => {
|
|
262
|
+
if (shape) virtualizer.measure();
|
|
263
|
+
}, [shape, virtualizer]);
|
|
264
|
+
|
|
265
|
+
// Measure what is mounted, and keep measuring it.
|
|
266
|
+
const watch = useRef<ResizeObserver | null>(null);
|
|
267
|
+
const indexed = useRef(new Map<Element, number>());
|
|
268
|
+
useEffect(() => {
|
|
269
|
+
const element = box.current;
|
|
270
|
+
if (!on || !element) {
|
|
271
|
+
setShape(undefined);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const found = drawn.current;
|
|
275
|
+
const mounted = [...element.children].slice(0, found.length);
|
|
276
|
+
if (dev()) check(found, mounted);
|
|
277
|
+
const first = found.findIndex((entry) => "row" in entry);
|
|
278
|
+
if (!shape && first >= 0) {
|
|
279
|
+
setShape({
|
|
280
|
+
gap: gapOf(element, horizontal),
|
|
281
|
+
size: extentOf(mounted[first]) || estimate(),
|
|
282
|
+
});
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const rowsAt = new Map<Element, number>();
|
|
286
|
+
found.forEach((entry, at) => {
|
|
287
|
+
const node = mounted[at];
|
|
288
|
+
if (!node || !("row" in entry)) return;
|
|
289
|
+
rowsAt.set(node, entry.row);
|
|
290
|
+
virtualizer.resizeItem(entry.row, extentOf(node));
|
|
291
|
+
});
|
|
292
|
+
indexed.current = rowsAt;
|
|
293
|
+
watch.current ??= new ResizeObserver((entries) => {
|
|
294
|
+
for (const entry of entries) {
|
|
295
|
+
const index = indexed.current.get(entry.target);
|
|
296
|
+
// A row just unmounted is reported once more, at no height.
|
|
297
|
+
if (index !== undefined && entry.target.isConnected) {
|
|
298
|
+
virtualizer.resizeItem(index, extentOf(entry.target));
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
watch.current.disconnect();
|
|
303
|
+
for (const node of rowsAt.keys()) watch.current.observe(node);
|
|
304
|
+
});
|
|
305
|
+
useEffect(() => () => watch.current?.disconnect(), []);
|
|
306
|
+
|
|
307
|
+
// The row holding focus stays mounted: tracked on the box, read by the
|
|
308
|
+
// extractor at the next range.
|
|
309
|
+
useEffect(() => {
|
|
310
|
+
const element = box.current;
|
|
311
|
+
if (!on || !element) return;
|
|
312
|
+
const focus = (event: FocusEvent) => {
|
|
313
|
+
const target = event.target instanceof Node ? event.target : null;
|
|
314
|
+
const at = [...element.children].findIndex((node) =>
|
|
315
|
+
node.contains(target),
|
|
316
|
+
);
|
|
317
|
+
const entry = drawn.current[at];
|
|
318
|
+
focused.current = entry && "row" in entry ? entry.row : undefined;
|
|
319
|
+
};
|
|
320
|
+
const blur = (event: FocusEvent) => {
|
|
321
|
+
const to =
|
|
322
|
+
event.relatedTarget instanceof Node ? event.relatedTarget : null;
|
|
323
|
+
if (!element.contains(to)) {
|
|
324
|
+
focused.current = undefined;
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
element.addEventListener("focusin", focus);
|
|
328
|
+
element.addEventListener("focusout", blur);
|
|
329
|
+
// A box that windows does not let the browser anchor inside it: the
|
|
330
|
+
// virtualizer's own adjustment is the only one.
|
|
331
|
+
element.style.overflowAnchor = "none";
|
|
332
|
+
return () => {
|
|
333
|
+
element.removeEventListener("focusin", focus);
|
|
334
|
+
element.removeEventListener("focusout", blur);
|
|
335
|
+
element.style.overflowAnchor = "";
|
|
336
|
+
};
|
|
337
|
+
}, [on, box]);
|
|
338
|
+
|
|
339
|
+
if (!on) return Array.from({ length: count }, (_, index) => row(index));
|
|
340
|
+
// A spacer (`data-spacer`, `aria-hidden`) is out of the accessibility tree
|
|
341
|
+
// and stands for rows outside the window: room by design and no dead box.
|
|
342
|
+
const Space = spacer;
|
|
343
|
+
return entries.map((entry) => {
|
|
344
|
+
if ("row" in entry) return row(entry.row);
|
|
345
|
+
const style = horizontal ? { width: entry.space } : { height: entry.space };
|
|
346
|
+
return Space === "tr" ? (
|
|
347
|
+
<tr key={`window-${entry.before}`} aria-hidden data-spacer>
|
|
348
|
+
<td style={{ ...style, padding: 0, border: 0 }} />
|
|
349
|
+
</tr>
|
|
350
|
+
) : (
|
|
351
|
+
<div
|
|
352
|
+
key={`window-${entry.before}`}
|
|
353
|
+
aria-hidden
|
|
354
|
+
data-spacer
|
|
355
|
+
style={style}
|
|
356
|
+
className="shrink-0"
|
|
357
|
+
/>
|
|
358
|
+
);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// A row that is not exactly one element of the box shifts every child after
|
|
363
|
+
// it off its entry, so the spacers are where it shows.
|
|
364
|
+
function check(
|
|
365
|
+
entries: ReturnType<typeof windowEntries>,
|
|
366
|
+
mounted: readonly Element[],
|
|
367
|
+
): void {
|
|
368
|
+
entries.forEach((entry, at) => {
|
|
369
|
+
const space = mounted[at]?.getAttribute("aria-hidden") === "true";
|
|
370
|
+
if (mounted[at] === undefined || space !== !("row" in entry)) {
|
|
371
|
+
throw new Error(
|
|
372
|
+
"Window: each row must be exactly one element, the first children of the box",
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
}
|