|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + afterRenderEffect, |
| 11 | + booleanAttribute, |
| 12 | + computed, |
| 13 | + contentChildren, |
| 14 | + Directive, |
| 15 | + ElementRef, |
| 16 | + inject, |
| 17 | + input, |
| 18 | + model, |
| 19 | + output, |
| 20 | + signal, |
| 21 | +} from '@angular/core'; |
| 22 | +import {SignalLike, MenuBarPattern} from '@angular/aria/private'; |
| 23 | +import {Directionality} from '@angular/cdk/bidi'; |
| 24 | +import {MenuItem} from './menu-item'; |
| 25 | +import {MENU_COMPONENT} from './menu-tokens'; |
| 26 | + |
| 27 | +/** |
| 28 | + * A menu bar of menu items. |
| 29 | + * |
| 30 | + * Like the `ngMenu`, a `ngMenuBar` is used to offer a list of menu item choices to users. |
| 31 | + * However, a menubar is used to display a persistent, top-level, always-visible set of |
| 32 | + * menu item choices, typically found at the top of an application window. |
| 33 | + * |
| 34 | + * ```html |
| 35 | + * <div ngMenuBar> |
| 36 | + * <button ngMenuTrigger [menu]="fileMenu">File</button> |
| 37 | + * <button ngMenuTrigger [menu]="editMenu">Edit</button> |
| 38 | + * </div> |
| 39 | + * |
| 40 | + * <div ngMenu #fileMenu="ngMenu"> |
| 41 | + * <div ngMenuItem>New</div> |
| 42 | + * <div ngMenuItem>Open</div> |
| 43 | + * </div> |
| 44 | + * |
| 45 | + * <div ngMenu #editMenu="ngMenu"> |
| 46 | + * <div ngMenuItem>Cut</div> |
| 47 | + * <div ngMenuItem>Copy</div> |
| 48 | + * </div> |
| 49 | + * ``` |
| 50 | + * |
| 51 | + * @developerPreview 21.0 |
| 52 | + */ |
| 53 | +@Directive({ |
| 54 | + selector: '[ngMenuBar]', |
| 55 | + exportAs: 'ngMenuBar', |
| 56 | + host: { |
| 57 | + 'role': 'menubar', |
| 58 | + '[attr.disabled]': '!softDisabled() && _pattern.disabled() ? true : null', |
| 59 | + '[attr.aria-disabled]': '_pattern.disabled()', |
| 60 | + '[attr.tabindex]': '_pattern.tabIndex()', |
| 61 | + '(keydown)': '_pattern.onKeydown($event)', |
| 62 | + '(mouseover)': '_pattern.onMouseOver($event)', |
| 63 | + '(click)': '_pattern.onClick($event)', |
| 64 | + '(focusin)': '_pattern.onFocusIn()', |
| 65 | + '(focusout)': '_pattern.onFocusOut($event)', |
| 66 | + }, |
| 67 | + providers: [{provide: MENU_COMPONENT, useExisting: MenuBar}], |
| 68 | +}) |
| 69 | +export class MenuBar<V> { |
| 70 | + /** The menu items contained in the menubar. */ |
| 71 | + readonly _allItems = contentChildren<MenuItem<V>>(MenuItem, {descendants: true}); |
| 72 | + |
| 73 | + readonly _items: SignalLike<MenuItem<V>[]> = () => |
| 74 | + this._allItems().filter(i => i.parent === this); |
| 75 | + |
| 76 | + /** A reference to the host element. */ |
| 77 | + private readonly _elementRef = inject(ElementRef); |
| 78 | + |
| 79 | + /** A reference to the host element. */ |
| 80 | + readonly element = this._elementRef.nativeElement as HTMLElement; |
| 81 | + |
| 82 | + /** Whether the menubar is disabled. */ |
| 83 | + readonly disabled = input(false, {transform: booleanAttribute}); |
| 84 | + |
| 85 | + /** Whether the menubar is soft disabled. */ |
| 86 | + readonly softDisabled = input(true, {transform: booleanAttribute}); |
| 87 | + |
| 88 | + /** The directionality (LTR / RTL) context for the application (or a subtree of it). */ |
| 89 | + readonly textDirection = inject(Directionality).valueSignal; |
| 90 | + |
| 91 | + /** The values of the currently selected menu items. */ |
| 92 | + readonly values = model<V[]>([]); |
| 93 | + |
| 94 | + /** Whether the menu should wrap its items. */ |
| 95 | + readonly wrap = input(true, {transform: booleanAttribute}); |
| 96 | + |
| 97 | + /** The delay in milliseconds before the typeahead buffer is cleared. */ |
| 98 | + readonly typeaheadDelay = input<number>(500); |
| 99 | + |
| 100 | + /** The menu ui pattern instance. */ |
| 101 | + readonly _pattern: MenuBarPattern<V>; |
| 102 | + |
| 103 | + /** The menu items as a writable signal. */ |
| 104 | + private readonly _itemPatterns = signal<any[]>([]); |
| 105 | + |
| 106 | + /** A callback function triggered when a menu item is selected. */ |
| 107 | + onSelect = output<V>(); |
| 108 | + |
| 109 | + constructor() { |
| 110 | + this._pattern = new MenuBarPattern({ |
| 111 | + ...this, |
| 112 | + items: this._itemPatterns, |
| 113 | + multi: () => false, |
| 114 | + softDisabled: () => true, |
| 115 | + focusMode: () => 'roving', |
| 116 | + orientation: () => 'horizontal', |
| 117 | + selectionMode: () => 'explicit', |
| 118 | + onSelect: (value: V) => this.onSelect.emit(value), |
| 119 | + activeItem: signal(undefined), |
| 120 | + element: computed(() => this._elementRef.nativeElement), |
| 121 | + }); |
| 122 | + |
| 123 | + afterRenderEffect(() => { |
| 124 | + this._itemPatterns.set(this._items().map(i => i._pattern)); |
| 125 | + }); |
| 126 | + |
| 127 | + afterRenderEffect(() => { |
| 128 | + if (!this._pattern.hasBeenFocused()) { |
| 129 | + this._pattern.setDefaultState(); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + /** Closes the menubar. */ |
| 135 | + close() { |
| 136 | + this._pattern.close(); |
| 137 | + } |
| 138 | +} |
0 commit comments