shoelace/src/internal/drag.ts

46 wiersze
1.5 KiB
TypeScript

2022-05-24 14:34:47 +00:00
interface DragOptions {
/** Callback that runs as dragging occurs. */
onMove: (x: number, y: number) => void;
/** Callback that runs when dragging stops. */
onStop: () => void;
2022-05-24 14:34:47 +00:00
/**
* When an initial event is passed, the first drag will be triggered immediately using the coordinates therein. This
* is useful when the drag is initiated by a mousedown/touchstart event but you want the initial "click" to activate
* a drag (e.g. positioning a handle initially at the click target).
*/
initialEvent: PointerEvent;
}
2022-12-06 16:48:57 +00:00
/** Begins listening for dragging. */
export function drag(container: HTMLElement, options?: Partial<DragOptions>) {
function move(pointerEvent: PointerEvent) {
const dims = container.getBoundingClientRect();
const defaultView = container.ownerDocument.defaultView!;
2023-11-20 16:57:54 +00:00
const offsetX = dims.left + defaultView.scrollX;
const offsetY = dims.top + defaultView.scrollY;
const x = pointerEvent.pageX - offsetX;
const y = pointerEvent.pageY - offsetY;
if (options?.onMove) {
options.onMove(x, y);
}
}
function stop() {
document.removeEventListener('pointermove', move);
document.removeEventListener('pointerup', stop);
if (options?.onStop) {
options.onStop();
}
}
document.addEventListener('pointermove', move, { passive: true });
document.addEventListener('pointerup', stop);
2022-05-24 14:34:47 +00:00
// If an initial event is set, trigger the first drag immediately
2022-12-07 22:40:46 +00:00
if (options?.initialEvent instanceof PointerEvent) {
2022-05-24 14:34:47 +00:00
move(options.initialEvent);
}
}