Alerts
Alerts are a way to display important information to the user. They are typically used to give feedback to the user about an action that has been completed or an error that has occurred. It can also be used to warn or provide important extra information to the user before an action is taken.
Design Hints
Accessiblity Hints
Alerts Specification
An alert contains two main parts :
- The alert container : The container that holds the alert message and any additional content. This container should have a role of alert if immediate user attention is required.
- The alert message : The message that is displayed in the alert container. This message should be informative and provide the user with a clear understanding of the issue or action that needs to be taken.
Colors enhance alert messages but should complement, not replace, clear textual information that conveys the alert’s primary content.
To know more you can refer to MDN documentation for the aria role=alert .
Simple Alerts
Different types of alerts are given below. Note that the last alert doesn’t have an alert role and instead uses a note role, but can be used to convey important information without interrupting the user experience.
Alternate Simple Alerts
A different style for simple alerts
Alerts with Icons
A variant of alerts which uses as icon to convey the type of alert. The icon is accompanied with a label for screen readers to convey the same information.
Note: The alerts above use aria-hidden="true"
to hide the icons from screen readers and a <span class="sr-only">
to provide the text which is only visible to screen readers.
Prominent Alerts
These alerts are designed in a way to be visually prominent and grab the user’s attention immediately. These only represent a higher visual prominence, not a higher semantic prominence with respect to accessibility compared to the alerts above.
Astrojs Component
This is a one off astrojs component for all the alerts above. Currently alternate style for simple alerts is not implemented in this component.
Usage
To call the alerts component in your astro project, follow these steps below after copying the code into a component file:
- Import the component from the correct location in your project.
- Call the component with the required props.
Example implementation is given below:
Component Props
The component accepts the following props which are all required:
Prop | Type | Description |
---|---|---|
type | string | Specifies the type of the alert. Possible values: ‘info’, ‘success’, ‘warning’, ‘error’ which sets the aria role alert. If no type is specified or another value is given, the type defaults to ‘note’ and aria role is set to ‘note’. |
message | string | The message content to be displayed in the alert. |
showIcon | boolean | Determines whether to display an icon corresponding to the alert type. |
prominent | boolean | Indicates if the alert should have a prominent style. There is only visual changes and no semantic changes for the alert. |
Styling
The component uses Tailwind CSS classes for styling. The alert container has a rounded shape with padding and margin. The background color and text color of the alert vary based on the type and prominent props. If prominent is true and the type is not one of the four allowed types, it will have no effect.
Accessibility:
The alert container has a role attribute set to ‘alert’ for all defined types and set to ‘note’ for values which are outside the defined types. The aria-label attribute is set to the value of the type prop to provide an accessible name for the alert.
Icon Display:
If showIcon is true, an SVG icon corresponding to the alert type is displayed. The icon is positioned to the left of the message content. The icon color varies based on the type and prominent props.
Message Display:
If showIcon is false, the alert type is displayed as a bold text prefix to the message. The message content is displayed after the icon or type prefix.
Accessibility Testing Status
Component | NVDA | Windows Narrator | WAVE | Axe | IBM Equal Access |
---|---|---|---|---|---|
Simple Alerts | Yes | Yes | Yes | Yes | Yes |
Alternate Simple Alerts | Yes | Yes | Yes | Yes | Yes |
Alerts with Icons | Yes | Yes | Yes | Yes | Yes |
Prominent Alerts | Yes | Yes | Yes | Yes | Yes |
Astrojs Component | Yes | Yes | Yes | Yes | Yes |
The alerts adhere to the WAI-ARIA 1.2 specification for alert patterns.
Additional manual testing maybe required to ensure the alerts are accessible in your specific use case.