fix(Alert): honour explicit role={undefined} to suppress role attribute#490
Open
jrlprost wants to merge 2 commits into
Open
fix(Alert): honour explicit role={undefined} to suppress role attribute#490jrlprost wants to merge 2 commits into
jrlprost wants to merge 2 commits into
Conversation
When role={undefined} is passed, the component was still rendering
role="alert" because the destructuring default applies to undefined
values unconditionally.
Fix: remove the default from the destructuring and use "role" in props
to distinguish "caller did not pass role" (default to "alert") from
"caller explicitly passed undefined" (no role attribute rendered).
Fixes codegouvfr#478
revolunet
reviewed
May 20, 2026
| style={style} | ||
| {...(refShouldSetRole.current && { "role": role })} | ||
| {...(role ? { "role": role } : {})} | ||
| {...(refShouldSetRole.current && role !== undefined && { "role": role })} |
Collaborator
There was a problem hiding this comment.
à quoi sert cette ligne, c'est toujours écrasé en-dessous, non ?
The first spread was always overridden by the second one: when role !== undefined both spreads set the same value, and when role is undefined neither fires. Drop the dead ref and simplify to a single conditional spread. Addresses review comment from revolunet.
Author
|
Vous avuez raison. Le premier est redondant : quand |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #478.
Problème
Passer
role={undefined}sur<Alert />rendait quand mêmerole="alert"dans le DOM, ce qui est une non-conformité RGAA (critère 8.7 — changement de contexte non demandé par l'utilisateur·rice).La cause : le default dans le destructuring (
role = "alert") remplace silencieusementundefinedpar"alert", même lorsqueundefinedest explicitement fourni par l'appelant·e.Correction
Au lieu d'un default dans le destructuring, on utilise
"role" in propspour distinguer les deux cas :rolenon fourni → comportement par défaut conservé :"alert"(compatibilité ascendante intacte)role={undefined}explicitement passé → aucun attributroledans le DOM renduLe rendu JSX passe de
role ?àrole !== undefinedpour gérer correctement la valeurundefined:La même correction est appliquée au spread conditionnel
refShouldSetRole(ré-annonce par les lecteurs d'écran quand l'alerte se rouvre).Tests
Notice.tsxavait une warning pre-existing).<Alert role={undefined} ... />→ pas d'attributroledans le DOM rendu.