Skip to content

fix(aria/accordion): update panel visibility when the trigger links after first render - #34198

Open
DuncanFaulkner wants to merge 1 commit into
angular:mainfrom
DuncanFaulkner:fix/aria-accordion-panel-visible
Open

DuncanFaulkner wants to merge 1 commit into
angular:mainfrom
DuncanFaulkner:fix/aria-accordion-panel-visible

Conversation

@DuncanFaulkner

Copy link
Copy Markdown
Contributor

Summary

Since #34061, AccordionTrigger links itself to its panel (panel._pattern = this._pattern) inside an effect, which can run after the panel's first render. AccordionPanel.visible is a computed over _pattern, and _pattern was a plain field. If anything reads visible() before the trigger has linked the panel, the computed caches false with no reactive dependencies and never re-evaluates.

A template that styles the panel from its public visible() signal is enough to hit this:

<div ngAccordionGroup>
  <button ngAccordionTrigger [panel]="panel">Trigger</button>
  <div ngAccordionPanel #panel="ngAccordionPanel" [class.is-hidden]="!panel.visible()">
    <ng-template ngAccordionContent>Content</ng-template>
  </div>
</div>

With @angular/aria 22.2.7 and 22.3.0, clicking the trigger sets aria-expanded="true", but visible() stays false, so the panel keeps the class and the panel's own [attr.inert] host binding (which reads the same computed) keeps it inert. The content is unreachable by keyboard and screen reader even though the trigger says it is expanded. The documented markup without a template read of visible() is not affected. It worked in 22.2.6 and earlier, when the link was made before the first render.

We hit this in a component library that hides the panel with [class.x]="!panel.visible()": every section stayed closed for anyone on the latest @angular/aria.

Fix

_pattern is now backed by a private signal through a getter and setter, so visible() (and the other readers of _pattern) update when the trigger links or unlinks the panel. The trigger's panel._pattern = ... assignments and the existing spec assertions on panel._pattern are unchanged.

A plain _pattern = signal(...) would also work; it would change the trigger, the panel's host bindings and the existing specs to _pattern() / .set(). I kept the accessor to keep the change small, and can switch if you prefer.

Tests

New spec AccordionPanel visibility > should update visible() when a template reads it before the trigger links the panel: a panel with [class.is-hidden]="!panel.visible()" is expanded and collapsed through the trigger, checking the class, inert, aria-expanded, aria-labelledby and the rendered content.

Validation

  • //src/aria/accordion:unit_tests (chromium and firefox) pass with the fix. With the previous accordion-panel.ts, the new spec fails in both browsers (Expected is-hidden not to contain 'is-hidden').
  • After that run I moved the spec into its own describe and added the aria-expanded / aria-labelledby assertions; those last edits are verified by this PR's CI.
  • The API golden (goldens/aria/accordion/index.api.md) is updated by hand for the field to accessor change; //goldens:aria_api runs in CI.
  • Prettier and commit-message validation pass.

…fter first render

Since angular#34061, `AccordionTrigger` links itself to its panel (`panel._pattern = ...`) inside an
effect, which can run after the panel's first render. `AccordionPanel.visible` is a computed over
`_pattern`, which was a plain field. If anything reads `visible()` before the trigger links (for
example a template binding such as `[class.hidden]="!panel.visible()"`), the computed caches
`false` with no reactive dependencies and never re-evaluates. The panel's own
`[attr.inert]` host binding reads the same computed, so the panel stays hidden and inert even
though the trigger reports `aria-expanded="true"`.

`_pattern` is now backed by a signal through a getter and setter, so `visible()` updates when the
trigger links or unlinks the panel. Callers that assign `panel._pattern` keep working.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant