Files
doona/src/shell/HubBar.tsx
T
Zakk 87778df73c refactor(ui): share one button style between Button and Link
ButtonStyle and buttonClass() carry the quiet, small, icon, accent and negative looks, so button-dressed links take props instead of class strings. The hub and side navigation use the kit Link, and the arrange tray's drag slot, which has to stay a react-aria button, takes its classes from buttonClass().
2026-09-26 05:22:47 +10:00

72 lines
2.7 KiB
TypeScript

import {useEffect, useState} from 'react';
import {useT} from '../i18n';
import {storageKeys} from '../api/storage';
import {Link, useSlider} from '../ui/ui';
import type {NavGroup} from './view';
const key = storageKeys.hubPages;
// Moving between hubs, or between a hub's pages, replaces the history entry as an Android navigation bar does, so Back
// leaves the app's top level instead of retracing every tap.
const replace = {replace: true};
function readLast(): Record<string, string> {
try {
return JSON.parse(sessionStorage.getItem(key) ?? '{}') ?? {};
} catch {
return {};
}
}
// Below the side navigation's breakpoint, the hubs sit in a bottom bar after the Material 3 navigation bar: an icon
// and a label each, the open hub's icon on a pill. A hub opens on the page last seen in it this session.
export function HubBar({groups}: {groups: NavGroup[]}) {
const t = useT();
const [last, setLast] = useState(readLast);
const open = groups.flatMap(group => group.items.filter(item => item.current).map(item => [group.id, item.path] as const))[0];
if (open && last[open[0]] !== open[1]) setLast({...last, [open[0]]: open[1]});
useEffect(() => {
try {
sessionStorage.setItem(key, JSON.stringify(last));
} catch {
/* Storage can be unavailable; the bar then remembers for this page load. */
}
}, [last]);
return (
<nav className="rp-hubbar" aria-label={t('hubs')}>
{groups.map(group => {
const Icon = group.items[0].Icon;
return (
<Link
key={group.id}
routerOptions={replace}
href={(group.items.find(item => item.path === last[group.id]) ?? group.items[0]).href}
aria-current={open?.[0] === group.id ? 'page' : undefined}
>
<span className="rp-hubbar-pill">
<Icon />
</span>
{group.label}
</Link>
);
})}
</nav>
);
}
// The open hub's pages, above the content: links in the segmented control's look, since picking one navigates rather
// than sets a value, and the pages carry tabs of their own.
export function HubPages({hub, route}: {hub: NavGroup; route: string}) {
const [ref, pos] = useSlider(route, '[aria-current="page"]');
return (
<nav className="rp-hubnav" aria-label={hub.label}>
<div ref={ref} className="rp-seg">
{pos && <span className="rp-slider" data-still={pos.still || undefined} style={{left: pos.x, width: pos.w}} />}
{hub.items.map(item => (
<Link key={item.id} appearance="button" href={item.href} routerOptions={replace} aria-current={item.current ? 'page' : undefined}>
{item.label}
</Link>
))}
</div>
</nav>
);
}