@rodrigodagostino/svelte-draggable-drawer 0.3.0 → 0.4.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,11 +1,18 @@
1
1
  <script lang="ts">
2
- import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
2
+ import {
3
+ RANGE_END_DEFAULT,
4
+ RANGE_START_DEFAULT,
5
+ SNAP_POINT_OFFSET,
6
+ } from '../constants/index.js';
3
7
  import { setDrawerRootState } from '../states/index.js';
4
8
  import type { DrawerRootProps } from '../types/props.js';
9
+ import { toPixels } from '../utils/index.js';
5
10
 
6
11
  let {
7
12
  ref = $bindable(null),
8
13
  range = { start: RANGE_START_DEFAULT, end: RANGE_END_DEFAULT },
14
+ snapPoints = [],
15
+ activeSnapPoint = $bindable(null),
9
16
  isOpen = $bindable(false),
10
17
  children,
11
18
  ...restProps
@@ -17,19 +24,22 @@
17
24
  rootState.props = {
18
25
  ref,
19
26
  range,
27
+ snapPoints: snapPoints.sort((a, b) => toPixels(a, 0) - toPixels(b, 0)),
28
+ activeSnapPoint,
20
29
  isOpen,
21
30
  };
22
31
  });
23
32
 
24
33
  $effect(() => {
34
+ activeSnapPoint = rootState.activeSnapPoint ?? null;
25
35
  isOpen = rootState.props.isOpen || false;
26
36
  });
27
37
 
28
38
  const classes = $derived(['sdd-root', restProps.class]);
29
39
  let pointerId: PointerEvent['pointerId'] | null = null;
30
40
 
31
- const TOP_THRESHOLD = 2 / 5;
32
- const BOTTOM_THRESHOLD = 3 / 5;
41
+ const START_THRESHOLD = 2 / 5;
42
+ const END_THRESHOLD = 2 / 5;
33
43
 
34
44
  let transitionTimeoutId: ReturnType<typeof setTimeout> | null = null;
35
45
 
@@ -61,10 +71,30 @@
61
71
  rootState.dragState = 'drag';
62
72
 
63
73
  rootState.pointer = { x: clientX, y: clientY };
64
- rootState.props.isOpen =
65
- rootState.pointerOrigin.y > window.innerHeight / 2
66
- ? clientY < window.innerHeight * BOTTOM_THRESHOLD
67
- : clientY < window.innerHeight * TOP_THRESHOLD;
74
+ const handleTop = rootState.handle?.getBoundingClientRect()?.top ?? clientY;
75
+ const visibleHeight = window.innerHeight - handleTop;
76
+ const snapPoints = rootState.props.snapPoints ?? [];
77
+ const snapPointsInPx = (snapPoints ?? []).map((point) => toPixels(point, 0));
78
+
79
+ if (snapPointsInPx.length) {
80
+ const isBelowFirst = visibleHeight <= snapPointsInPx[0] - SNAP_POINT_OFFSET;
81
+ const isAboveLast = visibleHeight >= snapPointsInPx.at(-1)! + SNAP_POINT_OFFSET;
82
+
83
+ if (isBelowFirst || isAboveLast) {
84
+ rootState.activeSnapPoint = null;
85
+ } else {
86
+ const index = snapPointsInPx.findIndex(
87
+ (point) => Math.abs(visibleHeight - point) <= SNAP_POINT_OFFSET
88
+ );
89
+ if (index !== -1) rootState.activeSnapPoint = { index, value: snapPoints[index] };
90
+ }
91
+
92
+ rootState.props.isOpen = !isBelowFirst;
93
+ } else {
94
+ const threshold =
95
+ rootState.pointerOrigin.y > window.innerHeight / 2 ? START_THRESHOLD : END_THRESHOLD;
96
+ rootState.props.isOpen = handleTop < window.innerHeight * threshold;
97
+ }
68
98
  }
69
99
 
