Skip to content
Slotted

Alert

Frames a message about a page, a section or the thing beside it, with a tone, a title, actions, and a control that puts it away.

Spring price list is live

The new prices took effect for 1,240 products at 6:00 AM.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { Alert, AlertActions, AlertContent, AlertDefaultsProvider,  AlertDescription, AlertTitle } from '@slotted/react';

Tones

tone sets the color and the mark of the message: accent and info draw the information mark, success, warning and danger draw their own, and secondary draws none. The mark carries the tone without color, so a message reads the same in grayscale and under forced colors. Use danger for a failure, warning for something that needs attention, success for a finished action and secondary for a neutral note.

Cycle count starts Monday
Carrier rates exclude tax
Reorder points were recalculated
Invoice INV-2041 was sent
Three shipments miss the pickup window
Payment to Harbor Freight failed

Variants

variant sets how the tone is painted. soft, the default, fills the box with the tone’s surface and draws its border. outline draws the border alone and takes the surface underneath. solid fills the box with the tone’s strong color and sets the text and the mark in the ink the theme measures against it.

Soft

Inventory sync is 40 minutes behind.

Outline

Inventory sync is 40 minutes behind.

Solid

Inventory sync is 40 minutes behind.

Reach

reach sets how far the message applies. inline is the tightest and sits beside the thing it is about, such as a field group. section, the default, sits inside a page section. page runs to both edges of the container that holds it and loses its corners, which suits a banner across an application. Put the message where it applies, and keep page for what concerns every screen.

Enter a valid EIN, such as 12-3456789.

Billing address is incomplete

Add a postal code before Northwind can issue invoices.

Scheduled maintenance on Saturday from 1:00 AM to 3:00 AM

Actions

AlertActions in React and slAlertActions in Angular hold the controls that act on the message, outside the part that announces it. Each button inside takes the alert’s tone and the sm size; a button that sets its own tone or size keeps it. Give the first action the default variant and the others ghost. actionsPlacement sets where the actions stand: below puts them on a row under the text, and end puts them at the end of the text’s line once the alert is 40rem wide, and under the text below that width. A page alert defaults to end, and every other reach to below.

Your Northwind plan renews in three days

The suggestion changes three lines of purchase order PO-7712.

Payments are paused for the Rotterdam warehouse

Dismissible

dismissible renders the control that closes the message. It is off by default: leave it off for a message that must stay until the condition is resolved. The control is a native button named by the dismiss message, which messages replaces key by key. Closing hides the alert and keeps it in the document. Escape does not close it. When the alert closes with focus inside it, focus returns to the element it came from, which restoreFocus set to false turns off. Tab to the dismiss control and press Enter: focus goes back to Export report.

Carrier tracking links expire in 14 days

Download the shipment manifests you still need.

Controlled

Pass open to own the state. A press on the dismiss control calls onOpenChange in React and emits openChange in Angular, and the alert closes only when the new value is passed back. In Angular the event carries a ChangeRequest: read the new value from $event.value. defaultOpen sets the starting state of an alert that owns its own. In Angular a template can also call setOpen on the instance that exportAs="slAlert" gives it.

Import finished

412 of 415 rows were added to the catalog.

Refusing a dismissal

The change request carries reason, which is dismiss for the control and programmatic for setOpen, and cancel(), which refuses the change. A refused alert stays open and focus stays on the control. Here the alert closes only after the switch confirms the review. The request is request.cancel() in React and $event.cancel() in Angular.

12 price changes are waiting for approval

The message stays until the changes are reviewed.

Live announcements

live sets whether the text announces itself. off, the default, writes no role. polite writes role="status" and assertive writes role="alert" on the content part, never on the box, so the controls beside the text are not read. Screen readers do not announce alerts that are present when the page loads, so use polite or assertive for a message that arrives afterwards. Use assertive only for a failure the reader must hear at once. Press each button to render the alert that follows.

Content

The description breaks a string with no break opportunity, such as an image digest, instead of overflowing. hideIcon removes the mark. A message may hold a title alone or a description alone. The title names the box through aria-labelledby, so an alert with a title is a region landmark, and an alert with no title is a plain grouping element.

