import type { VNode } from 'vue'; import type { TabPage } from './tab-page'; import { useId } from 'vue'; import { Prop, Setup, toNative } from 'vue-facing-decorator'; import PowerduckState from '../../app/powerduck-state'; import TsxComponent, { Component } from '../../app/vuetsx'; import { slotUnwrapper } from '../../common/slot-unwrapper'; import { isNullOrEmpty } from '../../common/utils/is-null-or-empty'; import Card from '../card/card'; import CardBody from '../card/card-body'; import CardHeader from '../card/card-header'; import HtmlLiteral from '../html-literal/html-literal'; import './css/tabs.css'; export enum TabsRenderMode { Normal = 0, SidePills = 1, SidePillsExtended = 2, } interface TabsArgs { renderMode?: TabsRenderMode; stickyOnMobile?: boolean; initialTabIndex?: number; allowTabSwitch?: boolean; wrapInCard?: boolean; cardHasShadow?: boolean; cardRenderMode?: 'default' | 'inlined'; renderHiddenTabs?: boolean; mountAllPanes?: boolean; /** Accessible name of the tab list (the page's heading, say): without it a screen reader announces an unnamed list of tabs. */ ariaLabel?: string; onTabChange?: (index: number) => void; } interface RenderedTabPane { slot: VNode; index: number; } @Component class TabsComponent extends TsxComponent implements TabsArgs { @Prop() renderMode!: TabsRenderMode; @Prop() stickyOnMobile!: boolean; @Prop() initialTabIndex!: number; @Prop() allowTabSwitch!: boolean; @Prop() wrapInCard!: boolean; @Prop() cardHasShadow!: boolean; @Prop() renderHiddenTabs!: boolean; /** * Side-pill modes without `renderHiddenTabs`: while true, every pane is mounted — the inactive * ones hidden — and the active page keeps being tracked as usual. `ModalSectionWrapper` raises it * for a moment so a validation scan can find an error in a section that is not on screen. */ @Prop() mountAllPanes!: boolean; @Prop() cardRenderMode?: 'default' | 'inlined'; @Prop() ariaLabel?: string; @Prop() onTabChange?: (index: number) => void; registeredCount: number = null; activePageIndex: number = null; /** * Prefix of the buttons' and panes' ids (`-button-N`, `-tab-N`), and the sticky-tabs key. `useId` gives it at the * first render, and the same in a server render and in its hydration (which keeps the server's attributes). The * random id `mounted()` assigned before re-rendered every Tabs right after its mount, building the active pane twice. * Unique per Vue app: a page that mounts a second app rendering Tabs gives that app its own `app.config.idPrefix`. */ @Setup(() => `t-${useId()}`) uuid!: string; created() { if (this.initialTabIndex != null) { this.setActivePageIndex(this.initialTabIndex, true); } else { this.setActivePageIndex(0, true); } } mounted() { this.ensureStickyTabs(); } updated() { this.ensureStickyTabs(); } beforeUnmount() { PowerduckState.unregisterStickyTabs(this.uuid); } ensureStickyTabs(): void { if (this.stickyOnMobile == true) { PowerduckState.registerStickyTabs(this.uuid, { HasSticky: this.stickyOnMobile == true, AllowTabChange: this.allowTabSwitch != false, }); } } /** The tabs' props, read off `slotNodes` — the default slot's nodes the render in progress already got — or a fresh call of the slot. */ getChildTabs(slotNodes?: VNode[]): typeof TabPage.prototype[] { return this.getSlotProperties('TabPage', slotNodes); } getContentCssClass(): string { if (this.allowTabSwitch != false) { return 'col-md-9 col-lg-10 tab-content'; } else { return 'col-md-12 tab-content'; } } getCardHasShadow(): boolean { if (this.cardHasShadow != null) { return this.cardHasShadow; } if (this.renderMode == TabsRenderMode.SidePillsExtended) { return true; } return false; } getCardRenderMode(): 'default' | 'inlined' { if (!isNullOrEmpty(this.cardRenderMode)) { return this.cardRenderMode; } if (this.renderMode == TabsRenderMode.SidePillsExtended) { return 'inlined'; } return 'default'; } /** `tabCount` — the number of tabs, when the caller already holds them; without it the slot is called to count them. */ getActivePageIndex(tabCount?: number): number { if (this.renderHiddenTabs != true) { if (this.activePageIndex > (tabCount ?? this.getChildTabs().length) - 1) { this.activePageIndex = 0; } return this.activePageIndex || 0; } else { return (this as any)._activeIndex || 0; } } setActivePageIndex(index: number, initial: boolean = false): void { if (this.renderHiddenTabs != true) { this.activePageIndex = index; } else { (this as any)._activeIndex = index; } if (!initial) { this.onTabChange?.(index); } } /** * The default slot is called ONCE per render, and its nodes are handed to everything that reads the tabs. Every call * re-runs the parent's slot function — with Vue's JSX, every expression written inside a `` — so the four * calls a normal-mode render used to make built each tab's content four times, the hidden tabs' included (gopass's * Discount Centre, 2026-10-23: 600 coupon-card builds per keystroke for 150 coupons, a frozen laptop). */ render(h) { const slotNodes = this.$slots.default?.() ?? []; if (this.renderMode == null || this.renderMode == TabsRenderMode.SidePills || this.renderMode == TabsRenderMode.SidePillsExtended) { return this.renderSidePillsMode(h, slotNodes); } else if (this.renderMode == TabsRenderMode.Normal) { return this.renderNormalMode(h, slotNodes); } else { console.error('Render mode not implemented'); return
Render mode not implemented
; } } getTabHref(tab: typeof TabPage.prototype, index: number): string { if (this.renderHiddenTabs != true) { return 'javascript:'; } else { return `#${this.uuid}-tab-${index}`; } } getActiveSlots(): any[] { return this.getRenderedPanes().map(pane => pane.slot); } /** * The tab pattern's keyboard half for tabs the component itself switches (`renderHiddenTabs` off): the active tab is * the list's one Tab stop (roving tabindex) and the arrow keys along the list's axis move to the previous / next * tab, Home / End to the first / last — each one activating the tab it lands on and carrying the focus there. * Tabs driven by Bootstrap's own tab plugin (`renderHiddenTabs`) get these keys from it. */ getTabTabindex(index: number, activeIndex: number): number | undefined { if (this.renderHiddenTabs == true) { return undefined; } return index == activeIndex ? 0 : -1; } /** Id of the pane a tab controls, only while that pane is in the DOM (`aria-controls` must name an element). */ getTabControls(index: number, activeIndex: number): string | undefined { const isRendered = this.renderHiddenTabs == true || this.mountAllPanes == true || index == activeIndex; return isRendered ? `${this.uuid}-tab-${index}` : undefined; } onTablistKeydown(e: KeyboardEvent, orientation: 'horizontal' | 'vertical'): void { if (this.renderHiddenTabs == true) { return; } const focusedTab = (e.target as HTMLElement).closest('[role="tab"]'); const count = this.getChildTabs().length; if (focusedTab == null || count < 2) { return; } const current = Number(focusedTab.closest('[data-index]')?.dataset.index ?? this.getActivePageIndex()); const previousKey = orientation == 'vertical' ? 'ArrowUp' : 'ArrowLeft'; const nextKey = orientation == 'vertical' ? 'ArrowDown' : 'ArrowRight'; let next: number = null; if (e.key == previousKey) { next = (current - 1 + count) % count; } else if (e.key == nextKey) { next = (current + 1) % count; } else if (e.key == 'Home') { next = 0; } else if (e.key == 'End') { next = count - 1; } if (next == null) { return; } e.preventDefault(); this.setActivePageIndex(next); void this.$nextTick(() => { (this.$refs.tablist as HTMLElement | undefined)?.querySelectorAll('[role="tab"]')[next]?.focus(); }); } /** * The panes to mount, each with its index among the tabs: all of them with `renderHiddenTabs` or * `mountAllPanes`, otherwise only the active one. `slotNodes` as for {@link getChildTabs}. */ getRenderedPanes(slotNodes: VNode[] = this.$slots.default?.()): RenderedTabPane[] { const slots = slotUnwrapper(slotNodes, 'TabPage'); if (this.renderHiddenTabs == true || this.mountAllPanes == true) { return slots.map((slot, index) => ({ slot, index })); } const index = this.getActivePageIndex(slots.length); return slots[index] == null ? [] : [{ slot: slots[index], index }]; } renderSidePillsMode(h, slotNodes: VNode[]) { const childTabs = this.getChildTabs(slotNodes); let activeIndex = this.getActivePageIndex(childTabs.length); if (activeIndex > childTabs.length - 1) { activeIndex = 0; } const activeTab: typeof TabPage.prototype = childTabs[activeIndex] || ({} as any); const fadeOutMode = this.renderHiddenTabs == true; return (
{this.allowTabSwitch != false && (
)}
{this.getRenderedPanes(slotNodes).map(({ slot, index }) => { // Each pane carries its OWN caption (pane ↔ childTabs[index]): with `renderHiddenTabs` every // pane renders up-front and Bootstrap toggles visibility via CSS (no Vue re-render), so a // single shared `activeTab` froze every header at the first tab's caption. // The pane's index, not its position among the rendered panes, names it: when only the // active pane is mounted it is the sole one rendered, yet it must still be the active pane, // point at its own button, and keep its key when `mountAllPanes` mounts the others around it. const paneTab: typeof TabPage.prototype = childTabs[index] || activeTab; return (
{this.wrapInCard === false ? slot : ( {slot} )}
); })}
); } renderNormalMode(h, slotNodes: VNode[]) { const childTabs = this.getChildTabs(slotNodes); const activeIndex = this.getActivePageIndex(childTabs.length); return (
{this.allowTabSwitch != false && (
)}
{this.renderHiddenTabs != true && this.renderActiveTab( h, slotNodes, activeIndex, )} {this.renderHiddenTabs == true && slotNodes.map(tab =>
{tab}
)}
); } /** `slotNodes` / `activeIndex` — the render's own, when it already holds them; without them they are read afresh. */ renderActiveTab( h, slotNodes: VNode[] = this.$slots.default?.(), activeIndex: number = this.getActivePageIndex(), ) { // The tab the keyboard / a screen reader is on names this pane (`aria-controls` / `aria-labelledby`). return (
{slotUnwrapper(slotNodes)?.[activeIndex]}
); } } const Tabs = toNative(TabsComponent); export default Tabs;