diff --git a/README.md b/README.md index fe06905..4b1535c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # SvelteDnD -[![npm version](https://badge.fury.io/js/%40thisux%2Fsveltednd.svg)](https://www.npmjs.com/package/@thisux/sveltednd) - A lightweight drag and drop library for Svelte 5 applications. Built with TypeScript and Svelte's new runes system. ## Installation @@ -14,7 +12,7 @@ npm install @thisux/sveltednd ```typescript import { draggable, droppable, type DragDropState } from '@thisux/sveltednd'; -import '@thisux/sveltednd/styles.css'; +import '@thisux/sveltednd/styles.css'; // optional // Create a list of items let items = $state(['Item 1', 'Item 2', 'Item 3']); @@ -145,9 +143,18 @@ interface DragDropState { -
+ }} +> + {#each items as item} +
+ {item.name} +
+ {/each} +
``` ### Additional Examples diff --git a/package-lock.json b/package-lock.json index 18a4b9d..d5bab68 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,14 @@ { "name": "@thisux/sveltednd", - "version": "0.0.4", + "version": "0.0.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@thisux/sveltednd", - "version": "0.0.4", + "version": "0.0.6", "license": "MIT", "devDependencies": { - "@sveltejs/adapter-auto": "^3.0.0", "@sveltejs/adapter-cloudflare": "^4.7.4", "@sveltejs/kit": "^2.0.0", "@sveltejs/package": "^2.0.0", @@ -1225,19 +1224,6 @@ "win32" ] }, - "node_modules/@sveltejs/adapter-auto": { - "version": "3.3.1", - "resolved": "https://registry.npmjs.org/@sveltejs/adapter-auto/-/adapter-auto-3.3.1.tgz", - "integrity": "sha512-5Sc7WAxYdL6q9j/+D0jJKjGREGlfIevDyHSQ2eNETHcB1TKlQWHcAo8AS8H1QdjNvSXpvOwNjykDUHPEAyGgdQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "import-meta-resolve": "^4.1.0" - }, - "peerDependencies": { - "@sveltejs/kit": "^2.0.0" - } - }, "node_modules/@sveltejs/adapter-cloudflare": { "version": "4.7.4", "resolved": "https://registry.npmjs.org/@sveltejs/adapter-cloudflare/-/adapter-cloudflare-4.7.4.tgz", diff --git a/package.json b/package.json index fc98129..c333bef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thisux/sveltednd", - "version": "0.0.6", + "version": "0.0.10", "private": false, "description": "A lightweight, flexible drag and drop library for Svelte 5 applications.", "author": "sanju ", @@ -52,7 +52,6 @@ "svelte": "^5.0.0" }, "devDependencies": { - "@sveltejs/adapter-auto": "^3.0.0", "@sveltejs/adapter-cloudflare": "^4.7.4", "@sveltejs/kit": "^2.0.0", "@sveltejs/package": "^2.0.0", diff --git a/src/lib/actions/draggable.ts b/src/lib/actions/draggable.ts index ac238b0..1df1861 100644 --- a/src/lib/actions/draggable.ts +++ b/src/lib/actions/draggable.ts @@ -1,11 +1,11 @@ import { dndState } from '$lib/stores/dnd.svelte.js'; -import type { DragDropOptions } from '$lib/types/index.js'; +import type { DragDropOptions, DragDropState } from '$lib/types/index.js'; -export function draggable(node: HTMLElement, options: DragDropOptions) { +export function draggable(node: HTMLElement, options: DragDropOptions) { function handleDragStart(event: DragEvent | TouchEvent) { + if (options.disabled) return; - // Update state using assignment (Svelte 5 style) dndState.isDragging = true; dndState.draggedItem = options.dragData; dndState.sourceContainer = options.container; @@ -17,12 +17,12 @@ export function draggable(node: HTMLElement, options: DragDropOptions) { } node.classList.add('dragging'); - options.callbacks?.onDragStart?.(dndState); + options.callbacks?.onDragStart?.(dndState as DragDropState); } function handleDragEnd(event: DragEvent | TouchEvent) { node.classList.remove('dragging'); - options.callbacks?.onDragEnd?.(dndState); + options.callbacks?.onDragEnd?.(dndState as DragDropState); // Reset state dndState.isDragging = false; @@ -48,7 +48,7 @@ export function draggable(node: HTMLElement, options: DragDropOptions) { node.addEventListener('touchend', handleTouchEnd); return { - update(newOptions: DragDropOptions) { + update(newOptions: DragDropOptions) { options = newOptions; node.draggable = !options.disabled; }, diff --git a/src/lib/actions/droppable.ts b/src/lib/actions/droppable.ts index b16d2f7..77cf974 100644 --- a/src/lib/actions/droppable.ts +++ b/src/lib/actions/droppable.ts @@ -1,16 +1,17 @@ import { dndState } from '$lib/stores/dnd.svelte.js'; -import type { DragDropOptions } from '$lib/types/index.js'; - -export function droppable(node: HTMLElement, options: DragDropOptions) { - let touchTimeout: number | null = null; +import type { DragDropOptions, DragDropState } from '$lib/types/index.js'; +export function droppable(node: HTMLElement, options: DragDropOptions) { + let touchTimeout: number | null = null; + function handleDragEnter(event: DragEvent | TouchEvent) { + if (options.disabled) return; event.preventDefault(); dndState.targetContainer = options.container; node.classList.add('drag-over'); - options.callbacks?.onDragEnter?.(dndState); + options.callbacks?.onDragEnter?.(dndState as DragDropState); } function handleDragLeave(event: DragEvent | TouchEvent) { @@ -20,7 +21,7 @@ export function droppable(node: HTMLElement, options: DragDropOptions) { if (!node.contains(target)) { dndState.targetContainer = null; node.classList.remove('drag-over'); - options.callbacks?.onDragLeave?.(dndState); + options.callbacks?.onDragLeave?.(dndState as DragDropState); } } @@ -32,7 +33,7 @@ export function droppable(node: HTMLElement, options: DragDropOptions) { event.dataTransfer.dropEffect = 'move'; } - options.callbacks?.onDragOver?.(dndState); + options.callbacks?.onDragOver?.(dndState as DragDropState); } async function handleDrop(event: DragEvent | TouchEvent) { @@ -42,12 +43,13 @@ export function droppable(node: HTMLElement, options: DragDropOptions) { node.classList.remove('drag-over'); try { + if (event instanceof DragEvent && event.dataTransfer) { - const dragData = JSON.parse(event.dataTransfer.getData('text/plain')); + const dragData = JSON.parse(event.dataTransfer.getData('text/plain')) as T; dndState.draggedItem = dragData; } - await options.callbacks?.onDrop?.(dndState); + await options.callbacks?.onDrop?.(dndState as DragDropState); } catch (error) { console.error('Drop handling failed:', error); } @@ -70,7 +72,7 @@ export function droppable(node: HTMLElement, options: DragDropOptions) { node.addEventListener('touchmove', handleTouchMove); return { - update(newOptions: DragDropOptions) { + update(newOptions: DragDropOptions) { options = newOptions; }, diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c07d74e..7195b29 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -93,7 +93,9 @@
@@ -110,7 +112,9 @@ {#each items as task (task.id)}
handleItemDrop(group.id, state) + onDrop: (state: DragDropState) => handleItemDrop(group.id, state) } }} >