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.

160 lines (158 loc) 4.86 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { FloatingFocusManager } from '@floating-ui/react'; import { useDialogPopup } from '../../dialog/popup/useDialogPopup.js'; import { useAlertDialogRootContext } from '../root/AlertDialogRootContext.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 { AlertDialogPopupDataAttributes } from './AlertDialogPopupDataAttributes.js'; import { jsx as _jsx } from "react/jsx-runtime"; const customStyleHookMapping = { ...baseMapping, ...transitionStatusMapping, hasNestedDialogs(value) { return value ? { [AlertDialogPopupDataAttributes.hasNestedDialogs]: '' } : null; } }; /** * A container for the alert dialog contents. * Renders a `<div>` element. * * Documentation: [Base UI Alert Dialog](https://base-ui.com/react/components/alert-dialog) */ const AlertDialogPopup = /*#__PURE__*/React.forwardRef(function AlertDialogPopup(props, forwardedRef) { const { className, id, keepMounted = false, render, initialFocus, finalFocus, ...other } = props; const { descriptionElementId, floatingRootContext, getPopupProps, mounted, nested, nestedOpenDialogCount, setOpen, open, openMethod, popupRef, setPopupElement, setPopupElementId, titleElementId, transitionStatus } = useAlertDialogRootContext(); const mergedRef = useForkRef(forwardedRef, popupRef); const { getRootProps, floatingContext, resolvedInitialFocus } = useDialogPopup({ descriptionElementId, floatingRootContext, getPopupProps, id, initialFocus, modal: true, mounted, setOpen, open, openMethod, ref: mergedRef, setPopupElement, setPopupElementId, titleElementId }); const hasNestedDialogs = nestedOpenDialogCount > 0; const state = React.useMemo(() => ({ open, nested, transitionStatus, hasNestedDialogs }), [open, nested, transitionStatus, hasNestedDialogs]); const { renderElement } = useComponentRenderer({ render: render ?? 'div', className, state, propGetter: getRootProps, extraProps: { ...other, style: { ...other.style, '--nested-dialogs': nestedOpenDialogCount }, role: 'alertdialog' }, customStyleHookMapping }); if (!keepMounted && !mounted) { return null; } return /*#__PURE__*/_jsx(FloatingFocusManager, { context: floatingContext, modal: open, disabled: !mounted, initialFocus: resolvedInitialFocus, returnFocus: finalFocus, outsideElementsInert: true, children: renderElement() }); }); process.env.NODE_ENV !== "production" ? AlertDialogPopup.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 element in the DOM while the alert 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 { AlertDialogPopup };