Skip to Content
CodeUI

Dialog

@codeui/styles v0.3.0

A modal panel over the page in plain HTML, with the scrim, sizes, and an inset layout.

Load @codeui/styles/dialog.css, or all.css. The Solid component is documented in Dialog.

Demo

dialog/basic.html
<div class="cui-dialog-positioner" data-cui="dialog-positioner" data-panel-size="auto">  <div role="dialog" aria-modal="true" aria-labelledby="delete-title" tabindex="-1" class="cui-dialog-panel" data-cui="dialog-panel" data-size="sm" data-expanded>    <div class="cui-dialog-title" data-cui="dialog-panel-title">      <h2 id="delete-title" data-cui="dialog-title" style="margin: 0; font: inherit">Delete snippet</h2>      <button type="button" aria-label="Close" class="cui-button cui-iconbutton" data-cui="dialog-close-button" data-size="xs" data-variant="secondary">        <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">          <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />        </svg>      </button>    </div>    <div class="cui-dialog-panel-content" data-cui="dialog-panel-content" data-layout="default">      The snippet and its export history are removed. This cannot be undone.    </div>    <div class="cui-dialog-panel-footer" data-cui="dialog-panel-footer">      <div class="cui-stack" data-direction="row" data-gap="2" data-justify="end">        <button type="button" class="cui-button" data-cui="button" data-variant="secondary">Cancel</button>        <button type="button" class="cui-button" data-cui="button" data-variant="negative">Delete</button>      </div>    </div>  </div></div>

The positioner is position: fixed, so in these demos it fills the frame. On a page it covers the viewport.

Markup

PartElementClassdata-cui
Positionerdivcui-dialog-positionerdialog-positioner
Paneldivcui-dialog-paneldialog-panel
Headerdivcui-dialog-titledialog-panel-title
Titleh2Nonedialog-title
Close buttonbuttonIconButton classesdialog-close-button
Descriptionpcui-dialog-descriptiondialog-description
Bodydivcui-dialog-panel-contentdialog-panel-content
Footerdivcui-dialog-panel-footerdialog-panel-footer
Overlaydivcui-dialog-overlaydialog-overlay
Sidebardivcui-dialog-sidebardialog-sidebar
Sidebar searchdivcui-dialog-sidebar-searchdialog-sidebar-search
Navnavcui-dialog-navdialog-nav
Nav listulcui-dialog-nav-listNone
Nav itembutton in an licui-dialog-nav-itemdialog-nav-item
Nav iconspancui-dialog-nav-icondialog-nav-icon
Maindivcui-dialog-maindialog-main
Sectionsectioncui-dialog-sectiondialog-section
Section titleh3cui-dialog-section-titledialog-section-title
Section contentdivcui-dialog-section-contentdialog-section-content

The header part carries dialog-panel-title, not dialog-header. The title has no class. It takes its size and weight from the header, so reset the heading's own margin and font, as the demos do with margin: 0; font: inherit. The description class sets padding but not margin. Reset the p margin too. The positioner paints the scrim in its ::before. The overlay is an extra fixed layer and paints nothing. You can leave it out.

AttributeValuesDefault
data-size on the panelauto, xs, sm, md, lg, xl, full, settings, exportauto
data-chrome on the panel and the positionerinsetNone
data-panel-size on the positionerauto, export, fullauto
data-layout on the bodydefault, splitdefault
data-variant on a sectiongroup, wellgroup
data-columns on a section2None
data-expanded on the panelPresenceOff
aria-current on a nav itempageNone

Variants

Sizes

dialog/sizes.html
<div class="cui-stack" data-gap="6" data-align="center">  <div role="dialog" aria-labelledby="size-xs-title" class="cui-dialog-panel" data-cui="dialog-panel" data-size="xs" data-expanded style="max-width: 100%">    <div class="cui-dialog-title" data-cui="dialog-panel-title">      <h2 id="size-xs-title" data-cui="dialog-title" style="margin: 0; font: inherit">Size xs</h2>    </div>    <div class="cui-dialog-panel-content" data-cui="dialog-panel-content">24rem wide.</div>  </div>  <div role="dialog" aria-labelledby="size-sm-title" class="cui-dialog-panel" data-cui="dialog-panel" data-size="sm" data-expanded style="max-width: 100%">    <div class="cui-dialog-title" data-cui="dialog-panel-title">      <h2 id="size-sm-title" data-cui="dialog-title" style="margin: 0; font: inherit">Size sm</h2>    </div>    <div class="cui-dialog-panel-content" data-cui="dialog-panel-content">28rem wide.</div>  </div>  <div role="dialog" aria-labelledby="size-md-title" class="cui-dialog-panel" data-cui="dialog-panel" data-size="md" data-expanded style="max-width: 100%">    <div class="cui-dialog-title" data-cui="dialog-panel-title">      <h2 id="size-md-title" data-cui="dialog-title" style="margin: 0; font: inherit">Size md</h2>    </div>    <div class="cui-dialog-panel-content" data-cui="dialog-panel-content">32rem wide.</div>  </div></div>

