'use client' import { Collapsible, YStack } from '@hanzo/gui'; import { css, sx } from '@hanzo/ui' import React from 'react'; import type { LinkProps } from './link'; import { Link } from './link'; // Two disclosure shapes on gui's Collapsible: one that hides a block of detail // behind a "View details" link, one that cuts a long list down to its first few // rows. // // The trigger is the `Link` itself rather than a wrapping `Collapsible.Trigger`. // gui's Trigger renders its OWN element — it has no `asChild` — so wrapping // would nest a button around an anchor, which is invalid HTML and swallows the // link's own keyboard behaviour. The state is already controlled here, so the // only thing the wrapper contributed was `aria-expanded`, and that is a prop. /** An effect that skips the first mount. */ function useUpdateEffect(effect: React.EffectCallback, deps: React.DependencyList): void { const isFirstMount = React.useRef(true); React.useEffect(() => { if (isFirstMount.current) { isFirstMount.current = false; return; } return effect(); // eslint-disable-next-line react-hooks/exhaustive-deps }, deps); } function scrollToElement(id: string): void { document.getElementById(id)?.scrollIntoView({ behavior: 'smooth', block: 'start' }); } export interface CollapsibleDetailsProps extends LinkProps { children: React.ReactNode; id?: string; isExpanded?: boolean; text?: [string, string]; noScroll?: boolean; } const CUT_ID = 'CollapsibleDetails'; export const CollapsibleDetails = (props: CollapsibleDetailsProps) => { const { children, id = CUT_ID, onClick, isExpanded: isExpandedProp = false, text: textProp, loading, noScroll, ...rest } = props; const [ isExpanded, setIsExpanded ] = React.useState(isExpandedProp); const handleClick = React.useCallback((event: React.MouseEvent) => { setIsExpanded((flag) => !flag); if (!noScroll) scrollToElement(id); onClick?.(event); }, [ id, noScroll, onClick ]); useUpdateEffect(() => { setIsExpanded(isExpandedProp); if (isExpandedProp && !noScroll) scrollToElement(id); }, [ isExpandedProp, id, noScroll ]); const text = isExpanded ? (textProp?.[1] ?? 'Hide details') : (textProp?.[0] ?? 'View details'); return ( { text } { children } ); }; // `items` and `text` are this component's own nouns — the rows, and the pair of // trigger labels. On gui they are the `alignItems` / `textAlign` shorthands. // Ours win; the shorthands stay reachable under their full names. interface CollapsibleListProps extends Omit, 'children' | 'text' | 'items'> { items: Array; renderItem: (item: T, index: number) => React.ReactNode; triggerProps?: LinkProps; cutLength?: number; text?: [React.ReactNode, React.ReactNode]; defaultExpanded?: boolean; } const CUT_LENGTH = 3; export const CollapsibleList = (props: CollapsibleListProps) => { const { items, renderItem, triggerProps, cutLength = CUT_LENGTH, text: textProp, defaultExpanded = false, ...rest } = props; const [ isExpanded, setIsExpanded ] = React.useState(defaultExpanded); React.useEffect(() => { setIsExpanded(defaultExpanded); }, [ defaultExpanded ]); const handleToggle = React.useCallback(() => setIsExpanded((flag) => !flag), []); const text = isExpanded ? (textProp?.[1] ?? 'Hide') : (textProp?.[0] ?? 'Show all'); return ( { items.slice(0, isExpanded ? undefined : cutLength).map(renderItem) } { items.length > cutLength && ( { text } ) } ); };