UNPKG

@scania/tegel

Version:
225 lines (220 loc) 11.3 kB
import { p as proxyCustomElement, H, d as createEvent, h, c as Host } from './p-28ef5186.js'; import { d as defineCustomElement$2 } from './p-b390ece5.js'; const inlineTabsCss = ":host{box-sizing:border-box;display:flex;background-color:var(--tds-inline-tabs-background);position:relative}:host *{box-sizing:border-box}:host::after{content:\" \";display:block;border-bottom:1px solid var(--tds-inline-tabs-horizontal-divider-background);left:0;right:0;bottom:0;position:absolute}:host .wrapper{display:flex;flex-wrap:nowrap;white-space:nowrap;width:100%;overflow-x:scroll;scrollbar-width:none;gap:16px}:host .wrapper::-webkit-scrollbar{display:none}:host .scroll-right-button{right:0;z-index:1}:host .scroll-left-button{left:0;z-index:1}:host .scroll-right-button,:host .scroll-left-button{color:var(--tds-folder-tabs-scroll-btn-color);cursor:pointer;border:0;width:0;background-color:var(--tds-inline-tabs-scroll-btn-background);display:none;justify-content:center;align-items:center;opacity:0;pointer-events:none;position:sticky}:host .scroll-right-button.show,:host .scroll-left-button.show{min-width:48px;display:block;opacity:1;pointer-events:all}:host .scroll-right-button:hover,:host .scroll-left-button:hover{background-color:var(--tds-folder-tabs-scroll-btn-background-hover)}:host .scroll-right-button:active,:host .scroll-left-button:active{background-color:var(--tds-folder-tabs-scroll-btn-background-active)}:host .scroll-right-button:focus,:host .scroll-left-button:focus{outline:2px solid var(--tds-focus-outline-color);box-shadow:0 0 0 1px var(--tds-white);outline-offset:1px;z-index:1}:host .scroll-right-button svg,:host .scroll-left-button svg{fill:var(--tds-folder-tabs-scroll-btn-color)}"; const TdsInlineTabsStyle0 = inlineTabsCss; const TdsInlineTabs$1 = /*@__PURE__*/ proxyCustomElement(class TdsInlineTabs extends H { constructor() { super(); this.__registerHost(); this.__attachShadow(); this.tdsChange = createEvent(this, "tdsChange", 7); this.navWrapperElement = null; // reference to container with nav buttons this.componentWidth = 0; // visible width of this component this.buttonsWidth = 0; // total width of all nav items combined this.scrollWidth = 0; // total amount that is possible to scroll in the nav wrapper this.clickHandlers = new WeakMap(); this.addResizeObserver = () => { const resizeObserver = new ResizeObserver((entries) => { entries.forEach((entry) => { const componentWidth = entry.contentRect.width; let buttonsWidth = 0; const navButtons = Array.from(this.host.children); navButtons.forEach((navButton) => { const style = window.getComputedStyle(navButton); buttonsWidth += navButton.clientWidth + parseFloat(style.marginLeft) + parseFloat(style.marginRight); }); this.componentWidth = componentWidth; this.buttonsWidth = buttonsWidth; this.scrollWidth = buttonsWidth - componentWidth; this.updateScrollButtons(); }); }); resizeObserver.observe(this.navWrapperElement); }; this.addEventListenerToTabs = () => { this.children = Array.from(this.host.children); this.children.map((item, index) => { const clickHandler = () => { if (!item.disabled) { const tdsChangeEvent = this.tdsChange.emit({ selectedTabIndex: this.children.indexOf(item), }); if (!tdsChangeEvent.defaultPrevented) { this.children.forEach((element) => element.setSelected(false)); item.setSelected(true); this.selectedIndex = index; } } }; item.addEventListener('click', clickHandler); this.clickHandlers.set(item, clickHandler); // Store the handler in WeakMap return item; }); }; this.removeEventListenerFromTabs = () => { this.children.forEach((item) => { const clickHandler = this.clickHandlers.get(item); if (clickHandler) { item.removeEventListener('click', clickHandler); this.clickHandlers.delete(item); } }); }; this.modeVariant = 'primary'; this.defaultSelectedIndex = 0; this.selectedIndex = undefined; this.tdsScrollLeftAriaLabel = 'Scroll left'; this.tdsScrollRightAriaLabel = 'Scroll right'; this.leftPadding = 32; this.showLeftScroll = false; this.showRightScroll = false; } /** Selects a Tab based on tabindex, will not select a disabled Tab. */ async selectTab(tabIndex) { if (tabIndex < 0 || tabIndex >= this.children.length) { throw new Error('Tab index out of bounds'); } this.children = Array.from(this.host.children); if (!this.children[tabIndex].disabled) { this.children.forEach((element) => element.setSelected(false)); this.children = this.children.map((element, index) => { if (index === tabIndex) { element.setSelected(true); this.selectedIndex = tabIndex; } return element; }); } return { selectedTabIndex: this.selectedIndex }; } /** Reinitializes the component. */ async reinitialize() { this.handleSlotChange(); } handleSelectedIndexUpdate() { this.children = Array.from(this.host.children).map((tabElement) => { tabElement.setSelected(false); return tabElement; }); this.children[this.selectedIndex].setSelected(true); } scrollRight() { const scroll = this.navWrapperElement.scrollLeft; this.navWrapperElement.scrollLeft = scroll + this.buttonsWidth; this.evaluateScrollButtons(); } scrollLeft() { const scroll = this.navWrapperElement.scrollLeft; this.navWrapperElement.scrollLeft = scroll - this.buttonsWidth; this.evaluateScrollButtons(); } evaluateScrollButtons() { const scroll = this.navWrapperElement.scrollLeft; this.showRightScroll = scroll <= this.scrollWidth; this.showLeftScroll = scroll > 0; } initializeTabs() { this.children = Array.from(this.host.children); // remove first and last class from other tabs in case of initialization this.children.forEach((child) => { child.classList.remove('last'); child.classList.remove('first'); }); this.children[0].classList.add('first'); this.children[this.children.length - 1].classList.add('last'); } initializeSelectedTab() { if (this.selectedIndex === undefined) { this.addEventListenerToTabs(); this.children[this.defaultSelectedIndex].setSelected(true); this.selectedIndex = this.defaultSelectedIndex; } else { this.children[this.selectedIndex].setSelected(true); } this.tdsChange.emit({ selectedTabIndex: this.selectedIndex, }); } updateScrollButtons() { if (this.buttonsWidth > this.componentWidth) { this.evaluateScrollButtons(); } else { this.showLeftScroll = false; this.showRightScroll = false; } } applyCustomLeftPadding() { if (this.navWrapperElement) { this.navWrapperElement.style.paddingLeft = `${this.leftPadding}px`; } } handleSlotChange() { this.initializeTabs(); this.addEventListenerToTabs(); this.initializeSelectedTab(); this.updateScrollButtons(); this.addResizeObserver(); this.applyCustomLeftPadding(); // Apply custom left padding to the wrapper } connectedCallback() { this.initializeTabs(); } componentDidLoad() { this.initializeSelectedTab(); } componentDidRender() { this.updateScrollButtons(); this.addResizeObserver(); } disconnectedCallback() { this.removeEventListenerFromTabs(); } render() { return (h(Host, { key: '9fa2d8cb54ac480fb5875432440506b0c12efaa5', role: "tablist", class: `${this.modeVariant ? `tds-mode-variant-${this.modeVariant}` : ''}` }, h("div", { key: 'aecb68d56088f84e271f3c829c84da4e732df9b8', class: "wrapper", ref: (el) => { this.navWrapperElement = el; }, style: { paddingLeft: `${this.leftPadding}px` } }, h("button", { key: '9b97d342cfe66209063b504b49480d587cd626d5', "aria-label": this.tdsScrollLeftAriaLabel, class: `scroll-left-button ${this.showLeftScroll ? 'show' : ''}`, onClick: () => this.scrollLeft(), disabled: !this.showLeftScroll }, h("tds-icon", { key: 'a447ce946eb5bc02a2527f210322aa76ca5ad5b8', name: "chevron_left", size: "20px" })), h("slot", { key: '1df34e68bbea35ef76de48e7b8fe96b3a8655792', onSlotchange: () => this.handleSlotChange() }), h("button", { key: '7c753dd66c173265c32a7b49197df12fbecc78d1', "aria-label": this.tdsScrollRightAriaLabel, class: `scroll-right-button ${this.showRightScroll ? 'show' : ''}`, onClick: () => this.scrollRight(), disabled: !this.showRightScroll }, h("tds-icon", { key: 'f76934b67470db5ff6ddb6fbae86f87fd3d0bdee', name: "chevron_right", size: "20px" }))))); } get host() { return this; } static get watchers() { return { "selectedIndex": ["handleSelectedIndexUpdate"] }; } static get style() { return TdsInlineTabsStyle0; } }, [1, "tds-inline-tabs", { "modeVariant": [1, "mode-variant"], "defaultSelectedIndex": [2, "default-selected-index"], "selectedIndex": [514, "selected-index"], "tdsScrollLeftAriaLabel": [1, "tds-scroll-left-aria-label"], "tdsScrollRightAriaLabel": [1, "tds-scroll-right-aria-label"], "leftPadding": [514, "left-padding"], "showLeftScroll": [32], "showRightScroll": [32], "selectTab": [64], "reinitialize": [64] }, undefined, { "selectedIndex": ["handleSelectedIndexUpdate"] }]); function defineCustomElement$1() { if (typeof customElements === "undefined") { return; } const components = ["tds-inline-tabs", "tds-icon"]; components.forEach(tagName => { switch (tagName) { case "tds-inline-tabs": if (!customElements.get(tagName)) { customElements.define(tagName, TdsInlineTabs$1); } break; case "tds-icon": if (!customElements.get(tagName)) { defineCustomElement$2(); } break; } }); } defineCustomElement$1(); const TdsInlineTabs = TdsInlineTabs$1; const defineCustomElement = defineCustomElement$1; export { TdsInlineTabs, defineCustomElement };