@octanejs/vaul 0.0.1

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.
@@ -0,0 +1,368 @@
1
+ import { useCallback, useEffect, useMemo, useState } from 'octane';
2
+ import type React from 'react';
3
+ import { set, isVertical } from './helpers';
4
+ import { TRANSITIONS, VELOCITY_THRESHOLD } from './constants';
5
+ import { useControllableState } from './use-controllable-state';
6
+ import type { DrawerDirection } from './types';
7
+ import { subSlot } from './internal';
8
+
9
+ export function useSnapPoints(
10
+ {
11
+ activeSnapPointProp,
12
+ setActiveSnapPointProp,
13
+ snapPoints,
14
+ drawerRef,
15
+ overlayRef,
16
+ fadeFromIndex,
17
+ onSnapPointChange,
18
+ direction = 'bottom',
19
+ container,
20
+ snapToSequentialPoint,
21
+ }: {
22
+ activeSnapPointProp?: number | string | null;
23
+ setActiveSnapPointProp?(snapPoint: number | null | string): void;
24
+ snapPoints?: (number | string)[];
25
+ fadeFromIndex?: number;
26
+ drawerRef: React.RefObject<HTMLDivElement | null>;
27
+ overlayRef: React.RefObject<HTMLDivElement | null>;
28
+ onSnapPointChange(activeSnapPointIndex: number): void;
29
+ direction?: DrawerDirection;
30
+ container?: HTMLElement | null | undefined;
31
+ snapToSequentialPoint?: boolean;
32
+ },
33
+ slot?: symbol,
34
+ ) {
35
+ const [activeSnapPoint, setActiveSnapPoint] = useControllableState<string | number | null>(
36
+ {
37
+ prop: activeSnapPointProp,
38
+ defaultProp: snapPoints?.[0],
39
+ onChange: setActiveSnapPointProp,
40
+ },
41
+ subSlot(slot, 'active'),
42
+ );
43
+
44
+ const [windowDimensions, setWindowDimensions] = useState(
45
+ typeof window !== 'undefined'
46
+ ? {
47
+ innerWidth: window.innerWidth,
48
+ innerHeight: window.innerHeight,
49
+ }
50
+ : undefined,
51
+ subSlot(slot, 'dimensions'),
52
+ );
53
+
54
+ useEffect(
55
+ () => {
56
+ if (!snapPoints?.length) return;
57
+
58
+ function onResize() {
59
+ setWindowDimensions((current) => {
60
+ if (
61
+ current?.innerWidth === window.innerWidth &&
62
+ current.innerHeight === window.innerHeight
63
+ ) {
64
+ return current;
65
+ }
66
+ return {
67
+ innerWidth: window.innerWidth,
68
+ innerHeight: window.innerHeight,
69
+ };
70
+ });
71
+ }
72
+ window.addEventListener('resize', onResize);
73
+
74
+ return () => window.removeEventListener('resize', onResize);
75
+ },
76
+ [Boolean(snapPoints?.length)],
77
+ subSlot(slot, 'resize'),
78
+ );
79
+
80
+ const isLastSnapPoint = useMemo(
81
+ () => activeSnapPoint === snapPoints?.[snapPoints.length - 1] || null,
82
+ [snapPoints, activeSnapPoint],
83
+ subSlot(slot, 'last'),
84
+ );
85
+
86
+ const activeSnapPointIndex = useMemo(
87
+ () => snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPoint) ?? null,
88
+ [snapPoints, activeSnapPoint],
89
+ subSlot(slot, 'index'),
90
+ );
91
+
92
+ const shouldFade =
93
+ (snapPoints &&
94
+ snapPoints.length > 0 &&
95
+ (fadeFromIndex || fadeFromIndex === 0) &&
96
+ !Number.isNaN(fadeFromIndex) &&
97
+ snapPoints[fadeFromIndex] === activeSnapPoint) ||
98
+ !snapPoints;
99
+
100
+ const snapPointsOffset = useMemo(
101
+ () => {
102
+ const containerRect = container?.getBoundingClientRect();
103
+ const containerSize = containerRect
104
+ ? { width: containerRect.width, height: containerRect.height }
105
+ : typeof window !== 'undefined'
106
+ ? { width: window.innerWidth, height: window.innerHeight }
107
+ : { width: 0, height: 0 };
108
+
109
+ return (
110
+ snapPoints?.map((snapPoint) => {
111
+ const isPx = typeof snapPoint === 'string';
112
+ let snapPointAsNumber = 0;
113
+
114
+ if (isPx) {
115
+ snapPointAsNumber = parseInt(snapPoint, 10);
116
+ }
117
+
118
+ if (isVertical(direction)) {
119
+ const height = isPx
120
+ ? snapPointAsNumber
121
+ : windowDimensions
122
+ ? snapPoint * containerSize.height
123
+ : 0;
124
+
125
+ if (windowDimensions) {
126
+ return direction === 'bottom'
127
+ ? containerSize.height - height
128
+ : -containerSize.height + height;
129
+ }
130
+
131
+ return height;
132
+ }
133
+ const width = isPx
134
+ ? snapPointAsNumber
135
+ : windowDimensions
136
+ ? snapPoint * containerSize.width
137
+ : 0;
138
+
139
+ if (windowDimensions) {
140
+ return direction === 'right'
141
+ ? containerSize.width - width
142
+ : -containerSize.width + width;
143
+ }
144
+
145
+ return width;
146
+ }) ?? []
147
+ );
148
+ },
149
+ [snapPoints, windowDimensions, container, direction],
150
+ subSlot(slot, 'offsets'),
151
+ );
152
+
153
+ const activeSnapPointOffset = useMemo(
154
+ () => (activeSnapPointIndex !== null ? snapPointsOffset?.[activeSnapPointIndex] : null),
155
+ [snapPointsOffset, activeSnapPointIndex],
156
+ subSlot(slot, 'active-offset'),
157
+ );
158
+
159
+ const snapToPoint = useCallback(
160
+ (dimension: number) => {
161
+ const newSnapPointIndex =
162
+ snapPointsOffset?.findIndex((snapPointDim) => snapPointDim === dimension) ?? null;
163
+ onSnapPointChange(newSnapPointIndex);
164
+
165
+ set(drawerRef.current, {
166
+ transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
167
+ transform: isVertical(direction)
168
+ ? `translate3d(0, ${dimension}px, 0)`
169
+ : `translate3d(${dimension}px, 0, 0)`,
170
+ });
171
+
172
+ if (
173
+ snapPointsOffset &&
174
+ newSnapPointIndex !== snapPointsOffset.length - 1 &&
175
+ fadeFromIndex !== undefined &&
176
+ newSnapPointIndex !== fadeFromIndex &&
177
+ newSnapPointIndex < fadeFromIndex
178
+ ) {
179
+ set(overlayRef.current, {
180
+ transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
181
+ opacity: '0',
182
+ });
183
+ } else {
184
+ set(overlayRef.current, {
185
+ transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
186
+ opacity: '1',
187
+ });
188
+ }
189
+
190
+ setActiveSnapPoint(snapPoints?.[Math.max(newSnapPointIndex, 0)]!);
191
+ },
192
+ [
193
+ drawerRef.current,
194
+ snapPoints,
195
+ snapPointsOffset,
196
+ fadeFromIndex,
197
+ overlayRef,
198
+ setActiveSnapPoint,
199
+ direction,
200
+ onSnapPointChange,
201
+ ],
202
+ subSlot(slot, 'snap'),
203
+ );
204
+
205
+ useEffect(
206
+ () => {
207
+ if (activeSnapPoint || activeSnapPointProp) {
208
+ const newIndex =
209
+ snapPoints?.findIndex(
210
+ (snapPoint) => snapPoint === activeSnapPointProp || snapPoint === activeSnapPoint,
211
+ ) ?? -1;
212
+ if (snapPointsOffset && newIndex !== -1 && typeof snapPointsOffset[newIndex] === 'number') {
213
+ snapToPoint(snapPointsOffset[newIndex] as number);
214
+ }
215
+ }
216
+ },
217
+ [activeSnapPoint, activeSnapPointProp, snapPoints, snapPointsOffset, snapToPoint],
218
+ subSlot(slot, 'sync'),
219
+ );
220
+
221
+ function onRelease({
222
+ draggedDistance,
223
+ closeDrawer,
224
+ velocity,
225
+ dismissible,
226
+ }: {
227
+ draggedDistance: number;
228
+ closeDrawer: () => void;
229
+ velocity: number;
230
+ dismissible: boolean;
231
+ }) {
232
+ if (fadeFromIndex === undefined) return;
233
+
234
+ const currentPosition =
235
+ direction === 'bottom' || direction === 'right'
236
+ ? (activeSnapPointOffset ?? 0) - draggedDistance
237
+ : (activeSnapPointOffset ?? 0) + draggedDistance;
238
+ const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
239
+ const isFirst = activeSnapPointIndex === 0;
240
+ const hasDraggedUp = draggedDistance > 0;
241
+
242
+ if (isOverlaySnapPoint) {
243
+ set(overlayRef.current, {
244
+ transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
245
+ });
246
+ }
247
+
248
+ if (!snapToSequentialPoint && velocity > 2 && !hasDraggedUp) {
249
+ if (dismissible) closeDrawer();
250
+ else snapToPoint(snapPointsOffset[0]); // snap to initial point
251
+ return;
252
+ }
253
+
254
+ if (!snapToSequentialPoint && velocity > 2 && hasDraggedUp && snapPointsOffset && snapPoints) {
255
+ snapToPoint(snapPointsOffset[snapPoints.length - 1] as number);
256
+ return;
257
+ }
258
+
259
+ // Find the closest snap point to the current position
260
+ const closestSnapPoint = snapPointsOffset?.reduce((prev, curr) => {
261
+ if (typeof prev !== 'number' || typeof curr !== 'number') return prev;
262
+
263
+ return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev;
264
+ });
265
+
266
+ const dim = isVertical(direction) ? window.innerHeight : window.innerWidth;
267
+ if (velocity > VELOCITY_THRESHOLD && Math.abs(draggedDistance) < dim * 0.4) {
268
+ const dragDirection = hasDraggedUp ? 1 : -1; // 1 = up, -1 = down
269
+
270
+ // Don't do anything if we swipe upwards while being on the last snap point
271
+ if (dragDirection > 0 && isLastSnapPoint && snapPoints) {
272
+ snapToPoint(snapPointsOffset[snapPoints.length - 1]);
273
+ return;
274
+ }
275
+
276
+ if (isFirst && dragDirection < 0 && dismissible) {
277
+ closeDrawer();
278
+ }
279
+
280
+ if (activeSnapPointIndex === null) return;
281
+
282
+ snapToPoint(snapPointsOffset[activeSnapPointIndex + dragDirection]);
283
+ return;
284
+ }
285
+
286
+ snapToPoint(closestSnapPoint);
287
+ }
288
+
289
+ function onDrag({ draggedDistance }: { draggedDistance: number }) {
290
+ if (activeSnapPointOffset === null) return;
291
+ const newValue =
292
+ direction === 'bottom' || direction === 'right'
293
+ ? activeSnapPointOffset - draggedDistance
294
+ : activeSnapPointOffset + draggedDistance;
295
+
296
+ // Don't do anything if we exceed the last(biggest) snap point
297
+ if (
298
+ (direction === 'bottom' || direction === 'right') &&
299
+ newValue < snapPointsOffset[snapPointsOffset.length - 1]
300
+ ) {
301
+ return;
302
+ }
303
+ if (
304
+ (direction === 'top' || direction === 'left') &&
305
+ newValue > snapPointsOffset[snapPointsOffset.length - 1]
306
+ ) {
307
+ return;
308
+ }
309
+
310
+ set(drawerRef.current, {
311
+ transform: isVertical(direction)
312
+ ? `translate3d(0, ${newValue}px, 0)`
313
+ : `translate3d(${newValue}px, 0, 0)`,
314
+ });
315
+ }
316
+
317
+ function getPercentageDragged(absDraggedDistance: number, isDraggingDown: boolean) {
318
+ if (
319
+ !snapPoints ||
320
+ typeof activeSnapPointIndex !== 'number' ||
321
+ !snapPointsOffset ||
322
+ fadeFromIndex === undefined
323
+ )
324
+ return null;
325
+
326
+ // If this is true we are dragging to a snap point that is supposed to have an overlay
327
+ const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
328
+ const isOverlaySnapPointOrHigher = activeSnapPointIndex >= fadeFromIndex;
329
+
330
+ if (isOverlaySnapPointOrHigher && isDraggingDown) {
331
+ return 0;
332
+ }
333
+
334
+ // Don't animate, but still use this one if we are dragging away from the overlaySnapPoint
335
+ if (isOverlaySnapPoint && !isDraggingDown) return 1;
336
+ if (!shouldFade && !isOverlaySnapPoint) return null;
337
+
338
+ // Either fadeFrom index or the one before
339
+ const targetSnapPointIndex = isOverlaySnapPoint
340
+ ? activeSnapPointIndex + 1
341
+ : activeSnapPointIndex - 1;
342
+
343
+ // Get the distance from overlaySnapPoint to the one before or vice-versa to calculate the opacity percentage accordingly
344
+ const snapPointDistance = isOverlaySnapPoint
345
+ ? snapPointsOffset[targetSnapPointIndex] - snapPointsOffset[targetSnapPointIndex - 1]
346
+ : snapPointsOffset[targetSnapPointIndex + 1] - snapPointsOffset[targetSnapPointIndex];
347
+
348
+ const percentageDragged = absDraggedDistance / Math.abs(snapPointDistance);
349
+
350
+ if (isOverlaySnapPoint) {
351
+ return 1 - percentageDragged;
352
+ } else {
353
+ return percentageDragged;
354
+ }
355
+ }
356
+
357
+ return {
358
+ isLastSnapPoint,
359
+ activeSnapPoint,
360
+ shouldFade,
361
+ getPercentageDragged,
362
+ setActiveSnapPoint,
363
+ activeSnapPointIndex,
364
+ onRelease,
365
+ onDrag,
366
+ snapPointsOffset,
367
+ };
368
+ }
package/status.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "upstream": {
3
+ "package": "vaul",
4
+ "version": "1.1.2",
5
+ "commit": "73d06cdd3fd990bf4b83214cfe240c246908af0d"
6
+ },
7
+ "surface": "Drawer, Root, NestedRoot, Portal, Overlay, Content, Handle, public props, and style.css.",
8
+ "divergences": [
9
+ "React DOM prop types are preserved publicly and normalized at Octane render boundaries."
10
+ ],
11
+ "ssr": "Closed drawer roots and triggers render without browser globals; portaled content remains absent while closed.",
12
+ "verified": "2026-08-02"
13
+ }