Modal
A cross-platform React modal dialog system. A provider/context/hook trio drives a stack of dialogs supporting three presentation types (popup, bottom-sheet, full-screen), structured headers, focus trapping, and programmatic open/close on both web and React Native.
A layered overlay component that interrupts the current flow to present focused content requiring the user's attention or a decision. Composed of a header, text block, content area, and footer. Supports three presentation types — Popup, Bottom sheet, and Full screen — covering desktop and mobile contexts.
When to use
- To confirm a destructive or irreversible action — delete, remove, revoke
- To collect a focused set of inputs without navigating to a new page — a short form, a settings change, a payment step
- To present critical information the user must acknowledge before continuing
- To display contextual content (a preview, a detail view) without losing the current page context
- When the interaction requires the user's undivided attention and the rest of the UI should be blocked
When not to use
- For non-critical notifications or status updates — use a Toast
- When the information is supplementary and the user can ignore it — use a Tooltip or inline helper text
- When navigation to a dedicated page is more appropriate — open a new route instead
- For complex multi-step workflows that would make the modal very tall — break into a dedicated page or a stepper flow
- For simple one-line confirmations where a native browser confirm() or a small inline confirmation would suffice
Content guidelines
- Header title — short and specific: "Delete project", "Add team member", "Confirm payment". Use sentence case. Do not end with a period.
- Body title — only needed when the header title is insufficient to explain the content. Example: header is "Confirm", body title is "You are about to permanently delete 12 files."
- Description — one or two sentences. Explain the consequence or what the user is being asked to do. Example: "This action cannot be undone. All associated data will be removed."
- Primary button label — imperative verb matching the action: "Delete", "Save changes", "Confirm", "Continue", "Add". For destructive actions, use Tone=Alert on the button.
- Cancel / secondary button label — "Cancel", "Go back", "Not now", "Keep" (for "Keep / Delete" confirmations).
- Avoid vague labels — do not use "OK", "Yes", or "No" as button labels. The label must describe the action being taken.
- Modal length — if the content requires scrolling more than one screen length, consider navigating to a dedicated page instead.
Behaviour guidelines
- Opening — the modal opens programmatically in response to a user action (button click, link click, confirmation trigger). It must never open on its own without user intent. The opening should be animated (fade + scale for Popup; slide-up for Bottom sheet).
- Backdrop — a semi-transparent backdrop covers the page behind Popup and Bottom sheet. Clicking the backdrop closes the modal unless the action is destructive or critical (in which case, force explicit button dismissal).
- Closing — the modal closes on: clicking the close button (✕), clicking the backdrop (if allowed), pressing Escape, completing the primary action, or clicking Cancel. On close, the modal is removed from the DOM (not just hidden).
- Focus management — when the modal opens, focus must move into the modal (to the first interactive element — typically the primary button or the first input). When the modal closes, focus must return to the element that triggered it.
- Focus trap — while the modal is open, keyboard focus must be trapped inside it. Tab and Shift+Tab must cycle through focusable elements within the modal only. Focus must not escape to the background page.
- Scroll — if the content area overflows, the content area scrolls internally; the header and footer remain fixed (sticky) at the top and bottom of the modal. The page behind the modal must not scroll while the modal is open (body { overflow: hidden }).
- Stacking — avoid opening a second modal on top of the first. If a second modal is unavoidable (e.g. a confirmation within a form modal), ensure both have correct z-index ordering and the focus trap applies to the topmost modal only.
- Animation — Popup: fade in + scale from 95% to 100%. Bottom sheet: slide up from the bottom. Full screen: fade or slide based on product design. Keep animations under 300ms. Respect prefers-reduced-motion — use instant transitions for users who prefer reduced motion.
Accessibility
- The modal container must have role="dialog" and aria-modal="true".
- The modal must have an accessible name via aria-labelledby pointing to the header title element's id, or aria-label if the title is not a visible text element.
- If the modal has a description, associate it via aria-describedby pointing to the description text's id.
- When the modal opens, move focus to the first interactive element inside the modal — typically the primary button, the close button, or the first form field.
- Implement a focus trap: Tab and Shift+Tab must cycle only within the modal's focusable elements.
- The close button (✕) must have aria-label="Close" since it contains only an icon.
- The Button left back button must have aria-label="Go back" or equivalent.
- When the modal closes, return focus to the element that triggered it.
- Announce the modal opening to screen readers — role="dialog" with aria-labelledby handles this natively in most assistive technologies.
- Prevent background scroll and interaction while the modal is open: use aria-hidden="true" on the content behind the modal so screen readers do not read it.
Installation
npm install @xsolla/xui-modal
Imports
import {
Modal,
ModalProvider,
ModalContext,
WorkArea,
useModal,
useModalId,
type ModalProps,
type ModalSize,
type ModalVariant,
type WorkAreaProps,
type WorkAreaSize,
} from "@xsolla/xui-modal";
useModalId is re-exported from @xsolla/xui-core and is consumed by portaled descendants (e.g. Select, Dropdown) so click-outside detection ignores content that logically belongs to the modal.
Quick start
import * as React from "react";
import { Modal, ModalProvider, useModal } from "@xsolla/xui-modal";
import { Button } from "@xsolla/xui-button";
function Trigger() {
const [open, close] = useModal(() => (
<Modal onClose={close} title="Confirm action">
<p>Are you sure you want to proceed?</p>
<Button onPress={close}>Close</Button>
</Modal>
));
return <Button onPress={open}>Open modal</Button>;
}
export default function QuickStart() {
return (
<ModalProvider>
<Trigger />
</ModalProvider>
);
}
API Reference
<Modal>
The dialog component with header, body, and footer zones. Accepts a ref forwarded to the inner WorkArea. Unknown props are spread onto the root Box. size is a typed ModalProps field but is never consumed by Modal's internal layout; it passes through via ...rest to the root Box with no built-in styling effect.