1
1
import { tick } from "svelte" ;
2
2
import { type Writable , derived , get } from "svelte/store" ;
3
3
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" ;
5
5
import type { DrawerDirection } from "./types.js" ;
6
6
7
7
export function handleSnapPoints ( {
@@ -61,7 +61,11 @@ export function handleSnapPoints({
61
61
const $direction = get ( direction ) ;
62
62
63
63
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 ;
65
69
66
70
if ( hasWindow ) {
67
71
return $direction === "bottom"
@@ -72,10 +76,16 @@ export function handleSnapPoints({
72
76
return height ;
73
77
}
74
78
75
- const width = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window . innerWidth : 0 ;
79
+ const width = isPx
80
+ ? snapPointAsNumber
81
+ : hasWindow
82
+ ? snapPoint * window . innerWidth
83
+ : 0 ;
76
84
77
85
if ( hasWindow ) {
78
- return $direction === "right" ? window . innerWidth - width : window . innerWidth + width ;
86
+ return $direction === "right"
87
+ ? window . innerWidth - width
88
+ : window . innerWidth + width ;
79
89
}
80
90
81
91
return width ;
@@ -94,8 +104,13 @@ export function handleSnapPoints({
94
104
if ( $activeSnapPoint && $drawerRef ) {
95
105
const $snapPoints = get ( snapPoints ) ;
96
106
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
+ ) {
99
114
snapToPoint ( $snapPointsOffset [ newIndex ] as number ) ;
100
115
}
101
116
}
@@ -112,7 +127,7 @@ export function handleSnapPoints({
112
127
113
128
onSnapPointChange ( newSnapPointIndex ) ;
114
129
115
- set ( $drawerRef , {
130
+ setStyles ( $drawerRef , {
116
131
transition : `transform ${ TRANSITIONS . DURATION } s cubic-bezier(${ TRANSITIONS . EASE . join (
117
132
","
118
133
) } )`,
@@ -129,14 +144,14 @@ export function handleSnapPoints({
129
144
newSnapPointIndex !== $snapPointsOffset . length - 1 &&
130
145
newSnapPointIndex !== $fadeFromIndex
131
146
) {
132
- set ( $overlayRef , {
147
+ setStyles ( $overlayRef , {
133
148
transition : `opacity ${ TRANSITIONS . DURATION } s cubic-bezier(${ TRANSITIONS . EASE . join (
134
149
","
135
150
) } )`,
136
151
opacity : "0" ,
137
152
} ) ;
138
153
} else {
139
- set ( $overlayRef , {
154
+ setStyles ( $overlayRef , {
140
155
transition : `opacity ${ TRANSITIONS . DURATION } s cubic-bezier(${ TRANSITIONS . EASE . join (
141
156
","
142
157
) } )`,
@@ -181,7 +196,7 @@ export function handleSnapPoints({
181
196
const hasDraggedUp = draggedDistance > 0 ;
182
197
183
198
if ( isOverlaySnapPoint ) {
184
- set ( $overlayRef , {
199
+ setStyles ( $overlayRef , {
185
200
transition : `opacity ${ TRANSITIONS . DURATION } s cubic-bezier(${ TRANSITIONS . EASE . join ( "," ) } )` ,
186
201
} ) ;
187
202
}
@@ -201,7 +216,9 @@ export function handleSnapPoints({
201
216
const closestSnapPoint = $snapPointsOffset ?. reduce ( ( prev , curr ) => {
202
217
if ( typeof prev !== "number" || typeof curr !== "number" ) return prev ;
203
218
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 ;
205
222
} ) ;
206
223
207
224
const dim = isVertical ( $direction ) ? window . innerHeight : window . innerWidth ;
@@ -251,7 +268,7 @@ export function handleSnapPoints({
251
268
return ;
252
269
}
253
270
254
- set ( $drawerRef , {
271
+ setStyles ( $drawerRef , {
255
272
transform : isVertical ( $direction )
256
273
? `translate3d(0, ${ newValue } px, 0)`
257
274
: `translate3d(${ newValue } px, 0, 0)` ,
0 commit comments