@rodrigodagostino/svelte-draggable-drawer 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,11 @@
1
1
  <script lang="ts">
2
+ import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
2
3
  import { setDrawerRootState } from '../states/index.js';
3
4
  import type { DrawerRootProps } from '../types/props.js';
5
+
4
6
  let {
5
7
  ref = $bindable(null),
8
+ range = { start: RANGE_START_DEFAULT, end: RANGE_END_DEFAULT },
6
9
  isOpen = $bindable(false),
7
10
  children,
8
11
  ...restProps
@@ -13,6 +16,7 @@
13
16
  $effect(() => {
14
17
  rootState.props = {
15
18
  ref,
19
+ range,
16
20
  isOpen,
17
21
  };
18
22
  });
@@ -1,6 +1,9 @@
1
1
  <script lang="ts">
2
+ import { untrack } from 'svelte';
3
+ import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
2
4
  import { getDrawerRootState } from '../states/drawer.svelte.js';
3
5
  import type { DrawerBackdropProps as BackdropProps } from '../types/props.js';
6
+ import { toPixels } from '../utils/index.js';
4
7
 
5
8
  let { ref = $bindable(null), ...restProps }: BackdropProps & { class?: string } = $props();
6
9
 
@@ -10,26 +13,42 @@
10
13
 
11
14
  const classes = $derived(['sdd-backdrop', restProps.class]);
12
15
 
13
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
14
- function getStyleOpacity(...args: unknown[]) {
15
- if (rootState.dragState === 'drag' && rootState.pointer) {
16
- return (rootState.pointer.y / offsetHeight / 2 - 0.5) * -1;
16
+ function getStyleOpacity() {
17
+ if (rootState.dragState.startsWith('drag') && rootState.pointer) {
18
+ const handleRect = rootState.handle?.getBoundingClientRect();
19
+ if (!handleRect) return 0;
20
+
21
+ const rangeStart = toPixels(rootState.props.range?.start, RANGE_START_DEFAULT);
22
+ const rangeEnd = toPixels(rootState.props.range?.end, RANGE_END_DEFAULT);
23
+ const windowHeightWithoutRange = window.innerHeight - rangeEnd - rangeStart;
24
+ const handleTop = Math.min(
25
+ Math.max((handleRect.top ?? 0) - rangeEnd, 0),
26
+ window.innerHeight - rangeStart
27
+ );
28
+ const opacity = (handleTop / windowHeightWithoutRange / 2 - 0.5) * -1;
29
+
30
+ return Math.max(Math.min(opacity, 0.5), 0);
17
31
  }
18
- if (rootState.props.isOpen) return 0.5;
19
- return 0;
32
+
33
+ return rootState.props.isOpen ? 0.5 : 0;
20
34
  }
21
35
 
22
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- function getStyleTransition(...args: unknown[]) {
24
- if (rootState.dragState !== 'drag-start' && rootState.dragState !== 'drag')
25
- return 'opacity 240ms';
36
+ function getStyleTransition() {
37
+ if (!rootState.dragState.startsWith('drag')) return 'opacity 240ms';
26
38
  return 'none';
27
39
  }
28
40
 
29
- const styleOpacity = $derived(
30
- getStyleOpacity(rootState.pointer, rootState.dragState, rootState.props.isOpen)
31
- );
32
- const styleTransition = $derived(getStyleTransition(rootState.dragState));
41
+ const styleOpacity = $derived.by(() => {
42
+ void rootState.pointer;
43
+ void rootState.dragState;
44
+ void rootState.props.isOpen;
45
+ return untrack(() => getStyleOpacity());
46
+ });
47
+
48
+ const styleTransition = $derived.by(() => {
49
+ void rootState.dragState;
50
+ return untrack(() => getStyleTransition());
51
+ });
33
52
 
34
53
  const handleClick = () => {
35
54
  rootState.props.isOpen = false;
@@ -1,6 +1,9 @@
1
1
  <script lang="ts">
2
+ import { untrack } from 'svelte';
3
+ import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
2
4
  import { getDrawerRootState } from '../states/drawer.svelte.js';
3
5
  import type { DrawerContentProps as ContentProps } from '../types/props.js';
6
+ import { applyRubberBand, toCSSLength, toPixels } from '../utils/index.js';
4
7
 
5
8
  let {
6
9
  ref = $bindable(null),
@@ -16,36 +19,48 @@
16
19
 
17
20
  const classes = $derived(['sdd-content', restProps.class]);
18
21
 
19
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
20
- function getStyleTransform(...args: unknown[]) {
22
+ function getStyleTransform() {
21
23
  if (rootState.dragState === 'drag') {
22
24
  if (!rootState.pointer || !rootState.pointerOrigin || !rootState.contentOrigin) return 'none';
23
- // Prevent the drawer from being dragged above the top of the screen or below the bottom.
24
- const y =
25
- rootState.pointerOrigin.y > window.innerHeight / 2
26
- ? rootState.pointer.y > rootState.pointerOrigin.y
27
- ? rootState.pointerOrigin.y
28
- : rootState.pointer.y
29
- : rootState.pointer.y < rootState.pointerOrigin.y
30
- ? rootState.pointerOrigin.y
31
- : rootState.pointer.y;
32
- // Set the position of the content relative to the pointer.
33
- const offset = rootState.contentOrigin.y - rootState.pointerOrigin.y;
34
-
35
- return `translate3d(0, ${y + offset}px, 0)`;
36
- }
37
25
 
38
- if (rootState.props.isOpen)
39
- return `translate3d(0, calc(0% + ${rootState.handle?.getBoundingClientRect().height || 24}px), 0)`;
26
+ const startY = rootState.pointerOrigin.y;
27
+ const currentY = rootState.pointer.y;
28
+ const offset = rootState.contentOrigin.y - startY;
29
+
30
+ // Calculate where the drawer would be without any resistance.
31
+ const rawY = currentY + offset;
32
+
33
+ const START_LIMIT =
34
+ window.innerHeight - toPixels(rootState.props.range?.start, RANGE_START_DEFAULT);
35
+ const END_LIMIT = 0 + toPixels(rootState.props.range?.end, RANGE_END_DEFAULT);
36
+ let y = rawY;
37
+ const MAX_OVERDRAG = 120;
40
38
 
41
- return `translate3d(0, calc(100% - ${rootState.handle?.getBoundingClientRect().height || 48}px), 0)`;
39
+ if (rawY < END_LIMIT) {
40
+ const overdrag = END_LIMIT - rawY;
41
+ y = END_LIMIT - applyRubberBand(overdrag, MAX_OVERDRAG);
42
+ } else if (rawY > START_LIMIT) {
43
+ const overdrag = rawY - START_LIMIT;
44
+ y = START_LIMIT + applyRubberBand(overdrag, MAX_OVERDRAG);
45
+ }
46
+
47
+ return `translate3d(0, ${y}px, 0)`;
48
+ }
49
+
50
+ return rootState.props.isOpen
51
+ ? `translate3d(0, calc(0% + ${toCSSLength(rootState.props.range?.end ?? RANGE_END_DEFAULT)}), 0)`
52
+ : `translate3d(0, calc(100% - ${toCSSLength(rootState.props.range?.start ?? RANGE_START_DEFAULT)}), 0)`;
42
53
  }
43
54
 
44
- const styleTransform = $derived(
45
- getStyleTransform(rootState.pointer, rootState.dragState, rootState.props.isOpen)
46
- );
55
+ const styleTransform = $derived.by(() => {
56
+ void rootState.pointer;
57
+ void rootState.dragState;
58
+ void rootState.props.isOpen;
59
+ return untrack(() => getStyleTransform());
60
+ });
61
+
47
62
  const styleTransition = $derived(
48
- rootState.dragState !== 'drag-start' && rootState.dragState !== 'drag'
63
+ !rootState.dragState.startsWith('drag')
49
64
  ? `transform 400ms cubic-bezier(0.32, 0.72, 0, 1)`
50
65
  : 'none'
51
66
  );
@@ -0,0 +1,2 @@
1
+ export declare const RANGE_START_DEFAULT = 0;
2
+ export declare const RANGE_END_DEFAULT = 48;
@@ -0,0 +1,2 @@
1
+ export const RANGE_START_DEFAULT = 0;
2
+ export const RANGE_END_DEFAULT = 48;
@@ -1,12 +1,22 @@
1
- import type { DrawerRootStateContext as Context } from '../types/index.js';
1
+ import type { DrawerRootProps as RootProps } from '../types/index.js';
2
2
  declare class DrawerRootState {
3
- props: Context['props'];
4
- dragState: Context['dragState'];
5
- content: Context['content'];
6
- handle: Context['handle'];
7
- pointer: Context['pointer'];
8
- pointerOrigin: Context['pointerOrigin'];
9
- contentOrigin: Context['contentOrigin'];
3
+ ref: HTMLElement | null;
4
+ props: RootProps;
5
+ dragState: 'idle' | 'drag-start' | 'drag' | 'drop';
6
+ content: HTMLElement | null;
7
+ handle: HTMLButtonElement | null;
8
+ pointer: {
9
+ x: number;
10
+ y: number;
11
+ } | null;
12
+ pointerOrigin: {
13
+ x: number;
14
+ y: number;
15
+ } | null;
16
+ contentOrigin: {
17
+ x: number;
18
+ y: number;
19
+ } | null;
10
20
  }
11
21
  export declare function setDrawerRootState(): DrawerRootState;
12
22
  export declare function getDrawerRootState(): DrawerRootState;
@@ -1,5 +1,6 @@
1
1
  import { getContext, setContext } from 'svelte';
2
2
  class DrawerRootState {
3
+ ref = $state(null);
3
4
  props = $state({});
4
5
  dragState = $state('idle');
5
6
  content = $state(null);
@@ -1,2 +1 @@
1
1
  export * from './props.js';
2
- export * from './states.js';
@@ -1,2 +1 @@
1
1
  export * from './props.js';
2
- export * from './states.js';
@@ -1,6 +1,10 @@
1
1
  import type { Snippet } from 'svelte';
2
2
  export interface DrawerRootProps {
3
- ref?: HTMLDivElement | null;
3
+ ref?: HTMLElement | null;
4
+ range?: {
5
+ start: string | number;
6
+ end: string | number;
7
+ };
4
8
  isOpen?: boolean;
5
9
  children?: Snippet;
6
10
  }
@@ -0,0 +1,2 @@
1
+ export declare function toCSSLength(value: number | string): string;
2
+ export declare function toPixels(value: string | number | undefined, fallback: number): number;
@@ -0,0 +1,12 @@
1
+ export function toCSSLength(value) {
2
+ return typeof value === 'number' ? `${value}px` : value;
3
+ }
4
+ export function toPixels(value, fallback) {
5
+ if (value === undefined)
6
+ return fallback;
7
+ if (typeof value === 'number')
8
+ return value;
9
+ if (value.endsWith('%'))
10
+ return (parseFloat(value) / 100) * window.innerHeight;
11
+ return parseFloat(value);
12
+ }
@@ -0,0 +1,2 @@
1
+ export * from './conversion.ts';
2
+ export * from './physics.js';
@@ -0,0 +1,2 @@
1
+ export * from "./conversion.js";
2
+ export * from './physics.js';
@@ -0,0 +1 @@
1
+ export declare function applyRubberBand(overdrag: number, overdragLimit: number): number;
@@ -0,0 +1,4 @@
1
+ export function applyRubberBand(overdrag, overdragLimit) {
2
+ // Apple iOS Rubber-Band formula.
3
+ return (overdrag * overdragLimit) / (overdrag + overdragLimit);
4
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rodrigodagostino/svelte-draggable-drawer",
3
3
  "description": "Create accessible, draggable drawers in Svelte, with keyboard, mouse, and touch support.",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "author": "Rodrigo D’Agostino <rodrigo.dagostino@proton.me>",
6
6
  "keywords": [
7
7
  "draggable",
@@ -1,20 +0,0 @@
1
- import type { DrawerRootProps as RootProps } from './props.js';
2
- export interface DrawerRootStateContext {
3
- ref: HTMLUListElement | null;
4
- props: RootProps;
5
- dragState: 'idle' | 'drag-start' | 'drag' | 'drop';
6
- content: HTMLDivElement | null;
7
- handle: HTMLButtonElement | null;
8
- pointer: {
9
- x: number;
10
- y: number;
11
- } | null;
12
- pointerOrigin: {
13
- x: number;
14
- y: number;
15
- } | null;
16
- contentOrigin: {
17
- x: number;
18
- y: number;
19
- } | null;
20
- }
@@ -1 +0,0 @@
1
- export {};