data-size sets the panel width: xs 24rem, sm 28rem, md 32rem, lg 48rem from the md breakpoint, xl 64rem from the lg breakpoint. auto fills the positioner, less the gutter. full covers the viewport with no radius. Set data-panel-size="full" on the positioner too, so it drops the gutter. settings is 1000×680 and export is 800 wide. Both shrink to fit a smaller viewport. Set data-panel-size="export" on the positioner of an export panel for its 16px gutter.

This demo shows bare panels without a positioner. The panel class declares its own colors, so it keeps them. Inside a positioner, the panel cannot grow past the gutter; here max-width: 100% does that job.

Inset chrome

dialog/inset.html
<div class="cui-dialog-positioner" data-cui="dialog-positioner" data-panel-size="auto" data-chrome="inset">  <div role="dialog" aria-modal="true" aria-labelledby="inset-title" tabindex="-1" class="cui-dialog-panel" data-cui="dialog-panel" data-size="sm" data-chrome="inset" data-expanded>    <div class="cui-dialog-title" data-cui="dialog-panel-title">      <h2 id="inset-title" data-cui="dialog-title" style="margin: 0; font: inherit">Delete index.tsx?</h2>      <button type="button" aria-label="Close" class="cui-button cui-iconbutton" data-cui="dialog-close-button" data-size="sm" data-variant="secondary" data-ghost>        <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">          <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />        </svg>      </button>    </div>    <div class="cui-dialog-panel-content" data-cui="dialog-panel-content">      The snippet and its export settings are removed from this device.    </div>    <div class="cui-dialog-panel-footer" data-cui="dialog-panel-footer">      <button type="button" class="cui-button" data-cui="button" data-variant="secondary">Cancel</button>      <button type="button" class="cui-button" data-cui="button" data-variant="negative">Delete</button>    </div>  </div></div>

data-chrome="inset" puts the title in the panel padding with no divider. The panel gets 20px padding, a 20px gap between its children, and 24px corners. The footer becomes a right-aligned row of actions that stacks at 768px and below. The media query reads the frame width, so the actions stack in this demo. Use a small ghost IconButton for the close button.

Set data-chrome="inset" on the positioner as well. The body and footer rules read it from any ancestor.

Title and description

dialog/description.html
<div class="cui-dialog-positioner" data-cui="dialog-positioner" data-panel-size="auto">  <div role="dialog" aria-modal="true" aria-labelledby="video-title" aria-describedby="video-description" tabindex="-1" class="cui-dialog-panel" data-cui="dialog-panel" data-size="md" data-expanded>    <div class="cui-dialog-title" data-cui="dialog-panel-title">      <h2 id="video-title" data-cui="dialog-title" style="margin: 0; font: inherit">Export as video</h2>      <button type="button" aria-label="Close" class="cui-button cui-iconbutton" data-cui="dialog-close-button" data-size="xs" data-variant="secondary">        <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">          <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />        </svg>      </button>    </div>    <p id="video-description" class="cui-dialog-description" data-cui="dialog-description" style="margin: 0">      Renders the timeline as MP4. Press <kbd class="cui-kbd" data-cui="kbd" data-size="sm" data-variant="keycap">Esc</kbd> to cancel.    </p>    <div class="cui-dialog-panel-content" data-cui="dialog-panel-content">      The export runs in the background. You can keep editing.    </div>  </div></div>

Put the description in a p between the header and the body. Link the title with aria-labelledby and the description with aria-describedby.

Settings layout

