|
| 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 | + Directive, |
| 12 | + ElementRef, |
| 13 | + inject, |
| 14 | + computed, |
| 15 | + input, |
| 16 | + booleanAttribute, |
| 17 | + signal, |
| 18 | + Signal, |
| 19 | + OnInit, |
| 20 | + OnDestroy, |
| 21 | +} from '@angular/core'; |
| 22 | +import {ToolbarPattern, RadioButtonPattern, ToolbarWidgetPattern} from '../ui-patterns'; |
| 23 | +import {Directionality} from '@angular/cdk/bidi'; |
| 24 | +import {_IdGenerator} from '@angular/cdk/a11y'; |
| 25 | + |
| 26 | +/** Interface for a radio button that can be used with a toolbar. Based on radio-button in ui-patterns */ |
| 27 | +interface CdkRadioButtonInterface<V> { |
| 28 | + /** The HTML element associated with the radio button. */ |
| 29 | + element: Signal<HTMLElement>; |
| 30 | + /** Whether the radio button is disabled. */ |
| 31 | + disabled: Signal<boolean>; |
| 32 | + |
| 33 | + pattern: RadioButtonPattern<V>; |
| 34 | +} |
| 35 | + |
| 36 | +interface HasElement { |
| 37 | + element: Signal<HTMLElement>; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Sort directives by their document order. |
| 42 | + */ |
| 43 | +function sortDirectives(a: HasElement, b: HasElement) { |
| 44 | + return (a.element().compareDocumentPosition(b.element()) & Node.DOCUMENT_POSITION_PRECEDING) > 0 |
| 45 | + ? 1 |
| 46 | + : -1; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * A toolbar widget container. |
| 51 | + * |
| 52 | + * Widgets such as radio groups or buttons are nested within a toolbar to allow for a single |
| 53 | + * place of reference for focus and navigation. The CdkToolbar is meant to be used in conjunction |
| 54 | + * with CdkToolbarWidget and CdkRadioGroup as follows: |
| 55 | + * |
| 56 | + * ```html |
| 57 | + * <div cdkToolbar> |
| 58 | + * <button cdkToolbarWidget>Button</button> |
| 59 | + * <div cdkRadioGroup> |
| 60 | + * <label cdkRadioButton value="1">Option 1</label> |
| 61 | + * <label cdkRadioButton value="2">Option 2</label> |
| 62 | + * <label cdkRadioButton value="3">Option 3</label> |
| 63 | + * </div> |
| 64 | + * </div> |
| 65 | + * ``` |
| 66 | + */ |
| 67 | +@Directive({ |
| 68 | + selector: '[cdkToolbar]', |
| 69 | + exportAs: 'cdkToolbar', |
| 70 | + host: { |
| 71 | + 'role': 'toolbar', |
| 72 | + 'class': 'cdk-toolbar', |
| 73 | + '[attr.tabindex]': 'pattern.tabindex()', |
| 74 | + '[attr.aria-disabled]': 'pattern.disabled()', |
| 75 | + '[attr.aria-orientation]': 'pattern.orientation()', |
| 76 | + '[attr.aria-activedescendant]': 'pattern.activedescendant()', |
| 77 | + '(keydown)': 'pattern.onKeydown($event)', |
| 78 | + '(pointerdown)': 'pattern.onPointerdown($event)', |
| 79 | + '(focusin)': 'onFocus()', |
| 80 | + }, |
| 81 | +}) |
| 82 | +export class CdkToolbar<V> { |
| 83 | + /** The CdkTabList nested inside of the container. */ |
| 84 | + private readonly _cdkWidgets = signal(new Set<CdkRadioButtonInterface<V> | CdkToolbarWidget>()); |
| 85 | + |
| 86 | + /** A signal wrapper for directionality. */ |
| 87 | + textDirection = inject(Directionality).valueSignal; |
| 88 | + |
| 89 | + /** Sorted UIPatterns of the child widgets */ |
| 90 | + items = computed(() => |
| 91 | + [...this._cdkWidgets()].sort(sortDirectives).map(widget => widget.pattern), |
| 92 | + ); |
| 93 | + |
| 94 | + /** Whether the toolbar is vertically or horizontally oriented. */ |
| 95 | + orientation = input<'vertical' | 'horizontal'>('horizontal'); |
| 96 | + |
| 97 | + /** Whether disabled items in the group should be skipped when navigating. */ |
| 98 | + skipDisabled = input(false, {transform: booleanAttribute}); |
| 99 | + |
| 100 | + /** Whether the toolbar is disabled. */ |
| 101 | + disabled = input(false, {transform: booleanAttribute}); |
| 102 | + |
| 103 | + /** Whether focus should wrap when navigating. */ |
| 104 | + readonly wrap = input(true, {transform: booleanAttribute}); |
| 105 | + |
| 106 | + /** The toolbar UIPattern. */ |
| 107 | + pattern: ToolbarPattern<V> = new ToolbarPattern<V>({ |
| 108 | + ...this, |
| 109 | + activeItem: signal(undefined), |
| 110 | + textDirection: this.textDirection, |
| 111 | + focusMode: signal('roving'), |
| 112 | + }); |
| 113 | + |
| 114 | + /** Whether the toolbar has received focus yet. */ |
| 115 | + private _hasFocused = signal(false); |
| 116 | + |
| 117 | + onFocus() { |
| 118 | + this._hasFocused.set(true); |
| 119 | + } |
| 120 | + |
| 121 | + constructor() { |
| 122 | + afterRenderEffect(() => { |
| 123 | + if (!this._hasFocused()) { |
| 124 | + this.pattern.setDefaultState(); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + afterRenderEffect(() => { |
| 129 | + if (typeof ngDevMode === 'undefined' || ngDevMode) { |
| 130 | + const violations = this.pattern.validate(); |
| 131 | + for (const violation of violations) { |
| 132 | + console.error(violation); |
| 133 | + } |
| 134 | + } |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + register(widget: CdkRadioButtonInterface<V> | CdkToolbarWidget) { |
| 139 | + const widgets = this._cdkWidgets(); |
| 140 | + if (!widgets.has(widget)) { |
| 141 | + widgets.add(widget); |
| 142 | + this._cdkWidgets.set(new Set(widgets)); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + unregister(widget: CdkRadioButtonInterface<V> | CdkToolbarWidget) { |
| 147 | + const widgets = this._cdkWidgets(); |
| 148 | + if (widgets.delete(widget)) { |
| 149 | + this._cdkWidgets.set(new Set(widgets)); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * A widget within a toolbar. |
| 156 | + * |
| 157 | + * A widget is anything that is within a toolbar. It should be applied to any native HTML element |
| 158 | + * that has the purpose of acting as a widget navigatable within a toolbar. |
| 159 | + */ |
| 160 | +@Directive({ |
| 161 | + selector: '[cdkToolbarWidget]', |
| 162 | + exportAs: 'cdkToolbarWidget', |
| 163 | + host: { |
| 164 | + 'role': 'button', |
| 165 | + 'class': 'cdk-toolbar-widget', |
| 166 | + '[class.cdk-active]': 'pattern.active()', |
| 167 | + '[attr.tabindex]': 'pattern.tabindex()', |
| 168 | + '[attr.inert]': 'hardDisabled() ? true : null', |
| 169 | + '[attr.disabled]': 'hardDisabled() ? true : null', |
| 170 | + '[attr.aria-disabled]': 'pattern.disabled()', |
| 171 | + '[id]': 'pattern.id()', |
| 172 | + }, |
| 173 | +}) |
| 174 | +export class CdkToolbarWidget implements OnInit, OnDestroy { |
| 175 | + /** A reference to the widget element. */ |
| 176 | + private readonly _elementRef = inject(ElementRef); |
| 177 | + |
| 178 | + /** The parent CdkToolbar. */ |
| 179 | + private readonly _cdkToolbar = inject(CdkToolbar); |
| 180 | + |
| 181 | + /** A unique identifier for the widget. */ |
| 182 | + private readonly _generatedId = inject(_IdGenerator).getId('cdk-toolbar-widget-'); |
| 183 | + |
| 184 | + /** A unique identifier for the widget. */ |
| 185 | + protected id = computed(() => this._generatedId); |
| 186 | + |
| 187 | + /** The parent Toolbar UIPattern. */ |
| 188 | + protected parentToolbar = computed(() => this._cdkToolbar.pattern); |
| 189 | + |
| 190 | + /** A reference to the widget element to be focused on navigation. */ |
| 191 | + element = computed(() => this._elementRef.nativeElement); |
| 192 | + |
| 193 | + /** Whether the widget is disabled. */ |
| 194 | + disabled = input(false, {transform: booleanAttribute}); |
| 195 | + |
| 196 | + readonly hardDisabled = computed( |
| 197 | + () => this.pattern.disabled() && this._cdkToolbar.skipDisabled(), |
| 198 | + ); |
| 199 | + |
| 200 | + pattern = new ToolbarWidgetPattern({ |
| 201 | + ...this, |
| 202 | + id: this.id, |
| 203 | + element: this.element, |
| 204 | + disabled: computed(() => this._cdkToolbar.disabled() || this.disabled()), |
| 205 | + parentToolbar: this.parentToolbar, |
| 206 | + }); |
| 207 | + |
| 208 | + ngOnInit() { |
| 209 | + this._cdkToolbar.register(this); |
| 210 | + } |
| 211 | + |
| 212 | + ngOnDestroy() { |
| 213 | + this._cdkToolbar.unregister(this); |
| 214 | + } |
| 215 | +} |
0 commit comments