'use client' import { Dialog, Sheet as GuiSheet, View, XStack, YStack } from '@hanzo/gui'; import * as React from 'react'; import { CloseButton } from './close-button'; import { ink } from './ink'; import { useIsNarrow } from './use-narrow'; // A drawer is a side panel on a wide screen and a bottom sheet on a phone. Below // `NARROW_PX` the content is presented by gui's Sheet — draggable, with // snap-to-dismiss and momentum — which is what a native app does and what a // `position: fixed` panel with a slide-in keyframe cannot fake. Same components, // same props, at every call site. The breakpoint itself lives in ./use-narrow, // so "is this phone-sized?" has one answer across the package. // // Both presentations now come from the same engine: the wide panel is gui's // Dialog where it used to be Radix's. That is the whole reason the root has to // own its open state — it did already, because the Sheet lives outside the // dialog tree and needs the same state, and now both sides read it. // // The slide is `enterStyle` / `exitStyle` on the content, driven by the CSS // animation driver that lux.config feeds the engine. It is not decoration: the // panel is `position: fixed` at the edge it flies in from, so without the // transform it appears instantly, fully formed, which reads as a repaint bug. // ─── DrawerRoot ───────────────────────────────────────────────── export interface DrawerRootProps { children?: React.ReactNode; open?: boolean; defaultOpen?: boolean; onOpenChange?: (details: { open: boolean }) => void; placement?: Placement; initialFocusEl?: (() => HTMLElement | null) | React.RefObject; lazyMount?: boolean; unmountOnExit?: boolean; modal?: boolean; size?: Size; } type Placement = 'left' | 'right' | 'top' | 'bottom'; type Size = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full'; const DrawerPlacementContext = React.createContext('right'); const DrawerSizeContext = React.createContext('md'); interface DrawerStateValue { readonly open: boolean; readonly setOpen: (next: boolean) => void; readonly modal: boolean; readonly narrow: boolean; } const DrawerStateContext = React.createContext({ open: false, setOpen: () => { /* noop */ }, modal: true, narrow: false, }); export const DrawerRoot = (props: DrawerRootProps) => { const { children, open, defaultOpen, onOpenChange, placement = 'right', modal = true, size = 'md', } = props; const [ uncontrolled, setUncontrolled ] = React.useState(defaultOpen ?? false); const isControlled = open !== undefined; const isOpen = isControlled ? open : uncontrolled; const narrow = useIsNarrow(); const setOpen = React.useCallback((nextOpen: boolean) => { if (!isControlled) setUncontrolled(nextOpen); onOpenChange?.({ open: nextOpen }); }, [ isControlled, onOpenChange ]); const state = React.useMemo( () => ({ open: isOpen, setOpen, modal, narrow }), [ isOpen, setOpen, modal, narrow ], ); return ( { /* WHICH presentation is one decision, and it is made here. A gui Sheet inside a gui Dialog is not a sheet in a dialog: the Dialog publishes a sheet controller and adopts any Sheet beneath it, so the phone panel stopped rendering entirely the moment the wide path became a gui Dialog. On a phone there is no dialog — the Sheet IS the presentation — so no Dialog is mounted at all, and the trigger below reads the same flag. */ } { narrow ? children : ( { children } ) } ); }; // ─── DrawerContent ────────────────────────────────────────────── export interface DrawerContentProps extends React.ComponentPropsWithoutRef<'div'> { portalled?: boolean; portalRef?: React.RefObject; offset?: string | number; backdrop?: boolean; } /** Where the panel is pinned, and which way it flies in from. */ const ANCHOR: Record> = { right: { top: 0, bottom: 0, right: 0, enterStyle: { x: 24, opacity: 0 }, exitStyle: { x: 24, opacity: 0 } }, left: { top: 0, bottom: 0, left: 0, enterStyle: { x: -24, opacity: 0 }, exitStyle: { x: -24, opacity: 0 } }, top: { left: 0, right: 0, top: 0, enterStyle: { y: -24, opacity: 0 }, exitStyle: { y: -24, opacity: 0 } }, bottom: { left: 0, right: 0, bottom: 0, enterStyle: { y: 24, opacity: 0 }, exitStyle: { y: 24, opacity: 0 } }, }; const ACROSS: Record = { xs: 240, sm: 320, md: 384, lg: 480, xl: 640, full: '100%', }; /** The panel's measured axis: width for a side panel, height for a top/bottom one. */ const extent = (placement: Placement, size: Size): Record => ( placement === 'left' || placement === 'right' ? { width: ACROSS[size], maxWidth: '100%' } : { height: ACROSS[size], maxHeight: '100%' } ); const SCRIM = 'rgba(0,0,0,0.5)'; export const DrawerContent = React.forwardRef( function DrawerContent(props, ref) { const { children, portalled = true, portalRef: _portalRef, offset: _offset, backdrop = true, ...rest } = props; const placement = React.useContext(DrawerPlacementContext); const size = React.useContext(DrawerSizeContext); const { open, setOpen, modal, narrow } = React.useContext(DrawerStateContext); if (narrow) { return ( { backdrop && ( ) } { /* Sheet.Frame is a gui component, not a DOM node — the DOM ref the Chakra contract hands us has nothing to attach to here. */ } { children } ); } const content = ( <> { backdrop && ( ) } { children } ); return portalled ? { content } : content; }, ); // ─── Triggers ─────────────────────────────────────────────────── export const DrawerCloseTrigger = React.forwardRef< HTMLButtonElement, React.ComponentPropsWithoutRef<'button'> >(function DrawerCloseTrigger(props, ref) { return ( // Placement belongs to the surrounding box, not to the button: CloseButton // is a fixed 20px control and has no opinion about where it sits. ); }); export const DrawerTrigger = ({ asChild = true, children, ...rest }: React.ComponentProps) => { const { setOpen, narrow } = React.useContext(DrawerStateContext); // On a phone there is no Dialog above this, so there is no Dialog.Trigger to // be: the child opens the Sheet directly. if (narrow) { return React.isValidElement(children) ? React.cloneElement(children as React.ReactElement<{ onClick?: () => void }>, { onClick: () => setOpen(true) }) : <>{ children }; } return { children }; }; // ─── Sub-components ───────────────────────────────────────────── // These three take caller-authored content, so a bare string is the normal // case and has to be given a host — a View cannot lay one out. export const DrawerHeader = ({ children, ...props }: React.ComponentProps) => ( { ink(children) } ); export const DrawerBody = ({ children, ...props }: React.ComponentProps) => ( { ink(children) } ); export const DrawerFooter = ({ children, ...props }: React.ComponentProps) => ( { ink(children) } ); export const DrawerTitle = Dialog.Title; export const DrawerDescription = Dialog.Description; export const DrawerActionTrigger = Dialog.Close;