UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

162 lines (160 loc) 4.78 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { FloatingFocusManager } from '@floating-ui/react'; import { useDialogPopup } from './useDialogPopup.js'; import { useDialogRootContext } from '../root/DialogRootContext.js'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { refType } from '../../utils/proptypes.js'; import { popupStateMapping as baseMapping } from '../../utils/popupStateMapping.js'; import { useForkRef } from '../../utils/useForkRef.js'; import { transitionStatusMapping } from '../../utils/styleHookMapping.js'; import { DialogPopupCssVars } from './DialogPopupCssVars.js'; import { DialogPopupDataAttributes } from './DialogPopupDataAttributes.js'; import { jsx as _jsx } from "react/jsx-runtime"; const customStyleHookMapping = { ...baseMapping, ...transitionStatusMapping, hasNestedDialogs(value) { return value ? { [DialogPopupDataAttributes.hasNestedDialogs]: '' } : null; } }; /** * A container for the dialog contents. * Renders a `<div>` element. * * Documentation: [Base UI Dialog](https://base-ui.com/react/components/dialog) */ const DialogPopup = /*#__PURE__*/React.forwardRef(function DialogPopup(props, forwardedRef) { const { className, finalFocus, id, initialFocus, keepMounted = false, render, ...other } = props; const { descriptionElementId, dismissible, floatingRootContext, getPopupProps, modal, mounted, nested, nestedOpenDialogCount, setOpen, open, openMethod, popupRef, setPopupElement, setPopupElementId, titleElementId, transitionStatus } = useDialogRootContext(); const mergedRef = useForkRef(forwardedRef, popupRef); const { getRootProps, floatingContext, resolvedInitialFocus } = useDialogPopup({ descriptionElementId, floatingRootContext, getPopupProps, id, initialFocus, modal, mounted, setOpen, open, openMethod, ref: mergedRef, setPopupElement, setPopupElementId, titleElementId }); const state = { open, nested, transitionStatus, hasNestedDialogs: nestedOpenDialogCount > 0 }; const { renderElement } = useComponentRenderer({ render: render ?? 'div', className, state, propGetter: getRootProps, extraProps: { ...other, style: { ...other.style, [DialogPopupCssVars.nestedDialogs]: nestedOpenDialogCount } }, customStyleHookMapping }); if (!keepMounted && !mounted) { return null; } return /*#__PURE__*/_jsx(FloatingFocusManager, { context: floatingContext, modal: open, disabled: !mounted, closeOnFocusOut: dismissible, initialFocus: resolvedInitialFocus, returnFocus: finalFocus, outsideElementsInert: modal, children: renderElement() }); }); process.env.NODE_ENV !== "production" ? DialogPopup.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * @ignore */ children: PropTypes.node, /** * CSS class applied to the element, or a function that * returns a class based on the component’s state. */ className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), /** * Determines the element to focus when the dialog is closed. * By default, focus returns to the trigger. */ finalFocus: refType, /** * @ignore */ id: PropTypes.string, /** * Determines the element to focus when the dialog is opened. * By default, the first focusable element is focused. */ initialFocus: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.func, refType]), /** * Whether to keep the HTML element in the DOM while the dialog is hidden. * @default false */ keepMounted: PropTypes.bool, /** * Allows you to replace the component’s HTML element * with a different tag, or compose it with another component. * * Accepts a `ReactElement` or a function that returns the element to render. */ render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]), /** * @ignore */ style: PropTypes.object } : void 0; export { DialogPopup };