@@ -8,6 +8,8 @@ import {useSlots} from '../hooks/useSlots'
8
8
import { BetterSystemStyleObject , merge , SxProp } from '../sx'
9
9
import { Theme } from '../ThemeProvider'
10
10
import { canUseDOM } from '../utils/environment'
11
+ import { invariant } from '../utils/invariant'
12
+ import { useOverflow } from '../utils/useOverflow'
11
13
import VisuallyHidden from '../_VisuallyHidden'
12
14
import { useStickyPaneHeight } from './useStickyPaneHeight'
13
15
@@ -604,7 +606,7 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
604
606
const MIN_PANE_WIDTH = 256 // 256px, related to `--pane-min-width CSS var.
605
607
const [ minPercent , setMinPercent ] = React . useState ( 0 )
606
608
const [ maxPercent , setMaxPercent ] = React . useState ( 0 )
607
- const [ overflow , setOverflow ] = React . useState ( false )
609
+ const hasOverflow = useOverflow ( paneRef )
608
610
609
611
const measuredRef = React . useCallback ( ( ) => {
610
612
if ( paneRef . current !== null ) {
@@ -624,11 +626,6 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
624
626
625
627
const widthPercent = Math . round ( ( 100 * paneWidth ) / viewportWidth )
626
628
setWidthPercent ( widthPercent . toString ( ) )
627
-
628
- const hasOverflow =
629
- paneRef . current . scrollHeight > paneRef . current . offsetHeight ||
630
- paneRef . current . scrollWidth > paneRef . current . offsetWidth
631
- setOverflow ( hasOverflow )
632
629
}
633
630
} , [ paneRef ] )
634
631
@@ -656,16 +653,13 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
656
653
const paneId = useId ( id )
657
654
658
655
let labelProp = undefined
659
- if ( overflow ) {
656
+ if ( hasOverflow ) {
657
+ invariant ( label !== undefined || labelledBy !== undefined )
660
658
if ( labelledBy ) {
661
659
labelProp = { 'aria-labelledby' : labelledBy }
662
660
} else {
663
661
labelProp = { 'aria-label' : label }
664
662
}
665
-
666
- if ( labelProp [ 'aria-label' ] === undefined ) {
667
- throw new Error ( 'PageLayout.Pane must have either `aria-labelledby` or `aria-label` when overflow is active.' )
668
- }
669
663
}
670
664
671
665
return (
@@ -755,7 +749,7 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
755
749
'--pane-max-width-diff' : '959px' ,
756
750
} ,
757
751
} ) }
758
- { ...( overflow && { tabIndex : 0 , role : 'region' } ) }
752
+ { ...( hasOverflow && { tabIndex : 0 , role : 'region' } ) }
759
753
{ ...labelProp }
760
754
{ ...( id && { id : paneId } ) }
761
755
>
0 commit comments