Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Alert

A status message in the content: something people need to know now, or the result of what they just did. It shows its status with an icon, a word and a colour. A screen reader (software that reads the screen aloud) says it only when you ask for that.

Package
@kvirn-ui/react

When to use it#

  • Use it for a message about the state of the page or the result of an action: a deadline, a saved draft, a failed send.
  • Choose the status by choosing the component: Alert.Info, Alert.Success, Alert.Warning or Alert.Danger. Write the outcome in the user’s words, not only the status.
  • Put it in the content, near what it is about. An alert that is already there when the page loads needs nothing more: a screen reader reads it in its place on the page. Use announce only for an alert that appears after the user did something. It makes the screen reader read the text aloud.
  • Not for a message that interrupts and needs an answer: use an AlertDialog. Not for a short message in the corner after something worked: use a Toast. Not for a field’s error: that belongs to the Field. Not for ordinary text: use Prose.

Example#

Warning: Your parking permit expires on 12 November 2026

Renew it by 5 November, or you may get a parking fine.

Use cases#

Four statuses

Each status has its own component. Its icon, word and colour always agree, so the status never depends on colour alone. For a one-sentence alert, show the Title as a paragraph with as="p" instead of a heading.

Information: The office is closed on 24 December

Success: Your application was sent

Warning: Your booking is not confirmed yet

Error: We couldn’t save your changes

Props in this example:

  • as on Alert.Title

Warning: Don't forget

The Title is an h2 by default. Set as so it sits one level below the heading of the part of the page it is in, and never skip a level. Headings give the page its outline (1.3.1 Info and Relationships, level A; 2.4.6 Headings and Labels, level AA).

With something to do

When the user can act on the message, put at most two actions in Alert.Actions. Start each with a verb, such as “Try again”. Use a Link to go somewhere and a Button to do something. People reach them with the Tab key, and they are not part of the announced text.

Error: We couldn’t send your application

Your answers are saved. Try again in a few minutes.

Contact us

Heard after an action

An alert that is there when the page loads is read in its place on the page. One that appears after the user pressed a button is silent unless you ask. Set announce="polite" so a screen reader reads the text aloud. Focus stays on the button. What is read, and how to change it, is under Announcements.

Props in this example:

Information: Did you know?

Use assertive (the screen reader says it at once, even if it must interrupt) only for an Alert.Danger the user must act on right now. On Info or Success it gives a development warning.

One the user can close

Add Alert.Close only to a message the user can safely dismiss, such as a tip or a saved confirmation. Don’t add it to an error or a warning they still have to act on. The alert doesn’t remember whether it is shown: you remove it yourself. Move focus to another place first, because the close button has focus and disappears with the alert.

Your cases

Information: The service is slower than usual today

Searching can take up to a minute. Your answers are still saved.

Props in this example:

Warning: Don't forget

Move focus to a sensible place in the same handler, such as the heading of that part of the page. An element that had focus and is removed sends focus to the page, so the user loses their place (2.4.3 Focus Order, level A). See What you need to do.

A message for the whole site

Some messages are about every page, such as a planned outage. Render the alert as a section and name it with the Title’s id. Screen reader users can then find it in their list of page regions (landmarks). Do this for one site-wide alert only, never for a message about one part of a page.

Warning: The booking service is closed for maintenance on Saturday 14 November

You can book again from 14:00. For urgent cases, call the service line.

Props in this example:

  • as on Alert.Warning

Information: Did you know?

The region’s name is the Title, so write a Title that makes sense on its own. Keep an outage message on the page for as long as it is true. Never remove it on a timer (2.2.1 Timing Adjustable, level A).

Accessibility#

What it does for you#

  • APG pattern: none for the visible block. APG's Alert pattern is a live region, which the Announcer already provides, so the Alert never renders one.
  • Deviations: none. No role on the box, announce goes through the Announcer, and there is a plain Root and four ready-made status roots. The optional Close is a native button: dismissing never changes how the alert is announced.
  • Native elements used: <div> for the roots, Body and Actions; <h2> for the Title (as changes it to another heading or to <p>); <svg> for the decorative icon; <span> for the status word; <button type="button"> for the optional Close.

