Hero-admin: tabbladen Inhoud en Afbeeldingen (toegankelijk, onthouden, fout- en wijzigingsmarkering, sprong naar eerste fout) en strakke opslaanbalk over de volle breedte

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 08:41:31 +01:00
co-authored by Claude Sonnet 5.5
parent 555ab10e14
commit 34ae16c902
5 changed files with 319 additions and 6 deletions
@@ -0,0 +1,106 @@
import { useRef, type KeyboardEvent, type ReactNode } from 'react';
export interface HeroTab<Id extends string> {
id: Id;
label: ReactNode;
/** Short note after the label, for example "2 busy". */
note?: string;
/** There are unsaved changes in this tab: a dot and a text for screen readers. */
changed?: boolean;
changedText?: string;
/** This tab holds a field with an error: a red mark and a text for screen readers. */
invalid?: boolean;
invalidText?: string;
}
interface HeroTabsProps<Id extends string> {
label: string;
tabs: HeroTab<Id>[];
selected: Id;
onSelect: (id: Id) => void;
idPrefix: string;
}
/**
* Accessible tabs (role tablist, tab, tabpanel): the arrow keys move between the tabs (and select, "automatic activation"),
* Home and End go to the first and the last, only the selected tab is in the tab order. The panels themselves are rendered
* by the page, with <c>id="{idPrefix}-panel-{id}"</c> and <c>aria-labelledby="{idPrefix}-tab-{id}"</c>.
*/
export function HeroTabs<Id extends string>({ label, tabs, selected, onSelect, idPrefix }: HeroTabsProps<Id>) {
const refs = useRef<Record<string, HTMLButtonElement | null>>({});
const move = (index: number) => {
const next = tabs[(index + tabs.length) % tabs.length];
onSelect(next.id);
refs.current[next.id]?.focus();
};
const onKeyDown = (event: KeyboardEvent, index: number) => {
switch (event.key) {
case 'ArrowRight':
case 'ArrowDown':
event.preventDefault();
move(index + 1);
break;
case 'ArrowLeft':
case 'ArrowUp':
event.preventDefault();
move(index - 1);
break;
case 'Home':
event.preventDefault();
move(0);
break;
case 'End':
event.preventDefault();
move(tabs.length - 1);
break;
default:
}
};
return (
<div role="tablist" aria-label={label} className="flex gap-1 border-b" data-testid="hero-tabs">
{tabs.map((tab, index) => {
const active = tab.id === selected;
return (
<button
key={tab.id}
ref={(element) => {
refs.current[tab.id] = element;
}}
type="button"
role="tab"
id={`${idPrefix}-tab-${tab.id}`}
aria-selected={active}
aria-controls={`${idPrefix}-panel-${tab.id}`}
tabIndex={active ? 0 : -1}
onClick={() => onSelect(tab.id)}
onKeyDown={(event) => onKeyDown(event, index)}
className={`-mb-px inline-flex items-center gap-2 rounded-t-md border-b-2 px-4 py-2 text-sm font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring ${
active ? 'border-primary text-foreground' : 'border-transparent text-muted-foreground hover:text-foreground'
}`}
data-testid={`hero-tab-${tab.id}`}
>
{tab.label}
{tab.note && <span className="text-xs font-normal text-muted-foreground">({tab.note})</span>}
{tab.changed && (
<>
<span className="size-2 rounded-full bg-amber-500" aria-hidden="true" data-testid={`hero-tab-${tab.id}-changed`} />
<span className="sr-only">{tab.changedText}</span>
</>
)}
{tab.invalid && (
<>
<span className="inline-flex size-4 items-center justify-center rounded-full bg-destructive text-[10px] font-bold text-destructive-foreground" aria-hidden="true" data-testid={`hero-tab-${tab.id}-invalid`}>
!
</span>
<span className="sr-only">{tab.invalidText}</span>
</>
)}
</button>
);
})}
</div>
);
}