'use client' import { Tabs, Text, useTabsContext } from '@hanzo/gui'; import * as React from 'react'; import { ink } from './ink'; // The Lux tab skin, on the gui engine. // // gui's Tabs already owns everything Radix was imported for: roving focus, // `activationMode`, the `data-state="active" | "inactive"` contract, and an // `activeStyle` prop that applies while a tab is selected. So this file is a // SKIN — a variant table and three wrappers — and nothing else. // // The styles are inline props rather than `styled()` wrappers, because // `Tabs.Tab` is `TabFrame.styleable(...)`: an HOC, not a frame. `styled()` over // one does not compile style props to classes — they leak onto the element as // lowercase HTML attributes (`backgroundcolor="…"`) and nothing is styled. // Extending `Tabs.Tab` would look right, typecheck, and render unstyled. // // What went with Radix: a `useTabsContext` that read `variant` and `size` off a // `data-*` attribute of the nearest ancestor DOM node, inside a `useEffect`. It // could only answer AFTER paint, so every trigger rendered one frame in the // wrong variant and then corrected itself, and during SSR it could not answer at // all. Variant and size are props of the root, so they travel as React context. type TabsVariant = 'solid' | 'secondary' | 'segmented' | 'unstyled'; type TabsSize = 'sm' | 'md' | 'free'; type Style = Record; /** Variant and size, declared on the root and read by every trigger. */ const Shape = React.createContext<{ variant: TabsVariant; size: TabsSize; fitted: boolean }>({ variant: 'solid', size: 'md', fitted: false, }); const SIZE: Record = { sm: { height: 32, minWidth: 32, paddingVertical: 4, paddingHorizontal: 12, fontSize: 14 }, md: { height: 40, minWidth: 40, paddingVertical: 8, paddingHorizontal: 16, fontSize: 16 }, free: {}, }; const SELECTED_BG = 'var(--color-selected-control-bg)'; const SELECTED_FG = 'var(--color-selected-control-text)'; // Resting look and selected look, per variant, as two plain objects that get // MERGED here rather than handed to gui's `activeStyle`. // // `activeStyle` is gui's own prop for "while this tab is selected", and it does // not survive contact with a variant that also styles the resting state: the // trigger spreads `activeStyle` and only THEN spreads the caller's remaining // props, so any key set in both — `backgroundColor` for solid, `borderColor` // for secondary — resolves to the resting value even while selected. Measured: // the selected tab's computed background stayed `rgba(0, 0, 0, 0)`, identical // to an unselected one, with every behavioural test still passing. // // Selection is readable instead (`useTabsContext`), so the style is a function // of state and one object reaches gui with nothing left to override it. const VARIANT: Record = { solid: { rest: { fontWeight: '600', borderRadius: 6, color: 'var(--color-tabs-solid-fg)', hoverStyle: { color: 'var(--color-hover)' }, }, active: { backgroundColor: SELECTED_BG, color: SELECTED_FG }, }, secondary: { rest: { fontWeight: '500', borderRadius: 6, borderWidth: 2, borderColor: 'var(--color-tabs-secondary-border)', color: 'var(--color-tabs-secondary-fg)', hoverStyle: { color: 'var(--color-hover)', borderColor: 'var(--color-hover)' }, }, active: { backgroundColor: SELECTED_BG, color: SELECTED_FG, borderColor: 'transparent' }, }, segmented: { rest: { borderWidth: 2, borderColor: SELECTED_BG, color: 'var(--color-text-primary)', hoverStyle: { color: 'var(--color-hover)' }, }, active: { backgroundColor: SELECTED_BG, borderColor: SELECTED_BG, color: SELECTED_FG }, }, unstyled: { rest: {}, active: {} }, }; const LIST_HEIGHT: Record = { sm: 32, md: 40, free: undefined }; // --------------------------------------------------------------------------- // Root // --------------------------------------------------------------------------- // `size` is narrowed away from gui's own token-valued `size` on purpose: a Lux // tab bar has three sizes, not the whole size ramp. export interface TabsProps extends Omit, 'onValueChange' | 'size'> { /** Visual variant. Default: "solid". */ variant?: TabsVariant; /** Size preset. Default: "md". */ size?: TabsSize; /** Stretch triggers to fill the list width. */ fitted?: boolean; /** Fires with the newly selected value. */ onValueChange?: (details: { value: string }) => void; } export const TabsRoot = ({ variant = 'solid', size = 'md', fitted = false, onValueChange, ...rest }: TabsProps): React.ReactElement => { const shape = React.useMemo(() => ({ variant, size, fitted }), [ variant, size, fitted ]); return ( onValueChange({ value }) : undefined } { ...rest } /> ); }; // --------------------------------------------------------------------------- // List // --------------------------------------------------------------------------- export type TabsListProps = React.ComponentProps; export const TabsList = (props: TabsListProps): React.ReactElement => { const { size } = React.useContext(Shape); return ( ); }; // --------------------------------------------------------------------------- // Trigger // --------------------------------------------------------------------------- export type TabsTriggerProps = React.ComponentProps; export const TabsTrigger = ({ children, ...props }: TabsTriggerProps): React.ReactElement => { const { variant, size, fitted } = React.useContext(Shape); const { rest, active } = VARIANT[variant]; const selected = useTabsContext().value === props.value; return ( { ink(children, undefined, { fontSize: 'inherit', color: 'inherit', fontWeight: 'inherit' }) } ); }; // --------------------------------------------------------------------------- // Content // --------------------------------------------------------------------------- export type TabsContentProps = React.ComponentProps; export const TabsContent = ({ children, ...props }: TabsContentProps): React.ReactElement => ( { ink(children) } ); // --------------------------------------------------------------------------- // Counter // --------------------------------------------------------------------------- export interface TabsCounterProps { count?: number | null; } const COUNTER_OVERLOAD = 50; /** * The count beside a tab label. Zero is deliberately dimmer than a real count — * "nothing here" should read quieter than "three things here". */ export const TabsCounter = ({ count }: TabsCounterProps): React.ReactElement | null => { if (count === undefined || count === null) return null; return ( 0 ? 'var(--color-text-secondary)' : 'var(--color-text-disabled)') as never }> { count > COUNTER_OVERLOAD ? `${ COUNTER_OVERLOAD }+` : count } ); };