Skip to content
FrameworkStyle

AlertDialog

A modal alert dialog for urgent messages that require acknowledgement

Import

import { AlertDialog } from '@videojs/react';

Anatomy

<AlertDialog.Root>
  <AlertDialog.Backdrop />
  <AlertDialog.Popup>
    <AlertDialog.Title />
    <AlertDialog.Description />
    <AlertDialog.Close />
  </AlertDialog.Popup>
</AlertDialog.Root>

Behavior

Use an alert dialog for an urgent message that interrupts the user and requires acknowledgement. Your application controls when it opens and supplies its content. AlertDialog.Root adds alert semantics, while the other AlertDialog parts are re-exported from Dialog. For general modal content, including a player opened from a thumbnail, use Dialog.Root instead.

Use open with onOpenChange for controlled state, or defaultOpen for uncontrolled state. Root provides state without rendering an element, while Popup renders only while the dialog is open.

The close part and Escape close the dialog. The dialog stays rendered during a closing transition, then restores focus to the element that was focused before it opened.

Styling

Use the open and transition data attributes to style visibility and animations:

Attribute Description
data-open Present while the dialog is open, including its closing transition
data-starting-style Present at the start of the opening transition
data-ending-style Present during the closing transition

React renders separate backdrop and popup elements. Add a className to each part and style them independently:

.alert-dialog-backdrop[data-starting-style],
.alert-dialog-backdrop[data-ending-style],
.alert-dialog-popup[data-starting-style],
.alert-dialog-popup[data-ending-style] {
  opacity: 0;
}

Accessibility

The dialog uses role="alertdialog" and aria-modal="true". The title and description receive generated IDs that are connected with aria-labelledby and aria-describedby. Focus moves into the dialog when it opens, stays within it while open, and returns to the previously focused element when it closes. Content outside the dialog is inert while it is open. Pressing Escape closes the dialog.

Examples

Basic usage

import { AlertDialog } from '@videojs/react';
import { useState } from 'react';

export default function BasicUsage() {
  const [open, setOpen] = useState(false);

  return (
    <div className="react-alert-dialog-basic">
      <button className="react-alert-dialog-basic__trigger" type="button" onClick={() => setOpen(true)}>
        Open alert dialog
      </button>
      <AlertDialog.Root open={open} onOpenChange={setOpen}>
        <AlertDialog.Backdrop className="react-alert-dialog-basic__backdrop" />
        <AlertDialog.Popup className="react-alert-dialog-basic__dialog">
          <AlertDialog.Title className="react-alert-dialog-basic__title">Stop playback?</AlertDialog.Title>
          <AlertDialog.Description className="react-alert-dialog-basic__description">
            Your current playback position will be lost.
          </AlertDialog.Description>
          <AlertDialog.Close className="react-alert-dialog-basic__close">Continue</AlertDialog.Close>
        </AlertDialog.Popup>
      </AlertDialog.Root>
    </div>
  );
}

API Reference

Root

Manages alert dialog state and provides it to the shared dialog parts.

Props

PropTypeDefaultDetails
closeOnEscapeboolean
defaultOpenboolean
openboolean

State

State is accessible via the render, className, and style props.

PropertyTypeDetails
transitionStartingboolean
transitionEndingboolean
openboolean
status'idle' | 'starting' | 'ending'
titleIdstring | undefined
descriptionIdstring | undefined

Backdrop

Presentational layer behind a dialog while it is rendered, including its exit transition.

Props

PropTypeDefaultDetails
classNamestring | ((state: DialogCore.State) => string | undefined)
renderReactElement | ((props: HTMLProps, state: DialogCore.State) => ReactElement | null)
styleCSSProperties | ((state: DialogCore.State) => CSSProperties | undefined)

Data attributes

AttributeTypeDetails
data-open
data-starting-style
data-ending-style

Close

Renders a button that closes the dialog.

Props

PropTypeDefaultDetails
classNamestring | ((state: DialogCore.State) => string | undefined)
renderReactElement | ((props: HTMLProps, state: DialogCore.State) => ReactElement | null)
styleCSSProperties | ((state: DialogCore.State) => CSSProperties | undefined)

Data attributes

AttributeTypeDetails
data-open
data-starting-style
data-ending-style

Description

Renders the description announced with the dialog.

Props

PropTypeDefaultDetails
classNamestring | ((state: DialogCore.State) => string | undefined)
renderReactElement | ((props: HTMLProps, state: DialogCore.State) => ReactElement | null)
styleCSSProperties | ((state: DialogCore.State) => CSSProperties | undefined)

Renders the modal dialog while it is open.

PropTypeDefaultDetails
classNamestring | ((state: DialogCore.State) => string | undefined)
renderReactElement | ((props: HTMLProps, state: DialogCore.State) => ReactElement | null)
styleCSSProperties | ((state: DialogCore.State) => CSSProperties | undefined)
AttributeTypeDetails
data-open
data-starting-style
data-ending-style

Title

Renders the heading that labels the dialog.

Props

PropTypeDefaultDetails
classNamestring | ((state: DialogCore.State) => string | undefined)
renderReactElement | ((props: HTMLProps, state: DialogCore.State) => ReactElement | null)
styleCSSProperties | ((state: DialogCore.State) => CSSProperties | undefined)