@primer/react 0.0.0-20260306164031 → 0.0.0-20260306165138

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @primer/react
2
2
 
3
- ## 0.0.0-20260306164031
3
+ ## 0.0.0-20260306165138
4
4
 
5
5
  ### Minor Changes
6
6
 
@@ -26,8 +26,6 @@
26
26
 
27
27
  - [#7588](https://github.com/primer/react/pull/7588) [`e649da3`](https://github.com/primer/react/commit/e649da3c89b38f477fb574acf5bb06a49b41ee9d) Thanks [@lukasoppermann](https://github.com/lukasoppermann)! - ToggleSwitch: Updated with a 1px space around the knob to work better with updated primitives.
28
28
 
29
- - [#7544](https://github.com/primer/react/pull/7544) [`9c24302`](https://github.com/primer/react/commit/9c2430281aba7f606d388a43b619bddf649d1eea) Thanks [@hectahertz](https://github.com/hectahertz)! - perf(TreeView): replace O(n) TreeWalker with O(depth) sibling traversal
30
-
31
29
  ## 38.14.0
32
30
 
33
31
  ### Minor Changes
@@ -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,aAAa,GAAG,WAAW,GAAG,SAAS,CAyDjH;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,GAAG,QAAQ,GAAG,KAAK,CAa/E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,SAAS,CAK/G;AAsFD,wBAAgB,oBAAoB,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"}
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,aAAa,GAAG,WAAW,GAAG,SAAS,CAyDjH;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,GAAG,QAAQ,GAAG,KAAK,CAa/E;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,SAAS,CAwB/G;AAED,wBAAgB,oBAAoB,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]');
@@ -1 +1 @@
1
- {"version":3,"file":"useRefObjectAsForwardedRef.d.ts","sourceRoot":"","sources":["../../src/hooks/useRefObjectAsForwardedRef.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAE,SAAS,EAAC,MAAM,OAAO,CAAA;AAGlD;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAIjH"}
1
+ {"version":3,"file":"useRefObjectAsForwardedRef.d.ts","sourceRoot":"","sources":["../../src/hooks/useRefObjectAsForwardedRef.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAE,SAAS,EAAC,MAAM,OAAO,CAAA;AAGlD;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAEjH"}
@@ -9,7 +9,7 @@ import { useImperativeHandle } from 'react';
9
9
  * **NOTE**: The `refObject` should be passed to the underlying element, NOT the `forwardedRef`.
10
10
  */
11
11
  function useRefObjectAsForwardedRef(forwardedRef, refObject) {
12
- const $ = c(4);
12
+ const $ = c(2);
13
13
  let t0;
14
14
  if ($[0] !== refObject.current) {
15
15
  t0 = () => refObject.current;
@@ -18,15 +18,7 @@ function useRefObjectAsForwardedRef(forwardedRef, refObject) {
18
18
  } else {
19
19
  t0 = $[1];
20
20
  }
21
- let t1;
22
- if ($[2] !== refObject) {
23
- t1 = [refObject];
24
- $[2] = refObject;
25
- $[3] = t1;
26
- } else {
27
- t1 = $[3];
28
- }
29
- useImperativeHandle(forwardedRef, t0, t1);
21
+ useImperativeHandle(forwardedRef, t0);
30
22
  }
31
23
 
32
24
  export { useRefObjectAsForwardedRef };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@primer/react",
3
3
  "type": "module",
4
- "version": "0.0.0-20260306164031",
4
+ "version": "0.0.0-20260306165138",
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",