Skip to content

Commit

Permalink
main - 8ae1431 docs(cdk/overlay): keep state in sync in example (#30237)
Browse files Browse the repository at this point in the history
  • Loading branch information
crisbeto committed Jan 3, 2025
1 parent a21424e commit 5f190b8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
21 changes: 18 additions & 3 deletions fesm2022/menu.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Overlay, OverlayConfig, STANDARD_DROPDOWN_BELOW_POSITIONS, STANDARD_DRO
import { ENTER, SPACE, UP_ARROW, hasModifierKey, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW, TAB, ESCAPE } from '@angular/cdk/keycodes';
import { startWith, debounceTime, distinctUntilChanged, takeUntil, mergeMap, mapTo, mergeAll, switchMap, skipWhile, skip } from 'rxjs/operators';
import { UniqueSelectionDispatcher } from '@angular/cdk/collections';
import { _IdGenerator, InputModalityDetector, FocusKeyManager } from '@angular/cdk/a11y';
import { _IdGenerator, InputModalityDetector, FocusMonitor, FocusKeyManager } from '@angular/cdk/a11y';
import { Subject, merge, partition } from 'rxjs';
import { TemplatePortal } from '@angular/cdk/portal';
import { Directionality } from '@angular/cdk/bidi';
Expand Down Expand Up @@ -1155,6 +1155,7 @@ class PointerFocusTracker {
* This class can be extended to create custom menu types.
*/
class CdkMenuBase extends CdkMenuGroup {
_focusMonitor = inject(FocusMonitor);
ngZone = inject(NgZone);
_renderer = inject(Renderer2);
/** The menu's native DOM host element. */
Expand Down Expand Up @@ -1195,12 +1196,14 @@ class CdkMenuBase extends CdkMenuGroup {
this.menuStack.push(this);
}
this._setKeyManager();
this._handleFocus();
this._subscribeToMenuStackHasFocus();
this._subscribeToMenuOpen();
this._subscribeToMenuStackClosed();
this._setUpPointerTracker();
}
ngOnDestroy() {
this._focusMonitor.stopMonitoring(this.nativeElement);
this.keyManager?.destroy();
this.destroyed.next();
this.destroyed.complete();
Expand Down Expand Up @@ -1301,8 +1304,21 @@ class CdkMenuBase extends CdkMenuGroup {
this.menuAim.initialize(this, this.pointerTracker);
}
}
/** Handles focus landing on the host element of the menu. */
_handleFocus() {
this._focusMonitor
.monitor(this.nativeElement, false)
.pipe(takeUntil(this.destroyed))
.subscribe(origin => {
// Don't forward focus on mouse interactions, because it can
// mess with the user's scroll position. See #30130.
if (origin !== null && origin !== 'mouse') {
this.focusFirstItem(origin);
}
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.0-next.3", ngImport: i0, type: CdkMenuBase, deps: null, target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.1.0-next.3", type: CdkMenuBase, isStandalone: true, inputs: { id: "id" }, host: { attributes: { "role": "menu" }, listeners: { "focus": "focusFirstItem()", "focusin": "menuStack.setHasFocus(true)", "focusout": "menuStack.setHasFocus(false)" }, properties: { "tabindex": "_getTabIndex()", "id": "id", "attr.aria-orientation": "orientation", "attr.data-cdk-menu-stack-id": "menuStack.id" } }, queries: [{ propertyName: "items", predicate: CdkMenuItem, descendants: true }], usesInheritance: true, ngImport: i0 });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.1.0-next.3", type: CdkMenuBase, isStandalone: true, inputs: { id: "id" }, host: { attributes: { "role": "menu" }, listeners: { "focusin": "menuStack.setHasFocus(true)", "focusout": "menuStack.setHasFocus(false)" }, properties: { "tabindex": "_getTabIndex()", "id": "id", "attr.aria-orientation": "orientation", "attr.data-cdk-menu-stack-id": "menuStack.id" } }, queries: [{ propertyName: "items", predicate: CdkMenuItem, descendants: true }], usesInheritance: true, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.0-next.3", ngImport: i0, type: CdkMenuBase, decorators: [{
type: Directive,
Expand All @@ -1314,7 +1330,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.0-next.3",
'[id]': 'id',
'[attr.aria-orientation]': 'orientation',
'[attr.data-cdk-menu-stack-id]': 'menuStack.id',
'(focus)': 'focusFirstItem()',
'(focusin)': 'menuStack.setHasFocus(true)',
'(focusout)': 'menuStack.setHasFocus(false)',
},
Expand Down
2 changes: 1 addition & 1 deletion fesm2022/menu.mjs.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions fesm2022/scrolling.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.0-next.3",
class CdkVirtualScrollableWindow extends CdkVirtualScrollable {
constructor() {
super();
const document = inject(DOCUMENT);
this.elementRef = new ElementRef(document.documentElement);
this._scrollElement = document;
}
Expand Down
2 changes: 1 addition & 1 deletion fesm2022/scrolling.mjs.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions menu/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export declare class CdkMenuBar extends CdkMenuBase implements AfterContentInit
* This class can be extended to create custom menu types.
*/
export declare abstract class CdkMenuBase extends CdkMenuGroup implements Menu, AfterContentInit, OnDestroy {
private _focusMonitor;
protected ngZone: NgZone;
private _renderer;
/** The menu's native DOM host element. */
Expand Down Expand Up @@ -223,6 +224,8 @@ export declare abstract class CdkMenuBase extends CdkMenuGroup implements Menu,
* with the latest menu item under mouse focus.
*/
private _setUpPointerTracker;
/** Handles focus landing on the host element of the menu. */
private _handleFocus;
static ɵfac: i0.ɵɵFactoryDeclaration<CdkMenuBase, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<CdkMenuBase, never, never, { "id": { "alias": "id"; "required": false; }; }, {}, ["items"], never, true, never>;
}
Expand Down

0 comments on commit 5f190b8

Please sign in to comment.