Repository navigation
fix(Tabs): update overflow when the container resizes #12669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ import { TabProps } from './Tab'; | |
| import { TabsContextProvider } from './TabsContext'; | ||
| import { OverflowTab, HorizontalOverflowPopperProps } from './OverflowTab'; | ||
| import { Button } from '../Button'; | ||
| import { getOUIAProps, OUIAProps, canUseDOM } from '../../helpers'; | ||
| import { getOUIAProps, OUIAProps, canUseDOM, getResizeObserver } from '../../helpers'; | ||
| import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; | ||
| import { GenerateId } from '../../helpers/GenerateId/GenerateId'; | ||
| import linkAccentLength from '@patternfly/react-tokens/dist/esm/c_tabs_link_accent_length'; | ||
|
|
@@ -184,7 +184,10 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| static displayName = 'Tabs'; | ||
| tabList = createRef<HTMLUListElement>(); | ||
| leftScrollButtonRef = createRef<HTMLButtonElement>(); | ||
| private containerRef = createRef<HTMLElement>(); | ||
| private direction = 'ltr'; | ||
| private unobserveResize = () => {}; | ||
| private lastObservedWidth: number | undefined; | ||
| constructor(props: TabsProps) { | ||
| super(props); | ||
| const hashActiveKey = getTabHashActiveKey(props); | ||
|
|
@@ -406,14 +409,30 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| }; | ||
|
|
||
| handleResize = () => { | ||
| if (!this.tabList.current) { | ||
| return; | ||
| } | ||
| this.handleScrollButtons(); | ||
| this.setAccentStyles(); | ||
| }; | ||
|
|
||
| private handleContainerResize = () => { | ||
| if (!this.containerRef.current) { | ||
| return; | ||
| } | ||
| const width = this.containerRef.current.getBoundingClientRect().width; | ||
| if (width === this.lastObservedWidth) { | ||
| return; | ||
| } | ||
| this.lastObservedWidth = width; | ||
| this.handleResize(); | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| if (!this.props.isVertical) { | ||
| this.unobserveResize = getResizeObserver(this.containerRef.current, this.handleContainerResize, true); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reattach the observer when the container element changes. If
|
||
| if (canUseDOM) { | ||
| window.addEventListener('resize', this.handleResize, false); | ||
| window.addEventListener('resize', this.handleResize); | ||
| } | ||
| this.direction = getLanguageDirection(this.tabList.current); | ||
| // call the handle resize function to check if scroll buttons should be shown | ||
|
|
@@ -424,10 +443,9 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| } | ||
|
|
||
| componentWillUnmount() { | ||
| if (!this.props.isVertical) { | ||
| if (canUseDOM) { | ||
| window.removeEventListener('resize', this.handleResize, false); | ||
| } | ||
| this.unobserveResize(); | ||
| if (canUseDOM) { | ||
| window.removeEventListener('resize', this.handleResize); | ||
| } | ||
| clearTimeout(this.scrollTimeout); | ||
| this.leftScrollButtonRef.current?.removeEventListener('transitionend', this.hideScrollButtons); | ||
|
|
@@ -607,6 +625,7 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| }} | ||
| > | ||
| <Component | ||
| ref={this.containerRef} | ||
| aria-label={ariaLabel} | ||
| className={css( | ||
| styles.tabs, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compare the observed content width.
If a fixed-width,
border-boxTabs container gains padding, its content box shrinks but its bounding width stays unchanged. The observer fires, but this check skips recalculation, so scroll controls can remain unavailable for overflowing tabs. Compare the observed content width instead.