1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
class Stack{constructor(){this.items=[]}push(element){this.items.push(element)}pop(){return 0==this.items.length?"Underflow":this.items.pop()}peek(){return this.items[this.items.length-1]}}const popupStack=new Stack,smPopup=document.createElement("template");smPopup.innerHTML='\n<style>\n*{\n padding: 0;\n margin: 0;\n -webkit-box-sizing: border-box;\n box-sizing: border-box;\n} \n:host{\n position: fixed;\n display: -ms-grid;\n display: grid;\n z-index: 10;\n --width: 100%;\n --height: auto;\n --min-width: auto;\n --min-height: auto;\n --backdrop-background: rgba(0, 0, 0, 0.6);\n --border-radius: 0.8rem 0.8rem 0 0;\n}\n.popup-container{\n display: -ms-grid;\n display: grid;\n position: fixed;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n place-items: center;\n z-index: 10;\n touch-action: none;\n}\n:host(.stacked) .popup{\n -webkit-transform: scale(0.9) translateY(-2rem) !important;\n transform: scale(0.9) translateY(-2rem) !important;\n}\n.backdrop{\n position: absolute;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n background: var(--backdrop-background);\n -webkit-transition: opacity 0.3s;\n -o-transition: opacity 0.3s;\n transition: opacity 0.3s;\n}\n.popup{\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n -webkit-box-orient: vertical;\n -webkit-box-direction: normal;\n flex-direction: column;\n position: relative;\n -ms-flex-item-align: end;\n align-self: flex-end;\n -webkit-box-align: start;\n -ms-flex-align: start;\n align-items: flex-start;\n width: var(--width);\n min-width: var(--min-width);\n height: var(--height);\n min-height: var(--min-height);\n max-height: 90vh;\n border-radius: var(--border-radius);\n background: rgba(var(--background-color, (255,255,255)), 1);\n -webkit-box-shadow: 0 -1rem 2rem #00000020;\n box-shadow: 0 -1rem 2rem #00000020;\n}\n.container-header{\n display: -webkit-box;\n display: flex;\n width: 100%;\n touch-action: none;\n -webkit-box-align: center;\n -ms-flex-align: center;\n align-items: center;\n}\n.popup-top{\n display: -webkit-box;\n display: flex;\n width: 100%;\n}\n.popup-body{\n display: -webkit-box;\n display: flex;\n -webkit-box-orient: vertical;\n -webkit-box-direction: normal;\n -ms-flex-direction: column;\n flex-direction: column;\n -webkit-box-flex: 1;\n -ms-flex: 1;\n flex: 1;\n width: 100%;\n padding: var(--body-padding, 1.5rem);\n overflow-y: auto;\n}\n.hide{\n display:none;\n}\n@media screen and (min-width: 640px){\n :host{\n --border-radius: 0.5rem;\n }\n .popup{\n -ms-flex-item-align: center;\n -ms-grid-row-align: center;\n align-self: center;\n border-radius: var(--border-radius);\n height: var(--height);\n -webkit-box-shadow: 0 3rem 2rem -0.5rem #00000040;\n box-shadow: 0 3rem 2rem -0.5rem #00000040;\n }\n}\n@media screen and (max-width: 640px){\n .popup-top{\n -webkit-box-orient: vertical;\n -webkit-box-direction: normal;\n flex-direction: column;\n -webkit-box-align: center;\n align-items: center;\n }\n .handle{\n height: 0.3rem;\n width: 2rem;\n background: rgba(var(--text-color, (17,17,17)), .4);\n border-radius: 1rem;\n margin: 0.5rem 0;\n }\n}\n@media (any-hover: hover){\n ::-webkit-scrollbar{\n width: 0.5rem;\n }\n \n ::-webkit-scrollbar-thumb{\n background: rgba(var(--text-color, (17,17,17)), 0.3);\n border-radius: 1rem;\n &:hover{\n background: rgba(var(--text-color, (17,17,17))), 0.5);\n }\n }\n}\n</style>\n<div class="popup-container hide" role="dialog">\n <div part="backdrop" class="backdrop"></div>\n <div part="popup" class="popup">\n <div part="popup-header" class="popup-top">\n <div class="handle"></div>\n <slot name="header"></slot>\n </div>\n <div part="popup-body" class="popup-body">\n <slot></slot>\n </div>\n </div>\n</div>\n',customElements.define("sm-popup",class extends HTMLElement{constructor(){super(),this.attachShadow({mode:"open"}).append(smPopup.content.cloneNode(!0)),this.allowClosing=!1,this.isOpen=!1,this.offset=0,this.touchStartY=0,this.touchEndY=0,this.touchStartTime=0,this.touchEndTime=0,this.touchEndAnimation=void 0,this.focusable,this.autoFocus,this.mutationObserver,this.popupContainer=this.shadowRoot.querySelector(".popup-container"),this.backdrop=this.shadowRoot.querySelector(".backdrop"),this.dialogBox=this.shadowRoot.querySelector(".popup"),this.popupBodySlot=this.shadowRoot.querySelector(".popup-body slot"),this.popupHeader=this.shadowRoot.querySelector(".popup-top")}static get observedAttributes(){return["open"]}get open(){return this.isOpen}animateTo=(element,keyframes,options)=>{const anime=element.animate(keyframes,{...options,fill:"both"});return anime.finished.then((()=>{anime.commitStyles(),anime.cancel()})),anime};resumeScrolling=()=>{const scrollY=document.body.style.top;window.scrollTo(0,-1*parseInt(scrollY||"0")),document.body.style.overflow="",document.body.style.top="initial"};setStateOpen=()=>{if(!this.isOpen||this.offset){const animOptions={duration:300,easing:"ease"},initialAnimation=window.innerWidth>640?"scale(1.1)":`translateY(${this.offset?`${this.offset}px`:"100%"})`;this.animateTo(this.dialogBox,[{opacity:this.offset?1:0,transform:initialAnimation},{opacity:1,transform:"none"}],animOptions)}};show=(options={})=>{const{pinned:pinned=!1,payload:payload}=options;if(this.isOpen)return;const animOptions={duration:300,easing:"ease"};return this.payload=payload,popupStack.push({popup:this,permission:pinned}),popupStack.items.length>1&&this.animateTo(popupStack.items[popupStack.items.length-2].popup.shadowRoot.querySelector(".popup"),[{transform:"none"},{transform:window.innerWidth>640?"scale(0.95)":"translateY(-1.5rem)"}],animOptions),this.popupContainer.classList.remove("hide"),this.offset||(this.backdrop.animate([{opacity:0},{opacity:1}],animOptions).onfinish=()=>{this.resolveOpen(this.payload)},this.dispatchEvent(new CustomEvent("popupopened",{bubbles:!0,composed:!0,detail:{payload:this.payload}})),document.body.style.overflow="hidden",document.body.style.top=`-${window.scrollY}px`),this.setStateOpen(),this.pinned=pinned,this.isOpen=!0,setTimeout((()=>{const elementToFocus=this.autoFocus||this.focusable?.[0]||this.dialogBox;elementToFocus&&(elementToFocus.tagName.includes("-")?elementToFocus.focusIn():elementToFocus.focus())}),0),this.hasAttribute("open")||(this.setAttribute("open",""),this.addEventListener("keydown",this.detectFocus),this.resizeObserver.observe(this),this.mutationObserver.observe(this,{attributes:!0,childList:!0,subtree:!0}),this.popupHeader.addEventListener("touchstart",this.handleTouchStart,{passive:!0}),this.backdrop.addEventListener("mousedown",this.handleSoftDismiss)),{opened:new Promise((resolve=>{this.resolveOpen=resolve})),closed:new Promise((resolve=>{this.resolveClose=resolve}))}};hide=(options={})=>{const{payload:payload}=options,animOptions={duration:150,easing:"ease"};this.backdrop.animate([{opacity:1},{opacity:0}],animOptions),this.animateTo(this.dialogBox,[{opacity:1,transform:window.innerWidth>640?"none":`translateY(${this.offset?`${this.offset}px`:"0"})`},{opacity:0,transform:window.innerWidth>640?"scale(1.1)":"translateY(100%)"}],animOptions).finished.finally((()=>{this.popupContainer.classList.add("hide"),this.dialogBox.style="",this.removeAttribute("open"),this.forms.length&&this.forms.forEach((form=>form.reset())),this.dispatchEvent(new CustomEvent("popupclosed",{bubbles:!0,composed:!0,detail:{payload:payload||this.payload}})),this.resolveClose(payload||this.payload),this.isOpen=!1})),popupStack.pop(),popupStack.items.length?this.animateTo(popupStack.items[popupStack.items.length-1].popup.shadowRoot.querySelector(".popup"),[{transform:window.innerWidth>640?"scale(0.95)":"translateY(-1.5rem)"},{transform:"none"}],animOptions):this.resumeScrolling(),this.resizeObserver.disconnect(),this.mutationObserver.disconnect(),this.removeEventListener("keydown",this.detectFocus),this.popupHeader.removeEventListener("touchstart",this.handleTouchStart,{passive:!0}),this.backdrop.removeEventListener("mousedown",this.handleSoftDismiss)};handleTouchStart=e=>{this.offset=0,this.popupHeader.addEventListener("touchmove",this.handleTouchMove,{passive:!0}),this.popupHeader.addEventListener("touchend",this.handleTouchEnd,{passive:!0}),this.touchStartY=e.changedTouches[0].clientY,this.touchStartTime=e.timeStamp};handleTouchMove=e=>{this.touchStartY<e.changedTouches[0].clientY&&(this.offset=e.changedTouches[0].clientY-this.touchStartY,this.touchEndAnimation=window.requestAnimationFrame((()=>{this.dialogBox.style.transform=`translateY(${this.offset}px)`})))};handleTouchEnd=e=>{if(this.touchEndTime=e.timeStamp,cancelAnimationFrame(this.touchEndAnimation),this.touchEndY=e.changedTouches[0].clientY,this.threshold=.3*this.dialogBox.getBoundingClientRect().height,this.touchEndTime-this.touchStartTime>200)if(this.touchEndY-this.touchStartY>this.threshold){if(this.pinned)return void this.setStateOpen();this.hide()}else this.setStateOpen();else if(this.touchEndY>this.touchStartY){if(this.pinned)return void this.setStateOpen();this.hide()}this.popupHeader.removeEventListener("touchmove",this.handleTouchMove,{passive:!0}),this.popupHeader.removeEventListener("touchend",this.handleTouchEnd,{passive:!0})};detectFocus=e=>{if("Tab"===e.key){if(!this.focusable.length)return;if(!this.firstFocusable)for(let i=0;i<this.focusable.length;i++)if(!this.focusable[i].disabled){this.firstFocusable=this.focusable[i];break}if(!this.lastFocusable)for(let i=this.focusable.length-1;i>=0;i--)if(!this.focusable[i].disabled){this.lastFocusable=this.focusable[i];break}e.shiftKey&&document.activeElement===this.firstFocusable?(e.preventDefault(),this.lastFocusable.tagName.includes("SM-")?this.lastFocusable.focusIn():this.lastFocusable.focus()):e.shiftKey||document.activeElement!==this.lastFocusable||(e.preventDefault(),this.firstFocusable.tagName.includes("SM-")?this.firstFocusable.focusIn():this.firstFocusable.focus())}};updateFocusableList=()=>{this.focusable=this.querySelectorAll('sm-button:not([disabled]), button:not([disabled]), [href], sm-input, input:not([readonly]), sm-select, select, sm-checkbox, sm-textarea, textarea, [tabindex]:not([tabindex="-1"])'),this.autoFocus=this.querySelector("[autofocus]"),this.firstFocusable=null,this.lastFocusable=null};handleSoftDismiss=()=>{this.pinned?this.dialogBox.animate([{transform:"translateX(-1rem)"},{transform:"translateX(1rem)"},{transform:"translateX(-0.5rem)"},{transform:"translateX(0.5rem)"},{transform:"translateX(0)"}],{duration:300,easing:"ease"}):this.hide()};debounce=(callback,wait)=>{let timeoutId=null;return(...args)=>{window.clearTimeout(timeoutId),timeoutId=window.setTimeout((()=>{callback.apply(null,args)}),wait)}};connectedCallback(){this.popupBodySlot.addEventListener("slotchange",this.debounce((()=>{this.forms=this.querySelectorAll("sm-form"),this.updateFocusableList()}),0)),this.resizeObserver=new ResizeObserver((entries=>{entries.forEach((entry=>{if(entry.contentBoxSize){const contentBoxSize=Array.isArray(entry.contentBoxSize)?entry.contentBoxSize[0]:entry.contentBoxSize;this.threshold=.3*contentBoxSize.blockSize.height}else this.threshold=.3*entry.contentRect.height}))})),this.mutationObserver=new MutationObserver((entries=>{this.updateFocusableList()}))}disconnectedCallback(){this.resizeObserver.disconnect(),this.mutationObserver.disconnect(),this.removeEventListener("keydown",this.detectFocus),this.popupHeader.removeEventListener("touchstart",this.handleTouchStart,{passive:!0}),this.backdrop.removeEventListener("mousedown",this.handleSoftDismiss)}attributeChangedCallback(name){"open"===name&&this.hasAttribute("open")&&this.show()}}); |