Bug - Tabs - Tabs overflow does not respond to container resize (only listens to window.resize)
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 65/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- react, typescript
- 领域
- frontend
调研方向
从 Tabs.tsx 开始,阅读 componentDidMount、componentWillUnmount、handleScrollButtons 和 componentDidUpdate,然后跟踪现有的 window 调整大小和 debounce 行为。在可调整大小的 Drawer 或其他会发生变化的容器中复现该问题,并验证 overflow 控件和 More 数量会更新,同时不会对仅高度变化反复作出响应。
由索引模型根据 Issue 内容生成。
描述
Description
The Tabs component only recalculates its overflow state (scroll buttons and "More" dropdown count) in response to window.resize events. It does not observe its own container for size changes. This means that when Tabs are placed inside any dynamically-sized container — such as a resizable Drawer, a CSS Grid/Flexbox layout that redistributes space, or a collapsible sidebar — the overflow state becomes stale:
-
Scroll buttons appear when all tabs fit, or disappear when tabs overflow
-
The "More (N)" dropdown count does not update
-
The active tab accent bar drifts out of position
Root Cause
In Tabs.tsx, componentDidMount (line 381) registers only:
window.addEventListener('resize', this.handleResize, false);
There is no ResizeObserver on this.tabList.current (the <ul> element), so container-level width changes are invisible to the component.
Additionally, handleScrollButtons() (line 258) uses a 100ms debounce and componentDidUpdate (line 429) accumulates overflowingTabCount rather than recalculating from scratch, which can compound errors during rapid resizes.
Steps to Reproduce
- Place a
<Tabs>component with 8+ tabs inside a<DrawerPanelContent isResizable> - Open the drawer — tabs may display correctly initially
- Drag the drawer splitter to make the panel narrower
- Expected: Scroll buttons appear / "More" count increases as tabs overflow
- Actual: Overflow state is unchanged until the browser window itself is resized
This also reproduces with any non-window container resize (e.g., a parent element resized via JavaScript, CSS transitions, or layout shifts).
Proposed Fix
Add a ResizeObserver on this.tabList.current in componentDidMount, alongside the existing window.resize listener. The observer should filter by width changes only — when scroll buttons render/hide, they change the container height, which would re-trigger the observer and create a feedback loop.
// New instance property
private resizeObserver: ResizeObserver | null = null;
private lastObservedWidth: number = 0;
componentDidMount() {
if (!this.props.isVertical) {
if (canUseDOM) {
window.addEventListener('resize', this.handleResize, false);
// Observe container size changes (e.g., inside resizable Drawer)
this.resizeObserver = new ResizeObserver((entries) => {
const newWidth = entries[0].contentRect.width;
// Only react to WIDTH changes — scroll button rendering changes height,
// which would cause an infinite feedback loop if not filtered out.
if (Math.abs(newWidth - this.lastObservedWidth) < 1) {
return;
}
this.lastObservedWidth = newWidth;
this.handleResize();
});
if (this.tabList.current) {
this.resizeObserver.observe(this.tabList.current);
}
}
this.direction = getLanguageDirection(this.tabList.current);
this.handleScrollButtons();
}
this.setAccentStyles(true);
}
componentWillUnmount() {
if (!this.props.isVertical) {
if (canUseDOM) {
window.removeEventListener('resize', this.handleResize, false);
this.resizeObserver?.disconnect();
}
}
// ...existing cleanup
}
This would fix Tabs in any dynamically-sized container, not just Drawer.
Current Workaround
Consumers can work around this by attaching their own ResizeObserver and forcing a Tabs remount via a key prop. However, the workaround has three non-obvious pitfalls:
-
Must use a callback ref, not
useRef+useEffect—DrawerPanelContentdelays rendering children until a CSS transition completes (isExpandedInternalis set ontransitionEnd). AuseEffectkeyed onisExpandedruns before the panel DOM node exists, so the observer never attaches after a drawer toggle. -
Must filter by width changes only — When scroll buttons appear/disappear, they change the container height. A naive
ResizeObservercallback re-triggers on height changes, causing an infinite remount loop (buttons render → height changes → observer fires → remount → repeat). -
Must debounce to ~150ms — Tabs internally uses a 100ms debounce in
handleScrollButtons()and a 100ms delay forshowScrollButtons. The consumer's debounce must be longer than these to avoid interfering with the multi-phase scroll button rendering cycle.
const [resizeKey, setResizeKey] = useState(0);
const lastWidthRef = useRef<number>(0);
const observerRef = useRef<ResizeObserver | null>(null);
const debounceRef = useRef<ReturnType<typeof setTimeout>>();
const panelCallbackRef = useCallback((node: HTMLDivElement | null) => {
clearTimeout(debounceRef.current);
observerRef.current?.disconnect();
observerRef.current = null;
if (!node) return;
lastWidthRef.current = 0;
observerRef.current = new ResizeObserver((entries) => {
const newWidth = entries[0].contentRect.width;
if (Math.abs(newWidth - lastWidthRef.current) < 1) return;
lastWidthRef.current = newWidth;
clearTimeout(debounceRef.current);
debounceRef.current = setTimeout(() => setResizeKey(c => c + 1), 150);
});
observerRef.current.observe(node);
}, []);
// In your DrawerPanelContent:
<div ref={panelCallbackRef}>
<Tabs key={resizeKey} activeKey={activeKey} onSelect={handleSelect}>
{/* ... */}
</Tabs>
</div>
Environment
-
@patternfly/react-coreversion: current main -
Browsers: all (ResizeObserver is widely supported)
Related
- Jira: PF-3907
Jira Issue: PF-4026
- 主要语言
- TypeScript
- 星标
- 862
- 派生
- 392
- 平均合并
- 4 天 8 小时
- 30 天内合并 PR
- 9
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
patternfly/patternfly-react 的其他 Issue
-
PF Team
难度 2/5 1-3 小时 新手友好度 68/100
patternfly/patternfly-react#12651 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
patternfly/patternfly-react#12637 ·
-
PF Team
难度 2/5 1-3 小时 新手友好度 72/100
patternfly/patternfly-react#12617 · 1 条评论 ·
-
PF Team
难度 2/5 1-3 小时 新手友好度 68/100
patternfly/patternfly-react#12610 · 1 条评论 · 1 个 reaction ·
-
难度 2/5 1-3 小时 新手友好度 68/100
patternfly/patternfly-react#12600 ·
查看 patternfly/patternfly-react 的全部 Issue
相似的 Issue
-
blocklist removal
难度 2/5 1-3 小时 新手友好度 65/100
MetaMask/eth-phishing-detect#296544 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
pastelsky/bundlephobia#1122 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
-
category/development priority/P2 scope/file-operations scope/testing type/enhancement
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 75/100