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

Function CollapsingTabs

packages/@react-spectrum/s2/src/Tabs.tsx:755–862  ·  view source on GitHub ↗
({
  collection,
  containerRef,
  ...props
}: {collection: Collection<Node<unknown>>; containerRef: any} & TabsProps)

Source from the content-addressed store, hash-verified

753};
754
755let CollapsingTabs = ({
756 collection,
757 containerRef,
758 ...props
759}: {collection: Collection<Node<unknown>>; containerRef: any} & TabsProps) => {
760 let {orientation = 'horizontal', onSelectionChange} = props;
761 let [showItems, _setShowItems] = useState(true);
762 showItems = orientation === 'vertical' ? true : showItems;
763 let setShowItems = useCallback(
764 (value: boolean) => {
765 if (orientation === 'vertical') {
766 // if orientation is vertical, we always show the items
767 _setShowItems(true);
768 } else {
769 _setShowItems(value);
770 }
771 },
772 [orientation]
773 );
774
775 let {direction} = useLocale();
776
777 let children = useMemo(() => [...collection], [collection]);
778
779 let listRef = useRef<HTMLDivElement | null>(null);
780
781 let updateOverflow = () => {
782 if (orientation === 'vertical' || !listRef.current || !containerRef?.current) {
783 return;
784 }
785 let container = listRef.current;
786 let containerRect = container.getBoundingClientRect();
787 let tabs = container.querySelectorAll('[data-hidden-tab]');
788 let lastTab = tabs[tabs.length - 1];
789 let lastTabRect = lastTab.getBoundingClientRect();
790 if (direction === 'ltr') {
791 setShowItems?.(lastTabRect.right <= containerRect.right);
792 } else {
793 setShowItems?.(lastTabRect.left >= containerRect.left);
794 }
795 };
796
797 let updateOverflowEffect = useEffectEvent(updateOverflow);
798
799 useResizeObserver({ref: containerRef, onResize: updateOverflow});
800
801 useLayoutEffect(() => {
802 if (collection.size > 0) {
803 queueMicrotask(updateOverflowEffect);
804 }
805 }, [collection.size]);
806
807 // start with null so that the first render won't have a flicker
808 let prevOrientation = useRef<Orientation | null>(null);
809 useLayoutEffect(() => {
810 if (collection.size > 0 && prevOrientation.current !== orientation) {
811 updateOverflowEffect();
812 }

Callers

nothing calls this directly

Calls 4

useLocaleFunction · 0.90
useEffectEventFunction · 0.90
useResizeObserverFunction · 0.90
useIdFunction · 0.90

Tested by

no test coverage detected