import { createContext, useContext, useMemo, type AnchorHTMLAttributes, type ButtonHTMLAttributes, type HTMLAttributes, type ReactNode, } from 'react'; import { cx } from '../utils/cx'; import { useControllableState } from '../hooks/useControllableState'; interface TabBarContextValue { current: string; setCurrent: (value: string) => void; } const TabBarContext = createContext(null); function useTabBar(): TabBarContextValue { const context = useContext(TabBarContext); if (!context) throw new Error(' must be rendered inside .'); return context; } export interface TabBarProps extends Omit, 'onChange'> { value?: string; defaultValue?: string; onValueChange?: (value: string) => void; /** Show only below 768px. `always` keeps it on desktop too. */ visibility?: 'mobile' | 'always'; children?: ReactNode; } /** * Primary mobile navigation: three to five destinations, icon plus label, * optional badge, and safe-area padding. Distinct from Tabs (in-page) and * Navbar (top chrome). */ export function TabBar({ value, defaultValue = '', onValueChange, visibility = 'mobile', className, children, ...rest }: TabBarProps) { const [current, setCurrent] = useControllableState(value, defaultValue, onValueChange); const context = useMemo(() => ({ current, setCurrent }), [current, setCurrent]); return ( ); } type TabBarItemShared = { value: string; icon: ReactNode; label: ReactNode; badge?: ReactNode; }; export type TabBarItemProps = TabBarItemShared & ( | ({ href: string } & Omit, 'children' | 'href'>) | ({ href?: undefined } & Omit, 'children' | 'value'>) ); export function TabBarItem({ value, icon, label, badge, className, href, ...rest }: TabBarItemProps) { const bar = useTabBar(); const selected = bar.current === value; const classNames = cx('mors-tab-bar-item', selected && 'mors-tab-bar-item--selected', className); const body = ( <> {icon} {badge != null && {badge}} {label} ); if (href) { const { onClick, ...linkRest } = rest as Omit< AnchorHTMLAttributes, 'href' | 'children' | 'className' >; return ( { onClick?.(event); if (!event.defaultPrevented) bar.setCurrent(value); }} {...linkRest} > {body} ); } const { onClick, ...buttonRest } = rest as Omit< ButtonHTMLAttributes, 'children' | 'className' | 'value' >; return ( ); }