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.WarningorAlert.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
announceonly 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.
import { Alert } from '@kvirn-ui/react'
import { useAlertTexts } from './texts.ts'
export function DefaultAlert() {
const { texts } = useAlertTexts()
return (
<Alert.Warning>
<Alert.Title>{texts.permit.title}</Alert.Title>
<Alert.Body>
<p>{texts.permit.body}</p>
</Alert.Body>
</Alert.Warning>
)
}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
import { Alert } from '@kvirn-ui/react'
import { useAlertTexts } from './texts.ts'
export function Statuses() {
const { texts } = useAlertTexts()
return (
<div>
<Alert.Info>
<Alert.Title as="p">{texts.statuses.info}</Alert.Title>
</Alert.Info>
<Alert.Success>
<Alert.Title as="p">{texts.statuses.success}</Alert.Title>
</Alert.Success>
<Alert.Warning>
<Alert.Title as="p">{texts.statuses.warning}</Alert.Title>
</Alert.Warning>
<Alert.Danger>
<Alert.Title as="p">{texts.statuses.danger}</Alert.Title>
</Alert.Danger>
</div>
)
}Props in this example:
ason 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.
import { Alert, Button, Link } from '@kvirn-ui/react'
import { useAlertTexts } from './texts.ts'
export function WithActions() {
const { texts } = useAlertTexts()
return (
<Alert.Danger>
<Alert.Title>{texts.failed.title}</Alert.Title>
<Alert.Body>
<p>{texts.failed.body}</p>
</Alert.Body>
<Alert.Actions>
<Button>{texts.failed.retry}</Button>
<Link href="#with-actions">{texts.failed.contact}</Link>
</Alert.Actions>
</Alert.Danger>
)
}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.
import { Alert, Button } from '@kvirn-ui/react'
import { useState } from 'react'
import { useAlertTexts } from './texts.ts'
export function Announced() {
const { texts } = useAlertTexts()
const [saveCount, setSaveCount] = useState(0)
return (
<div>
{saveCount === 0 ? null : (
<Alert.Success key={saveCount} announce="polite">
<Alert.Title as="p">{texts.saved.title}</Alert.Title>
</Alert.Success>
)}
<Button className="kv-button--primary" onClick={() => setSaveCount(saveCount + 1)}>
{texts.saved.save}
</Button>
</div>
)
}Props in this example:
announceon Alert.Success
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.
import { Alert, Button, Heading } from '@kvirn-ui/react'
import { useRef, useState } from 'react'
import { useAlertTexts } from './texts.ts'
export function Dismissible() {
const { texts } = useAlertTexts()
const [isShown, setIsShown] = useState(true)
const headingRef = useRef<HTMLHeadingElement>(null)
return (
<div>
<Heading as="h3" tabIndex={-1} ref={headingRef}>
{texts.dismissible.heading}
</Heading>
{isShown ? (
<Alert.Info>
<Alert.Title as="p">{texts.dismissible.title}</Alert.Title>
<Alert.Body>
<p>{texts.dismissible.body}</p>
</Alert.Body>
<Alert.Close
onClick={() => {
headingRef.current?.focus()
setIsShown(false)
}}
/>
</Alert.Info>
) : null}
<Button onClick={() => setIsShown(true)}>{texts.dismissible.showAgain}</Button>
</div>
)
}Props in this example:
onClickon Alert.Close
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.
import { Alert } from '@kvirn-ui/react'
import { useId } from 'react'
import { useAlertTexts } from './texts.ts'
export function SiteWide() {
const { texts } = useAlertTexts()
const titleId = useId()
return (
<Alert.Warning as="section" aria-labelledby={titleId}>
<Alert.Title id={titleId}>{texts.siteWide.title}</Alert.Title>
<Alert.Body>
<p>{texts.siteWide.body}</p>
</Alert.Body>
</Alert.Warning>
)
}Props in this example:
ason 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,
announcegoes 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 (aschanges 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.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Alert.Root | <div> → generic | none | class="kv-alert". No status class, no icon, no status word. Takes announce. Also exported as AlertRoot |
| Alert.Info, .Success, .Warning, .Danger | <div> → generic | none | class="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 2 | none | class="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"> | none | Text, 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> → generic | none | class="kv-alert-body". Optional |
| Alert.Actions | <div> → generic | none | class="kv-alert-actions". Optional. Links and Buttons inside keep their own roles and names |
| Alert.Close | <button type="button"> → button | aria-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 root | never | no role, aria-live, aria-atomic, aria-label, aria-labelledby | Not 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 Close | as (allowed elements below) | the chosen element's own | One 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 part | attributes | passed through | id, 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
messageson that alert (for examplemessages={{ 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
h2directly under the page'sh1,h3inside a section with anh2. Never skip levels. A one-sentence alert renders the Title as<p>withas="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-statusspan), 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"witharia-labelledby={titleId}, and the Title'sid. 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 setannounce. - 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, calluseAnnouncer()yourself. Remounting it with a new Reactkeyalso 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 useuseAnnouncer()instead, or move focus to a stable target first. - When to announce. Without a
KvirnProvider, announcements are dropped after one development warning:announceneeds 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
announceis announced after hydration. Setannounceonly 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" inen) follows the provider's locale. When the alert's content is in another language, setmessages={{ 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 thechildren. - Replacement icons (
defineIcons) forinfo,success,warningorerrormust keep a distinct shape from the other three. - Language. Text in another language goes in a nested
KvirnProvider, withuseLocale().localePropson 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 getstabIndex={0}. Moving focus to an alert and settingannounceon 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-paddingso 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, Danger | div (default), section | section with aria-labelledby is the named region for a site-wide alert. |
| Title | h2 (default), h3, h4, h5, h6, p | The consumer's level for the outline, or a paragraph for a one-sentence alert. No h1: the page title is not an alert's |
| Body | div (default), p, section | Plain text can be a paragraph; div holds several |
| Actions | div (default), section | A group of links and buttons, no list semantics added |
| Close | none | Always <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
messageson 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):
announcethrough 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.
| Key | Where | What happens |
|---|---|---|
| Tab | Before an alert with a link in its Actions | Moves to the link. The alert itself is skipped Test: alert.test.tsx › keyboard › Tab skips the alert and reaches its link |
| Tab | On the last link or button in the alert | Moves to the close button, after the actions Test: alert.test.tsx › keyboard › Tab reaches the close button after the actions |
| Shift+Tab | On the close button | Moves 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+Tab | On the control after the alert | Moves back to the alert's last button or link Test: alert.test.tsx › keyboard › Shift+Tab reaches the alert’s action |
| Enter | On the close button | Presses it: the consumer's onClick runs, removes the alert and moves focus to a set targetTest: alert.test.tsx › keyboard › Enter on the close button dismisses the alert and moves focus to the heading |
| Space | On the close button | Presses it, like Enter Test: alert.test.tsx › keyboard › Space on the close button dismisses the alert and moves focus to the heading |
| Enter | On a button inside the alert, such as a retry | Activates it. Focus stays on the button Test: alert.test.tsx › keyboard › Enter on a button inside the alert keeps focus on it |
| Tab | On 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 key | Every root | No tabindex is rendered by defaultTest: alert.test.tsx › the alert box stays without a role, a live region, a tabindex or a name of its own |
| No key | A disabled close button | Skipped 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).
| When | What it says | Message key | Politeness |
|---|---|---|---|
A root with announce mounts | alert.infoPrefix successPrefix warningPrefix dangerPrefix | Polite 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.
| Prop | Type | Default | Description |
|---|---|---|---|
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. |
ref | Ref<HTMLElement> | – | Reaches the element, whichever it is. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-alert | always | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
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. |
messages | Partial<KvirnMessages['alert']> | – | Per-instance override of this root’s status word, such as { dangerPrefix: "Important:" }, and of the close button’s name. |
icon | ReactNode | the status icon | Replaces 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. |
ref | Ref<HTMLElement> | – | Reaches the element, whichever it is. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-alert | always | The part class. The default theme styles it. |
kv-alert--info | on Alert.Info | The status class. Chosen by the component, never by a prop. |
kv-alert--success | on Alert.Success | The status class. |
kv-alert--warning | on Alert.Warning | The status class. |
kv-alert--danger | on Alert.Danger | The status class. |
kv-alert-icon | on the icon of a ready-made root | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
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. |
ref | Ref<HTMLElement> | – | Reaches the element, whichever it is. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-alert-title | always | The part class. |
kv-alert-status | on the status word of a ready-made root | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'div' | 'p' | 'section' | 'div' | Changes the element: a p for plain text, a div when it holds several blocks. |
ref | Ref<HTMLElement> | – | Reaches the element, whichever it is. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-alert-body | always | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'div' | 'section' | 'div' | Changes the element. Actions adds no list semantics. |
ref | Ref<HTMLElement> | – | Reaches the element, whichever it is. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-alert-actions | always | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
messages | Partial<KvirnMessages['alert']> | – | Per-instance override of the button’s name (close). |
disabled | boolean | false | Natively disabled: skipped by Tab, and onClick doesn’t run. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called on click, Enter and Space. Remove the alert here, then move focus: the button is gone with it. |
ref | Ref<HTMLButtonElement> | – | Reaches the button. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-alert-close | always | The part class. |
data-disabled | present or absent | The button is disabled. |
data-focus-visible | present or absent | The button has keyboard focus (:focus-visible). |
useAlert
Options
| Prop | Type | Default | Description |
|---|---|---|---|
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. |
messages | Partial<KvirnMessages['alert']> | – | Per-instance overrides: the status word of the variant, and the close name. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
rootProps | AlertRootPartProps | – | Spread on the root: kv-alert, the status class, and a callback ref. |
titleProps | AlertTitlePartProps | – | Spread on the Title. Its ref lets announce read the text. |
bodyProps | AlertBodyPartProps | – | Spread on the Body. Its ref lets announce read the text. |
actionsProps | AlertActionsPartProps | – | Spread on the actions element. |
closeProps | AlertClosePartProps | – | Spread on a <button>: class, type and the resolved aria-label. Drop the label if the button has visible text. |
iconProps | AlertIconPartProps | undefined | – | Spread on an Icon. Undefined without a variant. |
statusProps | AlertStatusPartProps | 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.
| Language | Default text |
|---|---|
| Svenska | Information: |
| Suomi | Tiedoksi: |
| Norsk bokmål | Informasjon: |
| Norsk nynorsk | Informasjon: |
| English | Information: |
alert.successPrefix
The status word that starts the Title of Alert.Success.
| Language | Default text |
|---|---|
| Svenska | Klart: |
| Suomi | Valmis: |
| Norsk bokmål | Fullført: |
| Norsk nynorsk | Fullført: |
| English | Success: |
alert.warningPrefix
The status word that starts the Title of Alert.Warning.
| Language | Default text |
|---|---|
| Svenska | Varning: |
| Suomi | Varoitus: |
| Norsk bokmål | Advarsel: |
| Norsk nynorsk | Åtvaring: |
| English | Warning: |
alert.dangerPrefix
The status word that starts the Title of Alert.Danger.
| Language | Default text |
|---|---|
| Svenska | Fel: |
| Suomi | Virhe: |
| Norsk bokmål | Feil: |
| Norsk nynorsk | Feil: |
| English | Error: |
alert.close
The accessible name of Alert.Close.
| Language | Default text |
|---|---|
| Svenska | Stäng meddelandet |
| Suomi | Sulje ilmoitus |
| Norsk bokmål | Lukk meldingen |
| Norsk nynorsk | Lukk meldinga |
| English | Close message |