@ac-kit/ui-common 0.1.0 → 0.2.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/CHANGELOG.md +155 -0
- package/README.md +327 -9
- package/dist/anchored/anchored.d.ts +19 -0
- package/dist/anchored/anchored.d.ts.map +1 -0
- package/dist/anchored/anchored.js +2 -0
- package/dist/anchored/anchored.js.map +1 -0
- package/dist/focus/typeahead.d.ts +112 -0
- package/dist/focus/typeahead.d.ts.map +1 -0
- package/dist/focus/typeahead.js +178 -0
- package/dist/focus/typeahead.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/menu/menu.d.ts +157 -0
- package/dist/menu/menu.d.ts.map +1 -0
- package/dist/menu/menu.js +162 -0
- package/dist/menu/menu.js.map +1 -0
- package/dist/preact/_anchor.d.ts +13 -0
- package/dist/preact/_anchor.d.ts.map +1 -0
- package/dist/preact/_anchor.js +39 -0
- package/dist/preact/_anchor.js.map +1 -0
- package/dist/preact/_dom.d.ts +14 -0
- package/dist/preact/_dom.d.ts.map +1 -0
- package/dist/preact/_dom.js +31 -0
- package/dist/preact/_dom.js.map +1 -0
- package/dist/preact/_live-announcer.d.ts +26 -0
- package/dist/preact/_live-announcer.d.ts.map +1 -0
- package/dist/preact/_live-announcer.js +101 -0
- package/dist/preact/_live-announcer.js.map +1 -0
- package/dist/preact/index.d.ts +7 -0
- package/dist/preact/index.d.ts.map +1 -1
- package/dist/preact/index.js +7 -0
- package/dist/preact/index.js.map +1 -1
- package/dist/preact/knob.d.ts.map +1 -1
- package/dist/preact/knob.js +5 -13
- package/dist/preact/knob.js.map +1 -1
- package/dist/preact/menu-button.d.ts +117 -0
- package/dist/preact/menu-button.d.ts.map +1 -0
- package/dist/preact/menu-button.js +330 -0
- package/dist/preact/menu-button.js.map +1 -0
- package/dist/preact/progress-ring.d.ts +42 -0
- package/dist/preact/progress-ring.d.ts.map +1 -0
- package/dist/preact/progress-ring.js +26 -0
- package/dist/preact/progress-ring.js.map +1 -0
- package/dist/preact/slider.d.ts.map +1 -1
- package/dist/preact/slider.js +6 -4
- package/dist/preact/slider.js.map +1 -1
- package/dist/preact/spin-button.d.ts +67 -0
- package/dist/preact/spin-button.d.ts.map +1 -0
- package/dist/preact/spin-button.js +132 -0
- package/dist/preact/spin-button.js.map +1 -0
- package/dist/preact/tabs.d.ts +97 -0
- package/dist/preact/tabs.d.ts.map +1 -0
- package/dist/preact/tabs.js +264 -0
- package/dist/preact/tabs.js.map +1 -0
- package/dist/preact/toaster.d.ts +43 -0
- package/dist/preact/toaster.d.ts.map +1 -0
- package/dist/preact/toaster.js +356 -0
- package/dist/preact/toaster.js.map +1 -0
- package/dist/preact/toolbar.d.ts.map +1 -1
- package/dist/preact/toolbar.js +2 -8
- package/dist/preact/toolbar.js.map +1 -1
- package/dist/preact/tooltip.d.ts +67 -0
- package/dist/preact/tooltip.d.ts.map +1 -0
- package/dist/preact/tooltip.js +165 -0
- package/dist/preact/tooltip.js.map +1 -0
- package/dist/preact/tree.d.ts +93 -0
- package/dist/preact/tree.d.ts.map +1 -0
- package/dist/preact/tree.js +399 -0
- package/dist/preact/tree.js.map +1 -0
- package/dist/progress/progress.d.ts +51 -0
- package/dist/progress/progress.d.ts.map +1 -0
- package/dist/progress/progress.js +43 -0
- package/dist/progress/progress.js.map +1 -0
- package/dist/spin-button/spin-button.d.ts +110 -0
- package/dist/spin-button/spin-button.d.ts.map +1 -0
- package/dist/spin-button/spin-button.js +179 -0
- package/dist/spin-button/spin-button.js.map +1 -0
- package/dist/tabs/tabs.d.ts +157 -0
- package/dist/tabs/tabs.d.ts.map +1 -0
- package/dist/tabs/tabs.js +120 -0
- package/dist/tabs/tabs.js.map +1 -0
- package/dist/toast/toast-queue.d.ts +212 -0
- package/dist/toast/toast-queue.d.ts.map +1 -0
- package/dist/toast/toast-queue.js +290 -0
- package/dist/toast/toast-queue.js.map +1 -0
- package/dist/toast/toasts-obscure.d.ts +25 -0
- package/dist/toast/toasts-obscure.d.ts.map +1 -0
- package/dist/toast/toasts-obscure.js +30 -0
- package/dist/toast/toasts-obscure.js.map +1 -0
- package/dist/tooltip/tooltip.d.ts +183 -0
- package/dist/tooltip/tooltip.d.ts.map +1 -0
- package/dist/tooltip/tooltip.js +185 -0
- package/dist/tooltip/tooltip.js.map +1 -0
- package/dist/tree/tree.d.ts +302 -0
- package/dist/tree/tree.d.ts.map +1 -0
- package/dist/tree/tree.js +439 -0
- package/dist/tree/tree.js.map +1 -0
- package/package.json +22 -12
- package/styles.css +894 -3
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
|
|
2
|
+
import { bindPointers } from "@ac-kit/ui-gesture/dom";
|
|
3
|
+
import { useId, useLayoutEffect, useRef, useState } from "preact/hooks";
|
|
4
|
+
import { TOOLTIP_CLOSED, TooltipGroup, tooltipAttributes, tooltipStep, tooltipTriggerAttributes, } from "../tooltip/tooltip.js";
|
|
5
|
+
import { addAnchorName_, anchorNameOf_ } from "./_anchor.js";
|
|
6
|
+
import { joinClasses } from "./join-classes.js";
|
|
7
|
+
import { useElementBinding, useLatest } from "./use-element-binding.js";
|
|
8
|
+
const PAGE_GROUP_ = new TooltipGroup();
|
|
9
|
+
/**
|
|
10
|
+
* A tooltip (WAI-ARIA tooltip pattern) over the Popover API: a short text in
|
|
11
|
+
* the top layer, anchored to its trigger by CSS anchor positioning, flipping
|
|
12
|
+
* and shifting to stay in the viewport. It describes or names the trigger for
|
|
13
|
+
* assistive technology at all times, and shows on a mouse or pen hover after a
|
|
14
|
+
* delay and at once on a keyboard focus. Per WCAG 2.2 success criterion 1.4.13,
|
|
15
|
+
* Escape hides it without moving the pointer or focus, the pointer can move
|
|
16
|
+
* onto it, and it stays while hovered or focused. A press of the trigger hides
|
|
17
|
+
* it. Touch does not hover, so it shows on touch screens only from a keyboard:
|
|
18
|
+
* keep what it says available elsewhere, or let it name an icon.
|
|
19
|
+
*
|
|
20
|
+
* Do not also give the trigger a `title`, which the browser would show too.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* <Tooltip content="Delete" relation="label">
|
|
24
|
+
* {(trigger) => (
|
|
25
|
+
* <IconButton {...trigger} label="Delete" title={undefined}>
|
|
26
|
+
* …
|
|
27
|
+
* </IconButton>
|
|
28
|
+
* )}
|
|
29
|
+
* </Tooltip>;
|
|
30
|
+
*/
|
|
31
|
+
export function Tooltip(props) {
|
|
32
|
+
const { content, relation, side, align, showDelayMs, hideDelayMs, group = PAGE_GROUP_, children, id: givenId, } = props;
|
|
33
|
+
const generatedId = useId();
|
|
34
|
+
const tooltipId = givenId ?? generatedId;
|
|
35
|
+
const anchorName = anchorNameOf_(tooltipId);
|
|
36
|
+
const tooltip = useRef(null);
|
|
37
|
+
const state = useRef(TOOLTIP_CLOSED);
|
|
38
|
+
const timer = useRef(undefined);
|
|
39
|
+
const timing = useLatest({ showDelayMs, hideDelayMs, group });
|
|
40
|
+
// One identity per mounted tooltip, the group's handle on it.
|
|
41
|
+
const [handle] = useState(() => ({
|
|
42
|
+
close: () => {
|
|
43
|
+
dispatch({ kind: "dismiss" });
|
|
44
|
+
},
|
|
45
|
+
}));
|
|
46
|
+
function dispatch(input) {
|
|
47
|
+
const time = performance.now();
|
|
48
|
+
const { group: currentGroup, ...delays } = timing.current;
|
|
49
|
+
const previous = state.current;
|
|
50
|
+
const next = tooltipStep(previous, input, time, {
|
|
51
|
+
...delays,
|
|
52
|
+
isWarm: currentGroup.isWarm(time),
|
|
53
|
+
});
|
|
54
|
+
state.current = next;
|
|
55
|
+
if (next.isOpen !== previous.isOpen) {
|
|
56
|
+
const element = tooltip.current;
|
|
57
|
+
if (next.isOpen) {
|
|
58
|
+
currentGroup.opened(handle.close);
|
|
59
|
+
element?.showPopover();
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
currentGroup.closed(handle.close, time);
|
|
63
|
+
element?.hidePopover();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (next.deadline !== previous.deadline) {
|
|
67
|
+
clearTimeout(timer.current);
|
|
68
|
+
timer.current =
|
|
69
|
+
next.deadline === undefined
|
|
70
|
+
? undefined
|
|
71
|
+
: setTimeout(() => {
|
|
72
|
+
timer.current = undefined;
|
|
73
|
+
dispatch({ kind: "time" });
|
|
74
|
+
}, Math.max(0, next.deadline - time));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const latestDispatch = useLatest(dispatch);
|
|
78
|
+
const trigger = useElementBinding((element, signal) => {
|
|
79
|
+
const send = (input) => {
|
|
80
|
+
latestDispatch.current(input);
|
|
81
|
+
};
|
|
82
|
+
const anchor = addAnchorName_(element, anchorName);
|
|
83
|
+
const pointers = bindPointers(element, {
|
|
84
|
+
onHover: () => {
|
|
85
|
+
if (!state.current.isTriggerHovered) {
|
|
86
|
+
send({ kind: "hover", part: "trigger", isHovered: true });
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
onHoverEnd: (_pointerId, event) => {
|
|
90
|
+
send(event?.type === "pointerdown"
|
|
91
|
+
? { kind: "press" }
|
|
92
|
+
: { kind: "hover", part: "trigger", isHovered: false });
|
|
93
|
+
},
|
|
94
|
+
}, { coordinates: "client", signal });
|
|
95
|
+
element.addEventListener("focus", () => {
|
|
96
|
+
if (element.matches(":focus-visible")) {
|
|
97
|
+
send({ kind: "focus", isFocused: true });
|
|
98
|
+
}
|
|
99
|
+
}, { signal });
|
|
100
|
+
element.addEventListener("blur", () => {
|
|
101
|
+
send({ kind: "focus", isFocused: false });
|
|
102
|
+
}, { signal });
|
|
103
|
+
// A touch, which never hovers, or a press before the show delay ran,
|
|
104
|
+
// hides it too.
|
|
105
|
+
element.addEventListener("pointerdown", () => {
|
|
106
|
+
send({ kind: "press" });
|
|
107
|
+
}, { signal });
|
|
108
|
+
element.ownerDocument.addEventListener("keydown", (event) => {
|
|
109
|
+
if (event.key === "Escape" && state.current.isOpen) {
|
|
110
|
+
// Consumed: a dialog around it stays open.
|
|
111
|
+
event.preventDefault();
|
|
112
|
+
send({ kind: "dismiss" });
|
|
113
|
+
}
|
|
114
|
+
}, { capture: true, signal });
|
|
115
|
+
return {
|
|
116
|
+
[Symbol.dispose]() {
|
|
117
|
+
pointers[Symbol.dispose]();
|
|
118
|
+
anchor[Symbol.dispose]();
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}, [anchorName]);
|
|
122
|
+
const surface = useElementBinding((element, signal) => {
|
|
123
|
+
tooltip.current = element;
|
|
124
|
+
element.style.setProperty("position-anchor", anchorName);
|
|
125
|
+
const pointers = bindPointers(element, {
|
|
126
|
+
onHover: () => {
|
|
127
|
+
if (!state.current.isTooltipHovered) {
|
|
128
|
+
latestDispatch.current({
|
|
129
|
+
kind: "hover",
|
|
130
|
+
part: "tooltip",
|
|
131
|
+
isHovered: true,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
onHoverEnd: () => {
|
|
136
|
+
latestDispatch.current({
|
|
137
|
+
kind: "hover",
|
|
138
|
+
part: "tooltip",
|
|
139
|
+
isHovered: false,
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
}, { coordinates: "client", signal });
|
|
143
|
+
return {
|
|
144
|
+
[Symbol.dispose]() {
|
|
145
|
+
pointers[Symbol.dispose]();
|
|
146
|
+
element.style.removeProperty("position-anchor");
|
|
147
|
+
if (tooltip.current === element) {
|
|
148
|
+
tooltip.current = null;
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}, [anchorName]);
|
|
153
|
+
useLayoutEffect(() => () => {
|
|
154
|
+
clearTimeout(timer.current);
|
|
155
|
+
if (state.current.isOpen) {
|
|
156
|
+
timing.current.group.closed(handle.close, performance.now());
|
|
157
|
+
}
|
|
158
|
+
state.current = TOOLTIP_CLOSED;
|
|
159
|
+
}, []);
|
|
160
|
+
return (_jsxs(_Fragment, { children: [children({
|
|
161
|
+
ref: trigger,
|
|
162
|
+
...tooltipTriggerAttributes({ tooltipId, relation }),
|
|
163
|
+
}), _jsx("div", { ...tooltipAttributes({ side, align }), id: tooltipId, ref: surface, popover: "manual", class: joinClasses("ack-tooltip", props.class), children: content })] }));
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=tooltip.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tooltip.js","sourceRoot":"","sources":["../../src/preact/tooltip.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAGxE,OAAO,EACN,cAAc,EAEd,YAAY,EAEZ,iBAAiB,EACjB,WAAW,EACX,wBAAwB,GACxB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AA2CxE,MAAM,WAAW,GAAG,IAAI,YAAY,EAAE,CAAC;AAEvC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,OAAO,CAAC,KAAmB;IAC1C,MAAM,EACL,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,KAAK,EACL,WAAW,EACX,WAAW,EACX,KAAK,GAAG,WAAW,EACnB,QAAQ,EACR,EAAE,EAAE,OAAO,GACX,GAAG,KAAK,CAAC;IACV,MAAM,WAAW,GAAG,KAAK,EAAE,CAAC;IAC5B,MAAM,SAAS,GAAG,OAAO,IAAI,WAAW,CAAC;IACzC,MAAM,UAAU,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;IAC5C,MAAM,OAA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CAAC;YACF,CAAC;SACD,CAAC;IACH,CAAC,EACD,CAAC,UAAU,CAAC,CACZ,CAAC;IAEF,eAAe,CACd,GAAG,EAAE,CAAC,GAAG,EAAE;QACV,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC5B,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAC1B,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC;QAC9D,CAAC;QACD,KAAK,CAAC,OAAO,GAAG,cAAc,CAAC;IAChC,CAAC,EACD,EAAE,CACF,CAAC;IAEF,OAAO,CACN,8BACE,QAAQ,CAAC;gBACT,GAAG,EAAE,OAAO;gBACZ,GAAG,wBAAwB,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;aACpD,CAAC,EACF,iBACK,iBAAiB,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EACtC,EAAE,EAAE,SAAS,EACb,GAAG,EAAE,OAAO,EACZ,OAAO,EAAC,QAAQ,EAChB,KAAK,EAAE,WAAW,CAAC,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,YAE7C,OAAO,GACH,IACJ,CACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Callable, Predicate } from "@ac-kit/core";
|
|
2
|
+
import { type ComponentChildren, type JSX } from "preact";
|
|
3
|
+
import { type TreeSelectionMode } from "../tree/tree.js";
|
|
4
|
+
/** What a tree item's content is drawn from: its row's state. */
|
|
5
|
+
export type TreeItemState = {
|
|
6
|
+
/** From 1 for a root. */
|
|
7
|
+
readonly level: number;
|
|
8
|
+
readonly isBranch: boolean;
|
|
9
|
+
readonly isExpanded: boolean;
|
|
10
|
+
readonly isSelected: boolean;
|
|
11
|
+
readonly isDisabled: boolean;
|
|
12
|
+
/** Expanded, its children not loaded yet. */
|
|
13
|
+
readonly isLoading: boolean;
|
|
14
|
+
};
|
|
15
|
+
/** Props of {@link Tree}: its own, its accessible name first. */
|
|
16
|
+
export type TreeProps<Node> = ({
|
|
17
|
+
label: string;
|
|
18
|
+
labelledBy?: undefined;
|
|
19
|
+
} | {
|
|
20
|
+
label?: undefined;
|
|
21
|
+
labelledBy: string;
|
|
22
|
+
}) & {
|
|
23
|
+
roots: readonly Node[];
|
|
24
|
+
/**
|
|
25
|
+
* A branch's children; `undefined` for a leaf, or for a branch whose children
|
|
26
|
+
* are not loaded yet. Keep it stable (a module function, or `useCallback`):
|
|
27
|
+
* the rows are recomputed when it changes.
|
|
28
|
+
*/
|
|
29
|
+
childrenOf: Callable<[node: Node], readonly Node[] | undefined>;
|
|
30
|
+
/**
|
|
31
|
+
* Whether a node can be expanded. Defaults to having a child; give it for
|
|
32
|
+
* lazy children, loaded once their branch is expanded. Keep it stable.
|
|
33
|
+
*/
|
|
34
|
+
isBranch?: Predicate<[node: Node]>;
|
|
35
|
+
/** The name typeahead matches, and the content drawn without `renderItem`. */
|
|
36
|
+
textValueOf: Callable<[node: Node], string>;
|
|
37
|
+
/**
|
|
38
|
+
* Draws a node's content. Keep it stable: a row is drawn again only when its
|
|
39
|
+
* state or this function changes.
|
|
40
|
+
*/
|
|
41
|
+
renderItem?: Callable<[node: Node, state: TreeItemState], ComponentChildren>;
|
|
42
|
+
/** The branches shown open. */
|
|
43
|
+
expanded: ReadonlySet<Node>;
|
|
44
|
+
/** Called with the branches to show open. */
|
|
45
|
+
onExpandedChange: Callable<[expanded: ReadonlySet<Node>]>;
|
|
46
|
+
/** Defaults to `single`. */
|
|
47
|
+
selectionMode?: TreeSelectionMode;
|
|
48
|
+
/** The selected nodes. Defaults to none. */
|
|
49
|
+
selected?: ReadonlySet<Node>;
|
|
50
|
+
/** Called with the nodes to select. */
|
|
51
|
+
onSelectedChange?: Callable<[selected: ReadonlySet<Node>]>;
|
|
52
|
+
/**
|
|
53
|
+
* In single selection, select the node focus moves to, as a file browser's
|
|
54
|
+
* preview does. Defaults to false.
|
|
55
|
+
*/
|
|
56
|
+
selectionFollowsFocus?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* A node's default action, on Enter and on a click. Without it, Enter expands
|
|
59
|
+
* or collapses a branch.
|
|
60
|
+
*/
|
|
61
|
+
onActivate?: Callable<[node: Node]>;
|
|
62
|
+
/** Nodes that take focus and expand but are never selected or activated. */
|
|
63
|
+
isDisabled?: Predicate<[node: Node]>;
|
|
64
|
+
id?: string;
|
|
65
|
+
class?: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* A hierarchy of nodes to browse, expand and select (WAI-ARIA tree view
|
|
69
|
+
* pattern), such as a file tree or the elements of a document. One tab stop, on
|
|
70
|
+
* the focused node (at first the first selected node, else the first); Arrow
|
|
71
|
+
* Down and Up move through the shown rows, Home and End to the ends; Arrow
|
|
72
|
+
* Right expands a branch, then moves to its first child; Arrow Left collapses
|
|
73
|
+
* it, or moves to the parent; `*` expands every branch beside the focused one;
|
|
74
|
+
* typing a name moves to it; Enter activates a node, or expands or collapses a
|
|
75
|
+
* branch with no action. Right to left, Arrow Right and Left swap.
|
|
76
|
+
*
|
|
77
|
+
* In single selection, Space or a click selects a node; with
|
|
78
|
+
* `selectionFollowsFocus`, moving focus does. In multiple selection, Space or
|
|
79
|
+
* Ctrl (Cmd) click flips a node, Shift Arrow Down and Up move and flip, Shift
|
|
80
|
+
* Space and Shift click select from the last node selected, Ctrl (Cmd) Shift
|
|
81
|
+
* Home and End to the first and last row, Ctrl (Cmd) A selects every node or
|
|
82
|
+
* none. A click on the expand mark, or a double click on a branch, expands or
|
|
83
|
+
* collapses it. Disabled nodes take focus and expand, but are never selected or
|
|
84
|
+
* activated.
|
|
85
|
+
*
|
|
86
|
+
* The rows are one flat list stating each item's level, position and set size,
|
|
87
|
+
* so a branch's children load lazily: an expanded branch whose children are
|
|
88
|
+
* `undefined` shows as busy until they come. When the focused row goes away
|
|
89
|
+
* (its branch collapsed, its node removed), focus moves to the nearest row
|
|
90
|
+
* still shown. Expansion and selection are the caller's.
|
|
91
|
+
*/
|
|
92
|
+
export declare function Tree<Node>(props: TreeProps<Node>): JSX.Element;
|
|
93
|
+
//# sourceMappingURL=tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../src/preact/tree.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAa,KAAK,iBAAiB,EAAE,KAAK,GAAG,EAAE,MAAM,QAAQ,CAAC;AAUrE,OAAO,EAKN,KAAK,iBAAiB,EAQtB,MAAM,iBAAiB,CAAC;AAKzB,iEAAiE;AACjE,MAAM,MAAM,aAAa,GAAG;IAC3B,yBAAyB;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,6CAA6C;IAC7C,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF,iEAAiE;AACjE,MAAM,MAAM,SAAS,CAAC,IAAI,IAAI,CAC3B;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,SAAS,CAAA;CAAE,GACzC;IAAE,KAAK,CAAC,EAAE,SAAS,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAC3C,GAAG;IACH,KAAK,EAAE,SAAS,IAAI,EAAE,CAAC;IACvB;;;;OAIG;IACH,UAAU,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,SAAS,IAAI,EAAE,GAAG,SAAS,CAAC,CAAC;IAChE;;;OAGG;IACH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|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
|
|
2
|
+
import { Component } from "preact";
|
|
3
|
+
import { useCallback, useLayoutEffect, useMemo, useRef, useState, } from "preact/hooks";
|
|
4
|
+
import { Typeahead } from "../focus/typeahead.js";
|
|
5
|
+
import { expandTreeSiblings, nextTreeSelection, treeAttributes, treeItemAttributes, treeKeyAction, treePath, treeTabStop, visibleTreeRows, } from "../tree/tree.js";
|
|
6
|
+
import { directionOf_, localesOf_ } from "./_dom.js";
|
|
7
|
+
import { joinClasses } from "./join-classes.js";
|
|
8
|
+
import { useLatest } from "./use-element-binding.js";
|
|
9
|
+
const ITEM_SELECTOR_ = '[role="treeitem"]';
|
|
10
|
+
const NO_SELECTION_ = new Set();
|
|
11
|
+
function chevron_() {
|
|
12
|
+
return (_jsx("svg", { viewBox: "0 0 16 16", focusable: "false", children: _jsx("path", { d: "M6 4l4 4-4 4" }) }));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* One row. A class for `shouldComponentUpdate`: a focus move draws again only
|
|
16
|
+
* the two rows whose tab stop or selection changed, not every row.
|
|
17
|
+
*/
|
|
18
|
+
class TreeItem_ extends Component {
|
|
19
|
+
shouldComponentUpdate(next) {
|
|
20
|
+
const current = this.props;
|
|
21
|
+
const { rows, index } = current;
|
|
22
|
+
return (next.node !== current.node ||
|
|
23
|
+
next.selectionMode !== current.selectionMode ||
|
|
24
|
+
next.isSelected !== current.isSelected ||
|
|
25
|
+
next.isTabStop !== current.isTabStop ||
|
|
26
|
+
next.isDisabled !== current.isDisabled ||
|
|
27
|
+
next.textValueOf !== current.textValueOf ||
|
|
28
|
+
next.renderItem !== current.renderItem ||
|
|
29
|
+
next.register !== current.register ||
|
|
30
|
+
next.rows.levelAt(next.index) !== rows.levelAt(index) ||
|
|
31
|
+
next.rows.positionAt(next.index) !== rows.positionAt(index) ||
|
|
32
|
+
next.rows.setSizeAt(next.index) !== rows.setSizeAt(index) ||
|
|
33
|
+
next.rows.isBranchAt(next.index) !== rows.isBranchAt(index) ||
|
|
34
|
+
next.rows.isExpandedAt(next.index) !== rows.isExpandedAt(index) ||
|
|
35
|
+
next.rows.isLoadingAt(next.index) !== rows.isLoadingAt(index));
|
|
36
|
+
}
|
|
37
|
+
render() {
|
|
38
|
+
const { rows, index, node, selectionMode, isSelected, isTabStop, isDisabled, textValueOf, renderItem, register, } = this.props;
|
|
39
|
+
const level = rows.levelAt(index);
|
|
40
|
+
const isBranch = rows.isBranchAt(index);
|
|
41
|
+
const state = {
|
|
42
|
+
level,
|
|
43
|
+
isBranch,
|
|
44
|
+
isExpanded: rows.isExpandedAt(index),
|
|
45
|
+
isSelected,
|
|
46
|
+
isDisabled,
|
|
47
|
+
isLoading: rows.isLoadingAt(index),
|
|
48
|
+
};
|
|
49
|
+
return (_jsxs("li", { ...treeItemAttributes({
|
|
50
|
+
rows,
|
|
51
|
+
index,
|
|
52
|
+
selectionMode,
|
|
53
|
+
isSelected,
|
|
54
|
+
isTabStop,
|
|
55
|
+
isDisabled,
|
|
56
|
+
}), ref: (element) => {
|
|
57
|
+
register(element, node);
|
|
58
|
+
}, class: "ack-tree-item", "data-selected": isSelected ? "" : undefined, style: { "--ack-tree-level": level }, children: [_jsx("span", { class: "ack-tree-item-toggle", "aria-hidden": "true", "data-loading": state.isLoading ? "" : undefined, children: isBranch && chevron_() }), _jsx("span", { class: "ack-tree-item-content", children: renderItem === undefined
|
|
59
|
+
? textValueOf(node)
|
|
60
|
+
: renderItem(node, state) })] }));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A hierarchy of nodes to browse, expand and select (WAI-ARIA tree view
|
|
65
|
+
* pattern), such as a file tree or the elements of a document. One tab stop, on
|
|
66
|
+
* the focused node (at first the first selected node, else the first); Arrow
|
|
67
|
+
* Down and Up move through the shown rows, Home and End to the ends; Arrow
|
|
68
|
+
* Right expands a branch, then moves to its first child; Arrow Left collapses
|
|
69
|
+
* it, or moves to the parent; `*` expands every branch beside the focused one;
|
|
70
|
+
* typing a name moves to it; Enter activates a node, or expands or collapses a
|
|
71
|
+
* branch with no action. Right to left, Arrow Right and Left swap.
|
|
72
|
+
*
|
|
73
|
+
* In single selection, Space or a click selects a node; with
|
|
74
|
+
* `selectionFollowsFocus`, moving focus does. In multiple selection, Space or
|
|
75
|
+
* Ctrl (Cmd) click flips a node, Shift Arrow Down and Up move and flip, Shift
|
|
76
|
+
* Space and Shift click select from the last node selected, Ctrl (Cmd) Shift
|
|
77
|
+
* Home and End to the first and last row, Ctrl (Cmd) A selects every node or
|
|
78
|
+
* none. A click on the expand mark, or a double click on a branch, expands or
|
|
79
|
+
* collapses it. Disabled nodes take focus and expand, but are never selected or
|
|
80
|
+
* activated.
|
|
81
|
+
*
|
|
82
|
+
* The rows are one flat list stating each item's level, position and set size,
|
|
83
|
+
* so a branch's children load lazily: an expanded branch whose children are
|
|
84
|
+
* `undefined` shows as busy until they come. When the focused row goes away
|
|
85
|
+
* (its branch collapsed, its node removed), focus moves to the nearest row
|
|
86
|
+
* still shown. Expansion and selection are the caller's.
|
|
87
|
+
*/
|
|
88
|
+
export function Tree(props) {
|
|
89
|
+
const { roots, childrenOf, isBranch, textValueOf, renderItem, expanded, onExpandedChange, selectionMode = "single", selected = NO_SELECTION_, onSelectedChange, selectionFollowsFocus = false, onActivate, isDisabled, id, } = props;
|
|
90
|
+
const nameInput = props.label === undefined
|
|
91
|
+
? { labelledBy: props.labelledBy }
|
|
92
|
+
: { label: props.label };
|
|
93
|
+
const source = useMemo(() => ({ roots, childrenOf, isBranch }), [roots, childrenOf, isBranch]);
|
|
94
|
+
const rows = useMemo(() => visibleTreeRows(source, (node) => expanded.has(node)), [source, expanded]);
|
|
95
|
+
const [focusedPath, setFocusedPath] = useState([]);
|
|
96
|
+
const tabStop = treeTabStop(rows, focusedPath, selected);
|
|
97
|
+
const tree = useRef(null);
|
|
98
|
+
const nodes = useRef(new WeakMap());
|
|
99
|
+
const typeahead = useRef(new Typeahead());
|
|
100
|
+
const anchor = useRef(undefined);
|
|
101
|
+
const labels = useRef(undefined);
|
|
102
|
+
// The row focus was last on, until focus leaves the tree for another place.
|
|
103
|
+
const focusedRow = useRef(null);
|
|
104
|
+
// What was last reported, until the next render brings the caller's answer:
|
|
105
|
+
// two changes in one event build on each other.
|
|
106
|
+
const reportedSelection = useRef(undefined);
|
|
107
|
+
reportedSelection.current = undefined;
|
|
108
|
+
const latest = useLatest({
|
|
109
|
+
rows,
|
|
110
|
+
source,
|
|
111
|
+
expanded,
|
|
112
|
+
selected,
|
|
113
|
+
selectionMode,
|
|
114
|
+
selectionFollowsFocus,
|
|
115
|
+
isDisabled,
|
|
116
|
+
onExpandedChange,
|
|
117
|
+
onSelectedChange,
|
|
118
|
+
onActivate,
|
|
119
|
+
focusedPath,
|
|
120
|
+
});
|
|
121
|
+
const register = useCallback((element, node) => {
|
|
122
|
+
if (element !== null) {
|
|
123
|
+
nodes.current.set(element, node);
|
|
124
|
+
}
|
|
125
|
+
}, []);
|
|
126
|
+
// Focus on a row that went away moves to the tab stop, the nearest row left.
|
|
127
|
+
useLayoutEffect(() => {
|
|
128
|
+
const element = tree.current;
|
|
129
|
+
const row = focusedRow.current;
|
|
130
|
+
if (element === null || row === null || row.isConnected) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const active = document.activeElement;
|
|
134
|
+
if (active !== null && active !== document.body) {
|
|
135
|
+
focusedRow.current = null;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const stop = element.children[tabStop];
|
|
139
|
+
if (stop instanceof HTMLElement) {
|
|
140
|
+
stop.focus({ preventScroll: true });
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
focusedRow.current = null;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
const rowElementAt = (index) => {
|
|
147
|
+
const row = tree.current?.children[index];
|
|
148
|
+
return row instanceof HTMLElement ? row : undefined;
|
|
149
|
+
};
|
|
150
|
+
const isNodeDisabled = (node) => latest.current.isDisabled?.(node) === true;
|
|
151
|
+
const changeSelection = (change) => {
|
|
152
|
+
const current = latest.current;
|
|
153
|
+
if (current.selectionMode === "none") {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const base = reportedSelection.current ?? current.selected;
|
|
157
|
+
const next = nextTreeSelection(base, change, {
|
|
158
|
+
source: current.source,
|
|
159
|
+
rows: current.rows,
|
|
160
|
+
isDisabled: current.isDisabled,
|
|
161
|
+
});
|
|
162
|
+
if (next !== base) {
|
|
163
|
+
reportedSelection.current = next;
|
|
164
|
+
current.onSelectedChange?.(next);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
const setExpanded = (node, isExpanded) => {
|
|
168
|
+
const current = latest.current.expanded;
|
|
169
|
+
if (current.has(node) === isExpanded) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const next = new Set(current);
|
|
173
|
+
if (isExpanded) {
|
|
174
|
+
next.add(node);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
next.delete(node);
|
|
178
|
+
}
|
|
179
|
+
latest.current.onExpandedChange(next);
|
|
180
|
+
};
|
|
181
|
+
const rowOf = (target) => {
|
|
182
|
+
const row = target instanceof Element
|
|
183
|
+
? target.closest(ITEM_SELECTOR_)
|
|
184
|
+
: null;
|
|
185
|
+
return row !== null && row.parentElement === tree.current ? row : null;
|
|
186
|
+
};
|
|
187
|
+
const indexOfRow = (row) => {
|
|
188
|
+
const node = nodes.current.get(row);
|
|
189
|
+
return node === undefined ? -1 : latest.current.rows.indexOf(node);
|
|
190
|
+
};
|
|
191
|
+
const activate = (index) => {
|
|
192
|
+
const { rows: currentRows, onActivate: action } = latest.current;
|
|
193
|
+
const node = currentRows.nodeAt(index);
|
|
194
|
+
if (action !== undefined) {
|
|
195
|
+
if (!isNodeDisabled(node)) {
|
|
196
|
+
action(node);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else if (currentRows.isBranchAt(index)) {
|
|
200
|
+
setExpanded(node, !currentRows.isExpandedAt(index));
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
return (_jsx("ul", { ...treeAttributes({ ...nameInput, selectionMode }), id: id, ref: tree, class: joinClasses("ack-control ack-tree", props.class), onFocusIn: (event) => {
|
|
204
|
+
const row = rowOf(event.target);
|
|
205
|
+
if (row === null) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
focusedRow.current = row;
|
|
209
|
+
const index = indexOfRow(row);
|
|
210
|
+
if (index === -1) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const current = latest.current;
|
|
214
|
+
const node = current.rows.nodeAt(index);
|
|
215
|
+
if (current.focusedPath.at(-1) !== node) {
|
|
216
|
+
setFocusedPath(treePath(current.rows, index));
|
|
217
|
+
}
|
|
218
|
+
if (current.selectionFollowsFocus &&
|
|
219
|
+
current.selectionMode === "single") {
|
|
220
|
+
changeSelection({ kind: "replace", index });
|
|
221
|
+
}
|
|
222
|
+
}, onFocusOut: (event) => {
|
|
223
|
+
const row = rowOf(event.target);
|
|
224
|
+
const next = event.relatedTarget;
|
|
225
|
+
if (row === null ||
|
|
226
|
+
(next instanceof Node && tree.current?.contains(next) === true)) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
// A row removed while focused may report its blur before it is
|
|
230
|
+
// detached (Chromium): decide once the removal is done, keeping a
|
|
231
|
+
// removed row so its focus is restored.
|
|
232
|
+
queueMicrotask(() => {
|
|
233
|
+
if (focusedRow.current === row && row.isConnected) {
|
|
234
|
+
focusedRow.current = null;
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}, onKeyDown: (event) => {
|
|
238
|
+
const row = rowOf(event.target);
|
|
239
|
+
if (row === null) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const current = indexOfRow(row);
|
|
243
|
+
if (current === -1) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const state = latest.current;
|
|
247
|
+
const action = treeKeyAction({
|
|
248
|
+
key: event.key,
|
|
249
|
+
shiftKey: event.shiftKey,
|
|
250
|
+
ctrlKey: event.ctrlKey,
|
|
251
|
+
altKey: event.altKey,
|
|
252
|
+
metaKey: event.metaKey,
|
|
253
|
+
current,
|
|
254
|
+
rows: state.rows,
|
|
255
|
+
direction: directionOf_(event.currentTarget),
|
|
256
|
+
selectionMode: state.selectionMode,
|
|
257
|
+
isTypingAhead: typeahead.current.isActive(event.timeStamp),
|
|
258
|
+
});
|
|
259
|
+
if (action === undefined) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
event.preventDefault();
|
|
263
|
+
switch (action.kind) {
|
|
264
|
+
case "focus":
|
|
265
|
+
rowElementAt(action.index)?.focus();
|
|
266
|
+
if (action.toggles) {
|
|
267
|
+
anchor.current = state.rows.nodeAt(action.index);
|
|
268
|
+
changeSelection({ kind: "toggle", index: action.index });
|
|
269
|
+
}
|
|
270
|
+
break;
|
|
271
|
+
case "expand":
|
|
272
|
+
setExpanded(state.rows.nodeAt(action.index), true);
|
|
273
|
+
break;
|
|
274
|
+
case "collapse":
|
|
275
|
+
setExpanded(state.rows.nodeAt(action.index), false);
|
|
276
|
+
break;
|
|
277
|
+
case "expandSiblings": {
|
|
278
|
+
const next = expandTreeSiblings(state.source, state.rows, action.index, state.expanded);
|
|
279
|
+
if (next !== state.expanded) {
|
|
280
|
+
state.onExpandedChange(next);
|
|
281
|
+
}
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
case "activate":
|
|
285
|
+
activate(action.index);
|
|
286
|
+
break;
|
|
287
|
+
case "select":
|
|
288
|
+
anchor.current = state.rows.nodeAt(action.index);
|
|
289
|
+
changeSelection({
|
|
290
|
+
kind: state.selectionMode === "multiple" ? "toggle" : "replace",
|
|
291
|
+
index: action.index,
|
|
292
|
+
});
|
|
293
|
+
break;
|
|
294
|
+
case "selectRange": {
|
|
295
|
+
const anchorIndex = anchor.current === undefined
|
|
296
|
+
? -1
|
|
297
|
+
: state.rows.indexOf(anchor.current);
|
|
298
|
+
const from = action.from === "anchor"
|
|
299
|
+
? anchorIndex === -1
|
|
300
|
+
? current
|
|
301
|
+
: anchorIndex
|
|
302
|
+
: action.from;
|
|
303
|
+
changeSelection({
|
|
304
|
+
kind: "range",
|
|
305
|
+
from,
|
|
306
|
+
to: action.to,
|
|
307
|
+
isAdded: true,
|
|
308
|
+
});
|
|
309
|
+
rowElementAt(action.focus)?.focus();
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
case "selectAll":
|
|
313
|
+
changeSelection({ kind: "all" });
|
|
314
|
+
break;
|
|
315
|
+
case "typeahead": {
|
|
316
|
+
let cached = labels.current;
|
|
317
|
+
if (cached?.rows !== state.rows ||
|
|
318
|
+
cached.textValueOf !== textValueOf) {
|
|
319
|
+
const all = [];
|
|
320
|
+
for (let index = 0; index < state.rows.count; index += 1) {
|
|
321
|
+
all.push(textValueOf(state.rows.nodeAt(index)));
|
|
322
|
+
}
|
|
323
|
+
cached = { rows: state.rows, textValueOf, labels: all };
|
|
324
|
+
labels.current = cached;
|
|
325
|
+
}
|
|
326
|
+
const next = typeahead.current.find(action.character, event.timeStamp, cached.labels, current, { locales: localesOf_(event.currentTarget) });
|
|
327
|
+
if (next !== undefined) {
|
|
328
|
+
rowElementAt(next)?.focus();
|
|
329
|
+
}
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}, onClick: (event) => {
|
|
334
|
+
const row = rowOf(event.target);
|
|
335
|
+
if (row === null) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const index = indexOfRow(row);
|
|
339
|
+
if (index === -1) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const state = latest.current;
|
|
343
|
+
if (document.activeElement !== row) {
|
|
344
|
+
row.focus({ preventScroll: true });
|
|
345
|
+
}
|
|
346
|
+
const isToggle = event.target instanceof Element &&
|
|
347
|
+
event.target.closest(".ack-tree-item-toggle") !== null;
|
|
348
|
+
if (isToggle) {
|
|
349
|
+
if (state.rows.isBranchAt(index)) {
|
|
350
|
+
setExpanded(state.rows.nodeAt(index), !state.rows.isExpandedAt(index));
|
|
351
|
+
}
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const node = state.rows.nodeAt(index);
|
|
355
|
+
const command = event.ctrlKey || event.metaKey;
|
|
356
|
+
if (state.selectionMode === "multiple" && (command || event.shiftKey)) {
|
|
357
|
+
if (event.shiftKey) {
|
|
358
|
+
const anchorIndex = anchor.current === undefined
|
|
359
|
+
? -1
|
|
360
|
+
: state.rows.indexOf(anchor.current);
|
|
361
|
+
changeSelection({
|
|
362
|
+
kind: "range",
|
|
363
|
+
from: anchorIndex === -1 ? index : anchorIndex,
|
|
364
|
+
to: index,
|
|
365
|
+
isAdded: command,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
anchor.current = node;
|
|
370
|
+
changeSelection({ kind: "toggle", index });
|
|
371
|
+
}
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
anchor.current = node;
|
|
375
|
+
changeSelection({ kind: "replace", index });
|
|
376
|
+
if (!event.altKey && !event.shiftKey && !command) {
|
|
377
|
+
const action = state.onActivate;
|
|
378
|
+
if (action !== undefined && !isNodeDisabled(node)) {
|
|
379
|
+
action(node);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}, onDblClick: (event) => {
|
|
383
|
+
const row = rowOf(event.target);
|
|
384
|
+
if (row === null ||
|
|
385
|
+
(event.target instanceof Element &&
|
|
386
|
+
event.target.closest(".ack-tree-item-toggle") !== null)) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
const index = indexOfRow(row);
|
|
390
|
+
const state = latest.current;
|
|
391
|
+
if (index !== -1 && state.rows.isBranchAt(index)) {
|
|
392
|
+
setExpanded(state.rows.nodeAt(index), !state.rows.isExpandedAt(index));
|
|
393
|
+
}
|
|
394
|
+
}, children: Array.from({ length: rows.count }, (_unused, index) => {
|
|
395
|
+
const node = rows.nodeAt(index);
|
|
396
|
+
return (_jsx(TreeItem_, { rows: rows, index: index, node: node, selectionMode: selectionMode, isSelected: selected.has(node), isTabStop: index === tabStop, isDisabled: isDisabled?.(node) === true, textValueOf: textValueOf, renderItem: renderItem, register: register }, node));
|
|
397
|
+
}) }));
|
|
398
|
+
}
|
|
399
|
+
//# sourceMappingURL=tree.js.map
|