Skip to content

fix(material/expansion-panel): Prevent focus issue during collapse animation #27684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
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
20 changes: 16 additions & 4 deletions src/material/expansion/expansion-panel-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {FocusableOption, FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {ENTER, hasModifierKey, SPACE} from '@angular/cdk/keycodes';
import {ENTER, hasModifierKey, SPACE, TAB} from '@angular/cdk/keycodes';
import {
AfterViewInit,
Attribute,
Expand Down Expand Up @@ -179,6 +179,13 @@ export class MatExpansionPanelHeader
return null;
}

/** Handle default for keydown event. */
private _handleDefaultKeydown(event: KeyboardEvent) {
if (this.panel.accordion) {
this.panel.accordion._handleHeaderKeydown(event);
}
}

/** Handle keydown event calling to toggle() if appropriate. */
_keydown(event: KeyboardEvent) {
switch (event.keyCode) {
Expand All @@ -191,10 +198,15 @@ export class MatExpansionPanelHeader
}

break;
default:
if (this.panel.accordion) {
this.panel.accordion._handleHeaderKeydown(event);
case TAB:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that preventing users from tabbing is the right way to solve this issue. E.g. one could trigger it by clicking quickly as well. Instead we should not allow the panel to be toggled while an animation is running.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary issue is that when a panel collapses, we expect the focus to smoothly transition to the next element when we press the tab key. However, if someone quickly presses the tab key during the collapse animation, the body panel isn't hidden fast enough, leading to an incorrect focus allocation, which causes some problems.

the collapse animation state:

state('collapsed, void', style({height: '0px', visibility: 'hidden'})),)

To address this, we can implement a solution where the panel animation happens rapidly while it's in the middle of collapsing when using the tab key. This adjustment ensures that the focus behaves correctly. If you have further thoughts, please feel free to share them.

if (this.panel.collapsingAnimation) {
this.panel._body.nativeElement.setAttribute('style', 'visibility: hidden');
}
this._handleDefaultKeydown(event);

break;
default:
this._handleDefaultKeydown(event);

return;
}
Expand Down
1 change: 1 addition & 0 deletions src/material/expansion/expansion-panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div class="mat-expansion-panel-content"
role="region"
[@bodyExpansion]="_getExpandedState()"
(@bodyExpansion.start)="_bodyAnimationStart($event)"
(@bodyExpansion.done)="_bodyAnimationDone.next($event)"
[attr.aria-labelledby]="_headerId"
[id]="id"
Expand Down
14 changes: 14 additions & 0 deletions src/material/expansion/expansion-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ export class MatExpansionPanel
private _document: Document;
private _hideToggle = false;
private _togglePosition: MatAccordionTogglePosition;
private _collapsingAnimation = false;

/** whether the element is currently undergoing the collapsing animation */
get collapsingAnimation(): boolean {
return this._collapsingAnimation;
}

/** Whether the toggle indicator should be hidden. */
@Input()
Expand Down Expand Up @@ -174,6 +180,7 @@ export class MatExpansionPanel
}),
)
.subscribe(event => {
this._collapsingAnimation = false;
if (event.fromState !== 'void') {
if (event.toState === 'expanded') {
this.afterExpand.emit();
Expand All @@ -188,6 +195,13 @@ export class MatExpansionPanel
}
}

/** Check and determine if the element is currently undergoing a collapsing animation. */
_bodyAnimationStart(event: AnimationEvent) {
if (event.toState === 'collapsed') {
this._collapsingAnimation = true;
}
}

/** Determines whether the expansion panel should have spacing between it and its siblings. */
_hasSpacing(): boolean {
if (this.accordion) {
Expand Down