Skip to content
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

fix(material/expansion): Focus remains on the header when clicked on the next or cancel button inside the panel #30308

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
21 changes: 17 additions & 4 deletions src/material/expansion/expansion-panel-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {FocusableOption, FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {
FOCUS_MONITOR_DEFAULT_OPTIONS,
FocusableOption,
FocusMonitor,
FocusMonitorDetectionMode,
FocusOrigin,
} from '@angular/cdk/a11y';
import {ENTER, hasModifierKey, SPACE} from '@angular/cdk/keycodes';
import {
AfterViewInit,
Expand Down Expand Up @@ -57,6 +63,15 @@ import {_StructuralStylesLoader} from '@angular/material/core';
'(click)': '_toggle()',
'(keydown)': '_keydown($event)',
},
providers: [
FocusMonitor,
{
provide: FOCUS_MONITOR_DEFAULT_OPTIONS,
useValue: {
detectionMode: FocusMonitorDetectionMode.EVENTUAL,
},
},
],
})
export class MatExpansionPanelHeader implements AfterViewInit, OnDestroy, FocusableOption {
panel = inject(MatExpansionPanel, {host: true});
Expand Down Expand Up @@ -98,9 +113,7 @@ export class MatExpansionPanelHeader implements AfterViewInit, OnDestroy, Focusa
).subscribe(() => this._changeDetectorRef.markForCheck());

// Avoids focus being lost if the panel contained the focused element and was closed.
panel.closed
.pipe(filter(() => panel._containsFocus()))
.subscribe(() => this._focusMonitor.focusVia(this._element, 'program'));
panel.closed.pipe(filter(() => panel._containsFocus())).subscribe(() => this.focus());

if (defaultOptions) {
this.expandedHeight = defaultOptions.expandedHeight;
Expand Down
Loading