@primer/react 0.0.0-20260220174352 → 0.0.0-20260220185847

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.
@@ -1,6 +1,6 @@
1
1
  import { c } from 'react-compiler-runtime';
2
2
  import { scrollIntoView, FocusKeys } from '@primer/behaviors';
3
- import { useCallback, useRef, useState, useEffect, forwardRef } from 'react';
3
+ import { useCallback, useRef, useState, useEffect, useMemo, forwardRef } from 'react';
4
4
  import { ActionList } from '../ActionList/index.js';
5
5
  import { useFocusZone } from '../hooks/useFocusZone.js';
6
6
  import { useId } from '../hooks/useId.js';
@@ -13,6 +13,7 @@ import { ActionListContainerContext } from '../ActionList/ActionListContainerCon
13
13
  import { isValidElementType } from 'react-is';
14
14
  import { useAnnouncements } from './useAnnouncements.js';
15
15
  import { clsx } from 'clsx';
16
+ import { useVirtualizer } from '@tanstack/react-virtual';
16
17
  import { jsxs, jsx } from 'react/jsx-runtime';
17
18
  import TextInput from '../TextInput/TextInput.js';
18
19
  import { VisuallyHidden } from '../VisuallyHidden/VisuallyHidden.js';
@@ -51,8 +52,19 @@ function FilteredActionList({
51
52
  setInitialFocus = false,
52
53
  focusPrependedElements,
53
54
  scrollBehavior,
55
+ virtualized = false,
54
56
  ...listProps
55
57
  }) {
58
+ if (process.env.NODE_ENV !== "production") {
59
+ if (virtualized && groupMetadata !== null && groupMetadata !== void 0 && groupMetadata.length) {
60
+ // eslint-disable-next-line no-console
61
+ console.warn('FilteredActionList: `virtualized` has no effect when `groupMetadata` is provided. ' + 'Grouped lists are rendered without virtualization.');
62
+ }
63
+ }
64
+
65
+ // Virtualization is disabled when groups are present — grouped lists render
66
+ // normally regardless of the `virtualized` prop.
67
+ const isVirtualized = virtualized && !(groupMetadata !== null && groupMetadata !== void 0 && groupMetadata.length);
56
68
  const [filterValue, setInternalFilterValue] = useProvidedStateOrCreate(externalFilterValue, undefined, '');
57
69
  const onInputChange = useCallback(e => {
58
70
  const value = e.target.value;
@@ -130,18 +142,61 @@ function FilteredActionList({
130
142
  useEffect(() => {
131
143
  onInputRefChanged === null || onInputRefChanged === void 0 ? void 0 : onInputRefChanged(inputRef);
132
144
  }, [inputRef, onInputRefChanged]);
145
+
146
+ // Matches the most common ActionList.Item height (single-line text + description).
147
+ // Items are measured dynamically via `measureElement`, so this only affects the
148
+ // initial total-height estimate before items scroll into view.
149
+ const DEFAULT_VIRTUAL_ITEM_HEIGHT = 32;
150
+ const virtualizer = useVirtualizer({
151
+ count: items.length,
152
+ getScrollElement: () => scrollContainerRef.current,
153
+ estimateSize: () => DEFAULT_VIRTUAL_ITEM_HEIGHT,
154
+ overscan: 10,
155
+ enabled: isVirtualized,
156
+ getItemKey: index => {
157
+ var _ref, _item_3$key, _item_3$id;
158
+ const item_3 = items[index];
159
+ return (_ref = (_item_3$key = item_3.key) !== null && _item_3$key !== void 0 ? _item_3$key : (_item_3$id = item_3.id) === null || _item_3$id === void 0 ? void 0 : _item_3$id.toString()) !== null && _ref !== void 0 ? _ref : index.toString();
160
+ },
161
+ measureElement: el => el.scrollHeight
162
+ });
163
+ const virtualItems = isVirtualized ? virtualizer.getVirtualItems() : undefined;
164
+ const virtualizedItemEntries = useMemo(() => {
165
+ if (!isVirtualized || !virtualItems) return undefined;
166
+ return virtualItems.map(virtualItem => {
167
+ const item_4 = items[virtualItem.index];
168
+ return {
169
+ virtualItem,
170
+ item: item_4,
171
+ index: virtualItem.index
172
+ };
173
+ });
174
+ }, [isVirtualized, virtualItems, items]);
133
175
  useFocusZone(!usingRovingTabindex ? {
134
176
  containerRef: {
135
177
  current: listContainerElement
136
178
  },
137
179
  bindKeys: FocusKeys.ArrowVertical | FocusKeys.PageUpDown,
138
- focusOutBehavior,
180
+ // With virtualization, only a subset of items exists in the DOM at any time.
181
+ // 'wrap' would cycle focus within the visible window instead of reaching the
182
+ // true end of the list. 'stop' lets the virtualizer's scrollToIndex bring
183
+ // the correct items into view when navigating past the rendered boundaries.
184
+ focusOutBehavior: isVirtualized ? 'stop' : focusOutBehavior,
139
185
  focusableElementFilter: element => {
140
186
  return !(element instanceof HTMLInputElement);
141
187
  },
142
188
  activeDescendantFocus: inputRef,
143
189
  onActiveDescendantChanged: (current, previous, directlyActivated) => {
144
190
  activeDescendantRef.current = current;
191
+ if (isVirtualized && current) {
192
+ const index_0 = current.getAttribute('data-index');
193
+ const range = virtualizer.range;
194
+ if (index_0 !== null && range && (Number(index_0) < range.startIndex || Number(index_0) >= range.endIndex)) {
195
+ virtualizer.scrollToIndex(Number(index_0), {
196
+ align: 'auto'
197
+ });
198
+ }
199
+ }
145
200
  if (current && scrollContainerRef.current && (directlyActivated || focusPrependedElements)) {
146
201
  scrollIntoView(current, scrollContainerRef.current, {
147
202
  ...menuScrollMargins,
@@ -153,7 +208,7 @@ function FilteredActionList({
153
208
  focusInStrategy: setInitialFocus ? 'initial' : 'previous',
154
209
  ignoreHoverEvents: disableSelectOnHover,
155
210
  focusPrependedElements
156
- } : undefined, [listContainerElement, usingRovingTabindex, onActiveDescendantChanged, focusPrependedElements]);
211
+ } : undefined, [listContainerElement, usingRovingTabindex, onActiveDescendantChanged, focusPrependedElements, isVirtualized]);
157
212
  useEffect(() => {
158
213
  if (activeDescendantRef.current && scrollContainerRef.current) {
159
214
  scrollIntoView(activeDescendantRef.current, scrollContainerRef.current, {
@@ -207,6 +262,82 @@ function FilteredActionList({
207
262
  return message;
208
263
  }
209
264
  let firstGroupIndex_0 = 0;
265
+ const renderListItems = () => {
266
+ if (groupMetadata !== null && groupMetadata !== void 0 && groupMetadata.length) {
267
+ return groupMetadata.map((group, index_1) => {
268
+ var _group$header, _group$header2;
269
+ if (index_1 === firstGroupIndex_0 && getItemListForEachGroup(group.groupId).length === 0) {
270
+ firstGroupIndex_0++;
271
+ }
272
+ return /*#__PURE__*/jsxs(ActionList.Group, {
273
+ children: [/*#__PURE__*/jsx(ActionList.GroupHeading, {
274
+ variant: (_group$header = group.header) !== null && _group$header !== void 0 && _group$header.variant ? group.header.variant : undefined,
275
+ children: (_group$header2 = group.header) !== null && _group$header2 !== void 0 && _group$header2.title ? group.header.title : `Group ${group.groupId}`
276
+ }), getItemListForEachGroup(group.groupId).map(({
277
+ key: itemKey,
278
+ ...item_5
279
+ }, itemIndex) => {
280
+ var _ref2, _item_5$id;
281
+ const key = (_ref2 = itemKey !== null && itemKey !== void 0 ? itemKey : (_item_5$id = item_5.id) === null || _item_5$id === void 0 ? void 0 : _item_5$id.toString()) !== null && _ref2 !== void 0 ? _ref2 : itemIndex.toString();
282
+ return /*#__PURE__*/jsx(MappedActionListItem, {
283
+ className: clsx(classes.ActionListItem, 'className' in item_5 ? item_5.className : undefined),
284
+ "data-input-focused": isInputFocused ? '' : undefined,
285
+ "data-first-child": index_1 === firstGroupIndex_0 && itemIndex === 0 ? '' : undefined,
286
+ ...item_5,
287
+ renderItem: listProps.renderItem
288
+ }, key);
289
+ })]
290
+ }, index_1);
291
+ });
292
+ }
293
+ if (isVirtualized && virtualizedItemEntries) {
294
+ return virtualizedItemEntries.map(({
295
+ virtualItem: virtualItem_0,
296
+ item: {
297
+ key: itemKey_0,
298
+ ...item_6
299
+ },
300
+ index: index_2
301
+ }) => {
302
+ var _ref3, _item_6$id;
303
+ const key_0 = (_ref3 = itemKey_0 !== null && itemKey_0 !== void 0 ? itemKey_0 : (_item_6$id = item_6.id) === null || _item_6$id === void 0 ? void 0 : _item_6$id.toString()) !== null && _ref3 !== void 0 ? _ref3 : index_2.toString();
304
+ return /*#__PURE__*/jsx(MappedActionListItem, {
305
+ className: clsx(classes.ActionListItem, 'className' in item_6 ? item_6.className : undefined),
306
+ "data-input-focused": isInputFocused ? '' : undefined,
307
+ "data-first-child": index_2 === 0 ? '' : undefined,
308
+ "data-index": virtualItem_0.index,
309
+ ref: node_0 => {
310
+ if (node_0) {
311
+ virtualizer.measureElement(node_0);
312
+ }
313
+ },
314
+ style: {
315
+ position: 'absolute',
316
+ top: 0,
317
+ left: 0,
318
+ right: 0,
319
+ transform: `translateY(${virtualItem_0.start}px)`
320
+ },
321
+ ...item_6,
322
+ renderItem: listProps.renderItem
323
+ }, key_0);
324
+ });
325
+ }
326
+ return items.map(({
327
+ key: itemKey_1,
328
+ ...item_7
329
+ }, index_3) => {
330
+ var _ref4, _item_7$id;
331
+ const key_1 = (_ref4 = itemKey_1 !== null && itemKey_1 !== void 0 ? itemKey_1 : (_item_7$id = item_7.id) === null || _item_7$id === void 0 ? void 0 : _item_7$id.toString()) !== null && _ref4 !== void 0 ? _ref4 : index_3.toString();
332
+ return /*#__PURE__*/jsx(MappedActionListItem, {
333
+ className: clsx(classes.ActionListItem, 'className' in item_7 ? item_7.className : undefined),
334
+ "data-input-focused": isInputFocused ? '' : undefined,
335
+ "data-first-child": index_3 === 0 ? '' : undefined,
336
+ ...item_7,
337
+ renderItem: listProps.renderItem
338
+ }, key_1);
339
+ });
340
+ };
210
341
  const actionListContent = /*#__PURE__*/jsx(ActionList, {
211
342
  ref: usingRovingTabindex ? listRef : listContainerRefCallback,
212
343
  showDividers: showItemDividers,
@@ -215,45 +346,19 @@ function FilteredActionList({
215
346
  ...actionListProps,
216
347
  role: "listbox",
217
348
  id: listId,
218
- className: clsx(classes.ActionList, actionListProps === null || actionListProps === void 0 ? void 0 : actionListProps.className),
219
- children: groupMetadata !== null && groupMetadata !== void 0 && groupMetadata.length ? groupMetadata.map((group, index) => {
220
- var _group$header, _group$header2;
221
- if (index === firstGroupIndex_0 && getItemListForEachGroup(group.groupId).length === 0) {
222
- firstGroupIndex_0++; // Increment firstGroupIndex if the first group has no items
223
- }
224
- return /*#__PURE__*/jsxs(ActionList.Group, {
225
- children: [/*#__PURE__*/jsx(ActionList.GroupHeading, {
226
- variant: (_group$header = group.header) !== null && _group$header !== void 0 && _group$header.variant ? group.header.variant : undefined,
227
- children: (_group$header2 = group.header) !== null && _group$header2 !== void 0 && _group$header2.title ? group.header.title : `Group ${group.groupId}`
228
- }), getItemListForEachGroup(group.groupId).map(({
229
- key: itemKey,
230
- ...item_3
231
- }, itemIndex) => {
232
- var _ref, _item_3$id;
233
- const key = (_ref = itemKey !== null && itemKey !== void 0 ? itemKey : (_item_3$id = item_3.id) === null || _item_3$id === void 0 ? void 0 : _item_3$id.toString()) !== null && _ref !== void 0 ? _ref : itemIndex.toString();
234
- return /*#__PURE__*/jsx(MappedActionListItem, {
235
- className: clsx(classes.ActionListItem, 'className' in item_3 ? item_3.className : undefined),
236
- "data-input-focused": isInputFocused ? '' : undefined,
237
- "data-first-child": index === firstGroupIndex_0 && itemIndex === 0 ? '' : undefined,
238
- ...item_3,
239
- renderItem: listProps.renderItem
240
- }, key);
241
- })]
242
- }, index);
243
- }) : items.map(({
244
- key: itemKey_0,
245
- ...item_4
246
- }, index_0) => {
247
- var _ref2, _item_4$id;
248
- const key_0 = (_ref2 = itemKey_0 !== null && itemKey_0 !== void 0 ? itemKey_0 : (_item_4$id = item_4.id) === null || _item_4$id === void 0 ? void 0 : _item_4$id.toString()) !== null && _ref2 !== void 0 ? _ref2 : index_0.toString();
249
- return /*#__PURE__*/jsx(MappedActionListItem, {
250
- className: clsx(classes.ActionListItem, 'className' in item_4 ? item_4.className : undefined),
251
- "data-input-focused": isInputFocused ? '' : undefined,
252
- "data-first-child": index_0 === 0 ? '' : undefined,
253
- ...item_4,
254
- renderItem: listProps.renderItem
255
- }, key_0);
256
- })
349
+ className: clsx(classes.ActionList, actionListProps === null || actionListProps === void 0 ? void 0 : actionListProps.className)
350
+ // When virtualized, the ActionList needs `position: relative` so that absolutely-positioned
351
+ // virtual items are placed correctly, and its `height` must equal the total virtual content
352
+ // size so the scroll container produces the right scrollbar.
353
+ // These styles are independent of SelectPanel's `height`/`width` props, which control the
354
+ // outer overlay dimensions, not the list content area.
355
+ ,
356
+ style: isVirtualized ? {
357
+ ...(actionListProps === null || actionListProps === void 0 ? void 0 : actionListProps.style),
358
+ height: virtualizer.getTotalSize(),
359
+ position: 'relative'
360
+ } : actionListProps === null || actionListProps === void 0 ? void 0 : actionListProps.style,
361
+ children: renderListItems()
257
362
  });
258
363
 
259
364
  // Use ActionListContainerContext.Provider only for the old behavior (when feature flag is disabled)