70
100
  function handlePointerUp() {
@@ -2,6 +2,6 @@ import type { DrawerRootProps } from '../types/props.js';
2
2
  type $$ComponentProps = DrawerRootProps & {
3
3
  class?: string;
4
4
  };
5
- declare const Drawer: import("svelte").Component<$$ComponentProps, {}, "ref" | "isOpen">;
5
+ declare const Drawer: import("svelte").Component<$$ComponentProps, {}, "ref" | "activeSnapPoint" | "isOpen">;
6
6
  type Drawer = ReturnType<typeof Drawer>;
7
7
  export default Drawer;
@@ -3,31 +3,32 @@
3
3
  import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
4
4
  import { getDrawerRootState } from '../states/drawer.svelte.js';
5
5
  import type { DrawerBackdropProps as BackdropProps } from '../types/props.js';
6
- import { toPixels } from '../utils/index.js';
6
+ import { clamp, toPixels } from '../utils/index.js';
7
7
 
8
8
  let { ref = $bindable(null), ...restProps }: BackdropProps & { class?: string } = $props();
9
9
 
10
- let offsetHeight: number = $state(0);
11
-
12
10
  const rootState = getDrawerRootState();
13
11
 
14
12
  const classes = $derived(['sdd-backdrop', restProps.class]);
15
13
 
16
14
  function getStyleOpacity() {
17
15
  if (rootState.dragState.startsWith('drag') && rootState.pointer) {
18
- const handleRect = rootState.handle?.getBoundingClientRect();
19
- if (!handleRect) return 0;
16
+ const handleTop = rootState.handle?.getBoundingClientRect().top ?? rootState.pointer.y;
20
17
 
21
18
  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
19
+ const rangeEnd = Math.max(
20
+ toPixels(rootState.props.range?.end, RANGE_END_DEFAULT),
21
+ rootState.props.snapPoints?.length && rootState.content
22
+ ? rootState.content?.offsetHeight -
23
+ toPixels(rootState.props.snapPoints[0], RANGE_END_DEFAULT)
24
+ : RANGE_END_DEFAULT
27
25
  );
28
- const opacity = (handleTop / windowHeightWithoutRange / 2 - 0.5) * -1;
26
+ const availableHeight = window.innerHeight - rangeEnd - rangeStart;
27
+ const handleOffset = clamp(handleTop - rangeEnd, 0, window.innerHeight - rangeStart);
28
+ const closedProgress = handleOffset / availableHeight;
29
+ const opacity = 0.5 - closedProgress / 2;
29
30
 
30
- return Math.max(Math.min(opacity, 0.5), 0);
31
+ return clamp(opacity, 0, 0.5);
31
32
  }
32
33
 
33
34
  return rootState.props.isOpen ? 0.5 : 0;
@@ -51,13 +52,13 @@
51
52
  });
52
53
 
53
54
  const handleClick = () => {
55
+ rootState.activeSnapPoint = null;
54
56
  rootState.props.isOpen = false;
55
57
  };
56
58
  </script>
57
59
 
58
60
  <div
59
61
  bind:this={ref}
60
- bind:offsetHeight
61
62
  class={classes}
62
63
  style:opacity={styleOpacity}
63
64
  style:transition={styleTransition}
@@ -47,6 +47,10 @@
47
47
  return `translate3d(0, ${y}px, 0)`;
48
48
  }
49
49
 
50
+ if (rootState.activeSnapPoint !== null) {
51
+ return `translate3d(0, calc(100% - ${toCSSLength(rootState.props.snapPoints![rootState.activeSnapPoint.index])}), 0)`;
52
+ }
53
+
50
54
  return rootState.props.isOpen
51
55
  ? `translate3d(0, calc(0% + ${toCSSLength(rootState.props.range?.end ?? RANGE_END_DEFAULT)}), 0)`
52
56
  : `translate3d(0, calc(100% - ${toCSSLength(rootState.props.range?.start ?? RANGE_START_DEFAULT)}), 0)`;
@@ -1,2 +1,3 @@
1
1
  export declare const RANGE_START_DEFAULT = 0;
2
2
  export declare const RANGE_END_DEFAULT = 48;
3
+ export declare const SNAP_POINT_OFFSET = 80;
@@ -1,2 +1,3 @@
1
1
  export const RANGE_START_DEFAULT = 0;
2
2
  export const RANGE_END_DEFAULT = 48;
3
+ export const SNAP_POINT_OFFSET = 80;
@@ -17,6 +17,10 @@ declare class DrawerRootState {
17
17
  x: number;
18
18
  y: number;
19
19
  } | null;
20
+ activeSnapPoint: {
21
+ index: number;
22
+ value: string | number;
23
+ } | null;
20
24
  }
21
25
  export declare function setDrawerRootState(): DrawerRootState;
22
26
  export declare function getDrawerRootState(): DrawerRootState;
@@ -8,6 +8,7 @@ class DrawerRootState {
8
8
  pointer = $state.raw(null);
9
9
  pointerOrigin = $state.raw(null);
10
10
  contentOrigin = $state.raw(null);
11
+ activeSnapPoint = $state(null);
11
12
  }
12
13
  const KEY = Symbol('Drawer.RootState');
13
14
  export function setDrawerRootState() {
@@ -5,6 +5,11 @@ export interface DrawerRootProps {
5
5
  start: string | number;
6
6
  end: string | number;
7
7
  };
8
+ snapPoints?: (string | number)[];
9
+ activeSnapPoint?: {
10
+ index: number;
11
+ value: string | number;
12
+ } | null;
8
13
  isOpen?: boolean;
9
14
  children?: Snippet;
10
15
  }
@@ -0,0 +1 @@
1
+ export declare function clamp(value: number, min: number, max: number): number;
@@ -0,0 +1,3 @@
1
+ export function clamp(value, min, max) {
2
+ return Math.min(Math.max(value, min), max);
3
+ }
@@ -1,2 +1,3 @@
1
1
  export * from './conversion.ts';
2
- export * from './physics.js';
2
+ export * from './geometry.ts';
3
+ export * from './physics.ts';
@@ -1,2 +1,3 @@
1
1
  export * from "./conversion.js";
2
- export * from './physics.js';
2
+ export * from "./geometry.js";
3
+ export * from "./physics.js";
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.3.0",
4
+ "version": "0.4.0",
5
5
  "author": "Rodrigo D’Agostino <rodrigo.dagostino@proton.me>",
6
6
  "keywords": [
7
7
  "draggable",