@primer/react 0.0.0-20260309030723 → 0.0.0-20260309030731

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-20260309030723
3
+ ## 0.0.0-20260309030731
4
4
 
5
5
  ### Minor Changes
6
6
 
@@ -20,8 +20,6 @@
20
20
 
21
21
  - [#7624](https://github.com/primer/react/pull/7624) [`f6d4311`](https://github.com/primer/react/commit/f6d431194d217fbb7d456e58bcbbcbb434896fe1) Thanks [@llastflowers](https://github.com/llastflowers)! - Fix FormControl + SelectPanel accessible name to address SR issues
22
22
 
23
- - [#7549](https://github.com/primer/react/pull/7549) [`b1b5603`](https://github.com/primer/react/commit/b1b5603e2ad27559ce54aef1e413e05ee1fc6c1f) Thanks [@hectahertz](https://github.com/hectahertz)! - perf(Autocomplete): eliminate re-renders on arrow-key navigation by converting highlightedItem to a ref
24
-
25
23
  - [#7617](https://github.com/primer/react/pull/7617) [`79c855a`](https://github.com/primer/react/commit/79c855abeb6f46d3fefafb236f00ea65dfcd1ed4) Thanks [@llastflowers](https://github.com/llastflowers)! - Push margin-top of TimelineBody +1px
26
24
 
27
25
  - [#7635](https://github.com/primer/react/pull/7635) [`74762e2`](https://github.com/primer/react/commit/74762e265a44b4fa46fcb4db8fd5194cb81b14c4) Thanks [@iansan5653](https://github.com/iansan5653)! - Fix(useRefObjectAsForwardedRef): fix ref failing to update when target changes
@@ -32,6 +30,8 @@
32
30
 
33
31
  - [#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.
34
32
 
33
+ - [#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
34
+
35
35
  ## 38.14.0
36
36
 
37
37
  ### Minor Changes
@@ -1 +1 @@
1
- {"version":3,"file":"AutocompleteMenu.d.ts","sourceRoot":"","sources":["../../src/Autocomplete/AutocompleteMenu.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAyD,MAAM,OAAO,CAAA;AAK7E,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,eAAe,CAAA;AAGtD,OAAO,KAAK,EAAC,cAAc,EAAE,YAAY,EAAW,MAAM,gBAAgB,CAAA;AAI1E,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,wBAAwB,CAAA;AAOrD,KAAK,gBAAgB,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,IAAI,CAAA;AAClD,MAAM,MAAM,oBAAoB,GAAG,YAAY,CAAC,mBAAmB,EAAE,IAAI,CAAC,GAAG;IAE3E,aAAa,CAAC,EAAE,KAAK,CAAC,iBAAiB,CAAC,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;IACrG,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,cAAc,CAAC,EAAE,KAAK,CAAC,iBAAiB,CAAC,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;CACvG,CAAA;AAoED,KAAK,qBAAqB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,oBAAoB,GAAG;IAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;CAAC,CAAA;AAE3F,MAAM,MAAM,6BAA6B,CAAC,CAAC,SAAS,qBAAqB,IAAI;IAC3E;;;OAGG;IACH,UAAU,CAAC,EAAE,CAAC,GAAG;QACf,a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,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,OAAO,CAAA;IAE1C;;;OAGG;IACH,KAAK,EAAE,CAAC,EAAE,CAAA;IAEV;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB;;OAEG;IAEH,eAAe,EAAE,MAAM,EAAE,CAAA;IAEzB;;;OAGG;IACH,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,CAAA;IAE5D;;OAEG;IACH,gBAAgB,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAA;IAExC;;OAEG;IACH,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAA;IAEtC;;OAEG;IACH,gBAAgB,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAA;IAEtC;;;;OAIG;IACH,wBAAwB,CAAC,EAAE,KAAK,CAAC,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC,CAAA;IAErE,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAC5B,CAAA;AAUD,iBAAS,gBAAgB,CAAC,CAAC,SAAS,qBAAqB,EAAE,KAAK,EAAE,6BAA6B,CAAC,CAAC,CAAC,qBAqRjG;kBArRQ,gBAAgB;;;;AAyRzB,MAAM,MAAM,qBAAqB,GAAG,cAAc,CAAC,OAAO,gBAAgB,CAAC,CAAA;AAC3E,eAAe,gBAAgB,CAAA"}
@@ -1,4 +1,4 @@
1
- import React, { useContext, useRef, useEffect, useState, useMemo } from 'react';
1
+ import React, { useContext, useRef, useState, useMemo, useEffect } from 'react';
2
2
  import { debounce } from '@github/mini-throttle';
3
3
  import { announce } from '@primer/live-region-element';
4
4
  import { scrollIntoView } from '@primer/behaviors';
@@ -33,41 +33,6 @@ function getdefaultCheckedSelectionChange(setInputValueFn) {
33
33
  };
34
34
  }
35
35
  const isItemSelected = (itemId, selectedItemIds) => selectedItemIds.includes(itemId);
36
-
37
- /**
38
- * Memoized wrapper around ActionList.Item that skips re-rendering when props
39
- * haven't changed. Combined with the `highlightedItem` ref (instead of state),
40
- * arrow-key navigation no longer triggers re-renders of the item list.
41
- */
42
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
- const MemoizedAutocompleteItem = /*#__PURE__*/React.memo(function MemoizedAutocompleteItem({
44
- item
45
- }) {
46
- const {
47
- id,
48
- onAction,
49
- children,
50
- text,
51
- leadingVisual: LeadingVisual,
52
- trailingVisual: TrailingVisual,
53
- role,
54
- ...itemProps
55
- } = item;
56
- // Remove key from itemProps so it doesn't spread onto the DOM
57
- delete itemProps.key;
58
- return /*#__PURE__*/jsxs(ActionList.Item, {
59
- onSelect: () => onAction(item),
60
- ...itemProps,
61
- id: id,
62
- "data-id": id,
63
- role: role,
64
- children: [LeadingVisual && /*#__PURE__*/jsx(ActionList.LeadingVisual, {
65
- children: isElement(LeadingVisual) ? LeadingVisual : /*#__PURE__*/jsx(LeadingVisual, {})
66
- }), children !== null && children !== void 0 ? children : text, TrailingVisual && /*#__PURE__*/jsx(ActionList.TrailingVisual, {
67
- children: isElement(TrailingVisual) ? TrailingVisual : /*#__PURE__*/jsx(TrailingVisual, {})
68
- })]
69
- });
70
- });
71
36
  function getItemById(itemId, items) {
72
37
  return items.find(item => item.id === itemId);
73
38
  }
@@ -120,19 +85,7 @@ function AutocompleteMenu(props) {
120
85
  } = props;
121
86
  const listContainerRef = useRef(null);
122
87
  const allItemsToRenderRef = useRef([]);
123
- // Using a ref instead of state to avoid triggering a re-render of all items
124
- // on every arrow-key press. The active styling was already handled via DOM by
125
- // useFocusZone (data-is-active-descendant) before this change, but the
126
- // previous useState caused React to re-render the entire item list anyway.
127
- const highlightedItemRef = useRef();
128
- const deferredInputValueRef = useRef(deferredInputValue);
129
- const selectedItemIdsRef = useRef(selectedItemIds);
130
- useEffect(() => {
131
- deferredInputValueRef.current = deferredInputValue;
132
- }, [deferredInputValue]);
133
- useEffect(() => {
134
- selectedItemIdsRef.current = selectedItemIds;
135
- }, [selectedItemIds]);
88
+ const [highlightedItem, setHighlightedItem] = useState();
136
89
  const [sortedItemIds, setSortedItemIds] = useState(items.map(({
137
90
  id: itemId
138
91
  }) => itemId));
@@ -142,6 +95,7 @@ function AutocompleteMenu(props) {
142
95
  ...selectableItem,
143
96
  role: 'option',
144
97
  id: selectableItem.id,
98
+ active: (highlightedItem === null || highlightedItem === void 0 ? void 0 : highlightedItem.id) === selectableItem.id,
145
99
  selected: selectionVariant === 'multiple' ? selectedItemIds.includes(selectableItem.id) : undefined,
146
100
  onAction: item => {
147
101
  const otherSelectedItemIds = selectedItemIds.filter(selectedItemId => selectedItemId !== item.id);
@@ -158,7 +112,7 @@ function AutocompleteMenu(props) {
158
112
  }
159
113
  }
160
114
  };
161
- }), [items, selectedItemIds, inputRef, onSelectedChange, selectionVariant, setAutocompleteSuggestion, setInputValue, setShowMenu]);
115
+ }), [highlightedItem, items, selectedItemIds, inputRef, onSelectedChange, selectionVariant, setAutocompleteSuggestion, setInputValue, setShowMenu]);
162
116
  const itemSortOrderData = useMemo(() => sortedItemIds.reduce((acc, curr, i) => {
163
117
  acc[curr] = i;
164
118
  return acc;
@@ -172,6 +126,7 @@ function AutocompleteMenu(props) {
172
126
  ...addNewItem,
173
127
  role: 'option',
174
128
  key: addNewItem.id,
129
+ active: (highlightedItem === null || highlightedItem === void 0 ? void 0 : highlightedItem.id) === addNewItem.id,
175
130
  selected: selectionVariant === 'multiple' ? selectedItemIds.includes(addNewItem.id) : undefined,
176
131
  leadingVisual: () => /*#__PURE__*/jsx(PlusIcon, {}),
177
132
  onAction: item => {
@@ -186,7 +141,7 @@ function AutocompleteMenu(props) {
186
141
  setAutocompleteSuggestion('');
187
142
  }
188
143
  }
189
- }] : [])], [sortedAndFilteredItemsToRender, addNewItem, setAutocompleteSuggestion, selectionVariant, setInputValue, generatedUniqueId, selectedItemIds]);
144
+ }] : [])], [sortedAndFilteredItemsToRender, addNewItem, setAutocompleteSuggestion, selectionVariant, setInputValue, generatedUniqueId, highlightedItem, selectedItemIds]);
190
145
  React.useEffect(() => {
191
146
  allItemsToRenderRef.current = allItemsToRender;
192
147
  });
