Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/html-reporter/src/tabbedPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,40 @@ export const TabbedPane: React.FunctionComponent<{
setSelectedTab: (tab: string) => void
}> = ({ tabs, selectedTab, setSelectedTab }) => {
const idPrefix = React.useId();
const tabStripRef = React.useRef<HTMLDivElement>(null);

const handleKeyDown = (e: React.KeyboardEvent) => {
const tabElements = Array.from(tabStripRef.current?.querySelectorAll('[role="tab"]') ?? []) as HTMLElement[];
const currentIndex = tabElements.findIndex(el => el === document.activeElement);
if (currentIndex === -1)
return;
let nextIndex = currentIndex;
if (e.key === 'ArrowRight')
nextIndex = (currentIndex + 1) % tabElements.length;
else if (e.key === 'ArrowLeft')
nextIndex = (currentIndex - 1 + tabElements.length) % tabElements.length;
else if (e.key === 'Home')
nextIndex = 0;
else if (e.key === 'End')
nextIndex = tabElements.length - 1;
else
return;
e.preventDefault();
tabElements[nextIndex].focus();
setSelectedTab(tabs[nextIndex].id);
};

return <div className='tabbed-pane'>
<div className='vbox'>
<div className='hbox' style={{ flex: 'none' }}>
<div className='tabbed-pane-tab-strip' role='tablist'>{
<div className='tabbed-pane-tab-strip' role='tablist' onKeyDown={handleKeyDown} ref={tabStripRef}>{
tabs.map(tab => (
<div className={clsx('tabbed-pane-tab-element', selectedTab === tab.id && 'selected')}
onClick={() => setSelectedTab(tab.id)}
id={`${idPrefix}-${tab.id}`}
key={tab.id}
role='tab'
tabIndex={selectedTab === tab.id ? 0 : -1}
aria-selected={selectedTab === tab.id}>
<div className='tabbed-pane-tab-label'>{tab.title}</div>
</div>
Expand Down
30 changes: 28 additions & 2 deletions packages/web/src/components/tabbedPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,40 @@ export const TabbedPane: React.FunctionComponent<{
mode?: 'default' | 'select',
}> = ({ tabs, selectedTab, setSelectedTab, leftToolbar, rightToolbar, dataTestId, mode }) => {
const id = React.useId();
const tabListRef = React.useRef<HTMLDivElement>(null);
if (!selectedTab)
selectedTab = tabs[0].id;
if (!mode)
mode = 'default';

const handleKeyDown = (e: React.KeyboardEvent) => {
const tabElements = Array.from(tabListRef.current?.querySelectorAll('[role="tab"]') ?? []) as HTMLElement[];
const currentIndex = tabElements.findIndex(el => el === document.activeElement);
if (currentIndex === -1)
return;
let nextIndex = currentIndex;
if (e.key === 'ArrowRight')
nextIndex = (currentIndex + 1) % tabElements.length;
else if (e.key === 'ArrowLeft')
nextIndex = (currentIndex - 1 + tabElements.length) % tabElements.length;
else if (e.key === 'Home')
nextIndex = 0;
else if (e.key === 'End')
nextIndex = tabElements.length - 1;
else
return;
e.preventDefault();
tabElements[nextIndex].focus();
setSelectedTab?.(tabs[nextIndex].id);
};

return <div className='tabbed-pane' data-testid={dataTestId}>
<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'>
{mode === 'default' && <div style={{ flex: 'auto', display: 'flex', height: '100%', overflow: 'hidden' }} role='tablist' onKeyDown={handleKeyDown} ref={tabListRef}>
{[...tabs.map(tab => (
<TabbedPaneTab
key={tab.id}
Expand All @@ -59,6 +82,7 @@ export const TabbedPane: React.FunctionComponent<{
errorCount={tab.errorCount}
selected={selectedTab === tab.id}
onSelect={setSelectedTab}
tabIndex={selectedTab === tab.id ? 0 : -1}
/>)),
]}
</div>}
Expand Down Expand Up @@ -101,11 +125,13 @@ export const TabbedPaneTab: React.FunctionComponent<{
selected?: boolean,
onSelect?: (id: string) => void,
ariaControls?: string,
}> = ({ id, title, count, errorCount, selected, onSelect, ariaControls }) => {
tabIndex?: number,
}> = ({ id, title, count, errorCount, selected, onSelect, ariaControls, tabIndex }) => {
return <div className={clsx('tabbed-pane-tab', selected && 'selected')}
onClick={() => onSelect?.(id)}
role='tab'
title={title}
tabIndex={tabIndex ?? (selected ? 0 : -1)}
aria-controls={ariaControls}
aria-selected={selected}>
<div className='tabbed-pane-tab-label'>{title}</div>
Expand Down
Loading