@primer/react 0.0.0-20260302143751 → 0.0.0-20260302165340

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.
@@ -5,20 +5,6 @@ export declare function useRovingTabIndex({ containerRef, mouseDownRef, }: {
5
5
  }): void;
6
6
  export declare function getNextFocusableElement(activeElement: HTMLElement, event: KeyboardEvent): HTMLElement | undefined;
7
7
  export declare function getElementState(element: HTMLElement): 'open' | 'closed' | 'end';
8
- /**
9
- * Find the next or previous visible treeitem using direct DOM traversal.
10
- *
11
- * PERFORMANCE: This is O(tree depth) instead of O(n) because it walks
12
- * siblings and parent/child edges directly, rather than creating a TreeWalker
13
- * that scans from the root to find the current element on every keystroke.
14
- *
15
- * NOTE: This relies on TreeView.SubTree unmounting its children when collapsed
16
- * (returning null when !isExpanded). Because collapsed subtree children are
17
- * never in the DOM, we can safely skip them by only entering children of nodes
18
- * with aria-expanded="true". If SubTree ever changes to keep collapsed children
19
- * mounted (e.g. via CSS display:none), this logic would need to add filtering
20
- * for items inside collapsed parents.
21
- */
22
8
  export declare function getVisibleElement(element: HTMLElement, direction: 'next' | 'previous'): HTMLElement | undefined;
23
9
  export declare function getFirstChildElement(element: HTMLElement): HTMLElement | undefined;
24
10
  export declare function getParentElement(element: HTMLElement): HTMLElement | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"useRovingTabIndex.d.ts","sourceRoot":"","sources":["../../src/TreeView/useRovingTabIndex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAI9B,wBAAgB,iBAAiB,CAAC,EAChC,YAAY,EACZ,YAAY,GACb,EAAE;IACD,YAAY,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAA;IAC1C,YAAY,EAAE,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;CACvC,QAwDA;AAID,wBAAgB,uBAAuB,CAAC,aAAa,EAAE,WAAW,EAAE,KAAK,EAAE,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CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,GAAG,SAAS,CAGlF;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,GAAG,SAAS,CAI9E;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,GAAG,SAAS,CAI7E;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,GAAG,SAAS,CAuB5E"}
@@ -123,103 +123,25 @@ function getElementState(element) {
123
123
  return 'end';
124
124
  }
125
125
  }
126
-
127
- /**
128
- * Find the next or previous visible treeitem using direct DOM traversal.
129
- *
130
- * PERFORMANCE: This is O(tree depth) instead of O(n) because it walks
131
- * siblings and parent/child edges directly, rather than creating a TreeWalker
132
- * that scans from the root to find the current element on every keystroke.
133
- *
134
- * NOTE: This relies on TreeView.SubTree unmounting its children when collapsed
135
- * (returning null when !isExpanded). Because collapsed subtree children are
136
- * never in the DOM, we can safely skip them by only entering children of nodes
137
- * with aria-expanded="true". If SubTree ever changes to keep collapsed children
138
- * mounted (e.g. via CSS display:none), this logic would need to add filtering
139
- * for items inside collapsed parents.
140
- */
141
126
  function getVisibleElement(element, direction) {
142
- if (direction === 'next') {
143
- return getNextVisibleElement(element);
144
- }
145
- return getPreviousVisibleElement(element);
146
- }
147
- function getNextVisibleElement(element) {
148
- // If the current item is expanded, the next visible item is its first child
149
- if (element.getAttribute('aria-expanded') === 'true') {
150
- const firstChild = getFirstChildElement(element);
151
- if (firstChild) return firstChild;
152
- }
153
-
154
- // Otherwise, walk up the tree looking for a next sibling
155
- let current = element;
156
- while (current) {
157
- const next = getNextSiblingTreeItem(current);
158
- if (next) return next;
159
-
160
- // No next sibling at this level, try the parent's next sibling
161
- current = getParentElement(current);
162
- }
163
- return undefined;
164
- }
165
- function getPreviousVisibleElement(element) {
166
- const prev = getPreviousSiblingTreeItem(element);
167
- if (prev) {
168
- // Navigate to the deepest last visible descendant of the previous sibling
169
- return getDeepestLastDescendant(prev);
170
- }
171
-
172
- // No previous sibling, the parent is the previous visible element
173
- return getParentElement(element);
174
- }
127
+ const root = element.closest('[role=tree]');
128
+ if (!root) return;
129
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, node => {
130
+ if (!(node instanceof HTMLElement)) return NodeFilter.FILTER_SKIP;
131
+ return node.getAttribute('role') === 'treeitem' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
132
+ });
133
+ let current = walker.firstChild();
134
+ while (current !== element) {
135
+ current = walker.nextNode();
136
+ }
137
+ let next = direction === 'next' ? walker.nextNode() : walker.previousNode();
175
138
 