An Alert is a status message in the content: something people need to know now, or the result of what they just did. It shows its status with an icon, a word and a colour, never with colour alone (1.4.1 Use of Color, A). It doesn't announce itself unless the consumer asks (announce), and it never takes focus on its own. It can have one optional close button (Alert.Close). The alert owns no open or closed state: the consumer removes it in the button's onClick and moves focus on.

There are five roots. Alert.Root is plain: kv-alert only, no status class, no icon and no status word. Alert.Info, .Success, .Warning and .Danger (also exported as AlertInfo and so on) are ready-made: each renders its status class, its icon and the status word from one internal table, so the colour, the icon and the word cannot disagree. Status is a class, not a prop: you choose it by choosing the component, and there is no prop that changes a ready-made root's status.

PartElement / roleARIANotes
Alert.Root<div> → genericnoneclass="kv-alert". No status class, no icon, no status word. Takes announce. Also exported as AlertRoot
Alert.Info, .Success, .Warning, .Danger<div> → genericnoneclass="kv-alert kv-alert--<status>", the status icon first (info, success, warning, error), and the status word for the Title. Take announce, messages and icon (a replacement for the status icon, decorative like it: aria-hidden, class kv-alert-icon). Also exported as AlertInfo, AlertSuccess, AlertWarning and AlertDanger
Icon (ready-made roots only)<svg class="kv-icon kv-alert-icon">aria-hidden="true"Decorative: the status is in text. The consumer's own <Icon> on a plain Root is decorative the same way
Alert.Title<h2> by default → heading level 2noneclass="kv-alert-title". Required. as sets the level (as="h3") or as="p" for a one-sentence alert. Inside a ready-made root it starts with <span class="kv-alert-status"> (the status word from i18n), then a normal space, then the children. Inside a plain Root it adds nothing
Status word<span class="kv-alert-status">noneText, never aria-hidden. Visually hidden by the theme but never removed from the page, so screen readers read it and it is in the heading list. Without the theme it shows, which is correct unstyled
Alert.Body<div> → genericnoneclass="kv-alert-body". Optional
Alert.Actions<div> → genericnoneclass="kv-alert-actions". Optional. Links and Buttons inside keep their own roles and names
Alert.Close<button type="button"> → buttonaria-label from alert.close (Close message)class="kv-alert-close". Optional, at most one, last in the root. Its content is the decorative <Icon name="close" /> (aria-hidden), so the name is the aria-label. children replace the icon with visible text, which then names the button and drops the aria-label. onClick is gated by disabled. Also exported as AlertClose
every rootneverno role, aria-live, aria-atomic, aria-label, aria-labelledbyNot a landmark and not a live region by default. No tabindex either. A development warning fires when a root is given role="alert", role="status" or aria-live
every part except Closeas (allowed elements below)the chosen element's ownOne element per part, chosen with a string. The part's class joins a className prop, never replaced. A value outside the list is a type error and, in JS, warns once (as-not-allowed:Alert.Title:<tag>) and renders the default. Alert.Close has no as: it is always a native <button>. For another element use useAlert
every partattributespassed throughid, data-*, aria-* and every other attribute reach the element unchanged

useAlert({ variant?, announce?, messages? }) gives the same props for your own elements: rootProps, titleProps, bodyProps, actionsProps, closeProps (className, type, and the resolved aria-label), and with variant also iconProps and statusProps, all from the same table as the components.

