|
| 1 | +import { forwardRef, useEffect, useState } from '@wordpress/element'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import { Tabs as _Tabs } from '@base-ui/react/tabs'; |
| 4 | +import { useMergeRefs } from '@wordpress/compose'; |
| 5 | +import styles from './style.module.css'; |
| 6 | +import type { TabListProps } from './types'; |
| 7 | + |
| 8 | +// Account for sub-pixel rounding errors. |
| 9 | +const SCROLL_EPSILON = 1; |
| 10 | + |
| 11 | +/** |
| 12 | + * Groups the individual tab buttons. |
| 13 | + * |
| 14 | + * `Tabs` is a collection of React components that combine to render |
| 15 | + * an [ARIA-compliant tabs pattern](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/). |
| 16 | + */ |
| 17 | +export const List = forwardRef< HTMLDivElement, TabListProps >( |
| 18 | + function TabList( |
| 19 | + { |
| 20 | + children, |
| 21 | + variant = 'default', |
| 22 | + className, |
| 23 | + activateOnFocus, |
| 24 | + render, |
| 25 | + ...otherProps |
| 26 | + }, |
| 27 | + forwardedRef |
| 28 | + ) { |
| 29 | + const [ listEl, setListEl ] = useState< HTMLDivElement | null >( null ); |
| 30 | + const [ overflow, setOverflow ] = useState< { |
| 31 | + first: boolean; |
| 32 | + last: boolean; |
| 33 | + isScrolling: boolean; |
| 34 | + } >( { |
| 35 | + first: false, |
| 36 | + last: false, |
| 37 | + isScrolling: false, |
| 38 | + } ); |
| 39 | + |
| 40 | + // Check if list is overflowing when it scrolls or resizes. |
| 41 | + useEffect( () => { |
| 42 | + if ( ! listEl ) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const measureOverflow = () => { |
| 47 | + const { scrollWidth, clientWidth, scrollLeft } = listEl; |
| 48 | + const maxScroll = Math.max( scrollWidth - clientWidth, 0 ); |
| 49 | + const direction = |
| 50 | + listEl.dir || |
| 51 | + ( typeof window !== 'undefined' |
| 52 | + ? window.getComputedStyle( listEl ).direction |
| 53 | + : 'ltr' ); |
| 54 | + |
| 55 | + const scrollFromStart = |
| 56 | + direction === 'rtl' && scrollLeft < 0 |
| 57 | + ? // In RTL layouts, scrollLeft is typically 0 at the visual "start" |
| 58 | + // (right edge) and becomes negative toward the "end" (left edge). |
| 59 | + // Normalize value for correct first/last detection logic. |
| 60 | + -scrollLeft |
| 61 | + : scrollLeft; |
| 62 | + |
| 63 | + // Use SCROLL_EPSILON to handle subpixel rendering differences. |
| 64 | + setOverflow( { |
| 65 | + first: scrollFromStart > SCROLL_EPSILON, |
| 66 | + last: scrollFromStart < maxScroll - SCROLL_EPSILON, |
| 67 | + isScrolling: scrollWidth > clientWidth, |
| 68 | + } ); |
| 69 | + }; |
| 70 | + |
| 71 | + const resizeObserver = new ResizeObserver( measureOverflow ); |
| 72 | + resizeObserver.observe( listEl ); |
| 73 | + |
| 74 | + let scrollTick = false; |
| 75 | + const throttleMeasureOverflowOnScroll = () => { |
| 76 | + if ( ! scrollTick ) { |
| 77 | + requestAnimationFrame( () => { |
| 78 | + measureOverflow(); |
| 79 | + scrollTick = false; |
| 80 | + } ); |
| 81 | + scrollTick = true; |
| 82 | + } |
| 83 | + }; |
| 84 | + listEl.addEventListener( |
| 85 | + 'scroll', |
| 86 | + throttleMeasureOverflowOnScroll, |
| 87 | + { passive: true } |
| 88 | + ); |
| 89 | + |
| 90 | + // Initial check. |
| 91 | + measureOverflow(); |
| 92 | + |
| 93 | + return () => { |
| 94 | + listEl.removeEventListener( |
| 95 | + 'scroll', |
| 96 | + throttleMeasureOverflowOnScroll |
| 97 | + ); |
| 98 | + resizeObserver.disconnect(); |
| 99 | + }; |
| 100 | + }, [ listEl ] ); |
| 101 | + |
| 102 | + const mergedListRef = useMergeRefs( [ |
| 103 | + forwardedRef, |
| 104 | + ( el: HTMLDivElement | null ) => setListEl( el ), |
| 105 | + ] ); |
| 106 | + |
| 107 | + return ( |
| 108 | + <_Tabs.List |
| 109 | + ref={ mergedListRef } |
| 110 | + activateOnFocus={ activateOnFocus } |
| 111 | + data-select-on-move={ activateOnFocus ? 'true' : 'false' } |
| 112 | + className={ clsx( |
| 113 | + styles.tablist, |
| 114 | + overflow.first && styles[ 'is-overflowing-first' ], |
| 115 | + overflow.last && styles[ 'is-overflowing-last' ], |
| 116 | + styles[ `is-${ variant }-variant` ], |
| 117 | + className |
| 118 | + ) } |
| 119 | + { ...otherProps } |
| 120 | + tabIndex={ |
| 121 | + otherProps.tabIndex ?? |
| 122 | + ( overflow.isScrolling ? -1 : undefined ) |
| 123 | + } |
| 124 | + > |
| 125 | + { children } |
| 126 | + <_Tabs.Indicator className={ styles.indicator } /> |
| 127 | + </_Tabs.List> |
| 128 | + ); |
| 129 | + } |
| 130 | +); |
0 commit comments