UNPKG

@scania/tegel

Version:
1 lines 5.79 kB
import{r as t,c as s,h as i,H as o,g as e}from"./p-2049fab2.js";const h=class{constructor(i){t(this,i),this.tdsChange=s(this,"tdsChange",7),this.navWrapperElement=null,this.componentWidth=0,this.buttonsWidth=0,this.scrollWidth=0,this.clickHandlers=new WeakMap,this.addResizeObserver=()=>{new ResizeObserver((t=>{t.forEach((t=>{const s=t.contentRect.width;let i=0;Array.from(this.host.children).forEach((t=>{const s=window.getComputedStyle(t);i+=t.clientWidth+parseFloat(s.marginLeft)+parseFloat(s.marginRight)})),this.componentWidth=s,this.buttonsWidth=i,this.scrollWidth=i-s,this.updateScrollButtons()}))})).observe(this.navWrapperElement)},this.addEventListenerToTabs=()=>{this.children=Array.from(this.host.children),this.children.map(((t,s)=>{const i=()=>{t.disabled||this.tdsChange.emit({selectedTabIndex:this.children.indexOf(t)}).defaultPrevented||(this.children.forEach((t=>t.setSelected(!1))),t.setSelected(!0),this.selectedIndex=s)};return t.addEventListener("click",i),this.clickHandlers.set(t,i),t}))},this.removeEventListenerFromTabs=()=>{this.children.forEach((t=>{const s=this.clickHandlers.get(t);s&&(t.removeEventListener("click",s),this.clickHandlers.delete(t))}))},this.modeVariant=null,this.defaultSelectedIndex=0,this.selectedIndex=void 0,this.tdsScrollLeftAriaLabel="Scroll left",this.tdsScrollRightAriaLabel="Scroll right",this.buttonWidth=0,this.showLeftScroll=!1,this.showRightScroll=!1}async selectTab(t){if(t<0||t>=this.children.length)throw new Error("Tab index out of bounds");return this.children[t].disabled||(this.children.forEach((t=>t.setSelected(!1))),this.children=this.children.map(((s,i)=>(i===t&&(s.setSelected(!0),this.selectedIndex=t),s)))),{selectedTabIndex:this.selectedIndex}}async reinitialize(){this.handleSlotChange()}handleSelectedIndexUpdate(){this.children=Array.from(this.host.children).map((t=>(t.setSelected(!1),t))),this.children[this.selectedIndex].setSelected(!0)}scrollRight(){this.navWrapperElement.scrollLeft=this.navWrapperElement.scrollLeft+this.buttonsWidth,this.evaluateScrollButtons()}scrollLeft(){this.navWrapperElement.scrollLeft=this.navWrapperElement.scrollLeft-this.buttonsWidth,this.evaluateScrollButtons()}evaluateScrollButtons(){const t=this.navWrapperElement.scrollLeft;this.showRightScroll=t<=this.scrollWidth,this.showLeftScroll=t>0}initializeTabs(){this.children=Array.from(this.host.children),this.children.forEach((t=>{t.classList.remove("last"),t.classList.remove("first")})),this.children[0].classList.add("first"),this.children[this.children.length-1].classList.add("last")}initializeSelectedTab(){void 0===this.selectedIndex?(this.addEventListenerToTabs(),this.children[this.defaultSelectedIndex].setSelected(!0),this.selectedIndex=this.defaultSelectedIndex):this.children[this.selectedIndex].setSelected(!0),this.tdsChange.emit({selectedTabIndex:this.selectedIndex})}updateScrollButtons(){this.buttonsWidth>this.componentWidth?this.evaluateScrollButtons():(this.showLeftScroll=!1,this.showRightScroll=!1)}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 i(o,{key:"c56bf987e23ade8b30cd2470ab1ed0f4273bd191",role:"tablist",class:this.modeVariant?`tds-mode-variant-${this.modeVariant}`:""},i("div",{key:"afab8a86427918d5ac4bbc17dd2a436a986df577",class:"wrapper",ref:t=>{this.navWrapperElement=t}},i("button",{key:"0b82a953932e888d05478d82944c90768f97bb36","aria-label":this.tdsScrollLeftAriaLabel,class:"scroll-left-button "+(this.showLeftScroll?"show":""),disabled:!this.showLeftScroll,onClick:()=>this.scrollLeft()},i("tds-icon",{key:"50cd22c5be43ae452e0896b449570774c3ab0151",name:"chevron_left",size:"20px"})),i("slot",{key:"3cb17dddc8592241c467167b3a0028355f2b7d5e",onSlotchange:()=>this.handleSlotChange()}),i("button",{key:"23c289b70d723b2d0705692835a510b8a650fcfa","aria-label":this.tdsScrollRightAriaLabel,class:"scroll-right-button "+(this.showRightScroll?"show":""),disabled:!this.showRightScroll,onClick:()=>this.scrollRight()},i("tds-icon",{key:"fff2bfcbcf916d1b391aa25ee5f6fda3c8e63d23",name:"chevron_right",size:"20px"}))))}get host(){return e(this)}static get watchers(){return{selectedIndex:["handleSelectedIndexUpdate"]}}};h.style=":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,: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)}";export{h as tds_folder_tabs}