|
| 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 | + Directive, |
| 11 | + ElementRef, |
| 12 | + inject, |
| 13 | + computed, |
| 14 | + input, |
| 15 | + booleanAttribute, |
| 16 | + OnInit, |
| 17 | + OnDestroy, |
| 18 | +} from '@angular/core'; |
| 19 | +import {ToolbarWidgetPattern, ToolbarWidgetGroupPattern, SignalLike} from '@angular/aria/private'; |
| 20 | +import {_IdGenerator} from '@angular/cdk/a11y'; |
| 21 | +import {Toolbar} from './toolbar'; |
| 22 | +import {TOOLBAR_WIDGET_GROUP} from './utils'; |
| 23 | +import type {ToolbarWidgetGroup} from './toolbar-widget-group'; |
| 24 | + |
| 25 | +/** |
| 26 | + * A widget within a toolbar. |
| 27 | + * |
| 28 | + * The `ngToolbarWidget` directive should be applied to any native HTML element that acts |
| 29 | + * as an interactive widget within an `ngToolbar` or `ngToolbarWidgetGroup`. It enables |
| 30 | + * keyboard navigation and selection within the toolbar. |
| 31 | + * |
| 32 | + * ```html |
| 33 | + * <button ngToolbarWidget value="action-id" [disabled]="isDisabled"> |
| 34 | + * Perform Action |
| 35 | + * </button> |
| 36 | + * ``` |
| 37 | + * |
| 38 | + * @developerPreview 21.0 |
| 39 | + */ |
| 40 | +@Directive({ |
| 41 | + selector: '[ngToolbarWidget]', |
| 42 | + exportAs: 'ngToolbarWidget', |
| 43 | + host: { |
| 44 | + '[attr.data-active]': 'active()', |
| 45 | + '[attr.tabindex]': '_pattern.tabIndex()', |
| 46 | + '[attr.inert]': 'hardDisabled() ? true : null', |
| 47 | + '[attr.disabled]': 'hardDisabled() ? true : null', |
| 48 | + '[attr.aria-disabled]': '_pattern.disabled()', |
| 49 | + '[id]': '_pattern.id()', |
| 50 | + }, |
| 51 | +}) |
| 52 | +export class ToolbarWidget<V> implements OnInit, OnDestroy { |
| 53 | + /** A reference to the host element. */ |
| 54 | + private readonly _elementRef = inject(ElementRef); |
| 55 | + |
| 56 | + /** A reference to the host element. */ |
| 57 | + readonly element = this._elementRef.nativeElement as HTMLElement; |
| 58 | + |
| 59 | + /** The parent Toolbar. */ |
| 60 | + private readonly _toolbar = inject<Toolbar<V>>(Toolbar); |
| 61 | + |
| 62 | + /** A unique identifier for the widget. */ |
| 63 | + readonly id = input(inject(_IdGenerator).getId('ng-toolbar-widget-', true)); |
| 64 | + |
| 65 | + /** The parent Toolbar UIPattern. */ |
| 66 | + readonly _toolbarPattern = computed(() => this._toolbar._pattern); |
| 67 | + |
| 68 | + /** Whether the widget is disabled. */ |
| 69 | + readonly disabled = input(false, {transform: booleanAttribute}); |
| 70 | + |
| 71 | + /** Whether the widget is 'hard' disabled, which is different from `aria-disabled`. A hard disabled widget cannot receive focus. */ |
| 72 | + readonly hardDisabled = computed(() => this._pattern.disabled() && !this._toolbar.softDisabled()); |
| 73 | + |
| 74 | + /** The optional ToolbarWidgetGroup this widget belongs to. */ |
| 75 | + readonly _group = inject<ToolbarWidgetGroup<V>>(TOOLBAR_WIDGET_GROUP, {optional: true}); |
| 76 | + |
| 77 | + /** The value associated with the widget. */ |
| 78 | + readonly value = input.required<V>(); |
| 79 | + |
| 80 | + /** Whether the widget is currently active (focused). */ |
| 81 | + readonly active = computed(() => this._pattern.active()); |
| 82 | + |
| 83 | + /** Whether the widget is selected (only relevant in a selection group). */ |
| 84 | + readonly selected = () => this._pattern.selected(); |
| 85 | + |
| 86 | + private readonly _groupPattern: SignalLike< |
| 87 | + ToolbarWidgetGroupPattern<ToolbarWidgetPattern<V>, V> | undefined |
| 88 | + > = () => this._group?._pattern; |
| 89 | + |
| 90 | + /** The ToolbarWidget UIPattern. */ |
| 91 | + readonly _pattern = new ToolbarWidgetPattern<V>({ |
| 92 | + ...this, |
| 93 | + group: this._groupPattern, |
| 94 | + toolbar: this._toolbarPattern, |
| 95 | + id: this.id, |
| 96 | + value: this.value, |
| 97 | + element: () => this.element, |
| 98 | + }); |
| 99 | + |
| 100 | + ngOnInit() { |
| 101 | + this._toolbar._register(this); |
| 102 | + } |
| 103 | + |
| 104 | + ngOnDestroy() { |
| 105 | + this._toolbar._unregister(this); |
| 106 | + } |
| 107 | +} |
0 commit comments