What you need to do#

  • Language of the status word. The word follows the provider's locale, not set per alert. When an alert's content is in another language, set messages on that alert (for example messages={{ warningPrefix: 'Warning:' }}), so the word and the text agree (3.1.2 Language of Parts, AA).
  • A Title. Every alert has one: it holds the status word and names the message. A development warning fires without one.
  • The heading level is yours (1.3.1 Info and Relationships, A; 2.4.6 Headings and Labels, AA): one level below the heading of the part of the page it is in, so usually h2 directly under the page's h1, h3 inside a section with an h2. Never skip levels. A one-sentence alert renders the Title as <p> with as="p".
  • Write the Title as the outcome in the user's words ("We could not send your application"), never only the status word. The Body says what to do and by when. One alert per region at a time. Never on a timer (2.2.1 Timing Adjustable, A; 2.2.3 No Timing, AAA): a transient message is a Toast, and a warning or danger message never toasts.
  • Pick the status by what happened, not by the colour you want. Choose by component. Don't pass a status class of another status to a ready-made root, and don't put one of our status classes on the plain Root (development warnings).
  • The plain Root is yours to complete: a status word first in the Title (in all your locales, in a kv-alert-status span), an icon with a distinct shape, both agreeing with your colour (1.3.1 Info and Relationships, A; 1.4.1 Use of Color, A).
  • The heading list and the landmark option. An alert isn't a landmark. For one site-wide alert only (a service outage), render it as a named region: as="section" with aria-labelledby={titleId}, and the Title's id. Never for messages about a part of the page.
  • Actions: at most two, verbs, Links for navigation and Buttons for actions. Don't put a destructive button in an alert.
  • Dismissing is optional (Alert.Close). Offer it only on a message the user can safely be done with: a tip, an info, a saved confirmation. Don't offer it on an error or a warning the user still has to act on: the problem doesn't go away when the message does. The alert owns no state and writes no storage, so whether it comes back on the next page (persistence) is yours to decide.
  • When to announce, present at load: no announce, no role, no focus. It is content, read in reading order and found by heading navigation. A live region announces changes, not what's there at load.
  • When to announce, inserted after an action, focus stays where it is: announce="polite", near the control that caused it. Polite waits for the screen reader to finish its own feedback on the button.
  • When to announce, urgent, not caused by the current action, and the user must act now: Alert.Danger announce="assertive". Rare. Never for Info or Success (a development warning fires).
  • When to announce, arrival after a page load or route change: move focus to the root once (tabIndex={-1}) and don't set announce.
  • When to announce, errors on submit: the error summary moves focus and doesn't set announce. Field errors are read when each field gets focus.
  • When to announce, its text changes while shown: not supported by announce, which announces on mount, once. To announce again, call useAnnouncer() yourself. Remounting it with a new React key also announces again, but only if nothing inside it holds focus: if the action that causes the change is a button inside the alert (a "Try again" that fails again), a remount destroys the focused button and focus falls to the page (2.4.3 Focus Order, A). Keep the instance and use useAnnouncer() instead, or move focus to a stable target first.
  • When to announce. Without a KvirnProvider, announcements are dropped after one development warning: announce needs a provider. Inside a KvirnUI Dialog an announcement goes to the dialog's own live region (dialog.test.tsx › a component inside announces in the dialog’s own live region, which exists only while open), because a modal makes the rest of the page inert. A modal you build yourself silences the page's regions, so it needs its own.
  • When to announce. An alert that is server-rendered with announce is announced after hydration. Set announce only from the state of the action that just happened.
  • On press: remove the alert and move focus to a sensible place in the same handler (Focus management). Never on a timer (2.2.1 Timing Adjustable, A).
  • The name (alert.close, "Close message" in en) follows the provider's locale. When the alert's content is in another language, set messages={{ close: … }} on the root or the button, like the status word (3.1.2 Language of Parts, AA). A visible label of your own goes in the children.
  • Replacement icons (defineIcons) for info, success, warning or error must keep a distinct shape from the other three.
  • Language. Text in another language goes in a nested KvirnProvider, with useLocale().localeProps on your own element (3.1.2 Language of Parts, AA). The status word follows the provider's locale.

Warning: Don't forget

When an alert’s text is in another language than the page, set messages on that alert, so the status word and the text agree (3.1.2 Language of Parts, level AA).

Visual and modes#