176
- /**
177
- * Walk into expanded subtrees to find the deepest last visible descendant.
178
- * For example, if the last sibling is an expanded directory whose last child
179
- * is also an expanded directory, we drill all the way down.
180
- */
181
- function getDeepestLastDescendant(element) {
182
- let current = element;
183
- while (current.getAttribute('aria-expanded') === 'true') {
184
- const lastChild = getLastChildTreeItem(current);
185
- if (!lastChild) break;
186
- current = lastChild;
187
- }
188
- return current;
189
- }
190
- function getNextSiblingTreeItem(element) {
191
- let sibling = element.nextElementSibling;
192
- while (sibling) {
193
- if (sibling instanceof HTMLElement && sibling.getAttribute('role') === 'treeitem') {
194
- return sibling;
195
- }
196
- sibling = sibling.nextElementSibling;
197
- }
198
- return undefined;
199
- }
200
- function getPreviousSiblingTreeItem(element) {
201
- let sibling = element.previousElementSibling;
202
- while (sibling) {
203
- if (sibling instanceof HTMLElement && sibling.getAttribute('role') === 'treeitem') {
204
- return sibling;
205
- }
206
- sibling = sibling.previousElementSibling;
207
- }
208
- return undefined;
209
- }
210
- function getLastChildTreeItem(element) {
211
- // Find the [role=group] child (the subtree container), then get its last treeitem
212
- for (let i = element.children.length - 1; i >= 0; i--) {
213
- const child = element.children[i];
214
- if (child instanceof HTMLElement && child.getAttribute('role') === 'group') {
215
- let lastChild = child.lastElementChild;
216
- while (lastChild && !(lastChild instanceof HTMLElement && lastChild.getAttribute('role') === 'treeitem')) {
217
- lastChild = lastChild.previousElementSibling;
218
- }
219
- return lastChild instanceof HTMLElement ? lastChild : undefined;
220
- }
139
+ // If next element is nested inside a collapsed subtree, continue iterating
140
+ while (next instanceof HTMLElement && (_next$parentElement = next.parentElement) !== null && _next$parentElement !== void 0 && _next$parentElement.closest('[role=treeitem][aria-expanded=false]')) {
141
+ var _next$parentElement;
142
+ next = direction === 'next' ? walker.nextNode() : walker.previousNode();
221
143
  }
222
- return undefined;
144
+ return next instanceof HTMLElement ? next : undefined;
223
145
  }
224
146
  function getFirstChildElement(element) {
225
147
  const firstChild = element.querySelector('[role=treeitem]');
@@ -0,0 +1,32 @@
1
+ import { type Dispatch, type ReactNode } from 'react';
2
+ export interface ProviderProps<T> {
3
+ children: ReactNode;
4
+ /** State setter from `useRegistryState`. */
5
+ setRegistry: Dispatch<React.SetStateAction<ReadonlyMap<string, T> | undefined>>;
6
+ }
7
+ /**
8
+ * Create a "descendant registry" for a component. This allows a parent to store and track an ordered registry of
9
+ * child components, even if they are deeply nested in the tree. For example, a menu component can use this to track
10
+ * every menu item inside of it, including menu items inside of fragments and subcomponents. The order of the resulting
11
+ * `Map` will match the render order in the React tree.
12
+ *
13
+ * Usage:
14
+ * 1. Create a registry at the file level in a similar manner to creating a new React context. Set `T` to the data type
15
+ * of the registry - this can be anything **except** a function.
16
+ * 2. In the parent component, instantiate the registry state via `useRegistryState`. Then wrap `children` in the
17
+ * registry context `Provider` with `setRegistry` set to the state setter function.
18
+ * 3. Register child components via `useRegisterDescendant` or `useRegisterDescendantCallback`.
19
+ * 4. Access the registered data using the value from `useRegistryState`. This will be a map of `string` to `T`, where
20
+ * the string key is a unique and stable identifier for each component which can be used as a `key` if necessary.
21
+ *
22
+ * @note Note that this pattern is not SSR compatible. It won't raise errors or hydration mismatches, but the
23
+ * registry will not be available during SSR. The registry is built during the effect phase, so it will be populated
24
+ * after hydration on the client. The initial `undefined` value can be used to safely show loading UI during SSR/initial
25
+ * render if necessary.
26
+ */
27
+ export declare function createDescendantRegistry<T>(): {
28
+ Provider: ({ children, setRegistry }: ProviderProps<T>) => import("react").JSX.Element;
29
+ useRegistryState: () => [ReadonlyMap<string, T> | undefined, Dispatch<import("react").SetStateAction<ReadonlyMap<string, T> | undefined>>];
30
+ useRegisterDescendant: (value: T) => string;
31
+ };
32
+ //# sourceMappingURL=descendant-registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"descendant-registry.d.ts","sourceRoot":"","sources":["../../src/utils/descendant-registry.tsx"],"names":[],"mappings":"AAAA,OAAO,EAUL,KAAK,QAAQ,EACb,KAAK,SAAS,EACf,MAAM,OAAO,CAAA;AAGd,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,QAAQ,EAAE,SAAS,CAAA;IACnB,4CAA4C;IAC5C,WAAW,EAAE,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAA;CAChF;AAQD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,wBAAwB,CAAC,CAAC;0CAgCG,aAAa,CAAC,CAAC,CAAC;;mCAfrB,CAAC;EA8FxC"}
@@ -0,0 +1,222 @@
1
+ import { c } from 'react-compiler-runtime';
2
+ import { useContext, useId, useEffect, useState, useRef, useReducer, createContext } from 'react';
3
+ import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect.js';
4
+ import { jsx } from 'react/jsx-runtime';
5
+
6
+ /**
7
+ * Create a "descendant registry" for a component. This allows a parent to store and track an ordered registry of
8
+ * child components, even if they are deeply nested in the tree. For example, a menu component can use this to track
9
+ * every menu item inside of it, including menu items inside of fragments and subcomponents. The order of the resulting
10
+ * `Map` will match the render order in the React tree.
11
+ *
12
+ * Usage:
13
+ * 1. Create a registry at the file level in a similar manner to creating a new React context. Set `T` to the data type
14
+ * of the registry - this can be anything **except** a function.
15
+ * 2. In the parent component, instantiate the registry state via `useRegistryState`. Then wrap `children` in the
16
+ * registry context `Provider` with `setRegistry` set to the state setter function.
17
+ * 3. Register child components via `useRegisterDescendant` or `useRegisterDescendantCallback`.
18
+ * 4. Access the registered data using the value from `useRegistryState`. This will be a map of `string` to `T`, where
19
+ * the string key is a unique and stable identifier for each component which can be used as a `key` if necessary.
20
+ *
21
+ * @note Note that this pattern is not SSR compatible. It won't raise errors or hydration mismatches, but the
22
+ * registry will not be available during SSR. The registry is built during the effect phase, so it will be populated
23
+ * after hydration on the client. The initial `undefined` value can be used to safely show loading UI during SSR/initial
24
+ * render if necessary.
25
+ */
26
+ function createDescendantRegistry() {
27
+ const Context = /*#__PURE__*/createContext({
28
+ register: () => () => {},
29
+ updateValue: () => {},
30
+ key: -1
31
+ });
32
+
33
+ /**
34
+ * Instantiate descendant registry state. The initial value will be `undefined`, indicating that the registry hasn't
35
+ * been built yet.
36
+ */
37
+ function useRegistryState() {
38
+ return useState();
39
+ }
40
+
41
+ /** Register a descendant component with the registry. */
42
+ function useRegisterDescendant(value) {
43
+ const $ = c(16);
44
+ const {
45
+ register,
46
+ updateValue,
47
+ key
48
+ } = useContext(Context);
49
+ const id = useId();
50
+ let t0;
51
+ if ($[0] !== id || $[1] !== register) {
52
+ t0 = () => register(id);
53
+ $[0] = id;
54
+ $[1] = register;
55
+ $[2] = t0;
56
+ } else {
57
+ t0 = $[2];
58
+ }
59
+ let t1;
60
+ if ($[3] !== id || $[4] !== key || $[5] !== register) {
61
+ t1 = [register, id, key];
62
+ $[3] = id;
63
+ $[4] = key;
64
+ $[5] = register;
65
+ $[6] = t1;
66
+ } else {
67
+ t1 = $[6];
68
+ }
69
+ useEffect(t0, t1);
70
+ let t2;
71
+ if ($[7] !== id || $[8] !== updateValue || $[9] !== value) {
72
+ t2 = () => updateValue(id, value);
73
+ $[7] = id;
74
+ $[8] = updateValue;
75
+ $[9] = value;
76
+ $[10] = t2;
77
+ } else {
78
+ t2 = $[10];
79
+ }
80
+ let t3;
81
+ if ($[11] !== id || $[12] !== key || $[13] !== updateValue || $[14] !== value) {
82
+ t3 = [updateValue, id, value, key];
83
+ $[11] = id;
84
+ $[12] = key;
85
+ $[13] = updateValue;
86
+ $[14] = value;
87
+ $[15] = t3;
88
+ } else {
89
+ t3 = $[15];
90
+ }
91
+ useEffect(t2, t3);
92
+ return id;
93
+ }
94
+ const unsetValue = Symbol('unset');
95
+
96
+ /** Provide context for registering descendant components. This only needs to wrap `children`. */
97
+ function Provider(t0) {
98
+ const $ = c(14);
99
+ const {
100
+ children,
101
+ setRegistry
102
+ } = t0;
103
+ const workingRegistryRef = useRef("queued");
104
+ const [key, rebuildRegistry] = useReducer(_temp, 0);
105
+ let t1;
106
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
107
+ t1 = function instantiateNewRegistry() {
108
+ if (workingRegistryRef.current === "queued") {
109
+ workingRegistryRef.current = new Map();
110
+ }
111
+ };
112
+ $[0] = t1;
113
+ } else {
114
+ t1 = $[0];
115
+ }
116
+ useIsomorphicLayoutEffect(t1);
117
+ let t2;
118
+ if ($[1] !== setRegistry) {
119
+ t2 = id => {
120
+ if (workingRegistryRef.current instanceof Map) {
121
+ workingRegistryRef.current.set(id, unsetValue);
122
+ } else {
123
+ if (workingRegistryRef.current === "idle") {
124
+ workingRegistryRef.current = "queued";
125
+ rebuildRegistry();
126
+ }
127
+ }
128
+ return function unregister() {
129
+ if (workingRegistryRef.current instanceof Map) {
130
+ workingRegistryRef.current.delete(id);
131
+ } else {
132
+ if (workingRegistryRef.current === "idle") {
133
+ setRegistry(prev_0 => {
134
+ const copy = new Map(prev_0);
135
+ copy.delete(id);
136
+ return copy;
137
+ });
138
+ }
139
+ }
140
+ };
141
+ };
142
+ $[1] = setRegistry;
143
+ $[2] = t2;
144
+ } else {
145
+ t2 = $[2];
146
+ }
147
+ const register = t2;
148
+ let t3;
149
+ if ($[3] !== setRegistry) {
150
+ t3 = (id_0, value) => {
151
+ if (workingRegistryRef.current instanceof Map) {
152
+ workingRegistryRef.current.set(id_0, value);
153
+ } else {
154
+ if (workingRegistryRef.current === "idle") {
155
+ setRegistry(prev_1 => new Map(prev_1).set(id_0, value));
156
+ }
157
+ }
158
+ };
159
+ $[3] = setRegistry;
160
+ $[4] = t3;
161
+ } else {
162
+ t3 = $[4];
163
+ }
164
+ const updateValue = t3;
165
+ let t4;
166
+ if ($[5] !== setRegistry) {
167
+ t4 = function commitWorkingRegistry() {
168
+ if (workingRegistryRef.current instanceof Map) {
169
+ const setEntries = Array.from(workingRegistryRef.current.entries()).filter(_temp2);
170
+ setRegistry(new Map(setEntries));
171
+ workingRegistryRef.current = "idle";
172
+ }
173
+ };
174
+ $[5] = setRegistry;
175
+ $[6] = t4;
176
+ } else {
177
+ t4 = $[6];
178
+ }
179
+ useEffect(t4);
180
+ let t5;
181
+ if ($[7] !== key || $[8] !== register || $[9] !== updateValue) {
182
+ t5 = {
183
+ register,
184
+ updateValue,
185
+ key
186
+ };
187
+ $[7] = key;
188
+ $[8] = register;
189
+ $[9] = updateValue;
190
+ $[10] = t5;
191
+ } else {
192
+ t5 = $[10];
193
+ }
194
+ const contextValue = t5;
195
+ let t6;
196
+ if ($[11] !== children || $[12] !== contextValue) {
197
+ t6 = /*#__PURE__*/jsx(Context.Provider, {
198
+ value: contextValue,
199
+ children: children
200
+ });
201
+ $[11] = children;
202
+ $[12] = contextValue;
203
+ $[13] = t6;
204
+ } else {
205
+ t6 = $[13];
206
+ }
207
+ return t6;
208
+ }
209
+ function _temp2(entry) {
210
+ return entry[1] !== unsetValue;
211
+ }
212
+ function _temp(prev) {
213
+ return prev + 1;
214
+ }
215
+ return {
216
+ Provider,
217
+ useRegistryState,
218
+ useRegisterDescendant
219
+ };
220
+ }
221
+
222
+ export { createDescendantRegistry };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@primer/react",
3
3
  "type": "module",
4
- "version": "0.0.0-20260302143751",
4
+ "version": "0.0.0-20260302165340",
5
5
  "description": "An implementation of GitHub's Primer Design System using React",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",