UNPKG

@scania/tegel

Version:
197 lines (192 loc) 10.1 kB
'use strict'; var index = require('./index-DGsdvbvx.js'); const folderTabsCss = () => `:host{box-sizing:border-box;overflow:hidden;outline:0}:host *{box-sizing:border-box}:host .wrapper{display:flex;overflow-x:scroll;scrollbar-width:none;position:relative}:host .wrapper::-webkit-scrollbar{display:none}:host .scroll-right-button{z-index:1;right:0}:host .scroll-left-button{z-index:1;left:0}:host .scroll-right-button,:host .scroll-left-button{height:50px;color:var(--tds-folder-tabs-scroll-btn-color);cursor:pointer;border:0;width:0;background-color:var(--tds-folder-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-within::before,:host .scroll-left-button:focus-within::before{outline:2px solid var(--tds-focus-outline-color);box-shadow:0 0 0 1px var(--tds-white);outline-offset:1px;z-index:1;content:"";position:absolute;top:3px;bottom:3px;left:3px;right:3px}:host .scroll-right-button:focus,:host .scroll-left-button:focus{outline:0}:host .scroll-right-button svg,:host .scroll-left-button svg{fill:var(--tds-folder-tabs-scroll-btn-color)}`; const TdsFolderTabs = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.tdsChange = index.createEvent(this, "tdsChange", 7); /** Variant of the Tabs, primary= on white, secondary= on grey50 */ this.modeVariant = null; /** Sets the default selected Tab. */ this.defaultSelectedIndex = 0; /** Defines aria-label on left scroll button */ this.tdsScrollLeftAriaLabel = 'Scroll left'; /** Defines aria-label on right scroll button */ this.tdsScrollRightAriaLabel = 'Scroll right'; this.buttonWidth = 0; this.showLeftScroll = false; this.showRightScroll = false; 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.scrollableWidth = 0; // total amount that is possible to scroll in the nav wrapper this.tabElements = []; 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.scrollableWidth = buttonsWidth - componentWidth; this.updateScrollButtons(); }); }); if (this.navWrapperElement) resizeObserver.observe(this.navWrapperElement); }; this.addEventListenerToTabs = () => { this.tabElements = Array.from(this.host.children); this.tabElements.map((item, index) => { const clickHandler = () => { if (!item.disabled) { const tdsChangeEvent = this.tdsChange.emit({ selectedTabIndex: this.tabElements.indexOf(item), }); if (!tdsChangeEvent.defaultPrevented) { this.tabElements.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.tabElements.forEach((item) => { const clickHandler = this.clickHandlers.get(item); if (clickHandler) { item.removeEventListener('click', clickHandler); this.clickHandlers.delete(item); } }); }; } /** Sets the passed tabindex as the selected Tab. */ async selectTab(tabIndex) { if (tabIndex < 0 || tabIndex >= this.tabElements.length) { throw new Error('Tab index out of bounds'); } if (!this.tabElements[tabIndex].disabled) { this.tabElements.forEach((element) => element.setSelected(false)); this.tabElements = this.tabElements.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() { const tabs = Array.from(this.host.children); this.tabElements = tabs.map((tabElement) => { tabElement.setSelected(false); return tabElement; }); if (this.selectedIndex !== undefined) { this.tabElements[this.selectedIndex].setSelected(true); } } scrollRight() { if (!this.navWrapperElement) return; const scroll = this.navWrapperElement.scrollLeft; this.navWrapperElement.scrollLeft = scroll + this.buttonsWidth; this.evaluateScrollButtons(); } scrollLeft() { if (!this.navWrapperElement) return; const scroll = this.navWrapperElement.scrollLeft; this.navWrapperElement.scrollLeft = scroll - this.buttonsWidth; this.evaluateScrollButtons(); } evaluateScrollButtons() { if (!this.navWrapperElement) return; const scroll = this.navWrapperElement.scrollLeft; this.showRightScroll = scroll <= this.scrollableWidth; this.showLeftScroll = scroll > 0; } initializeTabs() { this.tabElements = Array.from(this.host.children); // remove first and last class from other tabs in case of initialization this.tabElements.forEach((child) => { child.classList.remove('last'); child.classList.remove('first'); }); this.tabElements[0].classList.add('first'); this.tabElements[this.tabElements.length - 1].classList.add('last'); } initializeSelectedTab() { if (this.selectedIndex === undefined) { this.addEventListenerToTabs(); this.tabElements[this.defaultSelectedIndex].setSelected(true); this.selectedIndex = this.defaultSelectedIndex; } else { this.tabElements[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; } } handleSlotChange() { this.initializeTabs(); this.addEventListenerToTabs(); this.initializeSelectedTab(); this.updateScrollButtons(); this.addResizeObserver(); } connectedCallback() { this.initializeTabs(); } componentDidLoad() { this.initializeSelectedTab(); } componentDidRender() { this.updateScrollButtons(); this.addResizeObserver(); } disconnectedCallback() { this.removeEventListenerFromTabs(); } render() { return (index.h(index.Host, { key: 'e8b3a0c15cad9dc5cf7b68381de79786c5853c17', role: "tablist", class: `${this.modeVariant ? `tds-mode-variant-${this.modeVariant}` : ''}` }, index.h("div", { key: '459d2af9aa923196938b7c7848ef3494b1eb0015', class: "wrapper", ref: (el) => { this.navWrapperElement = el; } }, index.h("button", { key: '6c124a6cc60705c0eabe05f05e24308300c5c230', "aria-label": this.tdsScrollLeftAriaLabel, class: `scroll-left-button ${this.showLeftScroll ? 'show' : ''}`, disabled: !this.showLeftScroll, onClick: () => this.scrollLeft() }, index.h("tds-icon", { key: '82585953fd9a38d4b4fa9c0ddf514b7ddf0f5938', name: "chevron_left", size: "20px" })), index.h("slot", { key: '101d7936691d26bc4448d9c8800e28500c42c92e', onSlotchange: () => this.handleSlotChange() }), index.h("button", { key: 'a95a09608cf9388d0e55465a62ba6d3c5101d337', "aria-label": this.tdsScrollRightAriaLabel, class: `scroll-right-button ${this.showRightScroll ? 'show' : ''}`, disabled: !this.showRightScroll, onClick: () => this.scrollRight() }, index.h("tds-icon", { key: 'c36777a240da371d5f2145410f6b5635bb688d95', name: "chevron_right", size: "20px" }))))); } get host() { return index.getElement(this); } static get watchers() { return { "selectedIndex": [{ "handleSelectedIndexUpdate": 0 }] }; } }; TdsFolderTabs.style = folderTabsCss(); exports.tds_folder_tabs = TdsFolderTabs;