New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

web-draggable

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

web-draggable

A lightweight drag and drop library using Web Components

beta
latest
npmnpm
Version
0.1.0-beta2
Version published
Maintainers
1
Created
Source

Web Draggable

A lightweight, framework-agnostic drag and drop library built with Web Components and TypeScript.

Features

  • Zero dependencies - Pure Web Components implementation
  • Framework agnostic - Works with vanilla JS, React, Vue, Svelte, etc.
  • TypeScript support - Full type definitions included
  • Fully accessible - Complete keyboard navigation and screen reader support
  • ESM only - Modern module system
  • Lightweight - Minimal bundle size
  • Customizable - Style and extend as needed

Installation

# Using Bun
bun add web-draggable

# Using npm
npm install web-draggable

# Using pnpm
pnpm add web-draggable

Usage

Basic Example

<!DOCTYPE html>
<html>
<head>
    <style>
        draggable-element {
            display: block;
            padding: 1rem;
            background: #667eea;
            color: white;
            border-radius: 4px;
            cursor: move;
        }

        draggable-element.dragging {
            opacity: 0.5;
        }

        droppable-zone {
            display: block;
            min-height: 200px;
            padding: 1rem;
            border: 2px dashed #ccc;
            border-radius: 4px;
        }

        droppable-zone.drag-over {
            border-color: #667eea;
            background: #f0f4ff;
        }
    </style>
</head>
<body>
    <draggable-element drag-data='{"id": 1}'>
        Drag me!
    </draggable-element>

    <droppable-zone id="drop-zone">
        Drop here
    </droppable-zone>

    <script type="module">
        import 'web-draggable';

        document.addEventListener('drop-element', (e) => {
            const { draggedElement, droppedOn, data } = e.detail;
            droppedOn.appendChild(draggedElement);
            console.log('Dropped:', data);
        });
    </script>
</body>
</html>

TypeScript Usage

import { DraggableElement, DroppableZone, type DropEventDetail } from 'web-draggable';

// Listen to drop events
document.addEventListener('drop-element', (e: CustomEvent<DropEventDetail>) => {
    const { draggedElement, droppedOn, data } = e.detail;
    console.log('Element dropped:', data);
});

// Programmatically set data
const draggable = document.querySelector('draggable-element') as DraggableElement;
draggable.setData({ id: 123, name: 'My Item' });

Accessibility

This library is fully accessible with keyboard navigation and screen reader support.

Keyboard Controls

  • Tab - Navigate between draggable elements
  • Arrow Up / Arrow Down - Navigate focus between elements (when not in drag mode)
  • Space / Enter - Pick up or drop an element
  • Arrow Up / Arrow Down (during drag) - Move the picked element within its list
  • Escape - Cancel the drag operation and return element to original position

Screen Reader Support

  • ARIA attributes are automatically added to all draggable elements
  • Live region announcements inform users about drag operations:
    • When an element is picked up
    • When an element is moved to a new position
    • When an element is dropped
    • When a drag operation is cancelled

Known Limitations

  • Cross-zone keyboard drag: Currently, keyboard navigation only allows reordering elements within the same droppable zone. Moving an element from one zone to another requires mouse/touch interaction. This is a known limitation that may be addressed in future versions.

See examples/accessible.html for a complete accessible implementation.

Restricting Drops with Groups

You can restrict which elements can be dropped in which zones using the group system. This is useful for creating multiple independent drag & drop contexts on the same page.

How it works

  • Add a drag-group attribute to draggable elements
  • Add an accept-group attribute to droppable zones
  • Elements can only be dropped in zones with a matching group
  • Multiple groups: Use comma-separated values to accept multiple groups (e.g., accept-group="work,personal")

Example

<!-- Work tasks - can only be dropped in work zones -->
<draggable-element drag-group="work">
    Complete project proposal
</draggable-element>

<!-- Personal tasks - can only be dropped in personal zones -->
<draggable-element drag-group="personal">
    Buy groceries
</draggable-element>

<!-- Urgent tasks -->
<draggable-element drag-group="urgent">
    Critical bug fix
</draggable-element>

<!-- Work zone - only accepts work items -->
<droppable-zone accept-group="work">
    <!-- Work items go here -->
</droppable-zone>

<!-- Personal zone - only accepts personal items -->
<droppable-zone accept-group="personal">
    <!-- Personal items go here -->
</droppable-zone>

<!-- Mixed zone - accepts both work and personal items -->
<droppable-zone accept-group="work,personal">
    <!-- Both work and personal items can be dropped here -->
</droppable-zone>

<!-- Priority zone - accepts work and urgent items -->
<droppable-zone accept-group="work,urgent">
    <!-- Work and urgent items can be dropped here -->
</droppable-zone>

Important: If an element has a drag-group but no zone has the matching accept-group, it cannot be dropped anywhere. Similarly, if a zone has an accept-group but the element has no drag-group, the drop will be rejected.

See examples/groups.html for a complete demonstration.

API

<draggable-element>

A custom element that can be dragged.

Attributes

  • drag-data - JSON string containing custom data to attach to the drag operation
  • drag-group - Optional group identifier. When specified, this element can only be dropped in zones with a matching accept-group attribute
  • drag-handle - Optional CSS selector for a drag handle element within the draggable element

Events

  • drag-start - Fired when dragging starts
  • drag-end - Fired when dragging ends

Methods

  • setData(data: Record<string, unknown>) - Set custom data for the drag operation
  • getData(): Record<string, unknown> - Get the custom data

<droppable-zone>

A custom element that accepts dropped elements.

Attributes

  • accept-group - Optional group identifier. When specified, this zone only accepts draggable elements with a matching drag-group attribute
  • aria-label - Optional label for screen readers to describe the drop zone

Events

  • drag-over - Fired when a draggable element is over the zone
  • drop-element - Fired when an element is dropped

CSS Classes

  • drag-over - Applied when a draggable element is over the zone
  • dragging - Applied to draggable elements while being dragged

Development

# Install dependencies
bun install

# Check formatting and linting
bun run check

# Build the library
bun run build

# View the demo (requires a local server)
# Open examples/index.html in your browser

License

MIT

Keywords

drag-and-drop

FAQs

Package last updated on 10 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts