Skip to content

Commit

Permalink
fix(Alert): Add aria-live 'polite' or 'assertive' based on severity
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelrss committed Jun 28, 2023
1 parent 01184f7 commit 369162f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/layout/Alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import type { PropsFromGenericComponent } from 'src/layout';
export type AlertProps = PropsFromGenericComponent<'Alert'>;

import styles from 'src/layout/Alert/Alert.module.css';
import type { AlertSeverity } from 'src/layout/Alert/types';

function calculateAriaLive(severity: AlertSeverity): 'polite' | 'assertive' {
if (severity === 'warning' || severity === 'danger') {
return 'assertive';
}
return 'polite';
}

export const Alert = ({ node }: AlertProps) => {
const { severity, useAsAlert, textResourceBindings } = node.item;
Expand All @@ -20,7 +28,7 @@ export const Alert = ({ node }: AlertProps) => {
<AlertDesignSystem
severity={severity}
role={useAsAlert ? 'alert' : undefined}
aria-live={useAsAlert ? 'polite' : undefined}
aria-live={useAsAlert ? calculateAriaLive(severity) : undefined}
aria-label={useAsAlert ? title : undefined}
>
<span className={styles.title}>{title}</span>
Expand Down
4 changes: 3 additions & 1 deletion src/layout/Alert/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { ILayoutCompBase } from 'src/layout/layout';

export type AlertSeverity = 'success' | 'warning' | 'danger' | 'info';

export interface ILayoutCompAlertBase {
severity: 'success' | 'warning' | 'danger' | 'info';
severity: AlertSeverity;
useAsAlert?: boolean;
}

Expand Down

0 comments on commit 369162f

Please sign in to comment.