'use client'
import { Accordion, View, XStack } from '@hanzo/gui';
import * as React from 'react';
import { ink } from './ink';
// The Lux accordion, on gui's Accordion.
//
// Everything behavioural belongs to the primitive and none of it is
// re-implemented here: roving Arrow/Home/End focus on the root, `aria-expanded`
// + `aria-controls` on the trigger, `role="region"` + `aria-labelledby` on the
// content. This file names the four parts and dresses them.
//
// Three corrections the primitive needs, each of which is a real bug if skipped:
//
// 1. gui's `Accordion.Header` renders an `
` at the display type scale. An
// accordion is a list of sections inside a page that already has a title, so
// unqualified every row publishes a competing document heading and a screen
// reader's heading list comes out as N level-1s. `headingLevel` re-hosts it
// and sets the matching `aria-level`, which the inherited `role="heading"`
// requires to be valid.
// 2. `unstyled` means gui applies nothing — which on the web leaves the USER
// AGENT's chrome, and a bare `` is #efefef with an outset border.
// Unstyled has to mean "no chrome", so the reset is explicit.
// 3. That trigger is a bare ``, and a bare button inside a form
// SUBMITS it. `type="button"` or opening a section posts the form.
//
// The indicator's open state comes from the trigger's function child — gui
// exports no per-item context, and that is the only seam. It replaces a
// `group-data-[state=open]/trigger:` Tailwind chain that could not have worked
// here anyway: gui compiles style props to atomic classes and has no
// parent-state selector.
const IndicatorIcon = () => (
);
const BAR = 'var(--color-text-primary)' as never;
/**
* The open/closed mark. The FAQ variant is a plus that becomes a minus: two
* bars, and the upright one turns a quarter turn onto the flat one.
*/
const Indicator = ({ open, variant }: { open: boolean; variant?: 'outline' | 'faq' }) => (
variant === 'faq' ? (
) : (
)
);
// ---------------------------------------------------------------------------
// Root
// ---------------------------------------------------------------------------
export interface AccordionRootProps
extends Omit, 'type' | 'value' | 'defaultValue' | 'onValueChange' | 'size'> {
/** Controlled open items. */
readonly value?: Array;
/** Uncontrolled initial open items. */
readonly defaultValue?: Array;
/** Fires with the full set of open items. */
readonly onValueChange?: (details: { value: Array }) => void;
readonly variant?: 'outline' | 'faq';
readonly size?: 'sm' | 'md';
}
const Variant = React.createContext<'outline' | 'faq' | undefined>(undefined);
export const AccordionRoot = ({
value,
defaultValue,
onValueChange,
variant,
size,
children,
...rest
}: AccordionRootProps): React.ReactElement => (
) => onValueChange({ value: next }) : undefined }
data-variant={ variant }
data-size={ size }
{ ...rest }
>
{ children }
);
// ---------------------------------------------------------------------------
// Item
// ---------------------------------------------------------------------------
export type AccordionItemProps = React.ComponentProps;
export const AccordionItem = (props: AccordionItemProps): React.ReactElement => (
);
// ---------------------------------------------------------------------------
// Trigger
// ---------------------------------------------------------------------------
export type AccordionItemTriggerProps = React.ComponentProps & {
readonly indicatorPlacement?: 'start' | 'end';
readonly noIndicator?: boolean;
readonly variant?: 'outline' | 'faq';
readonly headingLevel?: 1 | 2 | 3 | 4 | 5 | 6;
/** The DOM button type — see (3) above. Defaults to `button`, not submit. */
readonly type?: 'button' | 'submit' | 'reset';
};
export const AccordionItemTrigger = ({
children,
indicatorPlacement: placementProp,
variant: variantProp,
noIndicator,
headingLevel = 3,
...rest
}: AccordionItemTriggerProps): React.ReactElement => {
const variant = variantProp ?? React.useContext(Variant);
const placement = variant === 'faq' ? 'start' : (placementProp ?? 'end');
return (
{ (({ open }: { open: boolean }) => (
<>
{ placement === 'start' && !noIndicator && }
{ ink(children, undefined, { color: 'inherit', textAlign: 'left' }) }
{ placement === 'end' && !noIndicator && }
>
)) as unknown as React.ReactNode }
);
};
// ---------------------------------------------------------------------------
// Content
// ---------------------------------------------------------------------------
export type AccordionItemContentProps = React.ComponentProps;
export const AccordionItemContent = ({ children, ...rest }: AccordionItemContentProps): React.ReactElement => (
{ ink(children, undefined, { color: 'var(--color-text-primary)' }) }
);
// ---------------------------------------------------------------------------
// useAccordion
// ---------------------------------------------------------------------------
/** Open state for an accordion whose items are addressable by URL fragment. */
export function useAccordion(items: Array<{ id: string }>) {
const [ value, setValue ] = React.useState>([]);
const onValueChange = React.useCallback(({ value: next }: { value: Array }) => {
setValue(next);
}, []);
const scrollToItemFromUrl = React.useCallback(() => {
const hash = window.location.hash.replace('#', '');
if (!hash) return;
const itemToScroll = items.find((item) => item.id === hash);
if (!itemToScroll) return;
document.getElementById(itemToScroll.id)?.scrollIntoView({ behavior: 'smooth', block: 'start' });
setValue([ itemToScroll.id ]);
}, [ items ]);
return React.useMemo(
() => ({ value, onValueChange, scrollToItemFromUrl }),
[ value, onValueChange, scrollToItemFromUrl ],
);
}