fix(dialog): add focus-trap and ARIA attributes to ZardDialogComponent - #87
Open
Aarti-panchal01 wants to merge 1 commit into
Open
fix(dialog): add focus-trap and ARIA attributes to ZardDialogComponent#87Aarti-panchal01 wants to merge 1 commit into
Aarti-panchal01 wants to merge 1 commit into
Conversation
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Team Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Summary
ZardDialogComponent(v2/ui/dialog/dialog.component.ts) is built directly on@angular/cdk/overlay, notMatDialog/CdkDialog, so it never got any of Angular Material's automatic dialog accessibility behavior. Confirmed via investigation: zerorole, zeroaria-modal, zeroaria-labelledby/aria-describedby, and zero focus trap — Tab could walk out of an open dialog into the page behind the backdrop, nothing moved focus into the dialog on open, and nothing restored it on close. This affects every dialog in every consumer app of this shared library (confirm dialogs, alerts, etc.).Escape-to-close and backdrop-click were already implemented in
dialog-ref.tsand are untouched here.Changes
role="dialog"andaria-modal="true"added as static host attributes.aria-labelledby/aria-describedbyadded as host bindings, pointing at newids on the title/description elements — conditionally set only when a title/description is actually configured (matches the existing conditional-render logic, doesn't change it).idattributes added to the title (<h4 data-testid="z-title">) and description (<p data-testid="z-description">) elements, uniqued per dialog instance via a simple module-scoped counter (a component could theoretically have more than one dialog open at once).FocusTrapFactorywired in: creates a realFocusTrapon the host element inngAfterViewInit()(after the container's own view — and, for component/template content, its synchronously-attached portal content — is in the DOM), destroys it inngOnDestroy().focusTrap.focusInitialElementWhenReady()— CDK's own "focuscdkFocusInitialelement, else first tabbable" behavior, matchingMatDialog's defaultAutoFocusTarget.FIRST_TABBABLE.document.activeElementright before trapping (inngAfterViewInit(), before anything else can move focus) and restores it inngOnDestroy().HTMLElement.focus()on a since-detached element is a no-op, so no extra guard is needed there.This matches
MatDialog's actual behavior as directly observed on the legacy app's live UAT:role="dialog",aria-labelledbywired to the title, real CDK-backed focus trap (confirmed via the presence of.cdk-focus-trap-anchorsentinel elements), initial focus onto the dialog on open, and focus moving to a real element after close — all reproduced here. (Legacy itself never setaria-modaloraria-describedbyeither, so this implementation is a strict superset of legacy's own behavior, not just a match.)Verification
@angular/cdk/a11yis the same@angular/cdkpackage already used elsewhere in this file (@angular/cdk/overlay,@angular/cdk/portal) — no new dependency. This repo has no standalone build/lint of its own (pure source, consumed via path alias); verified against the consuming app instead:ng build(Helpline104-UI-NEXT) — clean, zero errors.ng test(Helpline104-UI-NEXT) — 49/49 passing, no regressions from the real focus trap now being active in tests that render a dialog.Manual follow-up needed
None for this change itself. Consuming apps (e.g. Helpline104-UI-NEXT) will get real focus-trap/ARIA behavior automatically once this is merged and their submodule pin is bumped — no code change needed on their end.