dialog/settings.html
<div class="cui-dialog-positioner" data-cui="dialog-positioner" data-panel-size="auto" data-chrome="inset">  <div role="dialog" aria-modal="true" aria-labelledby="settings-title" tabindex="-1" class="cui-dialog-panel" data-cui="dialog-panel" data-size="settings" data-chrome="inset" data-expanded>    <div class="cui-dialog-sidebar" data-cui="dialog-sidebar">      <nav class="cui-dialog-nav" data-cui="dialog-nav" aria-label="Settings sections">        <ul class="cui-dialog-nav-list">          <li><button type="button" class="cui-dialog-nav-item" data-cui="dialog-nav-item" aria-current="page">General</button></li>          <li><button type="button" class="cui-dialog-nav-item" data-cui="dialog-nav-item">Editor</button></li>          <li><button type="button" class="cui-dialog-nav-item" data-cui="dialog-nav-item">Export</button></li>        </ul>      </nav>    </div>    <div class="cui-dialog-main" data-cui="dialog-main">      <div class="cui-dialog-title" data-cui="dialog-panel-title">        <h2 id="settings-title" data-cui="dialog-title" style="margin: 0; font: inherit">Settings / General</h2>        <button type="button" aria-label="Close settings" class="cui-button cui-iconbutton" data-cui="dialog-close-button" data-size="sm" data-variant="secondary" data-ghost>          <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">            <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />          </svg>        </button>      </div>      <div class="cui-dialog-panel-content" data-cui="dialog-panel-content" data-layout="default">        <section class="cui-dialog-section" data-cui="dialog-section" data-variant="group">          <h3 class="cui-dialog-section-title" data-cui="dialog-section-title">Editor</h3>          <div class="cui-dialog-section-content" data-cui="dialog-section-content">          <div role="group" aria-labelledby="line-numbers-label" class="cui-settings-row" data-cui="settings-row" data-layout="inline" data-compact-control>            <span id="line-numbers-label" class="cui-settings-row-label" data-cui="settings-row-label">Line numbers</span>            <div class="cui-settings-row-control" data-cui="settings-row-control">              <div class="cui-switch" data-cui="switch" data-size="md" data-checked>                <input id="line-numbers" type="checkbox" role="switch" aria-labelledby="line-numbers-label" class="cui-switch-input" data-cui="switch-input" data-size="md" checked data-checked style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" />                <div class="cui-switch-control" data-cui="switch-control" data-size="md" data-checked aria-hidden="true">                  <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md" data-checked></div>                </div>              </div>            </div>          </div>          <div role="group" aria-labelledby="ligatures-label" class="cui-settings-row" data-cui="settings-row" data-layout="inline" data-compact-control>            <span id="ligatures-label" class="cui-settings-row-label" data-cui="settings-row-label">Font ligatures</span>            <div class="cui-settings-row-control" data-cui="settings-row-control">              <div class="cui-switch" data-cui="switch" data-size="md" data-checked>                <input id="ligatures" type="checkbox" role="switch" aria-labelledby="ligatures-label" class="cui-switch-input" data-cui="switch-input" data-size="md" checked data-checked style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" />                <div class="cui-switch-control" data-cui="switch-control" data-size="md" data-checked aria-hidden="true">                  <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md" data-checked></div>                </div>              </div>            </div>          </div>          <div role="group" aria-labelledby="word-wrap-label" class="cui-settings-row" data-cui="settings-row" data-layout="inline" data-compact-control>            <span id="word-wrap-label" class="cui-settings-row-label" data-cui="settings-row-label">Wrap long lines</span>            <div class="cui-settings-row-control" data-cui="settings-row-control">              <div class="cui-switch" data-cui="switch" data-size="md">                <input id="word-wrap" type="checkbox" role="switch" aria-labelledby="word-wrap-label" class="cui-switch-input" data-cui="switch-input" data-size="md" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" />                <div class="cui-switch-control" data-cui="switch-control" data-size="md" aria-hidden="true">                  <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md"></div>                </div>              </div>            </div>          </div>          </div>        </section>      </div>    </div>  </div></div>

An inset settings panel with a sidebar as its first child lays out in two panes and drops its padding. Put the header and the body in main. Inside main, the body is a column of sections with 24px gaps. A group section puts its title over one island. The rows here are Switch controls in settings rows. At 768px and below, the sidebar moves on top and the nav items wrap into rows of three. The frame in this demo is narrow enough to show that layout on a phone.

data-layout="split" on the body puts its first child beside its second, at 1.1fr to 1fr. A well section is its own island. data-columns="2" lays out a section's content in two columns.

States

Open

data-expanded on the panel plays the open animation: a fade from scale(0.96) over 200ms. The positioner's scrim fades in when its direct child has data-expanded. Without data-expanded, the scrim stays transparent. Set it on an open panel even without a script.

Closing

Remove data-expanded while the panel is still in the page. The panel fades toward scale(0.98) over 120ms and the scrim fades out. Remove the markup when the animation ends.

No motion

data-motion="none" on the positioner and the panel turns off both animations. Set it when the dialog opens or closes from the keyboard. With reduced motion, the panel only fades.

Current section

aria-current="page" on a nav item gives it the field fill and default text. A disabled nav item is dimmed.

What you supply

  • A reset on the title heading and the description paragraph. The demos reset them inline; a page-wide reset works too.
  • Opening and closing, and data-expanded while open.
  • role="dialog", aria-modal="true", and aria-labelledby on the panel. aria-describedby when there is a description.
  • A focus trap: Tab and Shift+Tab wrap inside the panel. Move focus into the panel on open and return it to the opener on close.
  • Escape and the close button close the dialog. Hide the rest of the page from assistive technology while a modal dialog is open, for example with inert.
  • Arrow-key navigation between nav items, and aria-current on the current one.
  • Scroll locking on the page behind the dialog.

Helper

import "@codeui/styles/dialog.css";
import { applyStyleProps } from "@codeui/styles";
import { dialogStyleProps } from "@codeui/styles/dialog";

const positioner = document.querySelector<HTMLElement>("[data-dialog-positioner]")!;
const panel = positioner.querySelector<HTMLElement>("[role=dialog]")!;

applyStyleProps(positioner, dialogStyleProps.positioner({ chrome: "inset" }));
applyStyleProps(panel, dialogStyleProps({ size: "sm", chrome: "inset" }));
// panel: class="cui-dialog-panel" data-cui="dialog-panel" data-size="sm" data-chrome="inset"

dialogStyleProps.body({ layout: "split" }) and dialogStyleProps.section({ variant: "well" }) style the body and a section.

On this page