Tabs
A component for toggling between related panels on the same page.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Tabs;
use function Phem\Ui\{TabsIndicator, TabsList, TabsPanel, TabsTab};
#[Component]
function TabsHeroDemo(): Node
{
return (
<Tabs defaultValue="overview">
<TabsList>
<TabsTab value="overview">Overview</TabsTab>
<TabsTab value="projects">Projects</TabsTab>
<TabsTab value="account">Account</TabsTab>
<TabsIndicator />
</TabsList>
<div className="grid w-full min-h-32 grid-cols-1 border border-neutral-950 dark:border-white">
<TabsPanel value="overview">
<p>Workspace stats and activity.</p>
</TabsPanel>
<TabsPanel value="projects">
<p>Milestones and deadlines.</p>
</TabsPanel>
<TabsPanel value="account">
<p>Profile and preferences.</p>
</TabsPanel>
</div>
</Tabs>
);
}
Examples#
Animated panels#
Animate panels as they activate using the data-starting-style and data-ending-style attributes.
The data-activation-direction attribute indicates which direction the newly active tab is relative to the previously active one, letting panels slide in from the correct side.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Tabs;
use function Phem\Ui\{TabsIndicator, TabsList, TabsPanel, TabsTab};
#[Component]
function TabsAnimatedPanelsDemo(): Node
{
$panel = 'col-start-1 row-start-1 flex w-full items-center justify-center bg-transparent p-4 text-center text-sm text-neutral-950 outline-none focus-visible:z-1 focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white dark:text-white [transition:opacity_175ms_ease,translate_350ms_cubic-bezier(0.22,1,0.36,1)] data-starting-style:opacity-0 data-ending-style:opacity-0 motion-safe:data-starting-style:data-[activation-direction=left]:translate-x-[-50%] motion-safe:data-starting-style:data-[activation-direction=right]:translate-x-[50%] motion-safe:data-ending-style:data-[activation-direction=left]:translate-x-[50%] motion-safe:data-ending-style:data-[activation-direction=right]:translate-x-[-50%]';
return (
<Tabs id="animated" defaultValue="overview">
<TabsList>
<TabsTab value="overview">Overview</TabsTab>
<TabsTab value="projects">Projects</TabsTab>
<TabsTab value="account">Account</TabsTab>
<TabsIndicator />
</TabsList>
<div className="relative grid min-h-32 w-full grid-cols-1 overflow-hidden border border-neutral-950 bg-white dark:border-white dark:bg-neutral-950">
<TabsPanel value="overview" className={{ panel }}>
<p>Workspace stats and activity.</p>
</TabsPanel>
<TabsPanel value="projects" className={{ panel }}>
<p>Milestones and deadlines.</p>
</TabsPanel>
<TabsPanel value="account" className={{ panel }}>
<p>Profile and preferences.</p>
</TabsPanel>
</div>
</Tabs>
);
}
#
The hiddenUntilFound prop hides the panels that are not selected with hidden="until-found", so the browser can search their contents. A match in a hidden panel selects its tab. It can be set on each TabsPanel, or once on Tabs to apply to all panels.
To try it, press Ctrl+F (Cmd+F on macOS) and search for “deadlines”—the browser selects the tab of the panel that has the match. Older browsers that don’t support hidden="until-found" keep the panels hidden, and find-in-page skips over the contents.
API reference#
Import the components of the family:
use Phem\Ui\Tabs;
use function Phem\Ui\{TabsList, TabsTab, TabsIndicator, TabsPanel};Tabs#
Tabs and their panels: Base UI Tabs.Root, on the phem-tabs controller.
defaultValue selects a tab on load: the value of a tab. Each tab shows the panel with the same value. With no defaultValue, the first tab that is not disabled is selected. With hiddenUntilFound, a hidden panel has hidden="until-found": the search of the browser finds its text and selects its tab. A tab is not a link: for links to pages, use a <nav> with aria-current="page", or NavigationMenu.
| Prop | Type | Default |
|---|---|---|
defaultValue | ?string | null |
orientation | 'horizontal' | 'vertical' | Orientation::Horizontal |
activateOnFocus | bool | false |
loopFocus | bool | true |
hiddenUntilFound | bool | false |
children | ?Html | null |
...attrs | array | [] |
TabsTab#
A tab: a button that selects its panel.
| Prop | Type | Default |
|---|---|---|
value (required) | string | — |
disabled | bool | false |
children | ?Html | null |
...rest | mixed | — |
TabsPanel#
The content of one tab: it shows when the TabsTab with the same value is active. With hiddenUntilFound, this hidden panel has hidden="until-found", as with the prop of the root.
| Prop | Type | Default |
|---|---|---|
value (required) | string | — |
hiddenUntilFound | bool | false |
children | ?Html | null |
...rest | mixed | — |
TabsList#
The row of the TabsTab buttons (tablist), with an optional TabsIndicator.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
TabsIndicator#
A box under the selected tab that moves with the selection (--active-tab-left, --active-tab-width).
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
The source of the examples: examples/ui/Tabs.phem.