|
| 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 {Directionality} from '@angular/cdk/bidi'; |
| 10 | +import { |
| 11 | + booleanAttribute, |
| 12 | + computed, |
| 13 | + Directive, |
| 14 | + ElementRef, |
| 15 | + inject, |
| 16 | + input, |
| 17 | + model, |
| 18 | + signal, |
| 19 | + afterRenderEffect, |
| 20 | + OnInit, |
| 21 | + OnDestroy, |
| 22 | +} from '@angular/core'; |
| 23 | +import {TabListPattern} from '@angular/aria/private'; |
| 24 | +import {sortDirectives, TABS} from './utils'; |
| 25 | +import type {Tab} from './tab'; |
| 26 | + |
| 27 | +/** |
| 28 | + * A TabList container. |
| 29 | + * |
| 30 | + * The `ngTabList` directive controls a list of `ngTab` elements. It manages keyboard |
| 31 | + * navigation, selection, and the overall orientation of the tabs. It should be placed |
| 32 | + * within an `ngTabs` container. |
| 33 | + * |
| 34 | + * ```html |
| 35 | + * <ul ngTabList [(selectedTab)]="mySelectedTab" orientation="horizontal" selectionMode="explicit"> |
| 36 | + * <li ngTab value="first">First Tab</li> |
| 37 | + * <li ngTab value="second">Second Tab</li> |
| 38 | + * </ul> |
| 39 | + * ``` |
| 40 | + * |
| 41 | + * @developerPreview 21.0 |
| 42 | + */ |
| 43 | +@Directive({ |
| 44 | + selector: '[ngTabList]', |
| 45 | + exportAs: 'ngTabList', |
| 46 | + host: { |
| 47 | + 'role': 'tablist', |
| 48 | + '[attr.tabindex]': '_pattern.tabIndex()', |
| 49 | + '[attr.aria-disabled]': '_pattern.disabled()', |
| 50 | + '[attr.aria-orientation]': '_pattern.orientation()', |
| 51 | + '[attr.aria-activedescendant]': '_pattern.activeDescendant()', |
| 52 | + '(keydown)': '_pattern.onKeydown($event)', |
| 53 | + '(pointerdown)': '_pattern.onPointerdown($event)', |
| 54 | + '(focusin)': '_onFocus()', |
| 55 | + }, |
| 56 | +}) |
| 57 | +export class TabList implements OnInit, OnDestroy { |
| 58 | + /** A reference to the host element. */ |
| 59 | + private readonly _elementRef = inject(ElementRef); |
| 60 | + |
| 61 | + /** A reference to the host element. */ |
| 62 | + readonly element = this._elementRef.nativeElement as HTMLElement; |
| 63 | + |
| 64 | + /** The parent Tabs. */ |
| 65 | + private readonly _tabs = inject(TABS); |
| 66 | + |
| 67 | + /** The Tabs nested inside of the TabList. */ |
| 68 | + private readonly _unorderedTabs = signal(new Set<Tab>()); |
| 69 | + |
| 70 | + /** Text direction. */ |
| 71 | + readonly textDirection = inject(Directionality).valueSignal; |
| 72 | + |
| 73 | + /** The Tab UIPatterns of the child Tabs. */ |
| 74 | + readonly _tabPatterns = computed(() => |
| 75 | + [...this._unorderedTabs()].sort(sortDirectives).map(tab => tab._pattern), |
| 76 | + ); |
| 77 | + |
| 78 | + /** Whether the tablist is vertically or horizontally oriented. */ |
| 79 | + readonly orientation = input<'vertical' | 'horizontal'>('horizontal'); |
| 80 | + |
| 81 | + /** Whether focus should wrap when navigating. */ |
| 82 | + readonly wrap = input(true, {transform: booleanAttribute}); |
| 83 | + |
| 84 | + /** |
| 85 | + * Whether to allow disabled items to receive focus. When `true`, disabled items are |
| 86 | + * focusable but not interactive. When `false`, disabled items are skipped during navigation. |
| 87 | + */ |
| 88 | + readonly softDisabled = input(true, {transform: booleanAttribute}); |
| 89 | + |
| 90 | + /** |
| 91 | + * The focus strategy used by the tablist. |
| 92 | + * - `roving`: Focus is moved to the active tab using `tabindex`. |
| 93 | + * - `activedescendant`: Focus remains on the tablist container, and `aria-activedescendant` is used to indicate the active tab. |
| 94 | + */ |
| 95 | + readonly focusMode = input<'roving' | 'activedescendant'>('roving'); |
| 96 | + |
| 97 | + /** |
| 98 | + * The selection strategy used by the tablist. |
| 99 | + * - `follow`: The focused tab is automatically selected. |
| 100 | + * - `explicit`: Tabs are selected explicitly by the user (e.g., via click or spacebar). |
| 101 | + */ |
| 102 | + readonly selectionMode = input<'follow' | 'explicit'>('follow'); |
| 103 | + |
| 104 | + /** The current selected tab. */ |
| 105 | + readonly selectedTab = model<string | undefined>(); |
| 106 | + |
| 107 | + /** Whether the tablist is disabled. */ |
| 108 | + readonly disabled = input(false, {transform: booleanAttribute}); |
| 109 | + |
| 110 | + /** The TabList UIPattern. */ |
| 111 | + readonly _pattern: TabListPattern = new TabListPattern({ |
| 112 | + ...this, |
| 113 | + items: this._tabPatterns, |
| 114 | + activeItem: signal(undefined), |
| 115 | + element: () => this._elementRef.nativeElement, |
| 116 | + }); |
| 117 | + |
| 118 | + /** Whether the tree has received focus yet. */ |
| 119 | + private _hasFocused = signal(false); |
| 120 | + |
| 121 | + constructor() { |
| 122 | + afterRenderEffect(() => { |
| 123 | + if (!this._hasFocused()) { |
| 124 | + this._pattern.setDefaultState(); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + afterRenderEffect(() => { |
| 129 | + const tab = this._pattern.selectedTab(); |
| 130 | + if (tab) { |
| 131 | + this.selectedTab.set(tab.value()); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + afterRenderEffect(() => { |
| 136 | + const value = this.selectedTab(); |
| 137 | + if (value) { |
| 138 | + this._pattern.open(value); |
| 139 | + } |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + _onFocus() { |
| 144 | + this._hasFocused.set(true); |
| 145 | + } |
| 146 | + |
| 147 | + ngOnInit() { |
| 148 | + this._tabs._register(this); |
| 149 | + } |
| 150 | + |
| 151 | + ngOnDestroy() { |
| 152 | + this._tabs._unregister(this); |
| 153 | + } |
| 154 | + |
| 155 | + _register(child: Tab) { |
| 156 | + this._unorderedTabs().add(child); |
| 157 | + this._unorderedTabs.set(new Set(this._unorderedTabs())); |
| 158 | + } |
| 159 | + |
| 160 | + _unregister(child: Tab) { |
| 161 | + this._unorderedTabs().delete(child); |
| 162 | + this._unorderedTabs.set(new Set(this._unorderedTabs())); |
| 163 | + } |
| 164 | + |
| 165 | + /** Opens the tab panel with the specified value. */ |
| 166 | + open(value: string): boolean { |
| 167 | + return this._pattern.open(value); |
| 168 | + } |
| 169 | +} |
0 commit comments