Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ export const Alert = memo(
closable: isClosableByUser = false,
isClosed: props_isClosed,
onClose,
role = "alert",
role: roleFromProps,
...rest
} = props;

// Honour explicit `role={undefined}` to opt out of the role attribute (RGAA 8.7).
// When role is omitted entirely, default to "alert" for screen reader announcements.
const role = "role" in props ? roleFromProps : ("alert" as const);

assert<Equals<keyof typeof rest, never>>();

const id = useAnalyticsId({
Expand All @@ -90,7 +94,6 @@ export const Alert = memo(
const [buttonElement, setButtonElement] = useState<HTMLButtonElement | null>(null);

const refShouldButtonGetFocus = useRef(false);
const refShouldSetRole = useRef(false);
const DescriptionTag = typeof description === "string" ? "p" : "div";

useEffect(() => {
Expand All @@ -100,7 +103,6 @@ export const Alert = memo(
setIsClosed(isClosed => {
if (isClosed && !props_isClosed) {
refShouldButtonGetFocus.current = true;
refShouldSetRole.current = true;
}

return props_isClosed;
Expand Down Expand Up @@ -147,8 +149,7 @@ export const Alert = memo(
className
)}
style={style}
{...(refShouldSetRole.current && { "role": role })}
{...(role ? { "role": role } : {})}
{...(role !== undefined ? { "role": role } : {})}
ref={ref}
{...rest}
>
Expand Down