import type { MutableRefObject, ReactNode } from 'react'; import React from 'react'; import type { ExtractProps } from '../../util/utility-types'; import type { Size } from '../../util/variant-types'; import Heading from '../Heading'; import Text from '../Text'; type ModalContentProps = { /** * Optional aria-label for the modal. * * If undefined, the headingText of the Modal.Header will be used. * If there is no Modal.Header, an aria-label is required. */ 'aria-label'?: string; /** * Additional classnames passed in for styling. */ className?: string; /** * Contents of the modal. Only `Modal.Header`, `Modal.Body`, and `Modal.Footer` are allowed * as direct children; anything else logs an error. */ children: ReactNode; /** * Hides the close button in the top right of the modal. * * **Default is `false`**. */ hideCloseButton?: boolean; /** * A ref to an element that should receive focus when the modal first opens. * * If undefined, the first focusable element (usually the close button) will be used. * * ``` * const inputFieldRef = useRef(); * * * ... * * * ``` */ initialFocus?: MutableRefObject; /** * Method called when the close button is clicked. Use this to hide the modal. * This should be used to also reset the `open` state. * * This is required even if you don't have a close button so the ESC key can close the modal. * * Closing is cancellable by passing in a function that returns `void` or by not altering the state. * * ``` * const [isOpen, setIsOpen] = useState(true); * // .... * * setIsOpen(false)}> * ... * * ``` */ onClose: () => void; /** * CSS properties defined for the modal's content element. Includes the component's CSS Custom Properties: * * - `--modal-content__border` */ style?: ModalContentCSSProperties; open?: boolean; /** * The modal's footprint at each breakpoint: * - `"sm"` is a compact floating surface that sizes to its content, up to 480px tall * - `"lg"` fills the viewport at the smallest breakpoint. Above it, it sizes to its content, * up to the viewport height less a margin * - `"full"` takes the whole viewport at every breakpoint * * Height is managed for you at all three. The body takes whatever space the header and * footer leave over and scrolls once the content outgrows it, so the actions stay on screen * however long the content runs. The exception is a viewport under 320px tall, too short to * seat the header and footer and still leave a body worth scrolling, where the modal scrolls * as a whole instead and the footer does go off screen. * * **Default is `"lg"`**. */ size?: Extract | 'full'; }; export interface ModalContentCSSProperties extends React.CSSProperties { /** * Custom property to customize the border color of this component */ '--modal-content__border'?: string; } type ModalProps = ModalContentProps & { /** * Whether or not the modal is visible. Recommend using `useState` to set this * variable instead of a boolean literal, to avoid component control issues. * @see https://headlessui.com/react/dialog * * ``` * const [isOpen, setIsOpen] = useState(true); * // .... * * * ... * * ``` */ open: boolean; /** * Additional classnames passed in for the modal container. */ modalContainerClassName?: string; /** * Asks the user to confirm before the modal closes, so unsaved work in it isn't lost to a * stray ESC key, overlay click, or close button click. Uses the browser's `window.confirm`. * * - `true` always asks * - a function is called at close time, and asks only when it returns `true`. Use it to check * whether the modal's state is dirty * * Only warn when closing actually loses something. Check how the modal's state is held: * * - **State resets on close** (it lives inside the modal's children, which unmount, or * `onClose` clears it): closing is destructive, so warn * - **State survives close** (it is held above the modal and not cleared): reopening brings * the fields back, so closing loses nothing. Don't warn; the prompt only adds friction * * To warn, the state has to be readable where `Modal` renders. Lift it out of the modal's * children, clear it in `onClose`, and pass a function that compares it against its starting * values. A function keeps an untouched modal closing without a prompt; `true` asks every * time, and fits only when any open modal holds work worth keeping. * * This covers only the closes the modal triggers itself: ESC, an overlay click, and the close * button. A button you put in `Modal.Footer` calls your own handler, so check there too if it * discards work. * * ``` * const [draft, setDraft] = useState(''); * const handleClose = () => { * setIsOpen(false); * setDraft(''); // closing discards the draft, so warn while there is one * }; * // .... * * draft !== ''} onClose={handleClose} open={isOpen}> * ... * * ``` * * **Default is `false`**. */ warnOnClose?: boolean | (() => boolean); /** * The question `window.confirm` asks when `warnOnClose` applies. Name what will be lost, for * example `"Discard this note?"`, so the user can answer without guessing. * * **Default is `"You have unsaved changes. Close anyway?"`**. */ warnOnCloseMessage?: string; }; type ModalTitleProps = ExtractProps & { /** * Contents for the modal title. */ children: ReactNode; /** * CSS class names that can be appended to the component. */ className?: string; }; type ModalSubTitleProps = ExtractProps & { /** * Contents for the modal title. */ children: ReactNode; /** * CSS class names that can be appended to the component. */ className?: string; }; type ModalBodyProps = { /** * Child node(s) that can be nested inside component. `Modal.Header`, * `Modal.Body`, and `Modal.Footer` are the only permissible children of the Modal. */ children: ReactNode; /** * CSS class names that can be appended to the component. */ className?: string; }; type ModalHeaderProps = { /** * Child node(s) to place inside the Modal header. * Should include the */ children: ReactNode; /** * CSS class names that can be appended to the component. */ className?: string; }; type ModalFooterProps = { /** * Child node(s) to place inside the Modal footer. */ children: ReactNode; /** * CSS class names that can be appended to the component. */ className?: string; }; /** * ## Usage * * | Type/Use | Description | Example | * |----------|-------------|---------| * | Standard | Central overlay with focus trap, used for focused tasks or messages. | Form entry; settings dialogs; confirmations. | * | Confirmation | Asks the user to confirm or cancel a critical action. | Deletion prompts; submitting irreversible actions. | * | Alert | Displays an important message, usually with a single dismiss button. | Error notifications; access denied messages. | * | Full-screen | Takes up the entire viewport for complex or immersive tasks. | Onboarding; mobile workflows; media viewers. | * | Success/Feedback | Provides positive feedback after a completed action. | Success confirmation; "Thanks for submitting" messages. | * * ### Best Practices * * * Modals are disruptive and should be used sparingly. * * Use a modal to request minimal amounts of information from a user. Don't request large forms of information inside a modal. * * Don't use a modal when a separate, designated URL is desired. * * Show one modal at a time. Don't place a modal on top of another modal. This can create usability issues. * * For important error notifications, use Inline Notification or Banner. * * For short messaging confirming successful interactions, such as "Email sent", use Inline Notification or Toast. * * ### Warning on close * * `warnOnClose` asks the user to confirm before the modal closes. Use it only when closing throws work away. * * * Warn when the modal's state resets on close, like a partly filled form that comes back empty on reopening. * * Don't warn when the state survives closing and reopening. Nothing is lost, so the prompt is only friction. * * Pass a function that checks for changes, so a modal the user hasn't touched closes without asking. * * Footer buttons that discard work, like Cancel, call your own handler. Check there too. * * ## Content & Accessibility * * ### Do's * * * Use a primary title, body text, and a primary CTA. All other modal content is optional. * * Use a verb-noun question or statement for the primary title. * * Ensure people can scan the heading and CTAs and know what to do even if they skip the body text. * * Keep primary titles and subtitles to a max of 2 lines each. * * Use either a short phrase or a full sentence for subtitles. * * Keep section titles to less than 1 line; preferably a short phrase. * * Break up information with section titles for scanability. * * Try to avoid scrolling text within a modal. * * ### Don'ts * * * Include long headings or body text. The more words, the less likely people are to read any of it. * * Include long passages of informative text in a modal. Use a short summary and then link to a help article, FAQ etc. * * Avoid applying inline stylistic modifications to the Modal.Title sub-component. Use the defaults or preset props where present. * * ## Resources * * * https://headlessui.dev/react/dialog */ export declare const Modal: { (props: ModalProps): React.JSX.Element; displayName: string; Header: { ({ children, className, ...other }: ModalHeaderProps): React.JSX.Element; displayName: string; }; Content: { (props: ModalContentProps): React.JSX.Element; displayName: string; }; Title: { ({ children, className, preset, ...other }: ModalTitleProps): React.JSX.Element; displayName: string; }; SubTitle: { ({ children, className, preset, ...other }: ModalSubTitleProps): React.JSX.Element; displayName: string; }; Body: { (props: ModalBodyProps): React.JSX.Element; displayName: string; }; Footer: { ({ children, className, ...other }: ModalFooterProps): React.JSX.Element; displayName: string; }; }; export {};