Skip to content

Commit 79706a3

Browse files
committed
progress
1 parent 1bf0e30 commit 79706a3

File tree

5 files changed

+1040
-56
lines changed

5 files changed

+1040
-56
lines changed

packages/vaul-svelte/src/lib/internal/helpers/style.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Style {
77

88
const cache = new WeakMap();
99

10-
export function set(el?: Element | HTMLElement | null, styles?: Style, ignoreCache = false) {
10+
export function setStyles(el?: Element | HTMLElement | null, styles?: Style, ignoreCache = false) {
1111
if (!el || !(el instanceof HTMLElement) || !styles) return;
1212

1313
const originalStyles: Style = {};
@@ -29,7 +29,7 @@ export function set(el?: Element | HTMLElement | null, styles?: Style, ignoreCac
2929
cache.set(el, originalStyles);
3030
}
3131

32-
export function reset(el: Element | HTMLElement | null, prop?: string) {
32+
export function resetStyles(el: Element | HTMLElement | null, prop?: string) {
3333
if (!el || !(el instanceof HTMLElement)) return;
3434
const originalStyles = cache.get(el);
3535

@@ -66,6 +66,6 @@ export function getTranslate(element: HTMLElement, direction: DrawerDirection):
6666
export function styleToString(style: Record<string, number | string | undefined>): string {
6767
return Object.keys(style).reduce((str, key) => {
6868
if (style[key] === undefined) return str;
69-
return `${str }${key}:${style[key]};`;
69+
return `${str}${key}:${style[key]};`;
7070
}, "");
7171
}

packages/vaul-svelte/src/lib/internal/snap-points.ts

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { tick } from "svelte";
22
import { type Writable, derived, get } from "svelte/store";
33
import { TRANSITIONS, VELOCITY_THRESHOLD } from "./constants.js";
4-
import { effect, isBottomOrRight, isVertical, set } from "./helpers/index.js";
4+
import { effect, isBottomOrRight, isVertical, setStyles } from "./helpers/index.js";
55
import type { DrawerDirection } from "./types.js";
66

77
export function handleSnapPoints({
@@ -61,7 +61,11 @@ export function handleSnapPoints({
6161
const $direction = get(direction);
6262

6363
if (isVertical($direction)) {
64-
const height = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window.innerHeight : 0;
64+
const height = isPx
65+
? snapPointAsNumber
66+
: hasWindow
67+
? snapPoint * window.innerHeight
68+
: 0;
6569

6670
if (hasWindow) {
6771
return $direction === "bottom"
@@ -72,10 +76,16 @@ export function handleSnapPoints({
7276
return height;
7377
}
7478

75-
const width = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window.innerWidth : 0;
79+
const width = isPx
80+
? snapPointAsNumber
81+
: hasWindow
82+
? snapPoint * window.innerWidth
83+
: 0;
7684

7785
if (hasWindow) {
78-
return $direction === "right" ? window.innerWidth - width : window.innerWidth + width;
86+
return $direction === "right"
87+
? window.innerWidth - width
88+
: window.innerWidth + width;
7989
}
8090

8191
return width;
@@ -94,8 +104,13 @@ export function handleSnapPoints({
94104
if ($activeSnapPoint && $drawerRef) {
95105
const $snapPoints = get(snapPoints);
96106
const $snapPointsOffset = get(snapPointsOffset);
97-
const newIndex = $snapPoints?.findIndex((snapPoint) => snapPoint === $activeSnapPoint) ?? -1;
98-
if ($snapPointsOffset && newIndex !== -1 && typeof $snapPointsOffset[newIndex] === "number") {
107+
const newIndex =
108+
$snapPoints?.findIndex((snapPoint) => snapPoint === $activeSnapPoint) ?? -1;
109+
if (
110+
$snapPointsOffset &&
111+
newIndex !== -1 &&
112+
typeof $snapPointsOffset[newIndex] === "number"
113+
) {
99114
snapToPoint($snapPointsOffset[newIndex] as number);
100115
}
101116
}
@@ -112,7 +127,7 @@ export function handleSnapPoints({
112127

113128
onSnapPointChange(newSnapPointIndex);
114129

115-
set($drawerRef, {
130+
setStyles($drawerRef, {
116131
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(
117132
","
118133
)})`,
@@ -129,14 +144,14 @@ export function handleSnapPoints({
129144
newSnapPointIndex !== $snapPointsOffset.length - 1 &&
130145
newSnapPointIndex !== $fadeFromIndex
131146
) {
132-
set($overlayRef, {
147+
setStyles($overlayRef, {
133148
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(
134149
","
135150
)})`,
136151
opacity: "0",
137152
});
138153
} else {
139-
set($overlayRef, {
154+
setStyles($overlayRef, {
140155
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(
141156
","
142157
)})`,
@@ -181,7 +196,7 @@ export function handleSnapPoints({
181196
const hasDraggedUp = draggedDistance > 0;
182197

183198
if (isOverlaySnapPoint) {
184-
set($overlayRef, {
199+
setStyles($overlayRef, {
185200
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(",")})`,
186201
});
187202
}
@@ -201,7 +216,9 @@ export function handleSnapPoints({
201216
const closestSnapPoint = $snapPointsOffset?.reduce((prev, curr) => {
202217
if (typeof prev !== "number" || typeof curr !== "number") return prev;
203218

204-
return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev;
219+
return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition)
220+
? curr
221+
: prev;
205222
});
206223

207224
const dim = isVertical($direction) ? window.innerHeight : window.innerWidth;
@@ -251,7 +268,7 @@ export function handleSnapPoints({
251268
return;
252269
}
253270

254-
set($drawerRef, {
271+
setStyles($drawerRef, {
255272
transform: isVertical($direction)
256273
? `translate3d(0, ${newValue}px, 0)`
257274
: `translate3d(${newValue}px, 0, 0)`,

0 commit comments

Comments
 (0)