import {html, LitElement, nothing} from 'lit'; import {ref, createRef, type Ref} from 'lit/directives/ref.js'; import {BlockquoteMixinSlotContent} from '@blockquote-web-components/blockquote-mixin-slot-content'; import {SelectionController} from './controllers/SelectionController.js'; import {FocusGroupController} from './controllers/FocusGroupController.js'; import {ScrollController} from './controllers/ScrollController.js'; import {styles} from './styles/blockquote-tabs-styles.css.js'; // https://gist.github.com/ebidel/2d2bb0cdec3f2a16cf519dbaa791ce1b // https://darn.es/building-tabs-in-web-components/ // https://slides.com/daviddarnes/tabs-web-components interface SlotChangesDetail { assignedNodesContent: { assignedNodes: HTMLElement[]; }; assignedSlotContent: { slotName: string; }; } /** * ![Lit](https://img.shields.io/badge/lit-3.0.0-blue.svg) * * [ARIA patterns](https://www.w3.org/WAI/ARIA/apg/patterns/) * * Tabs are a set of layered sections of content, known as tab panels, that display one panel of content at a time. Each tab panel has an associated tab element, that when activated, displays the panel. The list of tab elements is arranged along one edge of the currently displayed panel, most commonly the top edge. * * ### Demo * * [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/oscarmarina/blockquote-web-components/tree/main/packages/components/blockquote-tabs) * * ### Usage * * ```html * * Tab 1 * Tab 2 * Tab 3 * Tab 4 * Tab 5 * Tab 6 * Tab 7 * Tab 8 * Tab 9 * Tab 10 *

Panel 1

*

Panel 2

*

Panel 3

*

Panel 4

*

Panel 5

*

Panel 6

*

Panel 7

*

Panel 8

*

Panel 9

*

Panel 10

*
* ``` * * @attribute autofocus * @attribute label * @attribute selected * @attribute activation * @attribute orientation * @fires selectedchange */ export class BlockquoteTabs extends BlockquoteMixinSlotContent(LitElement) { static override get styles() { return [styles]; } static override get properties() { return { /** * If present, the selected tab is focused on boot. Alternatively, set the * native `autofocus` attribute on the desired `` (takes * precedence): the component delegates it in JS because native support is * unreliable across browsers today (see focusgroup redesign notes). */ autofocus: { type: Boolean, }, /** * `aria-label` for tabs group */ label: { type: String, }, /** * The tab selected. */ selected: { type: Number, reflect: true, }, /** * Activation mode: * - `auto` (default): selection follows focus (arrow keys select). * - `manual`: arrow keys only move focus; Enter, Space or click select. */ activation: { type: String, }, /** * Orientation of the tablist: `horizontal` (default) or `vertical`. * Determines the arrow-key axis, `aria-orientation`, and the scroll * axis (scroll indicators, reveal-on-focus, separator). */ orientation: { type: String, reflect: true, }, }; } override autofocus!: boolean; label!: string; selected!: number; activation!: string; orientation!: string; _tabList: HTMLElement[]; _tabpanelList: HTMLElement[]; _scrollContentRef: Ref; _tablistRef: Ref; _selection: SelectionController; _focusGroup: FocusGroupController; _scroll: ScrollController; _indicators: NodeListOf | undefined; constructor() { super(); /* initial state */ this.autofocus = false; this.label = ''; this.selected = 1; this.activation = 'auto'; this.orientation = 'horizontal'; this._tabList = []; this._tabpanelList = []; this._scrollContentRef = createRef(); this._tablistRef = createRef(); this._selection = new SelectionController(this); this._focusGroup = new FocusGroupController(this, { getItems: () => this._tabList, getSelectedIndex: () => this._selection.selectedIndex, getOrientation: () => (this.orientation === 'vertical' ? 'vertical' : 'horizontal'), getActivation: () => (this.activation === 'manual' ? 'manual' : 'auto'), onSelect: (index) => { this.selected = index + 1; }, onReveal: (tab) => { this._scroll.scrollIntoView(tab); }, }); this._scroll = new ScrollController(this, { getScrollContent: () => this._scrollContentRef.value, getIndicators: () => this._indicators, getSelectedTab: () => this._selectedTab, getOrientation: () => (this.orientation === 'vertical' ? 'vertical' : 'horizontal'), }); this.addEventListener('slotchanges', this._onSlotChanges as EventListener); } get _selectedTab(): HTMLElement | undefined { return this._selection.selectedTab; } override firstUpdated(props: Map) { super.firstUpdated(props); const tabSlot = this.shadowRoot?.querySelector('[name="tab"]'); const tabpanelSlot = this.shadowRoot?.querySelector('[name="tabpanel"]'); this._tabList = (tabSlot as HTMLSlotElement | undefined)?.assignedElements() as HTMLElement[]; this._tabpanelList = ( tabpanelSlot as HTMLSlotElement | undefined )?.assignedElements() as HTMLElement[]; this._indicators = this.shadowRoot?.querySelectorAll('.indicator'); this._selection.setItems(this._tabList, this._tabpanelList); this._focusGroup.attach(this._tablistRef.value); // Boot focus: a tab-level `autofocus` attribute (delegated in JS, since // native global autofocus is unreliable across browsers today) takes // precedence over the host-level `autofocus` property. const bootTab = this._tabList.find((tab) => tab.hasAttribute('autofocus')) ?? (this.autofocus ? this._focusGroup.entryItem : undefined); if (bootTab) { window.requestAnimationFrame(() => bootTab.focus()); } } override updated(props: Map) { super.updated(props); // Before the `selected` branch: its clamping early-return must not skip // the orientation sync (the native engine's focusgroup token encodes the // axis; the fallback treats this as a no-op). if (props.has('orientation')) { this._focusGroup.syncOrientation(); } if (props.has('selected')) { const committed = this._selection.commit(this.selected); if (committed !== this.selected) { // Correct the reflected value; the commit happens on the next update. this.selected = committed; return; } this._onSelectionCommit(); /** * Fired when selected changes * @event selectedchange */ const event = new CustomEvent('selectedchange', { bubbles: true, detail: { selected: this._selection.selectedIndexFromOne, tab: this._selection.selectedTab, tabpanel: this._selection.selectedTabpanel, }, }); this.dispatchEvent(event); } } _onSlotChanges = (ev: CustomEvent) => { ev.stopPropagation(); ev.preventDefault(); const {detail} = ev; const assignedNodesList = detail.assignedNodesContent.assignedNodes; if (detail.assignedSlotContent.slotName === 'tab') { this._tabList = assignedNodesList; } if (detail.assignedSlotContent.slotName === 'tabpanel') { this._tabpanelList = assignedNodesList; } this._selection.setItems(this._tabList, this._tabpanelList); this._focusGroup.syncEntryPoint(); }; get _scrollContentTpl() { return html`
${this._tablistTpl}
`; } get _tablistTpl() { return html`
`; } get _separatorTpl() { return html` `; } get _indicatorsTpl() { return html` `; } get _holdTpl() { return html`
${this._scrollContentTpl} ${this._separatorTpl} ${this._indicatorsTpl}
`; } get _tabpanelTpl() { return html`
`; } override render() { return html` ${this._holdTpl} ${this._tabpanelTpl} `; } _scrollEdge(ev: Event) { this._scroll.scrollEdge(ev.target instanceof HTMLElement ? ev.target : undefined); } _onSelectionCommit() { this._focusGroup.syncEntryPoint(); this._scroll.scrollIntoView(); } }