-
Notifications
You must be signed in to change notification settings - Fork 4.3k
bug(trace-viewer): Overflow with TabbedPane Component #36251
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 |
---|---|---|
|
@@ -68,7 +68,6 @@ | |
.tab-network .toolbar { | ||
min-height: 30px !important; | ||
background-color: initial !important; | ||
border-bottom: 1px solid var(--vscode-panel-border); | ||
} | ||
|
||
.tab-network .toolbar::after { | ||
|
@@ -77,6 +76,7 @@ | |
|
||
.tab-network .tabbed-pane-tab.selected { | ||
font-weight: bold; | ||
background-color: var(--vscode-list-inactiveSelectionBackground) !important; | ||
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. Why is |
||
} | ||
|
||
.copy-request-dropdown { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,37 +36,164 @@ export const TabbedPane: React.FunctionComponent<{ | |
setSelectedTab?: (tab: string) => void, | ||
dataTestId?: string, | ||
mode?: 'default' | 'select', | ||
}> = ({ tabs, selectedTab, setSelectedTab, leftToolbar, rightToolbar, dataTestId, mode }) => { | ||
overflowMode?: 'none' | 'select' | ||
}> = ({ tabs, selectedTab, setSelectedTab, leftToolbar, rightToolbar, dataTestId, mode, overflowMode }) => { | ||
const id = React.useId(); | ||
if (!selectedTab) | ||
selectedTab = tabs[0].id; | ||
if (!mode) | ||
mode = 'default'; | ||
return <div className='tabbed-pane' data-testid={dataTestId}> | ||
if (!overflowMode) | ||
overflowMode = 'none'; | ||
|
||
const containerRef = React.useRef<HTMLDivElement>(null); | ||
const [visibleTabs, setVisibleTabs] = React.useState<TabbedPaneTabModel[]>(mode !== 'select' ? tabs : []); | ||
const [overflowTabs, setOverflowTabs] = React.useState<TabbedPaneTabModel[]>(mode === 'select' ? tabs : []); | ||
const [tabWidths, setTabWidths] = React.useState<Record<string, number>>({}); | ||
const [, setContainerWidth] = React.useState<number>(0); | ||
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. Are you using this for rerender management? Why does it matter, given you're also setting other state every time this is set? Those other sets will always be new/unique as well, so they will also cause a rerender. |
||
const [tabbedPaneWidth, setTabbedPaneWidth] = React.useState<number>(0); | ||
|
||
// Initial measurements | ||
const measureContainerTabs = React.useCallback(() => { | ||
const container = containerRef.current; | ||
if (!container) | ||
return; | ||
|
||
const containerWidth = container.getBoundingClientRect().width; | ||
setContainerWidth(containerWidth); | ||
|
||
const tabWidths: Record<string, number> = {}; | ||
const tabbedPaneTabs = container.querySelectorAll('.tabbed-pane-tab'); | ||
tabbedPaneTabs.forEach(tabbedPane => { | ||
const element = tabbedPane as HTMLElement; | ||
if (element && element.title) | ||
tabWidths[element.title] = tabbedPane.scrollWidth; | ||
}); | ||
setTabWidths(tabWidths); | ||
|
||
// For width calculation: Assume the dropdown width is 1.5x of the rightmost menu item | ||
const tabWidthValues = Object.values(tabWidths); | ||
const tabbedPaneWidth = 1.5 * tabWidthValues[tabWidthValues.length - 1] || 0; | ||
if (tabbedPaneWidth > 0) | ||
setTabbedPaneWidth(tabbedPaneWidth); | ||
}, []); | ||
|
||
const calculateVisibleTabCount = React.useCallback((availableWidth: number, tabWidths: Record<string, number>, tabs: TabbedPaneTabModel[]): number => { | ||
let requiredWidth = 0; | ||
|
||
for (const tabWidth of Object.values(tabWidths)) { | ||
requiredWidth += tabWidth; | ||
if (requiredWidth > availableWidth) { | ||
// Overflow detected, calculate how many tabs can fit | ||
let visibleCount = 0; | ||
let cumulativeWidth = 0; | ||
|
||
for (let index = 0; index < tabs.length; index++) { | ||
const tab = tabs[index]; | ||
const tabWidth = tabWidths[tab.title]; | ||
cumulativeWidth += tabWidth; | ||
|
||
if (cumulativeWidth > availableWidth) { | ||
visibleCount = index; | ||
break; | ||
} | ||
} | ||
return visibleCount; | ||
} | ||
} | ||
|
||
return tabs.length; | ||
}, []); | ||
|
||
const adjustElementRenderings = React.useCallback(() => { | ||
const container = containerRef.current; | ||
if (!container) | ||
return; | ||
|
||
const containerWidth = container.getBoundingClientRect().width; | ||
setContainerWidth(containerWidth); | ||
|
||
const initialAvailableWidth = containerWidth - (overflowTabs.length > 0 ? tabbedPaneWidth : 0); | ||
const finalAvailableWidth = containerWidth - tabbedPaneWidth; | ||
|
||
const visibleCount = calculateVisibleTabCount(initialAvailableWidth, tabWidths, tabs) === tabs.length | ||
? tabs.length | ||
: calculateVisibleTabCount(finalAvailableWidth, tabWidths, tabs); | ||
|
||
const visibleTabsList = tabs.slice(0, visibleCount); | ||
const overflowTabsList = tabs.slice(visibleCount); | ||
|
||
setVisibleTabs(visibleTabsList); | ||
setOverflowTabs(overflowTabsList); | ||
}, [tabWidths, overflowTabs, tabbedPaneWidth, tabs, calculateVisibleTabCount]); | ||
|
||
// Initial measurement and setup | ||
React.useEffect(() => { | ||
if (overflowMode !== 'select') | ||
return; | ||
|
||
measureContainerTabs(); | ||
}, [measureContainerTabs, overflowMode]); | ||
|
||
// Adjust when Tab widths change | ||
React.useEffect(() => { | ||
if (overflowMode !== 'select') | ||
return; | ||
|
||
if (overflowTabs.length > 0) | ||
adjustElementRenderings(); | ||
}, [adjustElementRenderings, overflowMode, overflowTabs.length]); | ||
|
||
React.useEffect(() => { | ||
if (overflowMode !== 'select') | ||
return; | ||
|
||
const container = containerRef.current; | ||
if (!container) | ||
return; | ||
|
||
const handleResize = () => { | ||
setTimeout(adjustElementRenderings, 0); | ||
}; | ||
|
||
const resizeObserver = new ResizeObserver(handleResize); | ||
resizeObserver.observe(container); | ||
handleResize(); | ||
|
||
return () => { | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [adjustElementRenderings, overflowMode]); | ||
|
||
return <div className='tabbed-pane' data-testid={dataTestId} ref={containerRef}> | ||
<div className='vbox'> | ||
<Toolbar> | ||
{ leftToolbar && <div style={{ flex: 'none', display: 'flex', margin: '0 4px', alignItems: 'center' }}> | ||
{...leftToolbar} | ||
</div>} | ||
{mode === 'default' && <div style={{ flex: 'auto', display: 'flex', height: '100%', overflow: 'hidden' }} role='tablist'> | ||
{[...tabs.map(tab => ( | ||
<TabbedPaneTab | ||
key={tab.id} | ||
id={tab.id} | ||
ariaControls={`${id}-${tab.id}`} | ||
title={tab.title} | ||
count={tab.count} | ||
errorCount={tab.errorCount} | ||
selected={selectedTab === tab.id} | ||
onSelect={setSelectedTab} | ||
/>)), | ||
]} | ||
{visibleTabs.length > 0 && <div style={{ flex: 'auto', display: 'flex', height: '100%', overflow: 'hidden' }} role='tablist'> | ||
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. Does |
||
{visibleTabs.map(visibleTab => { | ||
const tab = tabs.find(t => t.id === visibleTab.id) || visibleTab; | ||
return ( | ||
<TabbedPaneTab | ||
key={tab.id} | ||
id={tab.id} | ||
ariaControls={`${id}-${tab.id}`} | ||
title={tab.title} | ||
count={tab.count} | ||
errorCount={tab.errorCount} | ||
selected={selectedTab === tab.id} | ||
onSelect={setSelectedTab} | ||
/> | ||
); | ||
})} | ||
</div>} | ||
{mode === 'select' && <div style={{ flex: 'auto', display: 'flex', height: '100%', overflow: 'hidden' }} role='tablist'> | ||
{overflowTabs.length > 0 && <div style={{ flex: 'auto', display: 'flex', height: '100%', overflow: 'hidden' }} role='tablist'> | ||
<select style={{ width: '100%', background: 'none', cursor: 'pointer' }} value={selectedTab} onChange={e => { | ||
setSelectedTab?.(tabs[e.currentTarget.selectedIndex].id); | ||
setSelectedTab?.(overflowTabs[e.currentTarget.selectedIndex].id); | ||
}}> | ||
{tabs.map(tab => { | ||
{overflowTabs.map(overflowTab => { | ||
const tab = tabs.find(t => t.id === overflowTab.id) || overflowTab; | ||
let suffix = ''; | ||
if (tab.count) | ||
suffix = ` (${tab.count})`; | ||
|
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.
What about other places that use
TabbedPane
? Do they upgrade to the new functionality for free?