UNPKG

@scania/tegel

Version:
1 lines 12.4 kB
import{t,p as o,H as s,c as d,h as a,a as i}from"./index.js";import{h as e}from"./p-DDX6uFcm.js";import{g as r}from"./p-Cn4f8w1e.js";import{d as l}from"./p-CIt4YhvL.js";const n=o(class extends s{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.tdsClose=d(this,"tdsClose",7),this.tdsOpen=d(this,"tdsOpen",7),this.prevent=!1,this.size="md",this.actionsPosition="static",this.closable=!0,this.tdsAlertDialog="dialog",this.isShown=!1,this.activeElementIndex=0,this.handleClose=t=>{this.tdsClose.emit(t).defaultPrevented||(this.isShown=!1,this.returnFocusOnClose())},this.handleShow=()=>{this.tdsOpen.emit().defaultPrevented||(this.isShown=!0,this.onOpen())},this.handleOverlayClick=t=>{const o=t.composedPath()[0];("tds-modal-close"===o.classList[0]||"tds-modal-backdrop"===o.classList[0]&&!1===this.prevent)&&this.handleClose(t)},this.handleReferenceElementClick=t=>{this.isShown?this.handleClose(t):this.handleShow()},this.setShowButton=()=>{var t;if(this.selector||this.referenceEl){const o=null!==(t=this.referenceEl)&&void 0!==t?t:this.selector?document.querySelector(this.selector):null;o&&this.initializeReferenceElement(o)}},this.initializeReferenceElement=t=>{t&&t.addEventListener("click",this.handleReferenceElementClick)}}async showModal(){this.isShown=!0,this.onOpen()}async closeModal(){this.isShown=!1,this.returnFocusOnClose()}async isOpen(){return this.isShown}handleShowPropChange(t,o){t!==o&&void 0!==t&&(this.isShown=t,t?this.onOpen():this.returnFocusOnClose())}connectedCallback(){void 0===this.closable&&(this.closable=!0),void 0!==this.show&&(this.isShown=this.show),this.initializeModal(),this.header&&e("header",this.host)&&console.warn("Tegel Modal component: Using both header prop and header slot might break modal's design. Please use just one of them. "),this.selector||this.referenceEl||console.warn('Tegel Modal: Missing focus origin. Please provide either a "referenceEl" or a "selector" to ensure focus returns to the element that opened the modal. If the modal is opened programmatically, this message can be ignored.')}componentWillLoad(){this.initializeModal()}disconnectedCallback(){this.cleanupModal()}async initializeModal(){this.setDismissButtons(),this.setShowButton()}async cleanupModal(){var t;if(this.selector||this.referenceEl){const o=null!==(t=this.referenceEl)&&void 0!==t?t:this.selector?document.querySelector(this.selector):null;o&&o.removeEventListener("click",this.handleReferenceElementClick)}this.host.querySelectorAll("[data-dismiss-modal]").forEach((t=>{t.removeEventListener("click",this.handleClose)}))}returnFocusOnClose(){var t;const o=null!==(t=this.referenceEl)&&void 0!==t?t:this.selector?document.querySelector(this.selector):null;if(!o)return;const s=["BUTTON","A","INPUT"],d=s.includes(o.tagName)?o:o.querySelector(s.join(","));d&&(d.classList.remove("active"),d.focus())}getFocusableElements(){var t,o;const s='a[href],button:not([disabled]),textarea:not([disabled]),input:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])';return[...Array.from(null!==(o=null===(t=this.host.shadowRoot)||void 0===t?void 0:t.querySelectorAll(s))&&void 0!==o?o:[]),...Array.from(this.host.querySelectorAll(s))]}resetScrollPosition(){var t;const o=this.host.shadowRoot,s=null!==(t=null==o?void 0:o.querySelector(".tds-modal__actions-sticky .body"))&&void 0!==t?t:null==o?void 0:o.querySelector(".tds-modal");null==s||s.scrollTo(0,0)}focusFirstElement(){var t,o;const s='a[href],button:not([disabled]),textarea:not([disabled]),input:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])',d=Array.from(this.host.querySelectorAll(s));if(d.length>0)d[0].focus(),this.activeElementIndex=this.getFocusableElements().indexOf(d[0]);else{const d=Array.from(null!==(o=null===(t=this.host.shadowRoot)||void 0===t?void 0:t.querySelectorAll(s))&&void 0!==o?o:[]);d.length>0&&(d[0].focus(),this.activeElementIndex=0)}}onOpen(){this.focusFirstElement(),requestAnimationFrame((()=>{this.resetScrollPosition()}))}handleFocusTrap(t){if("Escape"===t.key&&this.isShown&&!this.prevent)return void this.handleClose(t);if(!this.isShown)return;if("Tab"!==t.key)return;const o=this.getFocusableElements();0!==o.length&&(t.preventDefault(),t.shiftKey&&(this.activeElementIndex-=1,-1===this.activeElementIndex&&(this.activeElementIndex=o.length-1)),t.shiftKey||(this.activeElementIndex+=1,this.activeElementIndex===o.length&&(this.activeElementIndex=0)),o[this.activeElementIndex].focus())}setDismissButtons(){this.host.querySelectorAll("[data-dismiss-modal]").forEach((t=>{t.addEventListener("click",this.handleClose)}))}render(){const t=e("header",this.host),o=e("actions",this.host),s=this.header?"tds-modal-header-"+r():void 0,d="tds-modal-body-"+r();return a(i,{key:"627470cc4fd39a05084149c125b34e8c41fca76b",role:this.tdsAlertDialog,"aria-modal":"true","aria-describedby":d,"aria-labelledby":s,class:{show:this.isShown,hide:!this.isShown},onClick:t=>this.handleOverlayClick(t)},a("div",{key:"64cbcfcc22244a706d448dab7834a83173943d85",class:"tds-modal-backdrop"}),a("div",{key:"ab7af811011361c8b5fb311dff6fd12143ad9fa4",class:`tds-modal tds-modal__actions-${this.actionsPosition} tds-modal-${this.size}`,tabindex:"-1"},a("div",{key:"4aa8706b05c2b2dfd286368b0f1dd7e4eaaec10f",id:s,class:"header"},this.header&&a("div",{key:"a24a3998f441a80a5ba39082e1b8c6a4568424a1",class:"header-text"},this.header),t&&a("slot",{key:"5aba912426f4743d5c136230cfbccbc3730b2adf",name:"header"}),this.closable&&a("button",{key:"14da162e80c29bdaad47990af70b533a1a2e0fc2",class:"tds-modal-close","aria-label":"close",onClick:t=>this.handleClose(t)},a("tds-icon",{key:"f28f4aedb9e432849803a5961db568db4c157003",name:"cross",size:"20px"}))),a("div",{key:"928a57c57f5bbbcd6411da9378d2001c9befb855",id:d,class:"body"},a("slot",{key:"800c2deda52545ce5ca457c3274734df18a1bd57",name:"body"})),o&&a("slot",{key:"d453d7e1fe3062aa238ca19883345aa09c0b87c5",name:"actions"})))}get host(){return this}static get watchers(){return{show:[{handleShowPropChange:0}]}}static get style(){return":host,:root{--tds-scrollbar-width-standard:thin;--tds-scrollbar-width:10px;--tds-scrollbar-height:10px;--tds-scrollbar-thumb-border-width:3px;--tds-scrollbar-thumb-border-hover-width:2px}body{scrollbar-width:thin}.tds-modal{box-sizing:border-box;box-shadow:var(--tds-modal-box-shadow);background-color:var(--background-elevation-layer-02);margin:auto;position:relative;border-radius:var(--radius-narrow);max-height:85vh;overflow-y:auto;pointer-events:auto}.tds-modal *{box-sizing:border-box}.tds-modal:hover::-webkit-scrollbar-thumb{border:var(--tds-scrollbar-thumb-border-hover-width) solid transparent;background-clip:padding-box}.tds-modal::-webkit-scrollbar{width:var(--tds-scrollbar-width)}.tds-modal::-webkit-scrollbar-track{background:var(--tds-scrollbar-track-color)}.tds-modal::-webkit-scrollbar-thumb{border-radius:40px;background:var(--tds-scrollbar-thumb-color);border:var(--tds-scrollbar-thumb-border-width) solid transparent;background-clip:padding-box}.tds-modal::-webkit-scrollbar-button{height:0;width:0}@supports not selector(::-webkit-scrollbar){.tds-modal{scrollbar-color:var(--tds-scrollbar-thumb-color) var(--tds-scrollbar-track-color);scrollbar-width:var(--tds-scrollbar-width-standard)}}.tds-modal__actions-sticky{overflow:hidden;display:flex;flex-direction:column}.tds-modal__actions-sticky .body{font-family:var(--body-01-font-family);font-size:var(--body-01-font-size);line-height:var(--body-01-line-height);font-weight:var(--body-01-font-weight);letter-spacing:var(--body-01-letter-spacing);text-transform:var(--body-01-text-transform);max-height:calc(85vh - 36px);overflow-y:auto}.tds-modal__actions-sticky slot[name=actions]{bottom:-1px;left:0;right:0;background-color:var(--background-elevation-layer-02);padding:24px 16px 16px;display:flex;gap:16px}.tds-modal__actions-static slot[name=actions]{background-color:var(--background-elevation-layer-02);display:flex;gap:16px;padding:24px 16px 16px}@media (min-width: 320px){.tds-modal-xs{width:100%}.tds-modal-sm{width:100%}.tds-modal-md{width:100%}.tds-modal-lg{width:100%}}@media (min-width: 672px){.tds-modal-xs{width:50%}.tds-modal-sm{width:62.5%}.tds-modal-md{width:75%}.tds-modal-lg{width:100%}}@media (min-width: 1056px){.tds-modal-xs{width:31.25%}.tds-modal-sm{width:43.75%}.tds-modal-md{width:62.5%}.tds-modal-lg{width:75%}}@media (min-width: 1312px){.tds-modal-xs{width:31.25%}.tds-modal-sm{width:37.5%}.tds-modal-md{width:62.5%}.tds-modal-lg{width:75%}}@media (min-width: 1584px){.tds-modal-xs{width:25%}.tds-modal-sm{width:37.5%}.tds-modal-md{width:50%}.tds-modal-lg{width:75%}}@media (max-width: 320px){.tds-modal-md,.tds-modal-lg,.tds-modal-sm{height:100%}.tds-modal-md slot[name=actions]::slotted(*),.tds-modal-lg slot[name=actions]::slotted(*),.tds-modal-sm slot[name=actions]::slotted(*){display:flex}}.header{display:flex;padding:16px;position:sticky;top:0;background-color:var(--background-elevation-layer-02);z-index:1}.header,slot[name=header]::slotted(*){font-family:var(--headline-05-font-family);font-size:var(--headline-05-font-size);line-height:var(--headline-05-line-height);font-weight:var(--headline-05-font-weight);letter-spacing:var(--headline-05-letter-spacing);text-transform:var(--headline-05-text-transform);color:var(--foreground-text-strong);margin:0;flex:1}.body{font-family:var(--body-01-font-family);font-size:var(--body-01-font-size);line-height:var(--body-01-line-height);font-weight:var(--body-01-font-weight);letter-spacing:var(--body-01-letter-spacing);text-transform:var(--body-01-text-transform);color:var(--foreground-text-strong);overflow-y:visible;padding:0 16px 16px}.body:hover::-webkit-scrollbar-thumb{border:var(--tds-scrollbar-thumb-border-hover-width) solid transparent;background-clip:padding-box}.body::-webkit-scrollbar{width:var(--tds-scrollbar-width)}.body::-webkit-scrollbar-track{background:var(--tds-scrollbar-track-color)}.body::-webkit-scrollbar-thumb{border-radius:40px;background:var(--tds-scrollbar-thumb-color);border:var(--tds-scrollbar-thumb-border-width) solid transparent;background-clip:padding-box}.body::-webkit-scrollbar-button{height:0;width:0}@supports not selector(::-webkit-scrollbar){.body{scrollbar-color:var(--tds-scrollbar-thumb-color) var(--tds-scrollbar-track-color);scrollbar-width:var(--tds-scrollbar-width-standard)}}.tds-modal-backdrop{box-sizing:border-box;position:fixed;top:0;right:0;bottom:0;left:0;background-color:var(--tds-modal-backdrop-background);pointer-events:auto}.tds-modal-backdrop *{box-sizing:border-box}button.tds-modal-close{margin:0 0 auto auto;background-color:transparent;border:0;padding:0;appearance:unset}.tds-modal-close{display:inline-block;height:auto;color:var(--foreground-text-strong);cursor:pointer}.tds-modal-close:focus{outline:2px solid var(--tds-focus-outline-color);box-shadow:0 0 0 1px var(--tds-white);outline-offset:1px;z-index:1}@media (min-width: 320px){.tds-modal-close{margin-left:16px}}@media (min-width: 1056px){.tds-modal-close{margin-left:48px}}.tds-modal-close-btn{display:inline-block;height:auto;background-repeat:no-repeat;cursor:pointer}@media (min-width: 320px){.tds-modal-close-btn{margin-left:16px}}@media (min-width: 1056px){.tds-modal-close-btn{margin-left:48px}}.tds-modal-close-btn svg{fill:var(--foreground-text-strong)}.tds-modal-overflow{overflow:hidden}:host{box-sizing:border-box;position:fixed;top:0;right:0;bottom:0;left:0;padding:0 16px;z-index:700;pointer-events:none}:host *{box-sizing:border-box}:host .tds-modal-close{border:none;background-color:transparent}:host .tds-modal-close-btn{border:none;background-color:transparent}@media (max-width: 320px){:host{padding:0}}:host.show{display:flex}:host.hide{display:none}:host(.show){display:flex}:host(.hide){display:none}"}},[257,"tds-modal",{header:[1],prevent:[4],size:[1],actionsPosition:[1,"actions-position"],selector:[1],referenceEl:[16],show:[4],closable:[4],tdsAlertDialog:[1,"tds-alert-dialog"],isShown:[32],activeElementIndex:[32],showModal:[64],closeModal:[64],isOpen:[64],initializeModal:[64],cleanupModal:[64]},[[10,"keydown","handleFocusTrap"]],{show:[{handleShowPropChange:0}]}]);function h(){"undefined"!=typeof customElements&&["tds-modal","tds-icon"].forEach((o=>{switch(o){case"tds-modal":customElements.get(t(o))||customElements.define(t(o),n);break;case"tds-icon":customElements.get(t(o))||l()}}))}h();const c=n,b=h;export{c as TdsModal,b as defineCustomElement}