Headless: Alert ships no CSS. With the theme stylesheet (@kvirn-ui/theme):

  • Focus indicator: the root has none of its own unless the consumer made it focusable (tabIndex={-1}): a focus ring with a gap, outside the box. The close button has the same ring on keyboard focus. Children keep their own rings, never clipped.
  • Target size: the close button is at least 24 by 24 CSS px (2.5.8 Target Size (Minimum), AA); the default theme makes it larger, and smaller than that only in its compact density. It is centred on the title's first line and reaches into the alert's padding, so the title keeps its height. Buttons and links in Actions keep their own sizes.
  • Contrast: the close button's cross is drawn in the text colour and its focus ring in the focus-ring colour, on the four lightly tinted status backgrounds. Text, links, muted text, the focus ring, button edges and the status's own bar and icon (3:1) are held to their minimums on those four backgrounds by the theme's contrast check, run on every theme (1.4.3 Contrast (Minimum), AA; 1.4.11 Non-text Contrast, AA). The lowest pairs are 3.13:1 and 3.32:1 in dark mode on the tinted primary background; a rebrand of the primary colours can break them first.
  • forced-colors behaviour: the alert uses the system's background, text and link colours, and all four edges are drawn in the system's text colour with the thick bar kept. The close button's icon uses the system's button-text colour and its ring the system's highlight colour. All four statuses then share one colour, so the status is carried by the icon's shape (square, circle, triangle, octagon), the status word in the accessibility tree and the title's words.
  • reduced-motion behaviour: no motion at all. It appears and disappears instantly.
  • Reflow and text spacing: no fixed sizes, nothing scrolls or is cut off, and long words hyphenate and then break; the icon stays on the title's first line. No horizontal scrolling at 320 CSS px (1.4.10 Reflow, AA): the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow, and one story uses a long Finnish text. Text spacing grows the height and clips nothing (1.4.12 Text Spacing, AA): the text-spacing sweep applies the 1.4.12 spacing to every story and fails on a box that clips more content than it holds.
  • RTL: logical properties only. The bar and icon sit on the inline start, and the close button on the inline end. Status icons never mirror, and neither does the cross.
  • Print: the thick bar (a border) and the icon print, so the status stays visible on paper. The close button is hidden in print.
  • Without the theme stylesheet: no colour, no bar, no hidden word. The icon and "Warning: …" show as plain text, which is a correct unstyled alert.

Focus management#

  • Initial focus: not moved. An Alert never moves focus, never traps it and is not a Tab stop.
  • Trap: no.
  • Focus target: the consumer may make a root a focus target with tabIndex={-1} (an arrival message, the later error summary block). It then shows the focus ring on :focus-visible, Tab from it goes to the first link or button inside, and it never gets tabIndex={0}. Moving focus to an alert and setting announce on it would read it twice: use one of them.
  • Inserting an alert never moves focus away from the control the user pressed (3.2.2 On Input, A). Removing one that holds focus is the consumer's bug: move focus first (see Restore to).
  • Restore to: not applicable. After dismissing, the consumer moves focus (2.4.3 Focus Order, A). The close button is removed with the alert, and a removed element that had focus sends focus to the page. In the same onClick, move focus to a sensible place first: the heading of the part of the page the alert belonged to (tabIndex={-1}), the control that caused the alert, or a control that brings the message back. Never leave it on a removed element. Alert cannot do this for you, because it doesn't know what the page is.
  • Never obscured by: the default theme never clips an alert, so a link's or button's focus ring inside is never clipped (2.4.11 Focus Not Obscured, AA; 2.4.13 Focus Appearance, AAA). With a sticky header, the page sets scroll-padding so a focused alert isn't hidden.

Allowed elements#

A tag outside a list changes the page's outline or semantics (1.3.1 Info and Relationships, A; 4.1.2 Name, Role, Value, A), so each part takes only these (as is a string, so it works from a Server Component).

