Skip to content
Components, switch product

Alert

<gui-alert> is a message box. Its children are the message: the element keeps your markup as it is, styles the box around it and gives it a role, so screen readers announce it.

Your trial ends on 14 November. Pick a plan to keep your projects.
Code of the example above
AlertBasic.tsx
import { GuiAlert } from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
export function AlertBasic() {
return (
<GuiAlert variant="info">
Your trial ends on 14 November. Pick a plan to keep your projects.
</GuiAlert>
);
}

The element’s entry point is @golemui/gui-components/alert, which registers <gui-alert> and nothing else. To install the package and load the stylesheet, see Installation.

The @golemui/gui-components/react components have typed props and an on* prop for each event. React 18 or later.

AlertExample.tsx
import { GuiAlert } from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
export function AlertExample() {
return (
<GuiAlert
variant="warning"
>
Your trial ends on 14 November.
</GuiAlert>
);
}

Write the message inside it, as text or markup.

Properties

Property Type Default Description
variant GuiAlertVariant 'default' The intent the alert shows.

variant sets the intent: default, the neutral box, info, success, warning or error. The color is not the only cue: say what happened in the message itself.

Ada Lovelace joined the workspace.
The new reports are ready to try.
Your changes are saved.
Your storage is 90% full.
The payment didn't go through.
Code of the example above
AlertVariants.tsx
import { GuiAlert } from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
export function AlertVariants() {
return (
<div className="gui-grid">
<div className="gui-grid__cell">
<GuiAlert role="status">Ada Lovelace joined the workspace.</GuiAlert>
</div>
<div className="gui-grid__cell">
<GuiAlert variant="info" role="status">
The new reports are ready to try.
</GuiAlert>
</div>
<div className="gui-grid__cell">
<GuiAlert variant="success" role="status">
Your changes are saved.
</GuiAlert>
</div>
<div className="gui-grid__cell">
<GuiAlert variant="warning" role="status">
Your storage is 90% full.
</GuiAlert>
</div>
<div className="gui-grid__cell">
<GuiAlert variant="error" role="status">
The payment didn't go through.
</GuiAlert>
</div>
</div>
);
}

The message can be any markup, such as a paragraph with links to the fields that need attention. The element never changes it.

Two fields need your attention: Billing email and City.

Code of the example above
AlertContent.tsx
import { GuiAlert } from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
export function AlertContent() {
return (
<GuiAlert variant="warning" role="status">
<p>
Two fields need your attention:{' '}
<a href="#billing-email">Billing email</a> and{' '}
<a href="#billing-city">City</a>.
</p>
</GuiAlert>
);
}

An alert has the alert role, which interrupts the screen reader to read it. For a message that can wait, such as a save confirmation, set role="status": screen readers read it when they finish what they are reading. Change the alert’s content, or its variant, from your own code: the element keeps whatever you put in it.

You have unsaved changes.
Code of the example above
AlertLive.tsx
import { useState } from 'react';
import { GuiAlert, GuiButton } from '@golemui/gui-components/react';
import type { GuiAlertVariant } from '@golemui/gui-components';
import '@golemui/gui-components/index.css';
export function AlertLive() {
const [variant, setVariant] = useState<GuiAlertVariant>('default');
const [message, setMessage] = useState('You have unsaved changes.');
const save = () => {
setVariant('success');
setMessage(`Profile saved at ${new Date().toLocaleTimeString()}.`);
};
return (
<div className="gui-grid">
<div className="gui-grid__cell">
<GuiAlert id="profile-status" role="status" variant={variant}>
{message}
</GuiAlert>
</div>
<div className="gui-grid__cell">
<GuiButton id="profile-save" label="Save profile" onClick={save} />
</div>
</div>
);
}

<gui-alert> renders nothing of its own. It is your content, with a role:

gui-alert
<gui-alert variant="info" role="alert">
Your trial ends on 14 November. Pick a plan to keep your projects.
</gui-alert>

The alert has no classes. Style it by tag, and each variant by its attribute, which the element reflects when you set the property:

SelectorPart
gui-alertThe box, in every variant.
gui-alert[variant='info']The info box.
gui-alert[variant='error']The error box.

The box is a flex row, display: flex, so the hidden attribute doesn’t hide it. Remove the alert from the page instead, or hide it with your own CSS.

The alert reads these design tokens. Set them on :root to change every alert, or on any element to change the alerts inside it:

TokenWhat it styles
--gui-widget-sizeThe smallest height of the box.
--gui-space-4The space at the start and end of the box.
--gui-radius-mdThe corner radius.
--gui-font-smThe text size.
--gui-bg-surface-hover, --gui-border-default, --gui-text-defaultThe background, border and text of default.
--gui-intent-info-bg, --gui-intent-info, --gui-intent-info-textThe background, border and text of info.
--gui-intent-success-bg, --gui-intent-success, --gui-intent-success-textThe background, border and text of success.
--gui-intent-warning-bg, --gui-intent-warning, --gui-intent-warning-textThe background, border and text of warning.
--gui-intent-error-bg, --gui-intent-error, --gui-intent-error-textThe background, border and text of error.

The intent tokens are listed in Tokens.

styles.css
/* Softer warning alerts, in one section only. */
.billing {
--gui-intent-warning-bg: #fff8e6;
--gui-intent-warning: #e0a800;
}

To restyle beyond the tokens, see Overriding styles; for dark mode and the Clay theme, see Themes and dark mode.

  • Role. The element sets role="alert" when it connects, unless it already has a role. Screen readers announce an alert when it appears in the page or when its content changes.
  • Status. Set role="status" for a message that can wait. Use alert only for what needs attention now, such as an error that blocks the user.
  • Color. Each variant has its own colors, but they carry no text of their own. Write the intent into the message, such as “Error:” or “Saved.”, when it matters.

The alert takes no focus and has no keys of its own. Links and buttons inside it work as anywhere else.

What every element shares is in Accessibility.