/**
* Generic tab bar — a top-of-content tablist used to split a section's
* content into named panes (e.g. Typography's Overview / Font Pairings /
* Headings / Body Text / Links / Buttons / Misc). Generalized from the
* `aureon-admin__pills` filter pattern already used by Colors/Typography,
* but renders as `role="tablist"`/`role="tab"` with `aria-selected` since
* each tab now swaps the entire panel rather than filtering a shared list.
*/
import type { ReactNode } from 'react';
import { __ } from '@wordpress/i18n';
import { ChevronLeft, ChevronRight } from 'lucide-react';
import type { PanelIcon } from '../module-icons';
export interface TabItem {
key: string;
label: string;
/**
* Optional leading icon. `PanelIcon` (not `LucideIcon` directly) so a
* declarative surface's tab — resolved through `moduleIcon()`, which
* also answers with a brand mark sometimes — type-checks the same as
* every other icon field on that surface.
*/
icon?: PanelIcon;
/**
* `subnav` only: a one-line summary shown under the label in the row
* list (e.g. WooCommerce's "The shop and product category/tag grids."),
* so each row explains itself before you drill in. Rows without one keep
* the plain single-line layout — this is opt-in per tab list.
*/
description?: string;
}
interface TabsProps {
tabs: TabItem[];
active: string;
onChange: ( key: string ) => void;
idPrefix: string;
/**
* Optional right-aligned content docked to the end of the tab row (e.g. the
* Header section's template switcher). When present, the tab buttons group to
* the left and this renders on the right.
*/
trailing?: ReactNode;
/**
* `underline` (the default) is the section-splitting tab row — a baseline of
* tabs with the active one underlined. `segmented` is the same tablist drawn
* as one grouped control with equal halves, for a section whose tabs are two
* ways of doing the same thing rather than separate topics (Colors: pick a
* preset, or set colours yourself). `subnav` drops the tab row entirely and
* makes each tab a screen of its own, reached from a list of rows — the only
* thing that works for a section with more tabs than fit one 320px line.
* `cards` draws the same few tabs `underline` would, but as always-visible,
* evenly-split cards (icon, title, one-line description) with the active
* one filled solid — for a handful of genuinely different destinations
* (Fields/Settings/Style) rather than a scan-and-pick list.
*/
variant?: 'underline' | 'segmented' | 'subnav' | 'cards';
/**
* `subnav` only: what the back row goes back *to*, i.e. the section's own
* name. Defaults to a plain "Back".
*/
backLabel?: string;
/**
* `subnav` only: `cards` draws each row as its own bordered card (roomier
* icon tile, title, description, chevron) instead of one grouped list —
* for a hub with only a few destinations, each worth its own line of copy
* (Content Templates' four views). Hover lifts a card with a brand border
* and fills its chevron.
*/
subnavStyle?: 'rows' | 'cards';
}
export default function Tabs( {
tabs,
active,
onChange,
idPrefix,
trailing,
variant = 'underline',
backLabel,
subnavStyle,
}: TabsProps ) {
if ( 'subnav' === variant ) {
return (
);
}
if ( 'cards' === variant ) {
return (
);
}
const variantClass =
'segmented' === variant ? ' aureon-tabs--segmented' : '';
const list = (
);
}
/**
* A section's tabs as a sub-navigation: a list of rows, one per tab, each a
* screen you drill into.
*
* The pane is 320px wide, which fits about two tabs to a line — Typography's six
* wrapped to three ragged rows, and no tab row reads well that way. Drilling is
* the same move the panel's own list makes one level up, so the rows are drawn to
* match those, chevron and all.
*
* An empty `active` means "no screen open", i.e. show the list. The open screen's
* heading carries the id `TabPanel` already points its `aria-labelledby` at, so
* the panel stays labelled by its own title even though the button that used to
* carry that id is no longer in the DOM.
*
* @param props Component props.
* @param props.tabs The tabs.
* @param props.active Open tab key, or '' for the list.
* @param props.onChange Called with a tab key, or '' when going back.
* @param props.idPrefix Id prefix shared with `TabPanel`.
* @param props.backLabel What the back row goes back to.
* @param props.subnavStyle Grouped rows, or one card per destination.
*/
function SubNav( {
tabs,
active,
onChange,
idPrefix,
backLabel,
subnavStyle = 'rows',
}: Omit< TabsProps, 'variant' | 'trailing' > ) {
const openTab = tabs.find( ( tab ) => tab.key === active );
const iconSize = ( tab: TabItem ) => {
if ( 'cards' === subnavStyle ) {
return 20;
}
return tab.description ? 18 : 16;
};
if ( openTab ) {
const Icon = openTab.icon;
return (
);
}
/**
* `cards` variant: the same tabs as `underline`, drawn as always-visible,
* evenly-split cards instead of a thin baseline row — each carries its own
* icon, title, and one-line description, with the active one filled solid.
* Unlike `SubNav`, nothing here ever drills in or shows a back row; `active`
* only decides which card is filled, the same as `underline`'s own tablist.
*
* @param props Component props.
* @param props.tabs The tabs.
* @param props.active The active tab's key.
* @param props.onChange Called with the clicked tab's key.
* @param props.idPrefix Id prefix shared with `TabPanel`.
*/
function CardTabs( {
tabs,
active,
onChange,
idPrefix,
}: Omit< TabsProps, 'variant' | 'trailing' | 'backLabel' > ) {
return (
);
}
interface TabPanelProps {
tabKey: string;
activeKey: string;
idPrefix: string;
children: ReactNode;
/**
* Keep the panel's DOM around (hidden via the native `hidden` attribute)
* instead of unmounting it when another tab is active. Every other panel
* is plain React content, cheap to remount from scratch each time it
* comes back — this exists for the one case that isn't: Checkout's Form
* tab holds a handful of WooCommerce core's own native controls, moved
* there by direct DOM surgery (`customize-bridge`'s controls aren't
* React's to re-render), which would be torn down and lost the moment
* their parent unmounted.
*/
keepMounted?: boolean;
}
export function TabPanel( {
tabKey,
activeKey,
idPrefix,
children,
keepMounted = false,
}: TabPanelProps ) {
if ( tabKey !== activeKey && ! keepMounted ) {
return null;
}
return (