MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useCollectionRender

Function useCollectionRender

packages/@react-spectrum/s2/src/Breadcrumbs.tsx:452–563  ·  view source on GitHub ↗
(collection: Collection<Node<unknown>>)

Source from the content-addressed store, hash-verified

450};
451
452let useCollectionRender = (collection: Collection<Node<unknown>>) => {
453 let {containerRef, onAction} = useContext(CollapseContext) ?? {};
454 let [visibleItems, setVisibleItems] = useState(collection.size);
455 let {size = 'M'} = useContext(InternalBreadcrumbsContext);
456
457 let children = useMemo(() => {
458 let result: Node<any>[] = [];
459 for (let key of collection.getKeys()) {
460 result.push(collection.getItem(key)!);
461 }
462 return result;
463 }, [collection]);
464
465 let listRef = useRef<HTMLDivElement | null>(null);
466 let updateOverflow = useCallback(() => {
467 let currListRef: HTMLDivElement | null = listRef.current;
468 if (!currListRef) {
469 setVisibleItems(collection.size);
470 return;
471 }
472 let container = getParentElement(currListRef) as HTMLElement | null;
473 if (!container) {
474 setVisibleItems(collection.size);
475 return;
476 }
477
478 let listItems = Array.from(
479 currListRef.querySelectorAll('[data-hidden-breadcrumb]')
480 ) as HTMLLIElement[];
481 let folder = currListRef.querySelector('button') as HTMLButtonElement;
482 if (listItems.length <= 0) {
483 setVisibleItems(collection.size);
484 return;
485 }
486 let containerWidth = container.offsetWidth;
487 let containerGap = parseInt(getComputedStyle(container).gap, 10);
488 let folderGap = parseInt(getComputedStyle(folder).marginInlineStart, 10);
489 let newVisibleItems = 0;
490 let maxVisibleItems = MAX_VISIBLE_ITEMS;
491
492 let widths: Array<number> = [];
493 let totalWidth = 0;
494 for (let breadcrumb of listItems) {
495 let width = breadcrumb.offsetWidth + 1; // offsetWidth is rounded down
496 widths.push(width);
497 totalWidth += width;
498 }
499
500 // can we fit all the items without collapsing
501 if (
502 totalWidth <= containerWidth - collection.size * containerGap &&
503 collection.size <= MAX_VISIBLE_ITEMS
504 ) {
505 setVisibleItems(collection.size);
506 return;
507 }
508
509 // we know there is always at least one item because of the listItems.length check up above

Callers 2

CollectionRootFunction · 0.70
CollectionBranchFunction · 0.70

Calls 8

getParentElementFunction · 0.90
useEffectEventFunction · 0.90
useResizeObserverFunction · 0.90
pushMethod · 0.80
sliceMethod · 0.80
getKeysMethod · 0.65
getItemMethod · 0.65
renderMethod · 0.45

Tested by

no test coverage detected