| 498 | |
| 499 | /** The menubar ui pattern class. */ |
| 500 | export class MenuBarPattern<V> { |
| 501 | /** Controls list behavior for the menu items. */ |
| 502 | readonly listBehavior: List<MenuItemPattern<V>, V | undefined>; |
| 503 | |
| 504 | /** The tab index of the menu. */ |
| 505 | readonly tabIndex = () => this.listBehavior.tabIndex(); |
| 506 | |
| 507 | /** The key used to navigate to the next item. */ |
| 508 | private readonly _nextKey = computed(() => { |
| 509 | return this.inputs.textDirection() === 'rtl' ? 'ArrowLeft' : 'ArrowRight'; |
| 510 | }); |
| 511 | |
| 512 | /** The key used to navigate to the previous item. */ |
| 513 | private readonly _previousKey = computed(() => { |
| 514 | return this.inputs.textDirection() === 'rtl' ? 'ArrowRight' : 'ArrowLeft'; |
| 515 | }); |
| 516 | |
| 517 | /** Represents the space key. Does nothing when the user is actively using typeahead. */ |
| 518 | readonly dynamicSpaceKey = computed(() => (this.listBehavior.isTyping() ? '' : ' ')); |
| 519 | |
| 520 | /** The regexp used to decide if a key should trigger typeahead. */ |
| 521 | readonly typeaheadRegexp = /^.$/; |
| 522 | |
| 523 | /** Whether the menubar or any of its children are currently focused. */ |
| 524 | readonly isFocused = signal(false); |
| 525 | |
| 526 | /** Whether the menubar has been interacted with. */ |
| 527 | readonly hasBeenInteracted = signal(false); |
| 528 | |
| 529 | /** Whether the menubar is disabled. */ |
| 530 | readonly disabled = () => this.inputs.disabled(); |
| 531 | |
| 532 | /** Handles keyboard events for the menu. */ |
| 533 | readonly keydownManager = computed(() => { |
| 534 | return new KeyboardEventManager() |
| 535 | .on(this._nextKey, () => this.next(), {ignoreRepeat: false}) |
| 536 | .on(this._previousKey, () => this.prev(), {ignoreRepeat: false}) |
| 537 | .on('End', () => this.listBehavior.last()) |
| 538 | .on('Home', () => this.listBehavior.first()) |
| 539 | .on('Enter', () => this.inputs.activeItem()?.open({first: true})) |
| 540 | .on('ArrowUp', () => this.inputs.activeItem()?.open({last: true})) |
| 541 | .on('ArrowDown', () => this.inputs.activeItem()?.open({first: true})) |
| 542 | .on(this.dynamicSpaceKey, () => this.inputs.activeItem()?.open({first: true})) |
| 543 | .on(this.typeaheadRegexp, e => this.listBehavior.search(e.key)); |
| 544 | }); |
| 545 | |
| 546 | constructor(readonly inputs: MenuBarInputs<V>) { |
| 547 | this.listBehavior = new List<MenuItemPattern<V>, V | undefined>(inputs); |
| 548 | } |
| 549 | |
| 550 | /** Sets the default state for the menubar. */ |
| 551 | setDefaultState() { |
| 552 | const firstFocusable = this.listBehavior.navigationBehavior.peekFirst(); |
| 553 | if (firstFocusable) { |
| 554 | this.inputs.activeItem.set(firstFocusable); |
| 555 | } |
| 556 | } |
| 557 |
nothing calls this directly
no test coverage detected