UNPKG

@sandlada/mdc

Version:

@sandlada/mdc is an open source component library that follows the Material Design 3 design specifications.

7 lines 2.4 kB
/** * @license * Copyright 2025 Kai-Orion & Sandlada * SPDX-License-Identifier: MIT */ var e=class{constructor(e,t){this.position={x:0,y:0},this.isDragging=!1,this.initialX=0,this.initialY=0,this.offsetX=0,this.offsetY=0,this.dragBounds={minX:-1/0,maxX:1/0,minY:-1/0,maxY:1/0},this.host=e,this.position.x=t?.initialX??0,this.position.y=t?.initialY??0,this.positioningMode=t?.positioningMode??`fixed`,this.boundaryProtection=t?.boundaryProtection??!1,this.boundDrag=this.drag.bind(this),this.boundEndDrag=this.endDrag.bind(this)}hostConnected(){}startDrag(e){e.preventDefault();let{clientX:t,clientY:n}=this.getCoordinates(e);this.isDragging=!0,this.initialX=t,this.initialY=n,this.offsetX=this.position.x,this.offsetY=this.position.y,this.boundaryProtection&&this.host instanceof HTMLElement?this.calculateBounds():this.dragBounds={minX:-1/0,maxX:1/0,minY:-1/0,maxY:1/0},window.addEventListener(`mousemove`,this.boundDrag,{passive:!1}),window.addEventListener(`touchmove`,this.boundDrag,{passive:!1}),window.addEventListener(`mouseup`,this.boundEndDrag),window.addEventListener(`touchend`,this.boundEndDrag),this.host.requestUpdate()}calculateBounds(){let e=this.host.offsetWidth,t=this.host.offsetHeight,n=window.innerWidth,r=window.innerHeight;if(this.positioningMode===`absolute`&&this.host.offsetParent){let e=this.host.offsetParent;n=e.clientWidth,r=e.clientHeight}this.dragBounds={minX:0,maxX:Math.max(0,n-e),minY:0,maxY:Math.max(0,r-t)}}drag(e){if(!this.isDragging)return;e.preventDefault();let{clientX:t,clientY:n}=this.getCoordinates(e),r=t-this.initialX,i=n-this.initialY,a=this.offsetX+r,o=this.offsetY+i;this.boundaryProtection&&(a=Math.max(this.dragBounds.minX,Math.min(a,this.dragBounds.maxX)),o=Math.max(this.dragBounds.minY,Math.min(o,this.dragBounds.maxY))),this.position.x=a,this.position.y=o,this.host.requestUpdate()}endDrag(){this.isDragging&&(this.isDragging=!1,window.removeEventListener(`mousemove`,this.boundDrag),window.removeEventListener(`touchmove`,this.boundDrag),window.removeEventListener(`mouseup`,this.boundEndDrag),window.removeEventListener(`touchend`,this.boundEndDrag),this.host.requestUpdate())}getCoordinates(e){if(e instanceof TouchEvent){let t=e.touches[0]||e.changedTouches[0];return{clientX:t.clientX,clientY:t.clientY}}return{clientX:e.clientX,clientY:e.clientY}}};export{e as DraggableModalController}; //# sourceMappingURL=draggable-modal-controller.js.map