Internal utilities shared between @dnd-kit packages providing React hooks, coordinate manipulation, and DOM utilities.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Types and utilities for handling x,y coordinates in drag and drop operations, including event coordinate extraction from mouse and touch events.
type Coordinates = {
x: number;
y: number;
};Extracts normalized x,y coordinates from mouse and touch events. Returns viewport-relative coordinates for consistent positioning across different event types.
/**
* Extracts normalized coordinates from mouse and touch events
* @param event - DOM event object (MouseEvent, TouchEvent, etc.)
* @returns Coordinates object with x,y properties, or null if coordinates cannot be extracted
*/
function getEventCoordinates(event: Event): Coordinates | null;Usage Examples:
import { getEventCoordinates, type Coordinates } from "@dnd-kit/utilities";
// Handle mouse events
function handleMouseMove(event: MouseEvent) {
const coordinates = getEventCoordinates(event);
if (coordinates) {
console.log(`Mouse at: ${coordinates.x}, ${coordinates.y}`);
}
}
// Handle touch events
function handleTouchStart(event: TouchEvent) {
const coordinates = getEventCoordinates(event);
if (coordinates) {
console.log(`Touch at: ${coordinates.x}, ${coordinates.y}`);
}
}
// Generic event handler for multiple event types
function handlePointerEvent(event: Event) {
const coordinates = getEventCoordinates(event);
if (!coordinates) {
console.log("Could not extract coordinates from event");
return;
}
// Use coordinates for positioning
updateElementPosition(coordinates);
}
function updateElementPosition(coords: Coordinates) {
const element = document.getElementById("draggable");
if (element) {
element.style.left = `${coords.x}px`;
element.style.top = `${coords.y}px`;
}
}
// React component example
import React from "react";
function DraggableComponent() {
const handleDragMove = (event: React.DragEvent) => {
const coordinates = getEventCoordinates(event.nativeEvent);
if (coordinates) {
// Update drag position
console.log("Dragging to:", coordinates);
}
};
return (
<div
draggable
onDragStart={handleDragMove}
onDrag={handleDragMove}
>
Drag me!
</div>
);
}Behavior Notes:
touches array, or changedTouches if touches is emptyclientX and clientY propertiesnull if the event type doesn't contain coordinate informationInstall with Tessl CLI
npx tessl i tessl/npm-dnd-kit--utilities