@northgraindata/dsui 0.4.1 → 0.5.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/bin/dsui.mjs +1 -1
- package/dist/plugin-sdk.mjs +1 -1
- package/dist/runtime/plugins/code-repository.browser.mjs +60 -0
- package/dist/runtime/plugins/code-repository.mjs +2 -2
- package/dist/runtime/plugins/example-plugin.mjs +1 -1
- package/dist/runtime/plugins/health.browser.mjs +202 -68
- package/dist/runtime/plugins/health.mjs +97 -6
- package/dist/runtime/plugins/monitoring.mjs +11 -1
- package/dist/runtime/plugins/status-pages.browser.mjs +5033 -0
- package/dist/runtime/plugins/status-pages.mjs +52 -0
- package/dist/server.mjs +205 -188
- package/package.json +1 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@northgraindata/dsui-ui/src/components/input.tsx +2 -5
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@northgraindata/dsui-ui/src/components/suggest-input.tsx +197 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@northgraindata/dsui-ui/src/index.tsx +5 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@northgraindata/dsui-ui/src/styles.css +54 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/dist/cjs/index.cjs +3 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/dist/cjs/index.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/dist/esm/index.js +3 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/dist/esm/index.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/package.json +2 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/src/index.tsx +11 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.cjs +79 -26
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.d.cts +12 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.cjs +14 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.d.cts +3 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.d.ts +12 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.js +79 -26
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.d.ts +3 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.js +14 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/package.json +1 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/src/index.ts +135 -24
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/src/scroll-classifier.ts +31 -3
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/define.ts +22 -1
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/index.ts +1 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/presentation.test.ts +73 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/presentation.ts +34 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/types.ts +23 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/layout/gauge.tsx +14 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/dataset/dataset.test.ts +134 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/dataset/index.ts +141 -0
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/index.ts +23 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/dist/cjs/index.cjs +3 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/dist/cjs/index.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/dist/esm/index.js +3 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/dist/esm/index.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/package.json +2 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/react-virtual/src/index.tsx +11 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.cjs +79 -26
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.d.cts +12 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.cjs +14 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.d.cts +3 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.d.ts +12 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.js +79 -26
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.d.ts +3 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.js +14 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/package.json +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/src/index.ts +135 -24
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/node_modules/@tanstack/virtual-core/src/scroll-classifier.ts +31 -3
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/define.ts +22 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/index.ts +1 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/presentation.test.ts +73 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/presentation.ts +34 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/adapter/types.ts +23 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/layout/gauge.tsx +14 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/dataset/dataset.test.ts +134 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/dataset/index.ts +141 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/index.ts +23 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/react-virtual/dist/cjs/index.cjs +3 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/react-virtual/dist/cjs/index.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/react-virtual/dist/esm/index.js +3 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/react-virtual/dist/esm/index.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/react-virtual/package.json +2 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/react-virtual/src/index.tsx +11 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.cjs +79 -26
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/cjs/index.d.cts +12 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.cjs +14 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.cjs.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/cjs/scroll-classifier.d.cts +3 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.d.ts +12 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.js +79 -26
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/esm/index.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.d.ts +3 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.js +14 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/dist/esm/scroll-classifier.js.map +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/package.json +1 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/src/index.ts +135 -24
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@tanstack/virtual-core/src/scroll-classifier.ts +31 -3
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/package.json +2 -1
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/src/diagnostics.ts +24 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/src/events.ts +6 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/src/index.ts +162 -2
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/src/modal.test.ts +76 -0
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/src/modal.ts +200 -0
- package/sdk/node_modules/@northgraindata/dsui-ui/src/components/input.tsx +2 -5
- package/sdk/node_modules/@northgraindata/dsui-ui/src/components/suggest-input.tsx +197 -0
- package/sdk/node_modules/@northgraindata/dsui-ui/src/index.tsx +5 -0
- package/sdk/node_modules/@northgraindata/dsui-ui/src/styles.css +54 -0
- package/web/assets/index-DQkPkPN7.css +1 -0
- package/web/assets/index-w07fgRf1.js +218 -0
- package/web/index.html +2 -2
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/keyed-items.test.ts +0 -23
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/query-editor/column-profile.test.ts +0 -109
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/query-editor/query-explorer.test.ts +0 -29
- package/sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/table-data.test.ts +0 -65
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/keyed-items.test.ts +0 -23
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/query-editor/column-profile.test.ts +0 -109
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/query-editor/query-explorer.test.ts +0 -29
- package/sdk/node_modules/@northgraindata/dsui-plugin-sdk/node_modules/@northgraindata/dsui-adapter-sdk/src/components/ui/table-data.test.ts +0 -65
- package/web/assets/index-BBd7EEtd.js +0 -98
- package/web/assets/index-C16grSph.css +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@northgraindata/dsui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "The AI-native control plane for your data stack. Connect your data tools and manage them from one place. Explore your infrastructure, run operations, and work across services with AI that understands your stack.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
2
|
import { cn } from "./cn";
|
|
3
3
|
|
|
4
|
-
export function Input({
|
|
5
|
-
className,
|
|
6
|
-
...props
|
|
7
|
-
}: InputHTMLAttributes<HTMLInputElement>) {
|
|
4
|
+
export function Input({ className, ...props }: ComponentProps<"input">) {
|
|
8
5
|
return (
|
|
9
6
|
<input
|
|
10
7
|
className={cn(
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { type ComponentProps, useEffect, useId, useRef, useState } from "react";
|
|
2
|
+
import { Input } from "./input";
|
|
3
|
+
|
|
4
|
+
export type SuggestOption = {
|
|
5
|
+
value: string;
|
|
6
|
+
/** Shown instead of the value when it reads better. */
|
|
7
|
+
label?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type SuggestInputProps = Omit<
|
|
12
|
+
ComponentProps<"input">,
|
|
13
|
+
"value" | "onChange" | "role"
|
|
14
|
+
> & {
|
|
15
|
+
value: string;
|
|
16
|
+
onValueChange(value: string): void;
|
|
17
|
+
/** What to offer. The input never filters them: the caller already did. */
|
|
18
|
+
options: SuggestOption[];
|
|
19
|
+
loading?: boolean;
|
|
20
|
+
/** Names the list for assistive technology. The input keeps its own label. */
|
|
21
|
+
listLabel?: string;
|
|
22
|
+
/** Called when the list opens or the text changes, so a caller can fetch. */
|
|
23
|
+
onSuggest?(): void;
|
|
24
|
+
/** Called with a chosen option. Defaults to `onValueChange(option.value)`. */
|
|
25
|
+
onSelectOption?(option: SuggestOption): void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A text input that offers matches while it has focus.
|
|
30
|
+
*
|
|
31
|
+
* Typing stays free: the list only helps fill the field in, so a value that is
|
|
32
|
+
* not on it is still accepted. The list is a popover so a scrolling or clipped
|
|
33
|
+
* container, such as a dialog body, cannot cut it off.
|
|
34
|
+
*/
|
|
35
|
+
export function SuggestInput({
|
|
36
|
+
value,
|
|
37
|
+
onValueChange,
|
|
38
|
+
options,
|
|
39
|
+
loading = false,
|
|
40
|
+
listLabel = "Suggestions",
|
|
41
|
+
onSuggest,
|
|
42
|
+
onSelectOption,
|
|
43
|
+
onFocus,
|
|
44
|
+
onBlur,
|
|
45
|
+
onClick,
|
|
46
|
+
onKeyDown,
|
|
47
|
+
...input
|
|
48
|
+
}: SuggestInputProps) {
|
|
49
|
+
const field = useRef<HTMLInputElement>(null);
|
|
50
|
+
const menu = useRef<HTMLDivElement>(null);
|
|
51
|
+
const menuId = useId();
|
|
52
|
+
const [open, setOpen] = useState(false);
|
|
53
|
+
// The chosen entry belongs to one list. A new list starts with nothing
|
|
54
|
+
// chosen, so Enter still submits the form.
|
|
55
|
+
const [choice, setChoice] = useState({ options, index: -1 });
|
|
56
|
+
const active = choice.options === options ? choice.index : -1;
|
|
57
|
+
const setActive = (next: (current: number) => number) =>
|
|
58
|
+
setChoice((current) => ({
|
|
59
|
+
options,
|
|
60
|
+
index: next(current.options === options ? current.index : -1),
|
|
61
|
+
}));
|
|
62
|
+
const visible = open && (loading || options.length > 0);
|
|
63
|
+
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
const popup = menu.current;
|
|
66
|
+
if (!popup) return;
|
|
67
|
+
if (!visible) {
|
|
68
|
+
if (popup.matches(":popover-open")) popup.hidePopover();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const place = () => {
|
|
72
|
+
const bounds = field.current?.getBoundingClientRect();
|
|
73
|
+
if (!bounds) return;
|
|
74
|
+
const availableBelow = window.innerHeight - bounds.bottom - 12;
|
|
75
|
+
const above = availableBelow < 150 && bounds.top > availableBelow;
|
|
76
|
+
popup.style.width = `${bounds.width}px`;
|
|
77
|
+
popup.style.left = `${bounds.left}px`;
|
|
78
|
+
popup.style.maxHeight = `${Math.max(100, Math.min(260, above ? bounds.top - 16 : availableBelow))}px`;
|
|
79
|
+
// Anchored by the edge that touches the input, so the list can grow or
|
|
80
|
+
// shrink with its content without being measured.
|
|
81
|
+
popup.style.top = above ? "auto" : `${bounds.bottom + 6}px`;
|
|
82
|
+
popup.style.bottom = above
|
|
83
|
+
? `${window.innerHeight - bounds.top + 6}px`
|
|
84
|
+
: "auto";
|
|
85
|
+
};
|
|
86
|
+
if (!popup.matches(":popover-open")) popup.showPopover();
|
|
87
|
+
place();
|
|
88
|
+
window.addEventListener("resize", place);
|
|
89
|
+
window.addEventListener("scroll", place, true);
|
|
90
|
+
return () => {
|
|
91
|
+
window.removeEventListener("resize", place);
|
|
92
|
+
window.removeEventListener("scroll", place, true);
|
|
93
|
+
};
|
|
94
|
+
}, [visible]);
|
|
95
|
+
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
document
|
|
98
|
+
.getElementById(`${menuId}-${active}`)
|
|
99
|
+
?.scrollIntoView({ block: "nearest" });
|
|
100
|
+
}, [active, menuId]);
|
|
101
|
+
|
|
102
|
+
const choose = (option: SuggestOption) => {
|
|
103
|
+
if (onSelectOption) onSelectOption(option);
|
|
104
|
+
else onValueChange(option.value);
|
|
105
|
+
setOpen(false);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<>
|
|
110
|
+
<Input
|
|
111
|
+
{...input}
|
|
112
|
+
ref={field}
|
|
113
|
+
role="combobox"
|
|
114
|
+
aria-expanded={visible}
|
|
115
|
+
aria-controls={menuId}
|
|
116
|
+
aria-autocomplete="list"
|
|
117
|
+
aria-activedescendant={active >= 0 ? `${menuId}-${active}` : undefined}
|
|
118
|
+
value={value}
|
|
119
|
+
onChange={(event) => {
|
|
120
|
+
onValueChange(event.target.value);
|
|
121
|
+
setOpen(true);
|
|
122
|
+
onSuggest?.();
|
|
123
|
+
}}
|
|
124
|
+
onFocus={(event) => {
|
|
125
|
+
setOpen(true);
|
|
126
|
+
onSuggest?.();
|
|
127
|
+
onFocus?.(event);
|
|
128
|
+
}}
|
|
129
|
+
onBlur={(event) => {
|
|
130
|
+
setOpen(false);
|
|
131
|
+
onBlur?.(event);
|
|
132
|
+
}}
|
|
133
|
+
// Focus is already there after Escape, so a click is how it comes back.
|
|
134
|
+
onClick={(event) => {
|
|
135
|
+
setOpen(true);
|
|
136
|
+
onSuggest?.();
|
|
137
|
+
onClick?.(event);
|
|
138
|
+
}}
|
|
139
|
+
onKeyDown={(event) => {
|
|
140
|
+
onKeyDown?.(event);
|
|
141
|
+
if (event.defaultPrevented) return;
|
|
142
|
+
if (event.key === "Escape" && visible) {
|
|
143
|
+
// Closes the list, not the dialog the input sits in.
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
event.stopPropagation();
|
|
146
|
+
setOpen(false);
|
|
147
|
+
} else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
148
|
+
if (options.length === 0) return;
|
|
149
|
+
event.preventDefault();
|
|
150
|
+
if (!open) setOpen(true);
|
|
151
|
+
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
152
|
+
setActive((current) =>
|
|
153
|
+
current === -1 && step === -1
|
|
154
|
+
? options.length - 1
|
|
155
|
+
: (current + step + options.length) % options.length,
|
|
156
|
+
);
|
|
157
|
+
} else if (event.key === "Enter" && visible && active >= 0) {
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
choose(options[active]);
|
|
160
|
+
}
|
|
161
|
+
}}
|
|
162
|
+
/>
|
|
163
|
+
<div
|
|
164
|
+
ref={menu}
|
|
165
|
+
id={menuId}
|
|
166
|
+
popover="manual"
|
|
167
|
+
role="listbox"
|
|
168
|
+
aria-label={listLabel}
|
|
169
|
+
className="dsui-suggest-menu"
|
|
170
|
+
>
|
|
171
|
+
{loading && options.length === 0 ? (
|
|
172
|
+
<div className="dsui-suggest-status">Searching…</div>
|
|
173
|
+
) : (
|
|
174
|
+
options.map((option, index) => (
|
|
175
|
+
// Keyboard use stays in the input (aria-activedescendant), so the
|
|
176
|
+
// options only need to answer the pointer.
|
|
177
|
+
// biome-ignore lint/a11y/useKeyWithClickEvents: see above
|
|
178
|
+
<div
|
|
179
|
+
key={option.value}
|
|
180
|
+
id={`${menuId}-${index}`}
|
|
181
|
+
role="option"
|
|
182
|
+
tabIndex={-1}
|
|
183
|
+
aria-selected={index === active}
|
|
184
|
+
className="dsui-suggest-option"
|
|
185
|
+
// Keeps focus in the input, so choosing does not blur it first.
|
|
186
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
187
|
+
onClick={() => choose(option)}
|
|
188
|
+
>
|
|
189
|
+
<strong>{option.label ?? option.value}</strong>
|
|
190
|
+
{option.description && <small>{option.description}</small>}
|
|
191
|
+
</div>
|
|
192
|
+
))
|
|
193
|
+
)}
|
|
194
|
+
</div>
|
|
195
|
+
</>
|
|
196
|
+
);
|
|
197
|
+
}
|
|
@@ -13,4 +13,9 @@ export type {
|
|
|
13
13
|
} from "./components/option-select";
|
|
14
14
|
export { OptionSelect } from "./components/option-select";
|
|
15
15
|
export { Status } from "./components/status";
|
|
16
|
+
export type {
|
|
17
|
+
SuggestInputProps,
|
|
18
|
+
SuggestOption,
|
|
19
|
+
} from "./components/suggest-input";
|
|
20
|
+
export { SuggestInput } from "./components/suggest-input";
|
|
16
21
|
export { Surface } from "./components/surface";
|
|
@@ -244,3 +244,57 @@
|
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
|
+
|
|
248
|
+
.dsui-suggest-menu:popover-open {
|
|
249
|
+
position: fixed;
|
|
250
|
+
z-index: 1000;
|
|
251
|
+
box-sizing: border-box;
|
|
252
|
+
inset: auto;
|
|
253
|
+
margin: 0;
|
|
254
|
+
overflow-y: auto;
|
|
255
|
+
padding: 6px;
|
|
256
|
+
border: 1px solid #2c496e;
|
|
257
|
+
border-radius: 10px;
|
|
258
|
+
background: linear-gradient(145deg, #101f36, #081321);
|
|
259
|
+
color: #dceaff;
|
|
260
|
+
box-shadow: 0 16px 48px #000b;
|
|
261
|
+
scrollbar-width: thin;
|
|
262
|
+
}
|
|
263
|
+
.dsui-suggest-option {
|
|
264
|
+
display: block;
|
|
265
|
+
min-width: 0;
|
|
266
|
+
padding: 8px;
|
|
267
|
+
border: 1px solid transparent;
|
|
268
|
+
border-radius: 7px;
|
|
269
|
+
cursor: pointer;
|
|
270
|
+
}
|
|
271
|
+
.dsui-suggest-option + .dsui-suggest-option {
|
|
272
|
+
margin-top: 2px;
|
|
273
|
+
}
|
|
274
|
+
.dsui-suggest-option:hover,
|
|
275
|
+
.dsui-suggest-option[aria-selected="true"] {
|
|
276
|
+
border-color: #315f9b;
|
|
277
|
+
background: #142b4b;
|
|
278
|
+
}
|
|
279
|
+
.dsui-suggest-option strong,
|
|
280
|
+
.dsui-suggest-option small {
|
|
281
|
+
display: block;
|
|
282
|
+
overflow: hidden;
|
|
283
|
+
text-overflow: ellipsis;
|
|
284
|
+
white-space: nowrap;
|
|
285
|
+
}
|
|
286
|
+
.dsui-suggest-option strong {
|
|
287
|
+
color: #e8f2ff;
|
|
288
|
+
font-size: 12px;
|
|
289
|
+
font-weight: 600;
|
|
290
|
+
}
|
|
291
|
+
.dsui-suggest-option small {
|
|
292
|
+
margin-top: 2px;
|
|
293
|
+
color: #92a9c8;
|
|
294
|
+
font-size: 10px;
|
|
295
|
+
}
|
|
296
|
+
.dsui-suggest-status {
|
|
297
|
+
padding: 8px;
|
|
298
|
+
color: #92a9c8;
|
|
299
|
+
font-size: 11px;
|
|
300
|
+
}
|
|
@@ -27,7 +27,8 @@ function useVirtualizerBase({
|
|
|
27
27
|
directDomUpdatesMode = "transform",
|
|
28
28
|
...options
|
|
29
29
|
}) {
|
|
30
|
-
const
|
|
30
|
+
const [, dispatch] = React__namespace.useReducer((x) => x + 1, 0);
|
|
31
|
+
const rerender = React__namespace.useRef(dispatch).current;
|
|
31
32
|
const directRef = React__namespace.useRef({
|
|
32
33
|
enabled: directDomUpdates,
|
|
33
34
|
mode: directDomUpdatesMode,
|
|
@@ -156,7 +157,7 @@ function useWindowVirtualizer(options) {
|
|
|
156
157
|
observeElementRect: virtualCore.observeWindowRect,
|
|
157
158
|
observeElementOffset: virtualCore.observeWindowOffset,
|
|
158
159
|
scrollToFn: virtualCore.windowScroll,
|
|
159
|
-
initialOffset: () => typeof document !== "undefined" ? window.scrollY : 0,
|
|
160
|
+
initialOffset: () => typeof document !== "undefined" ? options.horizontal ? window.scrollX : window.scrollY : 0,
|
|
160
161
|
...options
|
|
161
162
|
});
|
|
162
163
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport { flushSync } from 'react-dom'\nimport {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type {\n PartialKeys,\n VirtualItem,\n VirtualizerOptions,\n} from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nconst useIsomorphicLayoutEffect =\n typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type ReactVirtualizer<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = Virtualizer<TScrollElement, TItemElement> & {\n /**\n * Ref callback for the inner size container element. Only meaningful when\n * `directDomUpdates: true` — the virtualizer writes the container's\n * main-axis size (`height` or `width`) directly to skip React re-renders.\n */\n containerRef: (node: HTMLElement | null) => void\n}\n\nexport type ReactVirtualizerOptions<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = VirtualizerOptions<TScrollElement, TItemElement> & {\n useFlushSync?: boolean\n /**\n * Skip React re-renders for scroll-only updates. The virtualizer writes\n * item positions (`top`/`left`) and the container size (`height`/`width`)\n * directly to the DOM, and only re-renders when the visible index range\n * or `isScrolling` changes.\n *\n * Requirements when enabled:\n * - Item elements must be `position: absolute`; in `'transform'` mode they\n * must also be anchored with `top: 0` / `left: 0`.\n * - Item elements must NOT set the main-axis position in their style — the\n * virtualizer owns `top` / `left` in `'position'` mode and `transform` in\n * `'transform'` mode.\n * - The inner size container must receive `virtualizer.containerRef` and\n * must NOT set `height` / `width` in its style.\n * - For multi-lane layouts (grids / masonry), the cross-axis position\n * (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and\n * must still be set in your JSX — only the main axis is automated.\n *\n * This flag is intended to be set once at mount. Toggling it (or\n * `directDomUpdatesMode`) at runtime can leave stale inline styles on\n * items and the container.\n */\n directDomUpdates?: boolean\n /**\n * How `directDomUpdates` positions item elements.\n * - `'transform'` (default): writes `transform: translate3d(...)`.\n * Promotes items to their own compositor layer — usually smoother on long\n * lists, but creates a stacking context and can interfere with\n * `position: fixed` descendants. Item elements must still be anchored with\n * `position: absolute`, `top: 0`, and `left: 0`.\n * - `'position'`: writes `top` / `left`. Item elements must be\n * `position: absolute`.\n */\n directDomUpdatesMode?: 'position' | 'transform'\n}\n\nfunction useVirtualizerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n>({\n useFlushSync = true,\n directDomUpdates = false,\n directDomUpdatesMode = 'transform',\n ...options\n}: ReactVirtualizerOptions<TScrollElement, TItemElement>): ReactVirtualizer<\n TScrollElement,\n TItemElement\n> {\n const rerender = React.useReducer((x: number) => x + 1, 0)[1]\n\n // Mutable across renders so the onChange closure captured by setOptions\n // always reads the latest values without us having to re-create it.\n const directRef = React.useRef({\n enabled: directDomUpdates,\n mode: directDomUpdatesMode,\n container: null as HTMLElement | null,\n lastSize: null as number | null,\n // Keyed by the element itself so a remounted node (same key, new DOM\n // node — e.g. when `enabled` is toggled off then on) is treated as fresh\n // and gets its style written.\n lastPositions: new WeakMap<HTMLElement, number>(),\n prevRange: null as {\n startIndex: number\n endIndex: number\n isScrolling: boolean\n } | null,\n })\n directRef.current.enabled = directDomUpdates\n directRef.current.mode = directDomUpdatesMode\n\n // Set while the virtualizer measures an item through `measureElement`, which is\n // passed as a ref and therefore runs while React is committing.\n const measuringFromRef = React.useRef(false)\n\n // Writes the size container's total extent to the DOM. Idempotent — guarded\n // by lastSize. Split out from applyDirectStyles so it can run *before* the\n // scroll-position sync in the _willUpdate effect: an end-anchored prepend\n // grows the total and bumps scrollOffset in the same pass, and if scrollTop\n // is written before the container has grown the browser clamps it to the\n // stale (shorter) scrollHeight, leaving a gap at the top until the next\n // scroll (visible only in directDomUpdates mode — React-rendered sizers get\n // their height during render).\n const applyContainerSize = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled || !state.container) return\n\n const totalSize = instance.getTotalSize()\n if (totalSize !== state.lastSize) {\n state.lastSize = totalSize\n const sizeAxis = instance.options.horizontal ? 'width' : 'height'\n state.container.style[sizeAxis] = `${totalSize}px`\n }\n }\n\n // Writes one item's main-axis position to its element. Idempotent — guarded\n // by lastPositions.\n const applyItemPosition = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n item: VirtualItem,\n el: HTMLElement,\n ) => {\n const state = directRef.current\n const horizontal = !!instance.options.horizontal\n const next = item.start - instance.options.scrollMargin\n if (state.lastPositions.get(el) === next) return\n state.lastPositions.set(el, next)\n if (state.mode === 'transform') {\n el.style.transform = horizontal\n ? `translate3d(${next}px, 0, 0)`\n : `translate3d(0, ${next}px, 0)`\n } else {\n el.style[horizontal ? 'left' : 'top'] = `${next}px`\n }\n }\n\n // Writes container size + item positions to the DOM. Idempotent — guarded\n // by lastSize / lastPositions. Called from onChange (covers scroll-driven\n // updates) and from a layout effect (covers post-render commits when refs\n // have just registered new items in elementsCache).\n const applyDirectStyles = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled || !state.container) return\n\n applyContainerSize(instance)\n\n for (const item of instance.getVirtualItems()) {\n const el = instance.elementsCache.get(item.key) as HTMLElement | undefined\n if (el) applyItemPosition(instance, item, el)\n }\n }\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n const state = directRef.current\n let shouldRerender = true\n\n if (state.enabled) {\n applyDirectStyles(instance)\n\n // Only re-render on range / isScrolling changes\n const range = instance.range\n const prev = state.prevRange\n shouldRerender =\n !prev ||\n prev.isScrolling !== instance.isScrolling ||\n prev.startIndex !== range?.startIndex ||\n prev.endIndex !== range?.endIndex\n if (shouldRerender) {\n state.prevRange = range\n ? {\n startIndex: range.startIndex,\n endIndex: range.endIndex,\n isScrolling: instance.isScrolling,\n }\n : null\n }\n }\n\n if (shouldRerender) {\n // A sync notify raised from `measureElement` reaches us while React is\n // committing, because `measureElement` is a ref callback. `flushSync`\n // cannot flush there: React still runs the callback at sync priority, but\n // it skips the flush and warns in development. The commit phase already\n // runs at discrete (sync) priority, so leaving `flushSync` out for that\n // window keeps the same lane and the same flush point — without the\n // warning. Every other sync notify (ResizeObserver re-measures, scroll\n // adjustments) still flushes synchronously.\n if (useFlushSync && sync && !measuringFromRef.current) {\n flushSync(rerender)\n } else {\n rerender()\n }\n }\n\n options.onChange?.(instance, sync)\n },\n }\n\n const [instance] = React.useState(() => {\n const v = new Virtualizer<TScrollElement, TItemElement>(resolvedOptions)\n const measureElement = v.measureElement\n v.measureElement = (node: TItemElement | null) => {\n measuringFromRef.current = true\n try {\n measureElement(node)\n } finally {\n measuringFromRef.current = false\n }\n // A row can mount in a commit that does not include this component — a\n // child re-rendering on its own state, context or a resolved Suspense\n // boundary — which the `applyDirectStyles` effect below never sees.\n // Position it as it registers; a row the effect does cover is skipped by\n // `lastPositions`.\n const state = directRef.current\n if (node !== null && state.enabled && state.container) {\n const item = v.measurementsCache[v.indexFromElement(node)]\n if (item) applyItemPosition(v, item, node as unknown as HTMLElement)\n }\n }\n return Object.assign(v, {\n containerRef: (node: HTMLElement | null) => {\n const state = directRef.current\n state.container = node\n state.lastSize = null\n // Sizes the container, and positions the rows that mounted with it:\n // React attaches children's refs before their parent's, so they have\n // already registered but found no container to be positioned in.\n if (node !== null) {\n applyDirectStyles(v)\n }\n },\n })\n })\n\n instance.setOptions(resolvedOptions)\n\n useIsomorphicLayoutEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n // Grow the size container to the new total BEFORE _willUpdate syncs the\n // scroll position. On an end-anchored prepend the scroll target lands at\n // the new bottom; if the container is still at its old (shorter) height the\n // browser clamps the scrollTop write and the list is left with a gap at the\n // top until the next scroll. Positions are written afterwards by the\n // applyDirectStyles effect below.\n applyContainerSize(instance)\n return instance._willUpdate()\n })\n\n // After every render commit, newly mounted item refs have registered in\n // elementsCache; write their positions to the DOM so the user doesn't see\n // them at (0, 0) until the next onChange.\n useIsomorphicLayoutEffect(() => {\n applyDirectStyles(instance)\n })\n\n return instance\n}\n\nexport function useVirtualizer<\n TScrollElement extends Element,\n TItemElement extends Element,\n>(\n options: PartialKeys<\n ReactVirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): ReactVirtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement extends Element>(\n options: PartialKeys<\n ReactVirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): ReactVirtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),\n ...options,\n })\n}\n"],"names":["React","instance","flushSync","Virtualizer","observeElementRect","observeElementOffset","elementScroll","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAmBA,MAAM,4BACJ,OAAO,aAAa,cAAcA,iBAAM,kBAAkBA,iBAAM;AAuDlE,SAAS,mBAGP;AAAA,EACA,eAAe;AAAA,EACf,mBAAmB;AAAA,EACnB,uBAAuB;AAAA,EACvB,GAAG;AACL,GAGE;AACA,QAAM,WAAWA,iBAAM,WAAW,CAAC,MAAc,IAAI,GAAG,CAAC,EAAE,CAAC;AAI5D,QAAM,YAAYA,iBAAM,OAAO;AAAA,IAC7B,SAAS;AAAA,IACT,MAAM;AAAA,IACN,WAAW;AAAA,IACX,UAAU;AAAA;AAAA;AAAA;AAAA,IAIV,mCAAmB,QAAA;AAAA,IACnB,WAAW;AAAA,EAAA,CAKZ;AACD,YAAU,QAAQ,UAAU;AAC5B,YAAU,QAAQ,OAAO;AAIzB,QAAM,mBAAmBA,iBAAM,OAAO,KAAK;AAU3C,QAAM,qBAAqB,CACzBC,cACG;AACH,UAAM,QAAQ,UAAU;AACxB,QAAI,CAAC,MAAM,WAAW,CAAC,MAAM,UAAW;AAExC,UAAM,YAAYA,UAAS,aAAA;AAC3B,QAAI,cAAc,MAAM,UAAU;AAChC,YAAM,WAAW;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T;AAEO,SAAS,eAId,SAIgD;AAChD,SAAO,mBAAiD;AAAA,IAAA,oBACtDC,YAAAA;AAAAA,IAAA,sBACAC,YAAAA;AAAAA,IACA,YAAYC,YAAAA;AAAAA,IACZ,GAAG;AAAA,EAAA,CACJ;AACH;AAEO,SAAS,qBACd,SAOwC;AACxC,SAAO,mBAAyC;AAAA,IAC9C,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,IACpE,oBAAoBC,YAAAA;AAAAA,IACpB,sBAAsBC,YAAAA;AAAAA,IACtB,YAAYC,YAAAA;AAAAA,IACZ,eAAe,MAAO,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,IACzE,GAAG;AAAA,EAAA,CACJ;AACH;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport { flushSync } from 'react-dom'\nimport {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type {\n PartialKeys,\n VirtualItem,\n VirtualizerOptions,\n} from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nconst useIsomorphicLayoutEffect =\n typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type ReactVirtualizer<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = Virtualizer<TScrollElement, TItemElement> & {\n /**\n * Ref callback for the inner size container element. Only meaningful when\n * `directDomUpdates: true` — the virtualizer writes the container's\n * main-axis size (`height` or `width`) directly to skip React re-renders.\n */\n containerRef: (node: HTMLElement | null) => void\n}\n\nexport type ReactVirtualizerOptions<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = VirtualizerOptions<TScrollElement, TItemElement> & {\n useFlushSync?: boolean\n /**\n * Skip React re-renders for scroll-only updates. The virtualizer writes\n * item positions (`top`/`left`) and the container size (`height`/`width`)\n * directly to the DOM, and only re-renders when the visible index range\n * or `isScrolling` changes.\n *\n * Requirements when enabled:\n * - Item elements must be `position: absolute`; in `'transform'` mode they\n * must also be anchored with `top: 0` / `left: 0`.\n * - Item elements must NOT set the main-axis position in their style — the\n * virtualizer owns `top` / `left` in `'position'` mode and `transform` in\n * `'transform'` mode.\n * - The inner size container must receive `virtualizer.containerRef` and\n * must NOT set `height` / `width` in its style.\n * - For multi-lane layouts (grids / masonry), the cross-axis position\n * (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and\n * must still be set in your JSX — only the main axis is automated.\n *\n * This flag is intended to be set once at mount. Toggling it (or\n * `directDomUpdatesMode`) at runtime can leave stale inline styles on\n * items and the container.\n */\n directDomUpdates?: boolean\n /**\n * How `directDomUpdates` positions item elements.\n * - `'transform'` (default): writes `transform: translate3d(...)`.\n * Promotes items to their own compositor layer — usually smoother on long\n * lists, but creates a stacking context and can interfere with\n * `position: fixed` descendants. Item elements must still be anchored with\n * `position: absolute`, `top: 0`, and `left: 0`.\n * - `'position'`: writes `top` / `left`. Item elements must be\n * `position: absolute`.\n */\n directDomUpdatesMode?: 'position' | 'transform'\n}\n\nfunction useVirtualizerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n>({\n useFlushSync = true,\n directDomUpdates = false,\n directDomUpdatesMode = 'transform',\n ...options\n}: ReactVirtualizerOptions<TScrollElement, TItemElement>): ReactVirtualizer<\n TScrollElement,\n TItemElement\n> {\n const [, dispatch] = React.useReducer((x: number) => x + 1, 0)\n // React DevTools replays hooks with a no-op dispatch while inspecting them.\n // Keep the original dispatch so that setOptions below cannot replace the\n // virtualizer's render callback with the inspector's no-op.\n const rerender = React.useRef(dispatch).current\n\n // Mutable across renders so the onChange closure captured by setOptions\n // always reads the latest values without us having to re-create it.\n const directRef = React.useRef({\n enabled: directDomUpdates,\n mode: directDomUpdatesMode,\n container: null as HTMLElement | null,\n lastSize: null as number | null,\n // Keyed by the element itself so a remounted node (same key, new DOM\n // node — e.g. when `enabled` is toggled off then on) is treated as fresh\n // and gets its style written.\n lastPositions: new WeakMap<HTMLElement, number>(),\n prevRange: null as {\n startIndex: number\n endIndex: number\n isScrolling: boolean\n } | null,\n })\n directRef.current.enabled = directDomUpdates\n directRef.current.mode = directDomUpdatesMode\n\n // Set while the virtualizer measures an item through `measureElement`, which is\n // passed as a ref and therefore runs while React is committing.\n const measuringFromRef = React.useRef(false)\n\n // Writes the size container's total extent to the DOM. Idempotent — guarded\n // by lastSize. Split out from applyDirectStyles so it can run *before* the\n // scroll-position sync in the _willUpdate effect: an end-anchored prepend\n // grows the total and bumps scrollOffset in the same pass, and if scrollTop\n // is written before the container has grown the browser clamps it to the\n // stale (shorter) scrollHeight, leaving a gap at the top until the next\n // scroll (visible only in directDomUpdates mode — React-rendered sizers get\n // their height during render).\n const applyContainerSize = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled || !state.container) return\n\n const totalSize = instance.getTotalSize()\n if (totalSize !== state.lastSize) {\n state.lastSize = totalSize\n const sizeAxis = instance.options.horizontal ? 'width' : 'height'\n state.container.style[sizeAxis] = `${totalSize}px`\n }\n }\n\n // Writes one item's main-axis position to its element. Idempotent — guarded\n // by lastPositions.\n const applyItemPosition = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n item: VirtualItem,\n el: HTMLElement,\n ) => {\n const state = directRef.current\n const horizontal = !!instance.options.horizontal\n const next = item.start - instance.options.scrollMargin\n if (state.lastPositions.get(el) === next) return\n state.lastPositions.set(el, next)\n if (state.mode === 'transform') {\n el.style.transform = horizontal\n ? `translate3d(${next}px, 0, 0)`\n : `translate3d(0, ${next}px, 0)`\n } else {\n el.style[horizontal ? 'left' : 'top'] = `${next}px`\n }\n }\n\n // Writes container size + item positions to the DOM. Idempotent — guarded\n // by lastSize / lastPositions. Called from onChange (covers scroll-driven\n // updates) and from a layout effect (covers post-render commits when refs\n // have just registered new items in elementsCache).\n const applyDirectStyles = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled || !state.container) return\n\n applyContainerSize(instance)\n\n for (const item of instance.getVirtualItems()) {\n const el = instance.elementsCache.get(item.key) as HTMLElement | undefined\n if (el) applyItemPosition(instance, item, el)\n }\n }\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n const state = directRef.current\n let shouldRerender = true\n\n if (state.enabled) {\n applyDirectStyles(instance)\n\n // Only re-render on range / isScrolling changes\n const range = instance.range\n const prev = state.prevRange\n shouldRerender =\n !prev ||\n prev.isScrolling !== instance.isScrolling ||\n prev.startIndex !== range?.startIndex ||\n prev.endIndex !== range?.endIndex\n if (shouldRerender) {\n state.prevRange = range\n ? {\n startIndex: range.startIndex,\n endIndex: range.endIndex,\n isScrolling: instance.isScrolling,\n }\n : null\n }\n }\n\n if (shouldRerender) {\n // A sync notify raised from `measureElement` reaches us while React is\n // committing, because `measureElement` is a ref callback. `flushSync`\n // cannot flush there: React still runs the callback at sync priority, but\n // it skips the flush and warns in development. The commit phase already\n // runs at discrete (sync) priority, so leaving `flushSync` out for that\n // window keeps the same lane and the same flush point — without the\n // warning. Every other sync notify (ResizeObserver re-measures, scroll\n // adjustments) still flushes synchronously.\n if (useFlushSync && sync && !measuringFromRef.current) {\n flushSync(rerender)\n } else {\n rerender()\n }\n }\n\n options.onChange?.(instance, sync)\n },\n }\n\n const [instance] = React.useState(() => {\n const v = new Virtualizer<TScrollElement, TItemElement>(resolvedOptions)\n const measureElement = v.measureElement\n v.measureElement = (node: TItemElement | null) => {\n measuringFromRef.current = true\n try {\n measureElement(node)\n } finally {\n measuringFromRef.current = false\n }\n // A row can mount in a commit that does not include this component — a\n // child re-rendering on its own state, context or a resolved Suspense\n // boundary — which the `applyDirectStyles` effect below never sees.\n // Position it as it registers; a row the effect does cover is skipped by\n // `lastPositions`.\n const state = directRef.current\n if (node !== null && state.enabled && state.container) {\n const item = v.measurementsCache[v.indexFromElement(node)]\n if (item) applyItemPosition(v, item, node as unknown as HTMLElement)\n }\n }\n return Object.assign(v, {\n containerRef: (node: HTMLElement | null) => {\n const state = directRef.current\n state.container = node\n state.lastSize = null\n // Sizes the container, and positions the rows that mounted with it:\n // React attaches children's refs before their parent's, so they have\n // already registered but found no container to be positioned in.\n if (node !== null) {\n applyDirectStyles(v)\n }\n },\n })\n })\n\n instance.setOptions(resolvedOptions)\n\n useIsomorphicLayoutEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n // Grow the size container to the new total BEFORE _willUpdate syncs the\n // scroll position. On an end-anchored prepend the scroll target lands at\n // the new bottom; if the container is still at its old (shorter) height the\n // browser clamps the scrollTop write and the list is left with a gap at the\n // top until the next scroll. Positions are written afterwards by the\n // applyDirectStyles effect below.\n applyContainerSize(instance)\n return instance._willUpdate()\n })\n\n // After every render commit, newly mounted item refs have registered in\n // elementsCache; write their positions to the DOM so the user doesn't see\n // them at (0, 0) until the next onChange.\n useIsomorphicLayoutEffect(() => {\n applyDirectStyles(instance)\n })\n\n return instance\n}\n\nexport function useVirtualizer<\n TScrollElement extends Element,\n TItemElement extends Element,\n>(\n options: PartialKeys<\n ReactVirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): ReactVirtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement extends Element>(\n options: PartialKeys<\n ReactVirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): ReactVirtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () =>\n typeof document !== 'undefined'\n ? options.horizontal\n ? window.scrollX\n : window.scrollY\n : 0,\n ...options,\n })\n}\n"],"names":["React","instance","flushSync","Virtualizer","observeElementRect","observeElementOffset","elementScroll","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAmBA,MAAM,4BACJ,OAAO,aAAa,cAAcA,iBAAM,kBAAkBA,iBAAM;AAuDlE,SAAS,mBAGP;AAAA,EACA,eAAe;AAAA,EACf,mBAAmB;AAAA,EACnB,uBAAuB;AAAA,EACvB,GAAG;AACL,GAGE;AACA,QAAM,CAAA,EAAG,QAAQ,IAAIA,iBAAM,WAAW,CAAC,MAAc,IAAI,GAAG,CAAC;AAI7D,QAAM,WAAWA,iBAAM,OAAO,QAAQ,EAAE;AAIxC,QAAM,YAAYA,iBAAM,OAAO;AAAA,IAC7B,SAAS;AAAA,IACT,MAAM;AAAA,IACN,WAAW;AAAA,IACX,UAAU;AAAA;AAAA;AAAA;AAAA,IAIV,mCAAmB,QAAA;AAAA,IACnB,WAAW;AAAA,EAAA,CAKZ;AACD,YAAU,QAAQ,UAAU;AAC5B,YAAU,QAAQ,OAAO;AAIzB,QAAM,mBAAmBA,iBAAM,OAAO,KAAK;AAU3C,QAAM,qBAAqB,CACzBC,cACG;AACH,UAAM,QAAQ,UAAU;AACxB,QAAI,CAAC,MAAM,WAAW,CAAC,MAAM,UAAW;AAExC,UAAM,YAAYA,UAAS,aAAA;AAC3B,QAAI,cAAc,MAAM,UAAU;AAChC,YAAM,WAAW;AACjB,YAAM,WAAWA,UAAS,QAAQ,aAAa,UAAU;AACzD,YAAM,UAAU,MAAM,QAAQ,IAAI,GAAG,SAAS;AAAA,IAChD;AAAA,EACF;AAIA,QAAM,oBAAoB,CACxBA,WACA,MACA,OACG;AACH,UAAM,QAAQ,UAAU;AACxB,UAAM,aAAa,CAAC,CAACA,UAAS,QAAQ;AACtC,UAAM,OAAO,KAAK,QAAQA,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AAc,SAAS;AAAA,IACpE,oBAAoBC,YAAAA;AAAAA,IACpB,sBAAsBC,YAAAA;AAAAA,IACtB,YAAYC,YAAAA;AAAAA,IACZ,eAAe,MACb,OAAO,aAAa,cAChB,QAAQ,aACN,OAAO,UACP,OAAO,UACT;AAAA,IACN,GAAG;AAAA,EAAA,CACJ;AACH;;;;;;;;;"}
|
|
@@ -9,7 +9,8 @@ function useVirtualizerBase({
|
|
|
9
9
|
directDomUpdatesMode = "transform",
|
|
10
10
|
...options
|
|
11
11
|
}) {
|
|
12
|
-
const
|
|
12
|
+
const [, dispatch] = React.useReducer((x) => x + 1, 0);
|
|
13
|
+
const rerender = React.useRef(dispatch).current;
|
|
13
14
|
const directRef = React.useRef({
|
|
14
15
|
enabled: directDomUpdates,
|
|
15
16
|
mode: directDomUpdatesMode,
|
|
@@ -138,7 +139,7 @@ function useWindowVirtualizer(options) {
|
|
|
138
139
|
observeElementRect: observeWindowRect,
|
|
139
140
|
observeElementOffset: observeWindowOffset,
|
|
140
141
|
scrollToFn: windowScroll,
|
|
141
|
-
initialOffset: () => typeof document !== "undefined" ? window.scrollY : 0,
|
|
142
|
+
initialOffset: () => typeof document !== "undefined" ? options.horizontal ? window.scrollX : window.scrollY : 0,
|
|
142
143
|
...options
|
|
143
144
|
});
|
|
144
145
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport { flushSync } from 'react-dom'\nimport {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type {\n PartialKeys,\n VirtualItem,\n VirtualizerOptions,\n} from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nconst useIsomorphicLayoutEffect =\n typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type ReactVirtualizer<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = Virtualizer<TScrollElement, TItemElement> & {\n /**\n * Ref callback for the inner size container element. Only meaningful when\n * `directDomUpdates: true` — the virtualizer writes the container's\n * main-axis size (`height` or `width`) directly to skip React re-renders.\n */\n containerRef: (node: HTMLElement | null) => void\n}\n\nexport type ReactVirtualizerOptions<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = VirtualizerOptions<TScrollElement, TItemElement> & {\n useFlushSync?: boolean\n /**\n * Skip React re-renders for scroll-only updates. The virtualizer writes\n * item positions (`top`/`left`) and the container size (`height`/`width`)\n * directly to the DOM, and only re-renders when the visible index range\n * or `isScrolling` changes.\n *\n * Requirements when enabled:\n * - Item elements must be `position: absolute`; in `'transform'` mode they\n * must also be anchored with `top: 0` / `left: 0`.\n * - Item elements must NOT set the main-axis position in their style — the\n * virtualizer owns `top` / `left` in `'position'` mode and `transform` in\n * `'transform'` mode.\n * - The inner size container must receive `virtualizer.containerRef` and\n * must NOT set `height` / `width` in its style.\n * - For multi-lane layouts (grids / masonry), the cross-axis position\n * (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and\n * must still be set in your JSX — only the main axis is automated.\n *\n * This flag is intended to be set once at mount. Toggling it (or\n * `directDomUpdatesMode`) at runtime can leave stale inline styles on\n * items and the container.\n */\n directDomUpdates?: boolean\n /**\n * How `directDomUpdates` positions item elements.\n * - `'transform'` (default): writes `transform: translate3d(...)`.\n * Promotes items to their own compositor layer — usually smoother on long\n * lists, but creates a stacking context and can interfere with\n * `position: fixed` descendants. Item elements must still be anchored with\n * `position: absolute`, `top: 0`, and `left: 0`.\n * - `'position'`: writes `top` / `left`. Item elements must be\n * `position: absolute`.\n */\n directDomUpdatesMode?: 'position' | 'transform'\n}\n\nfunction useVirtualizerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n>({\n useFlushSync = true,\n directDomUpdates = false,\n directDomUpdatesMode = 'transform',\n ...options\n}: ReactVirtualizerOptions<TScrollElement, TItemElement>): ReactVirtualizer<\n TScrollElement,\n TItemElement\n> {\n const rerender = React.useReducer((x: number) => x + 1, 0)[1]\n\n // Mutable across renders so the onChange closure captured by setOptions\n // always reads the latest values without us having to re-create it.\n const directRef = React.useRef({\n enabled: directDomUpdates,\n mode: directDomUpdatesMode,\n container: null as HTMLElement | null,\n lastSize: null as number | null,\n // Keyed by the element itself so a remounted node (same key, new DOM\n // node — e.g. when `enabled` is toggled off then on) is treated as fresh\n // and gets its style written.\n lastPositions: new WeakMap<HTMLElement, number>(),\n prevRange: null as {\n startIndex: number\n endIndex: number\n isScrolling: boolean\n } | null,\n })\n directRef.current.enabled = directDomUpdates\n directRef.current.mode = directDomUpdatesMode\n\n // Set while the virtualizer measures an item through `measureElement`, which is\n // passed as a ref and therefore runs while React is committing.\n const measuringFromRef = React.useRef(false)\n\n // Writes the size container's total extent to the DOM. Idempotent — guarded\n // by lastSize. Split out from applyDirectStyles so it can run *before* the\n // scroll-position sync in the _willUpdate effect: an end-anchored prepend\n // grows the total and bumps scrollOffset in the same pass, and if scrollTop\n // is written before the container has grown the browser clamps it to the\n // stale (shorter) scrollHeight, leaving a gap at the top until the next\n // scroll (visible only in directDomUpdates mode — React-rendered sizers get\n // their height during render).\n const applyContainerSize = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled || !state.container) return\n\n const totalSize = instance.getTotalSize()\n if (totalSize !== state.lastSize) {\n state.lastSize = totalSize\n const sizeAxis = instance.options.horizontal ? 'width' : 'height'\n state.container.style[sizeAxis] = `${totalSize}px`\n }\n }\n\n // Writes one item's main-axis position to its element. Idempotent — guarded\n // by lastPositions.\n const applyItemPosition = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n item: VirtualItem,\n el: HTMLElement,\n ) => {\n const state = directRef.current\n const horizontal = !!instance.options.horizontal\n const next = item.start - instance.options.scrollMargin\n if (state.lastPositions.get(el) === next) return\n state.lastPositions.set(el, next)\n if (state.mode === 'transform') {\n el.style.transform = horizontal\n ? `translate3d(${next}px, 0, 0)`\n : `translate3d(0, ${next}px, 0)`\n } else {\n el.style[horizontal ? 'left' : 'top'] = `${next}px`\n }\n }\n\n // Writes container size + item positions to the DOM. Idempotent — guarded\n // by lastSize / lastPositions. Called from onChange (covers scroll-driven\n // updates) and from a layout effect (covers post-render commits when refs\n // have just registered new items in elementsCache).\n const applyDirectStyles = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled || !state.container) return\n\n applyContainerSize(instance)\n\n for (const item of instance.getVirtualItems()) {\n const el = instance.elementsCache.get(item.key) as HTMLElement | undefined\n if (el) applyItemPosition(instance, item, el)\n }\n }\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n const state = directRef.current\n let shouldRerender = true\n\n if (state.enabled) {\n applyDirectStyles(instance)\n\n // Only re-render on range / isScrolling changes\n const range = instance.range\n const prev = state.prevRange\n shouldRerender =\n !prev ||\n prev.isScrolling !== instance.isScrolling ||\n prev.startIndex !== range?.startIndex ||\n prev.endIndex !== range?.endIndex\n if (shouldRerender) {\n state.prevRange = range\n ? {\n startIndex: range.startIndex,\n endIndex: range.endIndex,\n isScrolling: instance.isScrolling,\n }\n : null\n }\n }\n\n if (shouldRerender) {\n // A sync notify raised from `measureElement` reaches us while React is\n // committing, because `measureElement` is a ref callback. `flushSync`\n // cannot flush there: React still runs the callback at sync priority, but\n // it skips the flush and warns in development. The commit phase already\n // runs at discrete (sync) priority, so leaving `flushSync` out for that\n // window keeps the same lane and the same flush point — without the\n // warning. Every other sync notify (ResizeObserver re-measures, scroll\n // adjustments) still flushes synchronously.\n if (useFlushSync && sync && !measuringFromRef.current) {\n flushSync(rerender)\n } else {\n rerender()\n }\n }\n\n options.onChange?.(instance, sync)\n },\n }\n\n const [instance] = React.useState(() => {\n const v = new Virtualizer<TScrollElement, TItemElement>(resolvedOptions)\n const measureElement = v.measureElement\n v.measureElement = (node: TItemElement | null) => {\n measuringFromRef.current = true\n try {\n measureElement(node)\n } finally {\n measuringFromRef.current = false\n }\n // A row can mount in a commit that does not include this component — a\n // child re-rendering on its own state, context or a resolved Suspense\n // boundary — which the `applyDirectStyles` effect below never sees.\n // Position it as it registers; a row the effect does cover is skipped by\n // `lastPositions`.\n const state = directRef.current\n if (node !== null && state.enabled && state.container) {\n const item = v.measurementsCache[v.indexFromElement(node)]\n if (item) applyItemPosition(v, item, node as unknown as HTMLElement)\n }\n }\n return Object.assign(v, {\n containerRef: (node: HTMLElement | null) => {\n const state = directRef.current\n state.container = node\n state.lastSize = null\n // Sizes the container, and positions the rows that mounted with it:\n // React attaches children's refs before their parent's, so they have\n // already registered but found no container to be positioned in.\n if (node !== null) {\n applyDirectStyles(v)\n }\n },\n })\n })\n\n instance.setOptions(resolvedOptions)\n\n useIsomorphicLayoutEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n // Grow the size container to the new total BEFORE _willUpdate syncs the\n // scroll position. On an end-anchored prepend the scroll target lands at\n // the new bottom; if the container is still at its old (shorter) height the\n // browser clamps the scrollTop write and the list is left with a gap at the\n // top until the next scroll. Positions are written afterwards by the\n // applyDirectStyles effect below.\n applyContainerSize(instance)\n return instance._willUpdate()\n })\n\n // After every render commit, newly mounted item refs have registered in\n // elementsCache; write their positions to the DOM so the user doesn't see\n // them at (0, 0) until the next onChange.\n useIsomorphicLayoutEffect(() => {\n applyDirectStyles(instance)\n })\n\n return instance\n}\n\nexport function useVirtualizer<\n TScrollElement extends Element,\n TItemElement extends Element,\n>(\n options: PartialKeys<\n ReactVirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): ReactVirtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement extends Element>(\n options: PartialKeys<\n ReactVirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): ReactVirtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),\n ...options,\n })\n}\n"],"names":["instance"],"mappings":";;;;AAmBA,MAAM,4BACJ,OAAO,aAAa,cAAc,MAAM,kBAAkB,MAAM;AAuDlE,SAAS,mBAGP;AAAA,EACA,eAAe;AAAA,EACf,mBAAmB;AAAA,EACnB,uBAAuB;AAAA,EACvB,GAAG;AACL,GAGE;AACA,QAAM,WAAW,MAAM,WAAW,CAAC,MAAc,IAAI,GAAG,CAAC,EAAE,CAAC;AAI5D,QAAM,YAAY,MAAM,OAAO;AAAA,IAC7B,SAAS;AAAA,IACT,MAAM;AAAA,IACN,WAAW;AAAA,IACX,UAAU;AAAA;AAAA;AAAA;AAAA,IAIV,mCAAmB,QAAA;AAAA,IACnB,WAAW;AAAA,EAAA,CAKZ;AACD,YAAU,QAAQ,UAAU;AAC5B,YAAU,QAAQ,OAAO;AAIzB,QAAM,mBAAmB,MAAM,OAAO,KAAK;AAU3C,QAAM,qBAAqB,CACzBA,cACG;AACH,UAAM,QAAQ,UAAU;AACxB,QAAI,CAAC,MAAM,WAAW,CAAC,MAAM,UAAW;AAExC,UAAM,YAAYA,UAAS,aAAA;AAC3B,QAAI,cAAc,MAAM,UAAU;AAChC,YAAM,WAAW;AACjB,YAAM,WAAWA,UAAS,QAAQ,aAAa,UAAU;AACzD,YAAM,UAAU,MAAM,QAAQ,IAAI,GAAG,SAAS;AAAA,IAChD;AAAA,EACF;AAIA,QAAM,oBAAoB,CACxBA,WACA,MACA,OACG;AACH,UAAM,QAAQ,UAAU;AACxB,UAA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a,cAAc,SAAS;AAAA,IACpE,oBAAoB;AAAA,IACpB,sBAAsB;AAAA,IACtB,YAAY;AAAA,IACZ,eAAe,MAAO,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,IACzE,GAAG;AAAA,EAAA,CACJ;AACH;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport { flushSync } from 'react-dom'\nimport {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type {\n PartialKeys,\n VirtualItem,\n VirtualizerOptions,\n} from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nconst useIsomorphicLayoutEffect =\n typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type ReactVirtualizer<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = Virtualizer<TScrollElement, TItemElement> & {\n /**\n * Ref callback for the inner size container element. Only meaningful when\n * `directDomUpdates: true` — the virtualizer writes the container's\n * main-axis size (`height` or `width`) directly to skip React re-renders.\n */\n containerRef: (node: HTMLElement | null) => void\n}\n\nexport type ReactVirtualizerOptions<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = VirtualizerOptions<TScrollElement, TItemElement> & {\n useFlushSync?: boolean\n /**\n * Skip React re-renders for scroll-only updates. The virtualizer writes\n * item positions (`top`/`left`) and the container size (`height`/`width`)\n * directly to the DOM, and only re-renders when the visible index range\n * or `isScrolling` changes.\n *\n * Requirements when enabled:\n * - Item elements must be `position: absolute`; in `'transform'` mode they\n * must also be anchored with `top: 0` / `left: 0`.\n * - Item elements must NOT set the main-axis position in their style — the\n * virtualizer owns `top` / `left` in `'position'` mode and `transform` in\n * `'transform'` mode.\n * - The inner size container must receive `virtualizer.containerRef` and\n * must NOT set `height` / `width` in its style.\n * - For multi-lane layouts (grids / masonry), the cross-axis position\n * (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and\n * must still be set in your JSX — only the main axis is automated.\n *\n * This flag is intended to be set once at mount. Toggling it (or\n * `directDomUpdatesMode`) at runtime can leave stale inline styles on\n * items and the container.\n */\n directDomUpdates?: boolean\n /**\n * How `directDomUpdates` positions item elements.\n * - `'transform'` (default): writes `transform: translate3d(...)`.\n * Promotes items to their own compositor layer — usually smoother on long\n * lists, but creates a stacking context and can interfere with\n * `position: fixed` descendants. Item elements must still be anchored with\n * `position: absolute`, `top: 0`, and `left: 0`.\n * - `'position'`: writes `top` / `left`. Item elements must be\n * `position: absolute`.\n */\n directDomUpdatesMode?: 'position' | 'transform'\n}\n\nfunction useVirtualizerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n>({\n useFlushSync = true,\n directDomUpdates = false,\n directDomUpdatesMode = 'transform',\n ...options\n}: ReactVirtualizerOptions<TScrollElement, TItemElement>): ReactVirtualizer<\n TScrollElement,\n TItemElement\n> {\n const [, dispatch] = React.useReducer((x: number) => x + 1, 0)\n // React DevTools replays hooks with a no-op dispatch while inspecting them.\n // Keep the original dispatch so that setOptions below cannot replace the\n // virtualizer's render callback with the inspector's no-op.\n const rerender = React.useRef(dispatch).current\n\n // Mutable across renders so the onChange closure captured by setOptions\n // always reads the latest values without us having to re-create it.\n const directRef = React.useRef({\n enabled: directDomUpdates,\n mode: directDomUpdatesMode,\n container: null as HTMLElement | null,\n lastSize: null as number | null,\n // Keyed by the element itself so a remounted node (same key, new DOM\n // node — e.g. when `enabled` is toggled off then on) is treated as fresh\n // and gets its style written.\n lastPositions: new WeakMap<HTMLElement, number>(),\n prevRange: null as {\n startIndex: number\n endIndex: number\n isScrolling: boolean\n } | null,\n })\n directRef.current.enabled = directDomUpdates\n directRef.current.mode = directDomUpdatesMode\n\n // Set while the virtualizer measures an item through `measureElement`, which is\n // passed as a ref and therefore runs while React is committing.\n const measuringFromRef = React.useRef(false)\n\n // Writes the size container's total extent to the DOM. Idempotent — guarded\n // by lastSize. Split out from applyDirectStyles so it can run *before* the\n // scroll-position sync in the _willUpdate effect: an end-anchored prepend\n // grows the total and bumps scrollOffset in the same pass, and if scrollTop\n // is written before the container has grown the browser clamps it to the\n // stale (shorter) scrollHeight, leaving a gap at the top until the next\n // scroll (visible only in directDomUpdates mode — React-rendered sizers get\n // their height during render).\n const applyContainerSize = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled || !state.container) return\n\n const totalSize = instance.getTotalSize()\n if (totalSize !== state.lastSize) {\n state.lastSize = totalSize\n const sizeAxis = instance.options.horizontal ? 'width' : 'height'\n state.container.style[sizeAxis] = `${totalSize}px`\n }\n }\n\n // Writes one item's main-axis position to its element. Idempotent — guarded\n // by lastPositions.\n const applyItemPosition = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n item: VirtualItem,\n el: HTMLElement,\n ) => {\n const state = directRef.current\n const horizontal = !!instance.options.horizontal\n const next = item.start - instance.options.scrollMargin\n if (state.lastPositions.get(el) === next) return\n state.lastPositions.set(el, next)\n if (state.mode === 'transform') {\n el.style.transform = horizontal\n ? `translate3d(${next}px, 0, 0)`\n : `translate3d(0, ${next}px, 0)`\n } else {\n el.style[horizontal ? 'left' : 'top'] = `${next}px`\n }\n }\n\n // Writes container size + item positions to the DOM. Idempotent — guarded\n // by lastSize / lastPositions. Called from onChange (covers scroll-driven\n // updates) and from a layout effect (covers post-render commits when refs\n // have just registered new items in elementsCache).\n const applyDirectStyles = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled || !state.container) return\n\n applyContainerSize(instance)\n\n for (const item of instance.getVirtualItems()) {\n const el = instance.elementsCache.get(item.key) as HTMLElement | undefined\n if (el) applyItemPosition(instance, item, el)\n }\n }\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n const state = directRef.current\n let shouldRerender = true\n\n if (state.enabled) {\n applyDirectStyles(instance)\n\n // Only re-render on range / isScrolling changes\n const range = instance.range\n const prev = state.prevRange\n shouldRerender =\n !prev ||\n prev.isScrolling !== instance.isScrolling ||\n prev.startIndex !== range?.startIndex ||\n prev.endIndex !== range?.endIndex\n if (shouldRerender) {\n state.prevRange = range\n ? {\n startIndex: range.startIndex,\n endIndex: range.endIndex,\n isScrolling: instance.isScrolling,\n }\n : null\n }\n }\n\n if (shouldRerender) {\n // A sync notify raised from `measureElement` reaches us while React is\n // committing, because `measureElement` is a ref callback. `flushSync`\n // cannot flush there: React still runs the callback at sync priority, but\n // it skips the flush and warns in development. The commit phase already\n // runs at discrete (sync) priority, so leaving `flushSync` out for that\n // window keeps the same lane and the same flush point — without the\n // warning. Every other sync notify (ResizeObserver re-measures, scroll\n // adjustments) still flushes synchronously.\n if (useFlushSync && sync && !measuringFromRef.current) {\n flushSync(rerender)\n } else {\n rerender()\n }\n }\n\n options.onChange?.(instance, sync)\n },\n }\n\n const [instance] = React.useState(() => {\n const v = new Virtualizer<TScrollElement, TItemElement>(resolvedOptions)\n const measureElement = v.measureElement\n v.measureElement = (node: TItemElement | null) => {\n measuringFromRef.current = true\n try {\n measureElement(node)\n } finally {\n measuringFromRef.current = false\n }\n // A row can mount in a commit that does not include this component — a\n // child re-rendering on its own state, context or a resolved Suspense\n // boundary — which the `applyDirectStyles` effect below never sees.\n // Position it as it registers; a row the effect does cover is skipped by\n // `lastPositions`.\n const state = directRef.current\n if (node !== null && state.enabled && state.container) {\n const item = v.measurementsCache[v.indexFromElement(node)]\n if (item) applyItemPosition(v, item, node as unknown as HTMLElement)\n }\n }\n return Object.assign(v, {\n containerRef: (node: HTMLElement | null) => {\n const state = directRef.current\n state.container = node\n state.lastSize = null\n // Sizes the container, and positions the rows that mounted with it:\n // React attaches children's refs before their parent's, so they have\n // already registered but found no container to be positioned in.\n if (node !== null) {\n applyDirectStyles(v)\n }\n },\n })\n })\n\n instance.setOptions(resolvedOptions)\n\n useIsomorphicLayoutEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n // Grow the size container to the new total BEFORE _willUpdate syncs the\n // scroll position. On an end-anchored prepend the scroll target lands at\n // the new bottom; if the container is still at its old (shorter) height the\n // browser clamps the scrollTop write and the list is left with a gap at the\n // top until the next scroll. Positions are written afterwards by the\n // applyDirectStyles effect below.\n applyContainerSize(instance)\n return instance._willUpdate()\n })\n\n // After every render commit, newly mounted item refs have registered in\n // elementsCache; write their positions to the DOM so the user doesn't see\n // them at (0, 0) until the next onChange.\n useIsomorphicLayoutEffect(() => {\n applyDirectStyles(instance)\n })\n\n return instance\n}\n\nexport function useVirtualizer<\n TScrollElement extends Element,\n TItemElement extends Element,\n>(\n options: PartialKeys<\n ReactVirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): ReactVirtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement extends Element>(\n options: PartialKeys<\n ReactVirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): ReactVirtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () =>\n typeof document !== 'undefined'\n ? options.horizontal\n ? window.scrollX\n : window.scrollY\n : 0,\n ...options,\n })\n}\n"],"names":["instance"],"mappings":";;;;AAmBA,MAAM,4BACJ,OAAO,aAAa,cAAc,MAAM,kBAAkB,MAAM;AAuDlE,SAAS,mBAGP;AAAA,EACA,eAAe;AAAA,EACf,mBAAmB;AAAA,EACnB,uBAAuB;AAAA,EACvB,GAAG;AACL,GAGE;AACA,QAA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AACzD,YAAI,KAAM,mBAAkB,GAAG,MAAM,IAA8B;AAAA,MACrE;AAAA,IACF;AACA,WAAO,OAAO,OAAO,GAAG;AAAA,MACtB,cAAc,CAAC,SAA6B;AAC1C,cAAM,QAAQ,UAAU;AACxB,cAAM,YAAY;AAClB,cAAM,WAAW;AAIjB,YAAI,SAAS,MAAM;AACjB,4BAAkB,CAAC;AAAA,QACrB;AAAA,MACF;AAAA,IAAA,CACD;AAAA,EACH,CAAC;AAED,WAAS,WAAW,eAAe;AAEnC,4BAA0B,MAAM;AAC9B,WAAO,SAAS,UAAA;AAAA,EAClB,GAAG,CAAA,CAAE;AAEL,4BAA0B,MAAM;AAO9B,uBAAmB,QAAQ;AAC3B,WAAO,SAAS,YAAA;AAAA,EAClB,CAAC;AAKD,4BAA0B,MAAM;AAC9B,sBAAkB,QAAQ;AAAA,EAC5B,CAAC;AAED,SAAO;AACT;AAEO,SAAS,eAId,SAIgD;AAChD,SAAO,mBAAiD;AAAA,IACtD;AAAA,IACA;AAAA,IACA,YAAY;AAAA,IACZ,GAAG;AAAA,EAAA,CACJ;AACH;AAEO,SAAS,qBACd,SAOwC;AACxC,SAAO,mBAAyC;AAAA,IAC9C,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,IACpE,oBAAoB;AAAA,IACpB,sBAAsB;AAAA,IACtB,YAAY;AAAA,IACZ,eAAe,MACb,OAAO,aAAa,cAChB,QAAQ,aACN,OAAO,UACP,OAAO,UACT;AAAA,IACN,GAAG;AAAA,EAAA,CACJ;AACH;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-virtual",
|
|
3
|
-
"version": "3.14.
|
|
3
|
+
"version": "3.14.28",
|
|
4
4
|
"description": "Headless UI for virtualizing scrollable elements in React",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"src"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@tanstack/virtual-core": "3.19.
|
|
48
|
+
"@tanstack/virtual-core": "3.19.13"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@testing-library/react": "^16.3.0",
|
|
@@ -85,7 +85,11 @@ function useVirtualizerBase<
|
|
|
85
85
|
TScrollElement,
|
|
86
86
|
TItemElement
|
|
87
87
|
> {
|
|
88
|
-
const
|
|
88
|
+
const [, dispatch] = React.useReducer((x: number) => x + 1, 0)
|
|
89
|
+
// React DevTools replays hooks with a no-op dispatch while inspecting them.
|
|
90
|
+
// Keep the original dispatch so that setOptions below cannot replace the
|
|
91
|
+
// virtualizer's render callback with the inspector's no-op.
|
|
92
|
+
const rerender = React.useRef(dispatch).current
|
|
89
93
|
|
|
90
94
|
// Mutable across renders so the onChange closure captured by setOptions
|
|
91
95
|
// always reads the latest values without us having to re-create it.
|
|
@@ -314,7 +318,12 @@ export function useWindowVirtualizer<TItemElement extends Element>(
|
|
|
314
318
|
observeElementRect: observeWindowRect,
|
|
315
319
|
observeElementOffset: observeWindowOffset,
|
|
316
320
|
scrollToFn: windowScroll,
|
|
317
|
-
initialOffset: () =>
|
|
321
|
+
initialOffset: () =>
|
|
322
|
+
typeof document !== 'undefined'
|
|
323
|
+
? options.horizontal
|
|
324
|
+
? window.scrollX
|
|
325
|
+
: window.scrollY
|
|
326
|
+
: 0,
|
|
318
327
|
...options,
|
|
319
328
|
})
|
|
320
329
|
}
|