Skip to content

Commit

Permalink
Merge pull request #6 from colecrouter/main
Browse files Browse the repository at this point in the history
feat: add custom dragstart event and improve drag enter/leave handling
  • Loading branch information
Spikeysanju authored Dec 12, 2024
2 parents 0ba36d3 + badc252 commit 6e08a96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/lib/actions/draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export function draggable<T>(node: HTMLElement, options: DragDropOptions<T>) {

node.classList.add(...draggingClass);
options.callbacks?.onDragStart?.(dndState as DragDropState<T>);

// **Dispatch the custom event that bubbles up to the container**
const customEvent = new CustomEvent('dragstart-on-container', { bubbles: true });
node.dispatchEvent(customEvent);
}

function handleDragEnd() {
Expand Down
22 changes: 18 additions & 4 deletions src/lib/actions/droppable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ const DEFAULT_DRAG_OVER_CLASS = 'drag-over';

export function droppable<T>(node: HTMLElement, options: DragDropOptions<T>) {
const dragOverClass = (options.attributes?.draggingClass || DEFAULT_DRAG_OVER_CLASS).split(' ');
let dragEnterCounter = 0; // Initialize the counter

function handleDragEnter(event: DragEvent) {
if (options.disabled) return;
event.preventDefault();

const target = event.target as HTMLElement;
dragEnterCounter++;

dndState.targetContainer = options.container;
dndState.targetElement = target;
dndState.targetElement = event.target as HTMLElement;

if (dragEnterCounter === 0) return;

node.classList.add(...dragOverClass);
options.callbacks?.onDragEnter?.(dndState as DragDropState<T>);
Expand All @@ -22,10 +25,10 @@ export function droppable<T>(node: HTMLElement, options: DragDropOptions<T>) {
function handleDragLeave(event: DragEvent) {
if (options.disabled) return;

const target = event.target as HTMLElement;
dragEnterCounter--;

// check if element is still being dragged over
if (!dndState.targetElement?.isSameNode(target)) return;
if (dragEnterCounter > 0) return;

node.classList.remove(...dragOverClass);

Expand All @@ -50,6 +53,7 @@ export function droppable<T>(node: HTMLElement, options: DragDropOptions<T>) {
if (options.disabled) return;
event.preventDefault();

dragEnterCounter = 0; // Reset the counter
node.classList.remove(...dragOverClass);

try {
Expand All @@ -64,6 +68,14 @@ export function droppable<T>(node: HTMLElement, options: DragDropOptions<T>) {
}
}

function handleDragStartOnContainer(event: Event) {
if (options.disabled) return;

// Reset the counter and remove the class
dragEnterCounter = 0;
node.classList.remove(...dragOverClass);
}

function handlePointerOver(event: PointerEvent) {
if (options.disabled || !dndState.isDragging) return;

Expand Down Expand Up @@ -91,6 +103,7 @@ export function droppable<T>(node: HTMLElement, options: DragDropOptions<T>) {
node.addEventListener('dragleave', handleDragLeave);
node.addEventListener('dragover', handleDragOver);
node.addEventListener('drop', handleDrop);
node.addEventListener('dragstart-on-container', handleDragStartOnContainer);

node.addEventListener('pointerover', handlePointerOver);
node.addEventListener('pointerout', handlePointerOut);
Expand All @@ -106,6 +119,7 @@ export function droppable<T>(node: HTMLElement, options: DragDropOptions<T>) {
node.removeEventListener('dragleave', handleDragLeave);
node.removeEventListener('dragover', handleDragOver);
node.removeEventListener('drop', handleDrop);
node.removeEventListener('dragstart-on-container', handleDragStartOnContainer);

node.removeEventListener('pointerover', handlePointerOver);
node.removeEventListener('pointerout', handlePointerOut);
Expand Down

0 comments on commit 6e08a96

Please sign in to comment.