Skip to content

Commit

Permalink
Merge pull request #12 from PuruVJ/fix/simultaneous-event-firing
Browse files Browse the repository at this point in the history
1.5.1 - Fix simultaneous event firing
  • Loading branch information
PuruVJ authored Oct 11, 2021
2 parents d36ac4b + 7e47e63 commit e5ae705
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
30 changes: 30 additions & 0 deletions demo/src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,36 @@
<div class="cancel-2">Cancel 2</div>
</div>

<div
use:draggable={options}
on:svelte-drag:start={console.log}
on:svelte-drag:end={console.log}
class="box"
>
hello

<div class="handle">Le handel</div>
<div class="cancel">Cancel</div>

<div class="handle-2">Le handel 2</div>
<div class="cancel-2">Cancel 2</div>
</div>

<div
use:draggable={options}
on:svelte-drag:start={console.log}
on:svelte-drag:end={console.log}
class="box"
>
hello

<div class="handle">Le handel</div>
<div class="cancel">Cancel</div>

<div class="handle-2">Le handel 2</div>
<div class="cancel-2">Cancel 2</div>
</div>

<style>
:global(body) {
background-color: rgb(26, 32, 39);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-drag",
"version": "1.5.0",
"version": "1.5.1",
"description": "Svelte port of react-draggable",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/draggable.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export declare namespace svelte.JSX {
interface HTMLAttributes<T> {
interface HTMLAttributes {
'onsvelte-drag:start'?: (e: CustomEvent<null>) => void;
'onsvelte-drag:end'?: (e: CustomEvent<null>) => void;
'onsvelte-drag'?: (e: CustomEvent<{ offsetX: number; offsetY: number }>) => void;
Expand Down
29 changes: 12 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,23 +287,15 @@ export const draggable = (node: HTMLElement, options: SvelteDragOptions = {}) =>
);
}

function setupListeners(
dragStart: (e: TouchEvent | MouseEvent) => void,
dragEnd: () => void,
drag: (e: TouchEvent | MouseEvent) => void
) {
const listen = addEventListener;

listen('touchstart', dragStart, false);
listen('touchend', dragEnd, false);
listen('touchmove', drag, false);

listen('mousedown', dragStart, false);
listen('mouseup', dragEnd, false);
listen('mousemove', drag, false);
}
const listen = addEventListener;

listen('touchstart', dragStart, false);
listen('touchend', dragEnd, false);
listen('touchmove', drag, false);

setupListeners(dragStart, dragEnd, drag);
listen('mousedown', dragStart, false);
listen('mouseup', dragEnd, false);
listen('mousemove', drag, false);

// On mobile, touch can become extremely janky without it
node.style.touchAction = 'none';
Expand Down Expand Up @@ -359,9 +351,12 @@ export const draggable = (node: HTMLElement, options: SvelteDragOptions = {}) =>
}
}

function dragEnd() {
function dragEnd(e: MouseEvent | TouchEvent) {
if (disabled) return;

// required, or the event will be fired for every single draggable instance present
if (!node.contains(e.target as HTMLElement)) return;

// Apply class defaultClassDragged
node.classList.remove(defaultClassDragging);
node.classList.add(defaultClassDragged);
Expand Down

0 comments on commit e5ae705

Please sign in to comment.