@@ -204,28 +159,13 @@ function AutocompleteMenu(props) {
204
159
  activeDescendantFocus: inputRef,
205
160
  onActiveDescendantChanged: (current, _previous, directlyActivated) => {
206
161
  activeDescendantRef.current = current || null;
207
-
208
- // Active styling was already handled via DOM before this change:
209
- // useFocusZone sets `data-is-active-descendant` on the active element,
210
- // and ActionList styles that attribute in CSS. The key change is that
211
- // we no longer call setHighlightedItem (state), avoiding re-renders.
212
-
213
162
  if (current) {
214
- var _selectedItem$text;
215
- const currentLi = current.closest('li');
216
163
  const selectedItem = allItemsToRenderRef.current.find(item => {
217
- return item.id === (currentLi === null || currentLi === void 0 ? void 0 : currentLi.getAttribute('data-id'));
164
+ var _current$closest;
165
+ return item.id === ((_current$closest = current.closest('li')) === null || _current$closest === void 0 ? void 0 : _current$closest.getAttribute('data-id'));
218
166
  });
219
- highlightedItemRef.current = selectedItem;
167
+ setHighlightedItem(selectedItem);
220
168
  setIsMenuDirectlyActivated(directlyActivated);
221
-
222
- // Update autocomplete suggestion inline (moved from the useEffect
223
- // that previously depended on highlightedItem state)
224
- if (selectedItem !== null && selectedItem !== void 0 && (_selectedItem$text = selectedItem.text) !== null && _selectedItem$text !== void 0 && _selectedItem$text.startsWith(deferredInputValueRef.current) && !selectedItemIdsRef.current.includes(selectedItem.id)) {
225
- setAutocompleteSuggestion(selectedItem.text);
226
- } else {
227
- setAutocompleteSuggestion('');
228
- }
229
169
  }
230
170
  if (current && customScrollContainerRef && customScrollContainerRef.current && directlyActivated) {
231
171
  scrollIntoView(current, customScrollContainerRef.current, menuScrollMargins);
@@ -235,16 +175,15 @@ function AutocompleteMenu(props) {
235
175
  }
236
176
  }, [loading]);
237
177
  useEffect(() => {
238
- var _highlighted$text;
239
- // When the input value changes, update the autocomplete suggestion based
240
- // on the currently highlighted item (tracked via ref, not state).
241
- const highlighted = highlightedItemRef.current;
242
- if (highlighted !== null && highlighted !== void 0 && (_highlighted$text = highlighted.text) !== null && _highlighted$text !== void 0 && _highlighted$text.startsWith(deferredInputValue) && !selectedItemIds.includes(highlighted.id)) {
243
- setAutocompleteSuggestion(highlighted.text);
178
+ var _highlightedItem$text;
179
+ // Use deferredInputValue to avoid running this effect on every keystroke
180
+ // The Input component guards against stale suggestions
181
+ if (highlightedItem !== null && highlightedItem !== void 0 && (_highlightedItem$text = highlightedItem.text) !== null && _highlightedItem$text !== void 0 && _highlightedItem$text.startsWith(deferredInputValue) && !selectedItemIds.includes(highlightedItem.id)) {
182
+ setAutocompleteSuggestion(highlightedItem.text);
244
183
  } else {
245
184
  setAutocompleteSuggestion('');
246
185
  }
247
- }, [deferredInputValue, selectedItemIds, setAutocompleteSuggestion]);
186
+ }, [highlightedItem, deferredInputValue, selectedItemIds, setAutocompleteSuggestion]);
248
187
  useEffect(() => {
249
188
  const itemIdSortResult = [...sortedItemIds].sort(sortOnCloseFn ? sortOnCloseFn : getDefaultSortFn(itemId => isItemSelected(itemId, selectedItemIds)));
250
189
  const sortResultMatchesState = itemIdSortResult.length === sortedItemIds.length && itemIdSortResult.every((element, index) => element === sortedItemIds[index]);
@@ -276,10 +215,29 @@ function AutocompleteMenu(props) {
276
215
  id: `${id}-listbox`,
277
216
  "aria-labelledby": ariaLabelledBy,
278
217
  children: allItemsToRender.map(item => {
279
- var _item$key;
280
- return /*#__PURE__*/jsx(MemoizedAutocompleteItem, {
281
- item: item
282
- }, (_item$key = item.key) !== null && _item$key !== void 0 ? _item$key : item.id);
218
+ const {
219
+ id,
220
+ onAction,
221
+ children,
222
+ text,
223
+ leadingVisual: LeadingVisual,
224
+ trailingVisual: TrailingVisual,
225
+ key,
226
+ role,
227
+ ...itemProps
228
+ } = item;
229
+ return /*#__PURE__*/jsxs(ActionList.Item, {
230
+ onSelect: () => onAction(item),
231
+ ...itemProps,
232
+ id: id,
233
+ "data-id": id,
234
+ role: role,
235
+ children: [LeadingVisual && /*#__PURE__*/jsx(ActionList.LeadingVisual, {
236
+ children: isElement(LeadingVisual) ? LeadingVisual : /*#__PURE__*/jsx(LeadingVisual, {})
237
+ }), children !== null && children !== void 0 ? children : text, TrailingVisual && /*#__PURE__*/jsx(ActionList.TrailingVisual, {
238
+ children: isElement(TrailingVisual) ? TrailingVisual : /*#__PURE__*/jsx(TrailingVisual, {})
239
+ })]
240
+ }, key !== null && key !== void 0 ? key : id);
283
241
  })
284
242
  }) : emptyStateText !== false && emptyStateText !== null ? /*#__PURE__*/jsx("div", {
285
243
  className: classes.EmptyStateWrapper,
@@ -5,6 +5,20 @@ 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
+ */
8
22
  export declare function getVisibleElement(element: HTMLElement, direction: 'next' | 'previous'): HTMLElement | undefined;
9
23
  export declare function getFirstChildElement(element: HTMLElement): HTMLElement | undefined;
10
24
  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,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AG,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"}
@@ -123,25 +123,103 @@ 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
+ */
126
141
  function getVisibleElement(element, direction) {
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();
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;
138
159
 
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();
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
+ }
175
+
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
+ }
143
221
  }
144
- return next instanceof HTMLElement ? next : undefined;
222
+ return undefined;
145
223
  }
146
224
  function getFirstChildElement(element) {
147
225
  const firstChild = element.querySelector('[role=treeitem]');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@primer/react",
3
3
  "type": "module",
4
- "version": "0.0.0-20260309030723",
4
+ "version": "0.0.0-20260309030731",
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",