standard-ui/components/dist/popup.min.js
2021-08-06 15:47:03 +05:30

1 line
8.8 KiB
JavaScript

const 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 --accent-color: #4d2588;\n --text-color: 17, 17, 17;\n --background-color: 255, 255, 255;\n --width: 100%;\n --height: auto;\n --min-width: auto;\n --min-height: auto;\n --body-padding: 1.5rem;\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 background: var(--backdrop-background);\n -webkit-transition: opacity 0.3s;\n -o-transition: opacity 0.3s;\n transition: opacity 0.3s;\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.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 -webkit-transform: scale(1) translateY(100%);\n transform: scale(1) translateY(100%);\n -webkit-transition: -webkit-transform 0.3s;\n transition: -webkit-transform 0.3s;\n -o-transition: transform 0.3s;\n transition: transform 0.3s, -webkit-transform 0.3s;\n transition: transform 0.3s;\n background: rgba(var(--background-color), 1);\n -webkit-box-shadow: 0 -1rem 2rem #00000020;\n box-shadow: 0 -1rem 2rem #00000020;\n content-visibility: auto;\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);\n overflow-y: auto;\n}\n.hide{\n opacity: 0;\n pointer-events: none;\n visiblity: none;\n}\n@media screen and (min-width: 640px){\n :host{\n --border-radius: 0.4rem;\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-transform: scale(1) translateY(3rem);\n transform: scale(1) translateY(3rem);\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), .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), 0.3);\n border-radius: 1rem;\n &:hover{\n background: rgba(var(--text-color), 0.5);\n }\n }\n}\n</style>\n<div part="background" class="popup-container hide" role="dialog">\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.pinned=!1,this.popupStack,this.offset,this.touchStartY=0,this.touchEndY=0,this.touchStartTime=0,this.touchEndTime=0,this.touchEndAnimataion,this.popupContainer=this.shadowRoot.querySelector(".popup-container"),this.popup=this.shadowRoot.querySelector(".popup"),this.popupBodySlot=this.shadowRoot.querySelector(".popup-body slot"),this.popupHeader=this.shadowRoot.querySelector(".popup-top"),this.resumeScrolling=this.resumeScrolling.bind(this),this.show=this.show.bind(this),this.hide=this.hide.bind(this),this.handleTouchStart=this.handleTouchStart.bind(this),this.handleTouchMove=this.handleTouchMove.bind(this),this.handleTouchEnd=this.handleTouchEnd.bind(this),this.movePopup=this.movePopup.bind(this)}static get observedAttributes(){return["open"]}get open(){return this.isOpen}resumeScrolling(){const t=document.body.style.top;window.scrollTo(0,-1*parseInt(t||"0")),setTimeout(()=>{document.body.style.overflow="auto",document.body.style.top="initial"},300)}show(t={}){const{pinned:e=!1,popupStack:n}=t;return n&&(this.popupStack=n),this.popupStack&&!this.hasAttribute("open")&&(this.popupStack.push({popup:this,permission:e}),this.popupStack.items.length>1&&this.popupStack.items[this.popupStack.items.length-2].popup.classList.add("stacked"),this.dispatchEvent(new CustomEvent("popupopened",{bubbles:!0,detail:{popup:this,popupStack:this.popupStack}})),this.setAttribute("open",""),this.pinned=e,this.isOpen=!0),this.popupContainer.classList.remove("hide"),this.popup.style.transform="none",document.body.style.overflow="hidden",document.body.style.top=`-${window.scrollY}px`,this.popupStack}hide(){window.innerWidth<640?this.popup.style.transform="translateY(100%)":this.popup.style.transform="translateY(3rem)",this.popupContainer.classList.add("hide"),this.removeAttribute("open"),void 0!==this.popupStack?(this.popupStack.pop(),this.popupStack.items.length?this.popupStack.items[this.popupStack.items.length-1].popup.classList.remove("stacked"):this.resumeScrolling()):this.resumeScrolling(),this.forms.length&&setTimeout(()=>{this.forms.forEach(t=>t.reset())},300),setTimeout(()=>{this.dispatchEvent(new CustomEvent("popupclosed",{bubbles:!0,detail:{popup:this,popupStack:this.popupStack}})),this.isOpen=!1},300)}handleTouchStart(t){this.touchStartY=t.changedTouches[0].clientY,this.popup.style.transition="transform 0.1s",this.touchStartTime=t.timeStamp}handleTouchMove(t){this.touchStartY<t.changedTouches[0].clientY&&(this.offset=t.changedTouches[0].clientY-this.touchStartY,this.touchEndAnimataion=window.requestAnimationFrame(()=>this.movePopup()))}handleTouchEnd(t){if(this.touchEndTime=t.timeStamp,cancelAnimationFrame(this.touchEndAnimataion),this.touchEndY=t.changedTouches[0].clientY,this.popup.style.transition="transform 0.3s",this.threshold=.3*this.popup.getBoundingClientRect().height,this.touchEndTime-this.touchStartTime>200)if(this.touchEndY-this.touchStartY>this.threshold){if(this.pinned)return void this.show();this.hide()}else this.show();else if(this.touchEndY>this.touchStartY){if(this.pinned)return void this.show();this.hide()}}movePopup(){this.popup.style.transform=`translateY(${this.offset}px)`}connectedCallback(){this.popupBodySlot.addEventListener("slotchange",()=>{this.forms=this.querySelectorAll("sm-form")}),this.popupContainer.addEventListener("mousedown",t=>{t.target!==this.popupContainer||this.pinned||(this.pinned?this.show():this.hide())});const t=new ResizeObserver(t=>{for(let e of t)if(e.contentBoxSize){const t=Array.isArray(e.contentBoxSize)?e.contentBoxSize[0]:e.contentBoxSize;this.threshold=.3*t.blockSize.height}else this.threshold=.3*e.contentRect.height});t.observe(this),this.popupHeader.addEventListener("touchstart",t=>{this.handleTouchStart(t)},{passive:!0}),this.popupHeader.addEventListener("touchmove",t=>{this.handleTouchMove(t)},{passive:!0}),this.popupHeader.addEventListener("touchend",t=>{this.handleTouchEnd(t)},{passive:!0})}disconnectedCallback(){this.popupHeader.removeEventListener("touchstart",this.handleTouchStart,{passive:!0}),this.popupHeader.removeEventListener("touchmove",this.handleTouchMove,{passive:!0}),this.popupHeader.removeEventListener("touchend",this.handleTouchEnd,{passive:!0}),resizeObserver.unobserve()}attributeChangedCallback(t,e,n){"open"===t&&this.hasAttribute("open")&&this.show()}});