Part`as`Why
Root, Info, Success, Warning, Dangerdiv (default), sectionsection with aria-labelledby is the named region for a site-wide alert.
Titleh2 (default), h3, h4, h5, h6, pThe consumer's level for the outline, or a paragraph for a one-sentence alert. No h1: the page title is not an alert's
Bodydiv (default), p, sectionPlain text can be a paragraph; div holds several
Actionsdiv (default), sectionA group of links and buttons, no list semantics added
ClosenoneAlways <button type="button"> (4.1.2 Name, Role, Value, A)

WCAG success criteria#

  • 1.1.1 Non-text Content (A): the status icon and the close icon are decorative, the status and the button's name are text.
  • 1.3.1 Info and Relationships (A): the Title is a heading (or a paragraph by choice), the status word is text in it. 1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A): DOM order is the reading and focus order. The close button is last in it (after the Actions), while the theme shows it at the inline end of the first line: it is reached last, which keeps the Title and Body together for a screen reader (2.4.3 allows a visual position that differs when the order is still meaningful).
  • 1.4.1 Use of Color (A): icon shape, the status word and the title's words, never colour alone.
  • 1.4.3 Contrast (Minimum) (AA), 1.4.11 Non-text Contrast (AA): the theme's contrast check on the four tinted status backgrounds.
  • 2.2.1 Timing Adjustable (A): no timers, nothing disappears. 2.2.3 No Timing (AAA, not counted toward AA): the same.
  • 2.4.6 Headings and Labels (AA): the Title describes the topic.
  • 2.4.7 Focus Visible (AA), 2.4.11 Focus Not Obscured (Minimum) (AA): a focused root's and a focused close button's ring, never clipped. 2.4.13 Focus Appearance (AAA, not counted toward AA): the same ring.
  • 2.1.1 Keyboard (A), 2.4.3 Focus Order (A): the close button is a native button, last in DOM order, and the consumer moves focus on dismissing.
  • 2.5.8 Target Size (Minimum) (AA): the close button is at least 24 by 24 CSS px.
  • 3.1.2 Language of Parts (AA): the status word and the close button's name follow the provider's locale, and messages on the alert makes them agree with content in another language.
  • 3.2.2 On Input (A): inserting an alert never moves focus.
  • 3.3.1 Error Identification (A), 3.3.3 Error Suggestion (AA): a danger alert names what happened and what to do (consumer's content).
  • 4.1.2 Name, Role, Value (A): the box has no role or name of its own to get wrong. The close button is a native button with a name from i18n (alert.close).
  • 4.1.3 Status Messages (AA): announce through the Announcer. See Known issues.

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none

An Alert is never a Tab stop of its own. Its only key handling is the optional close button's, a native <button>: Enter and Space press it, with no key code of ours. Links and buttons in its Body and Actions keep their own keys (see button.a11y.md and link.a11y.md). The close button comes last in the DOM, so it is the last Tab stop of the alert.

KeyWhereWhat happens
TabBefore an alert with a link in its ActionsMoves to the link. The alert itself is skipped
Test: alert.test.tsx › keyboard › Tab skips the alert and reaches its link
TabOn the last link or button in the alertMoves to the close button, after the actions
Test: alert.test.tsx › keyboard › Tab reaches the close button after the actions
Shift+TabOn the close buttonMoves back to the alert's last link or button
Test: alert.test.tsx › keyboard › Shift+Tab from the close button goes back to the link
Shift+TabOn the control after the alertMoves back to the alert's last button or link
Test: alert.test.tsx › keyboard › Shift+Tab reaches the alert’s action
EnterOn the close buttonPresses it: the consumer's onClick runs, removes the alert and moves focus to a set target
Test: alert.test.tsx › keyboard › Enter on the close button dismisses the alert and moves focus to the heading
SpaceOn the close buttonPresses it, like Enter
Test: alert.test.tsx › keyboard › Space on the close button dismisses the alert and moves focus to the heading
EnterOn a button inside the alert, such as a retryActivates it. Focus stays on the button
Test: alert.test.tsx › keyboard › Enter on a button inside the alert keeps focus on it
TabOn an alert focused by script (tabIndex={-1})Moves to the first link or button inside it
Test: alert.test.tsx › keyboard › Tab from a focused alert goes to its first action
No keyEvery rootNo tabindex is rendered by default
Test: alert.test.tsx › the alert box stays without a role, a live region, a tabindex or a name of its own
No keyA disabled close buttonSkipped by Tab, and click runs no handler
Test: alert.test.tsx › Alert.Close › disabled is native: no handler on click, skipped by Tab

Escape, arrow keys and Home / End are not handled. Children handle their own keys.

Announcements#

Alert tells screen reader users about these changes without moving focus. Each message waits until the screen reader is quiet (polite) or interrupts it (assertive).

WhenWhat it saysMessage keyPoliteness
A root with announce mountsalert.infoPrefix successPrefix warningPrefix dangerPrefixPolite or assertive, as you choose

Change the text#

For your whole app, pass messages to KvirnProvider. For one Alert, pass its messages prop.

<KvirnProvider messages={{ alert: { infoPrefix: '…' } }}>

Warning: Don't forget

Announcements need a KvirnProvider around your app, outside any <form>, or an Announcer of your own. Without one nothing is announced, and a development warning says so.

The announced text is the alert’s own visible text. To change it, change the Title and the Body. To change the status word, use the keys in Strings.

API reference#

import { Alert, useAlert } from '@kvirn-ui/react'

Alert.Root

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. The plain root, for your own design: it has only the kv-alert class, with no status class, icon or status word. You add a status word at the start of the Title and an icon with a shape of its own. Both must agree with your colour. Also exported as AlertRoot.

PropTypeDefaultDescription
announce'polite' | 'assertive'–Announces the Title and Body text once, when the alert mounts, through the Announcer. Only for an alert inserted after an action. Needs a KvirnProvider. Without it nothing is announced.
as'div' | 'section''div'Changes the element. A section with aria-labelledby is a named region, only for one site-wide alert such as an outage. Another tag warns once in development.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-alertalwaysThe part class. The default theme styles it.

Alert.Info, Alert.Success, Alert.Warning, Alert.Danger

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. These four ready-made roots take the same props. Each adds its status class, its icon and the status word that starts the Title, all from one place in the code, so the colour, the icon and the word can’t disagree. Also exported as AlertInfo and so on. The default theme is described on Theming.

PropTypeDefaultDescription
announce'polite' | 'assertive'–Announces the Title and Body text once, when the alert mounts, through the Announcer. Only for an alert inserted after an action. Needs a KvirnProvider. Without it nothing is announced. assertive on Info or Success gives a development warning.
messagesPartial<KvirnMessages['alert']>–Per-instance override of this root’s status word, such as { dangerPrefix: "Important:" }, and of the close button’s name.
iconReactNodethe status iconReplaces the status icon, such as a spinner for a job in progress. Give it className="kv-alert-icon" and aria-hidden="true": the status stays in the word.
as'div' | 'section''div'Changes the element. A section with aria-labelledby is a named region, only for one site-wide alert such as an outage. Another tag warns once in development.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-alertalwaysThe part class. The default theme styles it.
kv-alert--infoon Alert.InfoThe status class. Chosen by the component, never by a prop.
kv-alert--successon Alert.SuccessThe status class.
kv-alert--warningon Alert.WarningThe status class.
kv-alert--dangeron Alert.DangerThe status class.
kv-alert-iconon the icon of a ready-made rootThe decorative status icon, first in the root: info, success, warning or error.

Alert.Title

Renders <h2> with the role heading, level 2. Required: it names the message. It takes every attribute of an <h2> and passes ref to it.

PropTypeDefaultDescription
as'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p''h2'Sets the heading level (as="h3") or a paragraph for a one-sentence alert (as="p"). The status word stays. There is no h1: the page title is not an alert’s.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-alert-titlealwaysThe part class.
kv-alert-statuson the status word of a ready-made rootThe span that starts the Title. The theme hides it visually, and screen readers read it.

Alert.Body

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. Optional: what to do, and by when, in one to three short sentences.

PropTypeDefaultDescription
as'div' | 'p' | 'section''div'Changes the element: a p for plain text, a div when it holds several blocks.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-alert-bodyalwaysThe part class.

Alert.Actions

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. Optional: up to two Links or Buttons, which keep their own roles and names. They aren’t announced.

PropTypeDefaultDescription
as'div' | 'section''div'Changes the element. Actions adds no list semantics.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-alert-actionsalwaysThe part class.

Alert.Close

Renders <button type="button"> with the role button, named by alert.close. Optional, and last in the root. It owns no open or closed state: you remove the alert yourself. It takes every attribute of a <button> except type and aria-disabled.

PropTypeDefaultDescription
messagesPartial<KvirnMessages['alert']>–Per-instance override of the button’s name (close).
disabledbooleanfalseNatively disabled: skipped by Tab, and onClick doesn’t run.
onClickMouseEventHandler<HTMLButtonElement>–Called on click, Enter and Space. Remove the alert here, then move focus: the button is gone with it.
refRef<HTMLButtonElement>–Reaches the button.
Class or attributeValuesMeaning
kv-alert-closealwaysThe part class.
data-disabledpresent or absentThe button is disabled.
data-focus-visiblepresent or absentThe button has keyboard focus (:focus-visible).

useAlert

Options

PropTypeDefaultDescription
variant'info' | 'success' | 'warning' | 'danger'–Makes the result a ready-made root: the status class, iconProps and statusProps. Without it you bring your own icon and status word.
announce'polite' | 'assertive'–Announces the Title and Body text once, when the alert mounts, through the Announcer. Only for an alert inserted after an action. Needs a KvirnProvider. Without it nothing is announced.
messagesPartial<KvirnMessages['alert']>–Per-instance overrides: the status word of the variant, and the close name.

Result

PropTypeDefaultDescription
rootPropsAlertRootPartProps–Spread on the root: kv-alert, the status class, and a callback ref.
titlePropsAlertTitlePartProps–Spread on the Title. Its ref lets announce read the text.
bodyPropsAlertBodyPartProps–Spread on the Body. Its ref lets announce read the text.
actionsPropsAlertActionsPartProps–Spread on the actions element.
closePropsAlertClosePartProps–Spread on a <button>: class, type and the resolved aria-label. Drop the label if the button has visible text.
iconPropsAlertIconPartProps | undefined–Spread on an Icon. Undefined without a variant.
statusPropsAlertStatusPartProps | undefined–Spread on a span at the start of the Title. Undefined without a variant.

Strings

These are the default texts in @kvirn-ui/i18n.

For your whole app, pass messages to KvirnProvider. For one Alert, pass its messages prop.

alert.infoPrefix

The status word that starts the Title of Alert.Info.

LanguageDefault text
SvenskaInformation:
SuomiTiedoksi:
Norsk bokmålInformasjon:
Norsk nynorskInformasjon:
EnglishInformation:

alert.successPrefix

The status word that starts the Title of Alert.Success.

LanguageDefault text
SvenskaKlart:
SuomiValmis:
Norsk bokmålFullført:
Norsk nynorskFullført:
EnglishSuccess:

alert.warningPrefix

The status word that starts the Title of Alert.Warning.

LanguageDefault text
SvenskaVarning:
SuomiVaroitus:
Norsk bokmålAdvarsel:
Norsk nynorskÅtvaring:
EnglishWarning:

alert.dangerPrefix

The status word that starts the Title of Alert.Danger.

LanguageDefault text
SvenskaFel:
SuomiVirhe:
Norsk bokmålFeil:
Norsk nynorskFeil:
EnglishError:

alert.close

The accessible name of Alert.Close.

LanguageDefault text
SvenskaStäng meddelandet
SuomiSulje ilmoitus
Norsk bokmålLukk meldingen
Norsk nynorskLukk meldinga
EnglishClose message