import { EnvironmentProvider } from '@ark-ui/react/environment'; import { type ReactNode, useEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { Modal, ModalBody, ModalContent } from '../../../modal/src'; import { Select, SelectContent, SelectControl } from '../../../select/src'; import { DRAWER_POSITION, Drawer, type DrawerProp, DrawerBody, DrawerContent, DrawerTrigger } from '../../src'; export default { component: Drawer, title: 'Tests behavior', }; // A tall page to check whether the background scrolls. const TallPage = ({ children }: { children: ReactNode }) => (
{ children }
); const DrawerWithFocusables = ({ children, contentId, triggerId, ...props }: DrawerProp & { children?: ReactNode, contentId?: string, triggerId?: string }) => ( Trigger { children } ); const BackgroundButton = () => { const [clicks, setClicks] = useState(0); return ( ); }; const PageText = () => (

Some page text

); // Removes itself from the DOM when clicked, while it has the focus. const SelfRemovingButton = () => { const [isShown, setIsShown] = useState(true); return isShown ? ( ) : null; }; export const backdrop = () => ( ); export const backdropNoInteractOutside = () => ( ); export const backdropWithSelect = () => ( { /* Portaled on purpose, unlike the documented `createPortal={ false }` (see the docs OverlayElements story), to test that interacting with a nested layer is not an outside interaction: the z-index lifts its list (dropdown layer) above the drawer. */ } ); export const noBackdrop = () => ( ); export const noBackdropCustomContentId = () => ( ); // Open by default: the opener is body, so the trigger must be found by its owner, whatever its id. export const noBackdropCustomTriggerId = () => ( ); // Open on mount: zag's machine has started before DrawerContent can give it its id. export const noBackdropOpenCustomContentId = () => ( ); // The content id changes while the drawer is open, e.g. derived from the item a details drawer shows. export const noBackdropChangingContentId = () => { const [item, setItem] = useState(1); return ( ); }; // A trigger without value next to valued ones, as a header button above one trigger per row. export const noBackdropValuedTriggers = () => ( Header trigger { ['1', '2', '3'].map((row) => ( Row { row } )) } ); // A form drawer whose Close button asks for a confirmation in a Modal: Discard closes both. export const noBackdropNestedModal = () => { const [isDrawerOpen, setIsDrawerOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false); return ( setIsDrawerOpen(open) } open={ isDrawerOpen }> setIsModalOpen(open) } open={ isModalOpen }> ); }; // Rendered in a shadow root, next to an input of the page; the drawer is closed by code. The root // is exposed for the tests, as a closed one cannot be reached from the page. const ShadowRootDrawer = ({ mode }: { mode: ShadowRootMode }) => { const hostRef = useRef(null); const [shadowRoot, setShadowRoot] = useState(); const [isOpen, setIsOpen] = useState(false); useEffect(() => { const host = hostRef.current as (HTMLDivElement & { drawerShadowRoot?: ShadowRoot }) | null; if (host) { host.drawerShadowRoot ??= host.attachShadow({ mode }); setShadowRoot(host.drawerShadowRoot); } }, [mode]); return ( <>
{ shadowRoot && createPortal( shadowRoot }>
setIsOpen(false) }> Close by code
setIsOpen(open) } open={ isOpen }>
, shadowRoot, ) } ); }; export const noBackdropInShadowRoot = () => ( ); export const noBackdropInClosedShadowRoot = () => ( ); // An iframe of the page, outside the drawer: a focus inside it fires no event in the page. export const noBackdropOutsideIframe = () => { const [isOpen, setIsOpen] = useState(false); return (