@scania/tegel
Version:
Tegel Design System
1 lines • 7.38 kB
JavaScript
import{t,p as s,H as i,c as e,h as o,a as l}from"./index.js";import{d as r}from"./p-CIt4YhvL.js";const h=s(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.tdsChange=e(this,"tdsChange",7),this.modeVariant="primary",this.defaultSelectedIndex=0,this.tdsScrollLeftAriaLabel="Scroll left",this.tdsScrollRightAriaLabel="Scroll right",this.leftPadding=32,this.showLeftScroll=!1,this.showRightScroll=!1,this.navWrapperElement=null,this.componentWidth=0,this.buttonsWidth=0,this.scrollableWidth=0,this.tabElements=[],this.clickHandlers=new WeakMap,this.addResizeObserver=()=>{const t=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.scrollableWidth=i-s,this.updateScrollButtons()}))}));this.navWrapperElement&&t.observe(this.navWrapperElement)},this.addEventListenerToTabs=()=>{this.tabElements=Array.from(this.host.children),this.tabElements.map(((t,s)=>{const i=()=>{t.disabled||this.tdsChange.emit({selectedTabIndex:this.tabElements.indexOf(t)}).defaultPrevented||(this.tabElements.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.tabElements.forEach((t=>{const s=this.clickHandlers.get(t);s&&(t.removeEventListener("click",s),this.clickHandlers.delete(t))}))}}async selectTab(t){if(t<0||t>=this.tabElements.length)throw Error("Tab index out of bounds");return this.tabElements=Array.from(this.host.children),this.tabElements[t].disabled||(this.tabElements.forEach((t=>t.setSelected(!1))),this.tabElements=this.tabElements.map(((s,i)=>(i===t&&(s.setSelected(!0),this.selectedIndex=t),s)))),{selectedTabIndex:this.selectedIndex}}async reinitialize(){this.handleSlotChange()}handleSelectedIndexUpdate(){this.tabElements=Array.from(this.host.children).map((t=>(t.setSelected(!1),t))),void 0!==this.selectedIndex&&this.tabElements[this.selectedIndex].setSelected(!0)}scrollRight(){this.navWrapperElement&&(this.navWrapperElement.scrollLeft=this.navWrapperElement.scrollLeft+this.buttonsWidth,this.evaluateScrollButtons())}scrollLeft(){this.navWrapperElement&&(this.navWrapperElement.scrollLeft=this.navWrapperElement.scrollLeft-this.buttonsWidth,this.evaluateScrollButtons())}evaluateScrollButtons(){if(!this.navWrapperElement)return;const t=this.navWrapperElement.scrollLeft;this.showRightScroll=t<=this.scrollableWidth,this.showLeftScroll=t>0}initializeTabs(){this.tabElements=Array.from(this.host.children),this.tabElements.forEach((t=>{t.classList.remove("last"),t.classList.remove("first")})),this.tabElements[0].classList.add("first"),this.tabElements[this.tabElements.length-1].classList.add("last")}initializeSelectedTab(){void 0===this.selectedIndex?(this.addEventListenerToTabs(),this.tabElements[this.defaultSelectedIndex].setSelected(!0),this.selectedIndex=this.defaultSelectedIndex):this.tabElements[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 o(l,{key:"c560fe618bd56e56b83d98959a787d1d253b294a",role:"tablist",class:this.modeVariant?"tds-mode-variant-"+this.modeVariant:"",style:{"--padding-left":this.leftPadding+"px"}},o("div",{key:"4a2070625881a955ddc8b7d58869ac2908f1aeef",class:"wrapper",ref:t=>{this.navWrapperElement=t}},o("button",{key:"1df4f76b71faf9ea2e2f57c3da07c41315bcb59f","aria-label":this.tdsScrollLeftAriaLabel,class:"scroll-left-button "+(this.showLeftScroll?"show":""),onClick:()=>this.scrollLeft(),disabled:!this.showLeftScroll},o("tds-icon",{key:"9e0e80a1791ccf9fb28b1c597ce498808365f9c1",name:"chevron_left",size:"20px"})),o("slot",{key:"f702f8f7761d797cb121ce77f96276a615c211f9",onSlotchange:()=>this.handleSlotChange()}),o("button",{key:"735e8a5e76d32903ecf666e59a646d4bbf0b8c4d","aria-label":this.tdsScrollRightAriaLabel,class:"scroll-right-button "+(this.showRightScroll?"show":""),onClick:()=>this.scrollRight(),disabled:!this.showRightScroll},o("tds-icon",{key:"f0a59a1305c1ae93908ca307c03d57b9a171c4ec",name:"chevron_right",size:"20px"}))))}get host(){return this}static get watchers(){return{selectedIndex:[{handleSelectedIndexUpdate:0}]}}static get style(){return':host{box-sizing:border-box;--padding-left:0;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;padding-left:var(--padding-left)}:host .wrapper::-webkit-scrollbar{display:none}:host .scroll-right-button{right:0;z-index:1}:host .scroll-left-button{left:calc(0px - var(--padding-left));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-visible::before,:host .scroll-left-button:focus-visible::before{content:"";position:absolute;inset:3px;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:focus-visible,:host .scroll-left-button:focus-visible{outline:0}:host .scroll-right-button svg,:host .scroll-left-button svg{fill:var(--tds-folder-tabs-scroll-btn-color)}'}},[257,"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]},void 0,{selectedIndex:[{handleSelectedIndexUpdate:0}]}]);function a(){"undefined"!=typeof customElements&&["tds-inline-tabs","tds-icon"].forEach((s=>{switch(s){case"tds-inline-tabs":customElements.get(t(s))||customElements.define(t(s),h);break;case"tds-icon":customElements.get(t(s))||r()}}))}a();const n=h,c=a;export{n as TdsInlineTabs,c as defineCustomElement}