Deploy failed

Image sha256:9f2c1ab4e0d37c58b1e6a7d94f03c2b85a1d6e7f4039bc82e15d7a60f3b9c418 was not found in the registry.

Recount required

Aisle 14 differs from the system by 3.2%.

Pickup confirmed

Rates were updated this morning.

Icon and title element

icon in React and the slAlertIcon element in Angular replace the tone’s mark and keep its box and its place on the first line of the message. The title is a div by default. To put the message in the document outline, render the title as a heading: render on AlertTitle in React, and slAlertTitle on an h3 in Angular. Use a heading for a page banner and leave it a div beside a field group.

Rotterdam warehouse is closed

Orders ship from Antwerp until Monday.

Defaults

AlertDefaultsProvider in React and provideAlertDefaults in Angular set tone, variant, reach and live once for every alert under them. An alert that sets a value of its own keeps it. The last alert here sets danger, and the others take warning, outline and inline from the provider.

Low stock: Oak desk, 4 left
Low stock: Desk lamp, 7 left
Out of stock: Task chair

Accessibility

  • The applicable pattern is the WAI-ARIA APG Alert Pattern (w3.org/WAI/ARIA/apg/patterns/alert/). It declares one role and no keyboard interaction: an alert is not a widget, so it holds no keys. Its two prose requirements — that an alert must not affect keyboard focus, and must not disappear on its own — are why Alert never moves focus and never dismisses itself.
  • The box is a landmark, not a live region. MDN’s alert role reference states that the role “should only be used for text content, not interactive elements such as links or buttons”, and an alert can carry a dismiss control and actions. The root renders a section named by its own title through aria-labelledby, and the region that announces is the content part, which holds the title and the description alone.
  • live defaults to off because the APG records that “screen readers do not inform users of alerts that are present on the page before page load completes”. An alert already on the page announces nothing whatever role it carries; polite and assertive are for a message that arrives while the reader is working, and both are written as a role that already implies aria-live and aria-atomic.
  • Every tone with a severity carries a mark as well as a color, and secondary carries none: a neutral note has no severity to state. Under forced colors the grounds become one system color and the borders one more, so the mark, and its absence on a note, is the only thing distinguishing an error from a note — WCAG 2.2 1.4.1.
  • The dismiss control is a native button with an accessible name, not a Button: its box is the glyph plus a step of air on each side, floored at --slotted-target-min, which is the WCAG 2.2 2.5.8 minimum of 24 CSS pixels; at rest it has no frame and no ground, and hover moves only the ground.
  • When the message closes with focus inside it, the keyboard goes back to the element it came from. Without it, the dismiss control is hidden, focus falls to the document body, and the next Tab starts again at the top of the page.
  • Closing hides rather than unmounts, so the live region is still in the document if the message comes back and data-open is a state a consumer can style. An application that must not pay for the hidden subtree renders the alert conditionally and never passes defaultOpen.
  • Escape does not close the message. An alert is not a dismissable layer, and one inside a dialog would swallow the dialog’s own Escape; a message that must interrupt the reader is an alert dialog rather than an alert.

API

Alert

Props

