@carbon/utilities
Version:
Utilities and helpers to drive consistency across software products using the Carbon Design System
2 lines (1 loc) • 1.89 kB
JavaScript
;var m=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var L=(e,t)=>{for(var r in t)m(e,r,{get:t[r],enumerable:!0})},k=(e,t,r,u)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of y(t))!g.call(e,a)&&a!==r&&m(e,a,{get:()=>t[a],enumerable:!(u=b(t,a))||u.enumerable});return e};var w=e=>k(m({},"__esModule",{value:!0}),e);var T={};L(T,{makeDraggable:()=>D});module.exports=w(T);const D=({el:e,dragHandle:t,focusableDragHandle:r,dragStep:u,shiftDragStep:a})=>{t?(t.style.cursor="move",e.style.cursor="default"):e.style.cursor="move";let s=!1,v=0,d=0;const i=(o,n)=>{const E={detail:n,bubbles:!0};e.dispatchEvent(new CustomEvent(o,E))},l=o=>{if(o.key==="Enter"&&(s=!s),s?i("dragstart",{keyboard:!0}):i("dragend",{keyboard:!0}),!s)return;const n=o.shiftKey?a??32:u??8;switch(o.key){case"Enter":case" ":o.preventDefault();break;case"ArrowLeft":e.style.left=`${e.offsetLeft-n}px`;break;case"ArrowRight":e.style.left=`${e.offsetLeft+n}px`;break;case"ArrowUp":e.style.top=`${e.offsetTop-n}px`;break;case"ArrowDown":e.style.top=`${e.offsetTop+n}px`;break}},f=o=>{const n=o.target;!(n instanceof Node)||!(t?t.contains(n):e.contains(n))||(v=o.clientX-e.offsetLeft,d=o.clientY-e.offsetTop,s=!0,i("dragstart",{mouse:!0}),document.addEventListener("mousemove",c),document.addEventListener("mouseup",p,{once:!0}))},c=o=>{s&&(e.style.left=`${o.clientX-v}px`,e.style.top=`${o.clientY-d}px`)},p=()=>{s&&(s=!1,i("dragend",{mouse:!0}),document.removeEventListener("mousemove",c))};return t?t.addEventListener("mousedown",f):e.addEventListener("mousedown",f),r?.addEventListener("keydown",l),{cleanup:()=>{t?t.removeEventListener("mousedown",f):e.removeEventListener("mousedown",f),r?.removeEventListener("keydown",l),document.removeEventListener("mousemove",c),document.removeEventListener("mouseup",p)}}};