dragIntent()
Start dragging after clear movement. Drag threshold and capture.
Try it
Move the task handle slightly, then drag deliberately. Dragging starts after 8 px. Focus the same handle and use arrow keys to move it without dragging.
Move more than 8 px to start dragging.
<div id="your-component">
<div class="drag-track"><button class="drag-handle" aria-label="Move task card. Drag or use arrow keys."><span aria-hidden="true">⠿</span> Review design</button></div><p class="demo-status" role="status">Move more than 8 px to start dragging.</p>
</div>import * as kit from 'subtly';
const el = document.querySelector('#your-component');
const listeners = new AbortController();
const { signal } = listeners;
const status = text => {
el.querySelector('.demo-status').textContent = text;
};
// Local simulation only. Replace with your real data operation.
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
function setup() {
const handle = el.querySelector('button');
let x = 0, y = 0, startX = 0, startY = 0;
function move(nextX, nextY) {
x = Math.max(-55, Math.min(55, nextX));
y = Math.max(-35, Math.min(35, nextY));
handle.style.transform = `translate(${x}px, ${y}px)`;
}
const stop = kit.dragIntent(handle, {
threshold: 8,
onStart() { startX = x; startY = y; status('Dragging the task.'); },
onMove({ dx, dy }) { move(startX + dx, startY + dy); },
onEnd() { status(`Task position: ${x}px across, ${y}px down.`); }
});
handle.addEventListener('keydown', event => {
const delta = { ArrowLeft: [-5, 0], ArrowRight: [5, 0], ArrowUp: [0, -5], ArrowDown: [0, 5] }[event.key];
if (!delta) return;
event.preventDefault(); move(x + delta[0], y + delta[1]);
status(`Task moved with keyboard: ${x}px across, ${y}px down.`);
}, { signal });
return stop;
}
const stop = setup();
// Call when this component is removed.
function cleanup() {
listeners.abort();
stop?.();
}Add it to your project
- Copy the HTML. Use the HTML tab above and place the markup inside a dedicated component root. Layout classes belong to this documentation website; use your own styles.
- Connect the behavior. Copy the JavaScript tab, update
#your-componentand initialize it after the elements exist. Replace simulated operations with your own data. - Clean up on unmount. Call the example’s
cleanup()when the component is removed. It aborts application listeners and releases the utility’s timers or observers.
The preview and JavaScript tab use the same utility wiring. status() writes plain text into the example’s status element. wait() is a simulated local delay. For examples with data-ix attributes, start the declarative runtime once at application level, as shown in the code. Use your framework guide to place setup and cleanup correctly.
API and lifecycle
Attach the helper to a dedicated handle. A primary pointer press starts a session; onStart runs only after movement passes the threshold, which defaults to 5 px. onMove receives viewport-relative dx and dy values.
- Interface
threshold, onStart/onMove/onEnd; returns cleanup.- Lifecycle
- The returned cleanup removes pointer listeners and releases capture. onEnd receives cancelled=true if capture is lost, the pointer is cancelled, or cleanup ends an active drag.
Accessibility and safety
Implementation notes
Provide keyboard alternatives; touch-action:none blocks scroll on the handle.
Dragging needs a keyboard alternative such as move buttons or arrow-key controls. Choose touch-action on the handle deliberately: none prevents touch scrolling there.
What to check
Check small movements do not drag. Test pointer cancellation, releasing outside the handle, touch scrolling and equivalent movement using a keyboard.
View documented module source
/** Drag threshold and pointer capture. Attach to a dedicated drag handle.
* Provide keyboard move controls separately; set touch-action on handle
* intentionally (none blocks scrolling). Callbacks receive viewport deltas.
* onEnd receives cancelled=true on lost capture or pointer cancellation. */
export function dragIntent(handle,{threshold=5,onStart,onMove,onEnd}={}) {
let session;
function down(e) { if(session || !e.isPrimary || e.button!==0) return; session={id:e.pointerId,x:e.clientX,y:e.clientY,active:false}; handle.setPointerCapture(e.pointerId); }
function move(e) { if(session?.id!==e.pointerId)return; const dx=e.clientX-session.x,dy=e.clientY-session.y; if(!session.active && Math.hypot(dx,dy)>=threshold){session.active=true;onStart?.(e);} if(session.active)onMove?.({dx,dy,event:e}); }
function end(e) { if(session?.id!==e.pointerId)return; const old=session;session=null;if(handle.hasPointerCapture(old.id))handle.releasePointerCapture(old.id);if(old.active)onEnd?.({cancelled:e.type!=='pointerup',event:e}); }
const events={pointerdown:down,pointermove:move,pointerup:end,pointercancel:end,lostpointercapture:end};
for(const [name,fn] of Object.entries(events))handle.addEventListener(name,fn);
return ()=>{for(const [name,fn] of Object.entries(events))handle.removeEventListener(name,fn);if(session){const old=session;session=null;if(handle.hasPointerCapture(old.id))handle.releasePointerCapture(old.id);if(old.active)onEnd?.({cancelled:true});}};
}