tone'accent' | 'secondary' | 'info' | 'success' | 'warning' | 'danger'Default accent
Which of the six tones the message speaks in, and which mark it draws: status-info for accent and info, none for secondary, status-success, status-warning, status-danger.
variant'soft' | 'outline' | 'solid'Default soft
How that color is painted. soft carries the tone’s surface and its border, outline the border alone, solid the tone’s fill and the ink the theme measures against it.
reach'inline' | 'section' | 'page'Default section
The scope the message applies to. inline is tightest and sits beside a field group; page loses its corners and its inline edges because it runs to both edges of what holds it.
live'off' | 'polite' | 'assertive'Default off
Whether the text announces itself. polite writes role="status" on the content part and assertive writes role="alert"; both imply aria-live and aria-atomic, and neither is written on the box.
actionsPlacement'below' | 'end'Default end for reach='page', below otherwise
Where the actions stand. below puts them on a row under the words; end puts them at the end of the words’ line, before the dismiss control, once the box is 40rem wide, and under the words below that width.
hideIconbooleanDefault false
Draws no mark at all, for a callout that carries its own artwork in the body.
iconReactNode
Replaces the tone’s mark, keeping the box, the size and the place on the message’s first line. Angular projects an element carrying slAlertIcon instead.
dismissiblebooleanDefault false
Renders the control that closes the message. Leave it off for a critical banner that must stay until the condition is resolved.
messagesPartial<AlertMessages>
Words laid over the language in force, key by key: what that control announces (dismiss).
openboolean
The openness the consumer owns. Passing it puts the alert in controlled mode: the alert only requests changes and the consumer moves open.
defaultOpenbooleanDefault true
The openness an uncontrolled alert starts from, read once. A later change to it is not silently adopted.
restoreFocusbooleanDefault true
Returns the keyboard to the element it came from when the message closes with focus inside it. Nothing moves when focus was never there.

Events

NamePayloadWhen
onOpenChangeopen: boolean, request: ChangeRequest<boolean>The dismiss control is activated. request.reason is dismiss, request.source is the click event, and request.cancel() refuses the change — the message stays open and the keyboard stays where it is. Nothing is emitted when the consumer moves open themselves.

Composition

render(props) => ReactNode
On Alert, AlertContent, AlertTitle, AlertDescription, AlertActionsSubstitutes the rendered element of a part, and hands it every data attribute. A title rendered as a heading is how a page puts the message in its outline.
refRef<HTMLElement>
Reaches the section, which carries tabindex="-1" so focus() works. There is no imperative handle beyond it: Alert never moves focus on its own, because the APG says an alert must not affect keyboard focus.
AlertDefaultsProvider{ tone, variant, reach, live }
On a subtreeSets all four once for every alert under it; an explicit prop on one message still wins.
childrenReactNode
On AlertContentThe title and the description. Anything else placed here joins the live region, which is why the actions are a part of their own.

Parts

PartElementRequiredPurpose
rootsectionYesThe box. Carries data-ui="alert", the four axes, data-open, hidden when closed, and tabindex="-1". A section named by its own title is a region landmark; unnamed it is a plain grouping element.
iconspanNoThe tone’s mark, drawn as a mask from the status glyph set and hidden from assistive technology. Absent when hideIcon; holds the page’s own artwork when icon is passed.
contentdivYesThe title and the description, and nothing else. This is the live region: role="status" or role="alert" is written here, so the controls beside it are outside what a reader hears.
titlediv, or the element render returnsNoThe message’s name, and the box’s: the root points aria-labelledby at it. A div by default, because a heading belongs in the document outline and only the page knows whether this message does.
descriptionp, or the element render returnsNoThe message. It breaks a string no language can break — a digest, a host name — rather than overflowing, and removes nothing from the DOM to do it.
actionsdivNoThe controls that act on the message, outside the live region. They stack at a narrow container width, and rejoin the message’s own line on a page banner past 40rem.
dismissbuttonNoCloses the message. Rendered when dismissible, named by the dismiss message, and sized from its glyph plus a step of air, floored at the WCAG 2.2 minimum target size so it holds at every density.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every alert inside it.

Alert tokens

TokenDefault
--slotted-alert-actions-gapvar(--slotted-action-gap, 8px)
--slotted-alert-border-widthvar(--slotted-border-width-thin, 1px)
--slotted-alert-column-gapvar(--slotted-space-4, 0.5rem)
--slotted-alert-dismiss-radiusvar(--slotted-control-radius, 4px)
--slotted-alert-dismiss-sizemax( var(--slotted-target-min, 24px), min(calc(var(--_glyph-size) + 2 * var(--slotted-space-2, 0.25rem)), var(--_inner-size)) )
--slotted-alert-font-familyvar(--slotted-control-font-family, sans-serif)
--slotted-alert-padding-blockvar(--slotted-space-5, 0.75rem)
--slotted-alert-padding-inlinevar(--slotted-space-6, 1rem)
--slotted-alert-radiusvar(--slotted-radius-md, 6px)
--slotted-alert-row-gapvar(--slotted-space-5, 0.75rem)
--slotted-alert-state-layer-active16%
--slotted-alert-state-layer-hover10%
--slotted-alert-text-gapvar(--slotted-space-2, 0.25rem)
--slotted-alert-title-font-familyvar( --slotted-font-family-heading, var(--slotted-font-family-sans, ui-sans-serif, system-ui, sans-serif) )
--slotted-alert-title-font-weightvar(--slotted-font-weight-semibold, 600)

