Skip to content

Commit

Permalink
feat: Add domRect to events, Ship types (#36)
Browse files Browse the repository at this point in the history
* Add domRect to events
Ship types

* Code reduction
  • Loading branch information
PuruVJ authored Jan 26, 2022
1 parent c543bbc commit d0a0713
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 32 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

2 changes: 0 additions & 2 deletions .prettierignore

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Puru Vijay
Copyright (c) 2022 Puru Vijay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,7 @@ If you're a TypeScript user, read on below 👇

# TypeScript

The events above are custom events, and hence, not recognized by the TypeScript compiler, so your editor will basically yell at you and say these events do not exist. For that, you need to create a `types.d.ts` file in the `source` folder of your project and the code below to it 👇

```ts
export declare namespace svelte.JSX {
interface HTMLAttributes<T> {
'onsvelte-drag:start'?: (e: CustomEvent<{ offsetX: number; offsetY: number }>) => void;
'onsvelte-drag:end'?: (e: CustomEvent<{ offsetX: number; offsetY: number }>) => void;
'onsvelte-drag'?: (e: CustomEvent<{ offsetX: number; offsetY: number }>) => void;
}
}
```
This library ships with proper TypeScript typings, for the best Developer Experience, whether authoring JS or TS.

## Types Exported from package

Expand Down
12 changes: 9 additions & 3 deletions src/draggable.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export declare namespace svelte.JSX {
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;
'onsvelte-drag:start'?: (
e: CustomEvent<{ offsetX: number; offsetY: number; domRect: DOMRect }>
) => void;
'onsvelte-drag:end'?: (
e: CustomEvent<{ offsetX: number; offsetY: number; domRect: DOMRect }>
) => void;
'onsvelte-drag'?: (
e: CustomEvent<{ offsetX: number; offsetY: number; domRect: DOMRect }>
) => void;
}
}
23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,17 @@ export type DragOptions = {
/**
* Fires when dragging start
*/
onDragStart?: (data: { offsetX: number; offsetY: number }) => void;
onDragStart?: (data: { offsetX: number; offsetY: number; domRect: DOMRect }) => void;

/**
* Fires when dragging is going on
*/
onDrag?: (data: { offsetX: number; offsetY: number }) => void;
onDrag?: (data: { offsetX: number; offsetY: number; domRect: DOMRect }) => void;

/**
* Fires when dragging ends
*/
onDragEnd?: (data: { offsetX: number; offsetY: number }) => void;
onDragEnd?: (data: { offsetX: number; offsetY: number; domRect: DOMRect }) => void;
};

const DEFAULT_CLASS = {
Expand All @@ -282,8 +282,6 @@ const DEFAULT_CLASS = {
DRAGGED: 'svelte-draggable-dragged',
};

let needForRAF = true;

export const draggable = (node: HTMLElement, options: DragOptions = {}) => {
let {
bounds,
Expand Down Expand Up @@ -338,22 +336,27 @@ export const draggable = (node: HTMLElement, options: DragOptions = {}) => {

let isControlled = !!position;

function fireSvelteDragStartEvent(node: HTMLElement) {
const data = { offsetX: translateX, offsetY: translateY };
const getEventData = () => ({
offsetX: translateX,
offsetY: translateY,
domRect: node.getBoundingClientRect(),
});

function fireSvelteDragStartEvent(node: HTMLElement) {
const data = getEventData();
node.dispatchEvent(new CustomEvent('svelte-drag:start', { detail: data }));
onDragStart?.(data);
}

function fireSvelteDragStopEvent(node: HTMLElement) {
const data = { offsetX: translateX, offsetY: translateY };
const data = getEventData();

node.dispatchEvent(new CustomEvent('svelte-drag:end', { detail: data }));
onDragEnd?.(data);
}

function fireSvelteDragEvent(node: HTMLElement, translateX: number, translateY: number) {
const data = { offsetX: translateX, offsetY: translateY };
const data = getEventData();

node.dispatchEvent(new CustomEvent('svelte-drag', { detail: data }));
onDrag?.(data);
Expand Down Expand Up @@ -634,8 +637,6 @@ function computeBoundRect(bounds: string | Partial<DragBoundsCoords>, rootNode:
}

function setTranslate(xPos: number, yPos: number, el: HTMLElement, gpuAcceleration: boolean) {
needForRAF = true; // rAF now consumes the movement instruction so a new one can come

el.style.transform = gpuAcceleration
? `translate3d(${+xPos}px, ${+yPos}px, 0)`
: `translate(${+xPos}px, ${+yPos}px)`;
Expand Down
5 changes: 4 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { defineConfig } from 'tsup';
import { readFileSync } from 'node:fs';

const banner = readFileSync('./src/draggable.d.ts', 'utf8');

export default defineConfig({
sourcemap: true,
clean: true,
dts: true,
dts: { banner },
format: ['esm', 'cjs'],
external: [],
entryPoints: ['src/index.ts'],
Expand Down

0 comments on commit d0a0713

Please sign in to comment.