Components
Alert Dialog
A dialog that requires a user response to proceed.
Anatomy#
Import the component and assemble its parts:
import { AlertDialog } from '@egoist/solid-base/alert-dialog';
<AlertDialog.Root>
<AlertDialog.Trigger />
<AlertDialog.Portal>
<AlertDialog.Backdrop />
<AlertDialog.Viewport>
<AlertDialog.Popup>
<AlertDialog.Title />
<AlertDialog.Description />
<AlertDialog.Close />
</AlertDialog.Popup>
</AlertDialog.Viewport>
</AlertDialog.Portal>
</AlertDialog.Root>;Examples#
Detached triggers#
An alert dialog can be controlled by a trigger located either inside or outside the
<AlertDialog.Root> component. For simple, one-off interactions, place the <AlertDialog.Trigger>
inside <AlertDialog.Root>, as shown in the example at the top of this page.
However, if defining the alert dialog's content next to its trigger is not practical, you can use a
detached trigger. This involves placing the <AlertDialog.Trigger> outside of <AlertDialog.Root>
and linking them with a handle created by the AlertDialog.createHandle() function.
The imperative methods on the handle, such as open() and openWithPayload(), require an
<AlertDialog.Root> using the same handle to be mounted. Calls made while no root is attached to
the handle — before one mounts, or after it unmounts — are ignored. Each time a root mounts, it
starts from fresh state: a call made while no root was attached is not replayed, and no open state
carries over from a previous mount.
const demoAlertDialog = AlertDialog.createHandle();
<AlertDialog.Trigger handle={demoAlertDialog}>Open</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
...
</AlertDialog.Root>Multiple triggers#
A single alert dialog can be opened by multiple trigger elements. You can achieve this by using the
same handle for several detached triggers, or by placing multiple <AlertDialog.Trigger>
components inside a single <AlertDialog.Root>.
<AlertDialog.Root>
<AlertDialog.Trigger>Trigger 1</AlertDialog.Trigger>
<AlertDialog.Trigger>Trigger 2</AlertDialog.Trigger>
...
</AlertDialog.Root>const demoAlertDialog = AlertDialog.createHandle();
<AlertDialog.Trigger handle={demoAlertDialog}>Trigger 1</AlertDialog.Trigger>
<AlertDialog.Trigger handle={demoAlertDialog}>Trigger 2</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
...
</AlertDialog.Root>The alert dialog can render different content depending on which trigger opened it. This is
achieved by passing a payload to the <AlertDialog.Trigger> and using the function-as-a-child
pattern in <AlertDialog.Root>.
The payload can be strongly typed by providing a type argument to the createHandle() function:
const demoAlertDialog = AlertDialog.createHandle<{ message: string }>();
<AlertDialog.Trigger handle={demoAlertDialog} payload={{ message: 'Trigger 1' }}>
Trigger 1
</AlertDialog.Trigger>
<AlertDialog.Trigger handle={demoAlertDialog} payload={{ message: 'Trigger 2' }}>
Trigger 2
</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
{(state) => (
<AlertDialog.Portal>
<AlertDialog.Popup>
<AlertDialog.Title>Alert dialog</AlertDialog.Title>
<Show when={state.payload}>
{(payload) => (
<AlertDialog.Description>Confirming {payload().message}</AlertDialog.Description>
)}
</Show>
</AlertDialog.Popup>
</AlertDialog.Portal>
)}
</AlertDialog.Root>Controlled mode with multiple triggers#
You can control the alert dialog's open state externally using the open and onOpenChange props
on <AlertDialog.Root>. This allows you to manage the alert dialog's visibility based on your
application's state. When using multiple triggers, you have to manage which trigger is active with
the triggerId prop on <AlertDialog.Root> and the id prop on each <AlertDialog.Trigger>.
Note that there is no separate onTriggerIdChange prop. Instead, the onOpenChange callback
receives an additional argument, eventDetails, which contains the trigger element that initiated
the state change.
Open from a menu#
In order to open a dialog using a menu, control the dialog state and open it imperatively using the
onClick handler on the menu item.
API reference#
AlertDialog shares the Backdrop, Portal, Viewport, Popup, Title, Description and
Close parts with Dialog. Unlike a dialog, an alert dialog is always modal
and can't be dismissed by clicking outside of it, so <AlertDialog.Root> has no modal or
disablePointerDismissal props.
<AlertDialog.Root>
Groups all parts of the alert dialog. Doesn't render its own HTML element.
onOpenChange| ((open: boolean, eventDetails: AlertDialogRoot.ChangeEventDetails) => void)—Event handler called when the alert dialog is opened or closed.
actionsRefActionsRef<AlertDialogRoot.Actions>—A ref to imperative actions: a ref object ({ current: null }) or a callback receiving them (and null on unmount). - unmount: Ends the closing phase of the alert dialog after an externally controlled closing animation finishes. Call preventUnmountOnClose() in onOpenChange first, otherwise the alert dialog completes closing on its own. Whether it leaves the DOM is decided by keepMounted on the portal. - close: Closes the alert dialog imperatively when called.
handleAlertDialogHandle<Payload>—A handle to associate the alert dialog with a trigger. If specified, allows external triggers to control the alert dialog's open state. Can be created with the AlertDialog.createHandle() method.
defaultOpenbooleanfalseWhether the dialog is initially open.
To render a controlled dialog, use the open prop instead.
openboolean—Whether the dialog is currently open.
onOpenChangeComplete((open: boolean) => void)—Event handler called after any animations complete when the dialog is opened or closed.
triggerIdstring | null—ID of the trigger that the dialog is associated with. This is useful in conjunction with the open prop to create a controlled dialog. There's no need to specify this prop when the dialog is uncontrolled (that is, when the open prop is not set).
defaultTriggerIdstring | null—ID of the trigger that the dialog is associated with. This is useful in conjunction with the defaultOpen prop to create an initially open dialog.
<AlertDialog.Trigger>
A button that opens the alert dialog. Renders a <button> element.
handleAlertDialogHandle<Payload>—A handle to associate the trigger with an alert dialog. Can be created with the AlertDialog.createHandle() method.
idstring—ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the dialog in controlled mode (with the DialogRoot triggerId prop).
disabledbooleanfalseWhether the component ignores user interaction.
payloadPayload—A payload to pass to the dialog when it is opened.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-disabledPresent when the trigger is disabled.
<AlertDialog.Backdrop>
An overlay displayed beneath the popup. Renders a <div> element.
forceRenderbooleanfalseWhether the backdrop is forced to render even when nested.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<AlertDialog.Close>
A button that closes the dialog. Renders a <button> element.
disabledbooleanfalseWhether the component ignores user interaction.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<AlertDialog.Description>
A paragraph with additional information about the dialog. Renders a <p> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<AlertDialog.Popup>
A container for the dialog contents. Renders a <div> element.
initialFocusFocusTargetProp—Determines the element to focus when the dialog is opened. By default, focus moves to the first tabbable element inside the popup, except when the dialog is opened by touch — then the popup itself is focused to avoid opening the virtual keyboard.
- false: Do not move focus. - true: Move focus based on the default behavior (first tabbable element or popup). - An element or a ref object ({ current }): Move focus to that element. - function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, null to fall back to the default behavior, or false/undefined to do nothing.
finalFocusFocusTargetProp—Determines the element to focus when the dialog is closed.
- false: Do not move focus. - true: Move focus based on the default behavior (trigger or previously focused element). - An element or a ref object ({ current }): Move focus to that element. - function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, null to fall back to the default behavior, or false/undefined to do nothing.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<AlertDialog.Portal>
A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to <body>. Renders a <div> element.
keepMountedbooleanfalseWhether to keep the portal mounted in the DOM while the popup is hidden.
containerPortalContainer—A parent element to render the portal element into.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<AlertDialog.Title>
A heading that labels the dialog. Renders an <h2> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<AlertDialog.Viewport>
A positioning container for the dialog popup that can be made scrollable. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
createHandle#
Creates an AlertDialog.Handle that connects an <AlertDialog.Root> with detached
<AlertDialog.Trigger> components. Pass a type argument to type the payload:
AlertDialog.createHandle<Payload>().
Handle#
| Method / property | Description |
|---|---|
open(triggerId) |
Opens the alert dialog and associates it with the trigger that has this id (an AlertDialog.Trigger using this handle). Pass null to open without one. |
openWithPayload(payload) |
Opens the alert dialog with the given payload, without associating it with any trigger. |
close() |
Closes the alert dialog. |
isOpen |
Whether the alert dialog is currently open (false while no root is attached). Tracked when read in a reactive scope. |
Call these methods from event handlers or effects, not while rendering.