import { Drawer as _Drawer } from '@base-ui/react/drawer'; import clsx from 'clsx'; import { forwardRef } from '@wordpress/element'; import { useMergeRefs } from '@wordpress/compose'; import { useDeprioritizedInitialFocus } from '../utils/use-deprioritized-initial-focus'; import { SCROLL_CONTAINER_ATTR } from '../utils/use-overlay-scroll-state-attributes'; import { renderSlotWithChildren } from '../utils/render-slot-with-children'; import { DrawerValidationProvider, useDrawerModal } from './context'; import { Portal } from './portal'; import styles from './style.module.scss'; import type { PopupProps } from './types'; const CLOSE_ICON_ATTR = 'data-wp-ui-drawer-close-icon'; /** * Renders the drawer popup element that contains the drawer content. * Uses a portal to render outside the DOM hierarchy. * * When `portal` is omitted, defaults to `Drawer.Portal`. * * The popup is a flex column; scroll ownership lives on `Drawer.Content`, * which children are expected to render. Without it, long body content will * clip instead of scrolling and Base UI's swipe-dismiss-on-scroll-edge * logic on up/down drawers cannot engage. */ const Popup = forwardRef< HTMLDivElement, PopupProps >( function DrawerPopup( { className, portal, children, size, initialFocus, finalFocus, ...props }, ref ) { const { resolvedInitialFocus, popupRef } = useDeprioritizedInitialFocus( { initialFocus, deprioritizedAttributes: [ CLOSE_ICON_ATTR, SCROLL_CONTAINER_ATTR ], } ); const mergedRef = useMergeRefs( [ ref, popupRef ] ); const modal = useDrawerModal(); const portalChildren = ( <> { /* * Only render a backdrop for fully modal drawers. Non-modal drawers * should not dim the page, and `trap-focus` keeps outside pointer * interactions enabled, so a backdrop would misrepresent that mode. */ } { modal === true && ( <_Drawer.Backdrop className={ styles.backdrop } data-testid="drawer-backdrop" /> ) } <_Drawer.Viewport className={ styles.viewport }> <_Drawer.Popup ref={ mergedRef } className={ ( state ) => { const isVertical = state.swipeDirection === 'up' || state.swipeDirection === 'down'; const resolvedSize = size ?? ( isVertical ? 'auto' : 'medium' ); return clsx( styles.popup, className, styles[ `is-${ resolvedSize }` ] ); } } initialFocus={ resolvedInitialFocus } finalFocus={ finalFocus } { ...props } data-wp-ui-overlay-modal={ modal === true ? '' : undefined } > { children } ); return renderSlotWithChildren( portal, , portalChildren ); } ); export { Popup };