MCPcopy Create free account
hub / github.com/angular/components / MenuTriggerPattern

Class MenuTriggerPattern

src/aria/private/menu/menu.ts:664–796  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

662
663/** The menu trigger ui pattern class. */
664export class MenuTriggerPattern<V> {
665 /** Whether the menu trigger is expanded. */
666 readonly expanded = signal(false);
667
668 /** Whether the menu trigger has received interaction. */
669 readonly hasBeenInteracted = signal(false);
670
671 /** The pending focus target when the menu is opened before the menu instance is available. */
672 readonly pendingFocus = signal<'first' | 'last' | undefined>(undefined);
673
674 /** The role of the menu trigger. */
675 readonly role = () => 'button';
676
677 /** Whether the menu trigger has a popup. */
678 readonly hasPopup = () => true;
679
680 /** The menu associated with the trigger. */
681 readonly menu: SignalLike<MenuPattern<V> | undefined>;
682
683 /** The tab index of the menu trigger. */
684 readonly tabIndex = computed(() =>
685 this.expanded() && this.menu()?.inputs.activeItem() ? -1 : 0,
686 );
687
688 /** Whether the menu trigger is disabled. */
689 readonly disabled = () => this.inputs.disabled();
690
691 /** Handles keyboard events for the menu trigger. */
692 readonly keydownManager = computed(() => {
693 return new KeyboardEventManager()
694 .on(' ', () => this.open({first: true}))
695 .on('Enter', () => this.open({first: true}))
696 .on('ArrowDown', () => this.open({first: true}))
697 .on('ArrowUp', () => this.open({last: true}))
698 .on(
699 'Escape',
700 event => {
701 if (this.expanded()) {
702 event.preventDefault();
703 this.close({refocus: true});
704 }
705 },
706 {preventDefault: false},
707 );
708 });
709
710 constructor(readonly inputs: MenuTriggerInputs<V>) {
711 this.menu = this.inputs.menu;
712 }
713
714 /** Flushes any pending focus when the menu instance becomes available. */
715 pendingFocusEffect(): void {
716 const menu = this.inputs.menu();
717 const intent = this.pendingFocus();
718 const items = menu?.items();
719
720 // We check the items so that we don't try calling into
721 // `first/last` until the items are actually available.

Callers

nothing calls this directly

Calls 9

openMethod · 0.95
closeMethod · 0.95
signalFunction · 0.90
computedFunction · 0.90
expandedMethod · 0.80
activeItemMethod · 0.80
menuMethod · 0.45
disabledMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected