CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-dnd-kit--utilities

Internal utilities shared between @dnd-kit packages providing React hooks, coordinate manipulation, and DOM utilities.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

coordinates.mddocs/

Coordinate System

Types and utilities for handling x,y coordinates in drag and drop operations, including event coordinate extraction from mouse and touch events.

Types

type Coordinates = {
  x: number;
  y: number;
};

Capabilities

getEventCoordinates

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:

  • For touch events: Extracts coordinates from the first touch in touches array, or changedTouches if touches is empty
  • For mouse/pointer events: Uses clientX and clientY properties
  • Returns null if the event type doesn't contain coordinate information
  • All coordinates are viewport-relative (relative to the browser window)
  • Works with cross-frame events by using the correct window context

Install with Tessl CLI

npx tessl i tessl/npm-dnd-kit--utilities

docs

coordinates.md

css-utilities.md

event-handling.md

execution-context.md

focus-management.md

hooks.md

index.md

math-operations.md

type-guards.md

typescript-utilities.md

tile.json