import { attr, FASTElement, observable } from '@microsoft/fast-element'; import { uniqueId } from '../utils/unique-id.js'; import type { Tab } from '../tab/tab.js'; import { isTab } from '../tab/tab.options.js'; import { swapStates, toggleState } from '../utils/element-internals.js'; import { waitForConnectedDescendants } from '../utils/request-idle-callback.js'; import { TablistOrientation } from './tablist.options.js'; /** * A Tablist element that wraps a collection of tab elements * @public */ export class BaseTablist extends FASTElement { /** * The internal {@link https://developer.mozilla.org/docs/Web/API/ElementInternals | `ElementInternals`} instance for the component. * * @internal */ public elementInternals: ElementInternals = this.attachInternals(); /** * Used for disabling all click and keyboard events for the tabs, child tab elements. * @public * @remarks * HTML Attribute: disabled. */ @attr({ mode: 'boolean' }) public disabled: boolean = false; /** @internal */ protected disabledChanged(prev: boolean, next: boolean): void { if (this.elementInternals) { toggleState(this.elementInternals, 'disabled', next); } this.setTabs({ forceDisabled: true }); } /** * The orientation * @public * @remarks * HTML Attribute: orientation */ @attr public orientation: TablistOrientation = TablistOrientation.horizontal; protected orientationChanged(prev: TablistOrientation, next: TablistOrientation): void { if (this.elementInternals) { this.elementInternals.ariaOrientation = next ?? TablistOrientation.horizontal; swapStates(this.elementInternals, prev, next, TablistOrientation); } this.setTabs(); } /** * The id of the active tab * * @public * @remarks * HTML Attribute: activeid */ @attr public activeid!: string; /** @internal */ protected activeidChanged(oldValue: string, newValue: string): void { if (this.tabs?.length > 0) { this.changeTab(oldValue, newValue); } } /** * Content slotted in the tab slot. * @internal */ @observable public slottedTabs!: Node[]; /** @internal */ protected slottedTabsChanged(prev: Node[] | undefined, next: Node[] | undefined): void { this.tabs = (next?.filter(tab => isTab(tab)) as Tab[]) ?? []; } /** @internal */ @observable public tabs: Tab[] = []; /** @internal */ protected tabsChanged(prev: Tab[] | undefined, next: Tab[] | undefined): void { if (this.tabs?.length > 0) { this.setTabs({ connectToPanel: true }); } } /** * A reference to the active tab * @public */ public activetab!: Tab; private tabPanelMap = new WeakMap(); private change = (): void => { this.$emit('change', this.activetab); }; /** * Function that is invoked whenever the selected tab or the tab collection changes. */ protected setTabs({ connectToPanel = false, forceDisabled = false } = {}): void { if (!this.tabs) { return; } const rootNode = this.getRootNode() as Document | ShadowRoot; let firstEnabledTabId = ''; for (const tab of this.tabs) { if (tab.slot !== 'tab') { continue; } tab.id ||= uniqueId('tab-'); if (forceDisabled) { tab.disabled = this.disabled; } else { tab.disabled = tab.disabled || this.disabled; } if (!firstEnabledTabId && !tab.disabled) { firstEnabledTabId = tab.id; } const isSelected = this.activeid === tab.id; tab.toggleAttribute('focusgroupstart', isSelected); tab.setAttribute('aria-selected', isSelected.toString()); if (connectToPanel) { const ariaControls = tab.getAttribute('aria-controls') ?? ''; const panel = rootNode.getElementById(ariaControls); if (ariaControls && panel) { panel.role ??= 'tabpanel'; panel.hidden = this.activeid !== tab.id; this.tabPanelMap.set(tab, panel); } } } if (!this.disabled) { if (this.activeid) { this.changeTab(undefined, this.activeid); } else if (firstEnabledTabId) { this.activeid = firstEnabledTabId; } } } /** @internal */ public handleFocusIn(event: FocusEvent) { const target = event.target as Node; if (!isTab(target) || target.disabled) { return; } this.activeid = target.id; } /** @internal */ public handleClick(event: PointerEvent) { // We only need to handle click event when `tab.click()` is called, a user // click will be handled by `focusin` event. And as per the DOM spec, // calling `Element.click()` results in `isTrusted=false`: // https://dom.spec.whatwg.org/#dom-event-istrusted if (event.isTrusted) { return; } const target = event.target as Node; if (!isTab(target) || target.disabled) { return; } this.activeid = target.id; } private changeTab(oldId: undefined | string, newId: string) { const rootNode = this.getRootNode() as Document | ShadowRoot; const prevTab = oldId ? rootNode.getElementById(oldId) : null; const nextTab = rootNode.getElementById(newId); if (!isTab(nextTab) || nextTab.disabled || !this.contains(nextTab)) { return; } if (prevTab) { prevTab.setAttribute('aria-selected', 'false'); const prevPanel = this.tabPanelMap.get(prevTab); if (prevPanel) { prevPanel.hidden = true; } } nextTab.setAttribute('aria-selected', 'true'); const nextPanel = this.tabPanelMap.get(nextTab); if (nextPanel) { nextPanel.hidden = false; } this.activetab = nextTab; this.change(); } constructor() { super(); this.elementInternals.role = 'tablist'; this.elementInternals.ariaOrientation = this.orientation ?? TablistOrientation.horizontal; } /** * @internal */ public connectedCallback(): void { super.connectedCallback(); this.role ??= 'tablist'; if (!this.hasAttribute('focusgroup')) { this.setAttribute('focusgroup', 'tablist inline block'); } waitForConnectedDescendants( this, () => { this.setTabs(); }, { shallow: true }, ); } }