Theme tokens it reads

TokenDefault
--slotted-action-gapcomfortable 8px · compact 6px · dense 4px
--slotted-border-width-thin1px
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-font-size-lgcomfortable 14px · compact 13px · dense 12px
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-font-size-smcomfortable 12px · compact 12px · dense 12px
--slotted-control-icon-size-lgcomfortable 18px · compact 16px · dense 14px
--slotted-control-icon-size-mdcomfortable 16px · compact 14px · dense 13px
--slotted-control-icon-size-smcomfortable 14px · compact 13px · dense 12px
--slotted-control-inner-size-lgcomfortable 28px · compact 24px · dense 22px
--slotted-control-inner-size-mdcomfortable 24px · compact 20px · dense 18px
--slotted-control-inner-size-smcomfortable 22px · compact 18px · dense 16px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-short120ms
--slotted-easing-entercubic-bezier(0.05, 0.7, 0.1, 1)
--slotted-easing-exitcubic-bezier(0.3, 0, 0.8, 0.15)
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-family-headingvar(--slotted-font-family-sans)
--slotted-font-family-sans'Geist Variable', ui-sans-serif, system-ui, sans-serif
--slotted-font-weight-semibold600
--slotted-line-height-md1.5
--slotted-radius-md6px
--slotted-radius-sm4px
--slotted-space-20.25rem
--slotted-space-40.5rem
--slotted-space-50.75rem
--slotted-space-61rem
--slotted-space-71.25rem
--slotted-target-comfortable44px
--slotted-target-min24px
--slotted-tone-accent-borderSet per scheme by the theme
--slotted-tone-accent-on-solidSet per scheme by the theme
--slotted-tone-accent-solidSet per scheme by the theme
--slotted-tone-accent-surfaceSet per scheme by the theme
--slotted-tone-accent-textSet per scheme by the theme
--slotted-tone-danger-borderSet per scheme by the theme
--slotted-tone-danger-on-solidSet per scheme by the theme
--slotted-tone-danger-solidSet per scheme by the theme
--slotted-tone-danger-surfaceSet per scheme by the theme
--slotted-tone-danger-textSet per scheme by the theme
--slotted-tone-info-borderSet per scheme by the theme
--slotted-tone-info-on-solidSet per scheme by the theme
--slotted-tone-info-solidSet per scheme by the theme
--slotted-tone-info-surfaceSet per scheme by the theme
--slotted-tone-info-textSet per scheme by the theme
--slotted-tone-secondary-borderSet per scheme by the theme
--slotted-tone-secondary-on-solidSet per scheme by the theme
--slotted-tone-secondary-solidSet per scheme by the theme
--slotted-tone-secondary-surfaceSet per scheme by the theme
--slotted-tone-secondary-textSet per scheme by the theme
--slotted-tone-success-borderSet per scheme by the theme
--slotted-tone-success-on-solidSet per scheme by the theme
--slotted-tone-success-solidSet per scheme by the theme
--slotted-tone-success-surfaceSet per scheme by the theme
--slotted-tone-success-textSet per scheme by the theme
--slotted-tone-warning-borderSet per scheme by the theme
--slotted-tone-warning-on-solidSet per scheme by the theme
--slotted-tone-warning-solidSet per scheme by the theme
--slotted-tone-warning-surfaceSet per scheme by the theme
--slotted-tone-warning-textSet per scheme by the theme