@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,13 @@
1
+ const subSlotCache = new Map<symbol, Map<string, symbol>>();
2
+
3
+ export function subSlot(slot: symbol | undefined, tag: string): symbol | undefined {
4
+ if (slot === undefined) return undefined;
5
+ let byTag = subSlotCache.get(slot);
6
+ if (byTag === undefined) subSlotCache.set(slot, (byTag = new Map()));
7
+ let value = byTag.get(tag);
8
+ if (value === undefined) {
9
+ value = Symbol.for(`@octanejs/vaul:${slot.description ?? ''}:${tag}`);
10
+ byTag.set(tag, value);
11
+ }
12
+ return value;
13
+ }
package/src/style.css ADDED
@@ -0,0 +1,256 @@
1
+ [data-vaul-drawer] {
2
+ touch-action: none;
3
+ will-change: transform;
4
+ transition: transform 0.5s cubic-bezier(0.32, 0.72, 0, 1);
5
+ animation-duration: 0.5s;
6
+ animation-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
7
+ }
8
+
9
+ [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='bottom'][data-state='open'] {
10
+ animation-name: slideFromBottom;
11
+ }
12
+ [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='bottom'][data-state='closed'] {
13
+ animation-name: slideToBottom;
14
+ }
15
+
16
+ [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='top'][data-state='open'] {
17
+ animation-name: slideFromTop;
18
+ }
19
+ [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='top'][data-state='closed'] {
20
+ animation-name: slideToTop;
21
+ }
22
+
23
+ [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='left'][data-state='open'] {
24
+ animation-name: slideFromLeft;
25
+ }
26
+ [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='left'][data-state='closed'] {
27
+ animation-name: slideToLeft;
28
+ }
29
+
30
+ [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='right'][data-state='open'] {
31
+ animation-name: slideFromRight;
32
+ }
33
+ [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='right'][data-state='closed'] {
34
+ animation-name: slideToRight;
35
+ }
36
+
37
+ [data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='bottom'] {
38
+ transform: translate3d(0, var(--initial-transform, 100%), 0);
39
+ }
40
+
41
+ [data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='top'] {
42
+ transform: translate3d(0, calc(var(--initial-transform, 100%) * -1), 0);
43
+ }
44
+
45
+ [data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='left'] {
46
+ transform: translate3d(calc(var(--initial-transform, 100%) * -1), 0, 0);
47
+ }
48
+
49
+ [data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='right'] {
50
+ transform: translate3d(var(--initial-transform, 100%), 0, 0);
51
+ }
52
+
53
+ [data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='top'] {
54
+ transform: translate3d(0, var(--snap-point-height, 0), 0);
55
+ }
56
+
57
+ [data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='bottom'] {
58
+ transform: translate3d(0, var(--snap-point-height, 0), 0);
59
+ }
60
+
61
+ [data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='left'] {
62
+ transform: translate3d(var(--snap-point-height, 0), 0, 0);
63
+ }
64
+
65
+ [data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='right'] {
66
+ transform: translate3d(var(--snap-point-height, 0), 0, 0);
67
+ }
68
+
69
+ [data-vaul-overlay][data-vaul-snap-points='false'] {
70
+ animation-duration: 0.5s;
71
+ animation-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
72
+ }
73
+ [data-vaul-overlay][data-vaul-snap-points='false'][data-state='open'] {
74
+ animation-name: fadeIn;
75
+ }
76
+ [data-vaul-overlay][data-state='closed'] {
77
+ animation-name: fadeOut;
78
+ }
79
+
80
+ [data-vaul-animate='false'] {
81
+ animation: none !important;
82
+ }
83
+
84
+ [data-vaul-overlay][data-vaul-snap-points='true'] {
85
+ opacity: 0;
86
+ transition: opacity 0.5s cubic-bezier(0.32, 0.72, 0, 1);
87
+ }
88
+
89
+ [data-vaul-overlay][data-vaul-snap-points='true'] {
90
+ opacity: 1;
91
+ }
92
+
93
+ [data-vaul-drawer]:not([data-vaul-custom-container='true'])::after {
94
+ content: '';
95
+ position: absolute;
96
+ background: inherit;
97
+ background-color: inherit;
98
+ }
99
+
100
+ [data-vaul-drawer][data-vaul-drawer-direction='top']::after {
101
+ top: initial;
102
+ bottom: 100%;
103
+ left: 0;
104
+ right: 0;
105
+ height: 200%;
106
+ }
107
+
108
+ [data-vaul-drawer][data-vaul-drawer-direction='bottom']::after {
109
+ top: 100%;
110
+ bottom: initial;
111
+ left: 0;
112
+ right: 0;
113
+ height: 200%;
114
+ }
115
+
116
+ [data-vaul-drawer][data-vaul-drawer-direction='left']::after {
117
+ left: initial;
118
+ right: 100%;
119
+ top: 0;
120
+ bottom: 0;
121
+ width: 200%;
122
+ }
123
+
124
+ [data-vaul-drawer][data-vaul-drawer-direction='right']::after {
125
+ left: 100%;
126
+ right: initial;
127
+ top: 0;
128
+ bottom: 0;
129
+ width: 200%;
130
+ }
131
+
132
+ [data-vaul-overlay][data-vaul-snap-points='true']:not([data-vaul-snap-points-overlay='true']):not(
133
+ [data-state='closed']
134
+ ) {
135
+ opacity: 0;
136
+ }
137
+
138
+ [data-vaul-overlay][data-vaul-snap-points-overlay='true'] {
139
+ opacity: 1;
140
+ }
141
+
142
+ [data-vaul-handle] {
143
+ display: block;
144
+ position: relative;
145
+ opacity: 0.7;
146
+ background: #e2e2e4;
147
+ margin-left: auto;
148
+ margin-right: auto;
149
+ height: 5px;
150
+ width: 32px;
151
+ border-radius: 1rem;
152
+ touch-action: pan-y;
153
+ }
154
+
155
+ [data-vaul-handle]:hover,
156
+ [data-vaul-handle]:active {
157
+ opacity: 1;
158
+ }
159
+
160
+ [data-vaul-handle-hitarea] {
161
+ position: absolute;
162
+ left: 50%;
163
+ top: 50%;
164
+ transform: translate(-50%, -50%);
165
+ width: max(100%, 2.75rem); /* 44px */
166
+ height: max(100%, 2.75rem); /* 44px */
167
+ touch-action: inherit;
168
+ }
169
+
170
+ @media (hover: hover) and (pointer: fine) {
171
+ [data-vaul-drawer] {
172
+ user-select: none;
173
+ }
174
+ }
175
+
176
+ @media (pointer: fine) {
177
+ [data-vaul-handle-hitarea]: {
178
+ width: 100%;
179
+ height: 100%;
180
+ }
181
+ }
182
+
183
+ @keyframes fadeIn {
184
+ from {
185
+ opacity: 0;
186
+ }
187
+ to {
188
+ opacity: 1;
189
+ }
190
+ }
191
+
192
+ @keyframes fadeOut {
193
+ to {
194
+ opacity: 0;
195
+ }
196
+ }
197
+
198
+ @keyframes slideFromBottom {
199
+ from {
200
+ transform: translate3d(0, var(--initial-transform, 100%), 0);
201
+ }
202
+ to {
203
+ transform: translate3d(0, 0, 0);
204
+ }
205
+ }
206
+
207
+ @keyframes slideToBottom {
208
+ to {
209
+ transform: translate3d(0, var(--initial-transform, 100%), 0);
210
+ }
211
+ }
212
+
213
+ @keyframes slideFromTop {
214
+ from {
215
+ transform: translate3d(0, calc(var(--initial-transform, 100%) * -1), 0);
216
+ }
217
+ to {
218
+ transform: translate3d(0, 0, 0);
219
+ }
220
+ }
221
+
222
+ @keyframes slideToTop {
223
+ to {
224
+ transform: translate3d(0, calc(var(--initial-transform, 100%) * -1), 0);
225
+ }
226
+ }
227
+
228
+ @keyframes slideFromLeft {
229
+ from {
230
+ transform: translate3d(calc(var(--initial-transform, 100%) * -1), 0, 0);
231
+ }
232
+ to {
233
+ transform: translate3d(0, 0, 0);
234
+ }
235
+ }
236
+
237
+ @keyframes slideToLeft {
238
+ to {
239
+ transform: translate3d(calc(var(--initial-transform, 100%) * -1), 0, 0);
240
+ }
241
+ }
242
+
243
+ @keyframes slideFromRight {
244
+ from {
245
+ transform: translate3d(var(--initial-transform, 100%), 0, 0);
246
+ }
247
+ to {
248
+ transform: translate3d(0, 0, 0);
249
+ }
250
+ }
251
+
252
+ @keyframes slideToRight {
253
+ to {
254
+ transform: translate3d(var(--initial-transform, 100%), 0, 0);
255
+ }
256
+ }
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export type DrawerDirection = 'top' | 'bottom' | 'left' | 'right';
2
+ export interface SnapPoint {
3
+ fraction: number;
4
+ height: number;
5
+ }
6
+
7
+ export type AnyFunction = (...args: any) => any;
@@ -0,0 +1 @@
1
+ export { composeRefs, useComposedRefs } from '@octanejs/radix';
@@ -0,0 +1 @@
1
+ export { useControllableState } from '@octanejs/radix';
@@ -0,0 +1,174 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'octane';
2
+ import { isSafari } from './browser';
3
+ import { subSlot } from './internal';
4
+
5
+ let previousBodyPosition: Record<string, string> | null = null;
6
+
7
+ /**
8
+ * This hook is necessary to prevent buggy behavior on iOS devices (need to test on Android).
9
+ * I won't get into too much detail about what bugs it solves, but so far I've found that setting the body to `position: fixed` is the most reliable way to prevent those bugs.
10
+ * Issues that this hook solves:
11
+ * https://github.com/emilkowalski/vaul/issues/435
12
+ * https://github.com/emilkowalski/vaul/issues/433
13
+ * And more that I discovered, but were just not reported.
14
+ */
15
+
16
+ export function usePositionFixed(
17
+ {
18
+ isOpen,
19
+ modal,
20
+ nested,
21
+ hasBeenOpened,
22
+ preventScrollRestoration,
23
+ noBodyStyles,
24
+ }: {
25
+ isOpen: boolean;
26
+ modal: boolean;
27
+ nested: boolean;
28
+ hasBeenOpened: boolean;
29
+ preventScrollRestoration: boolean;
30
+ noBodyStyles: boolean;
31
+ },
32
+ slot?: symbol,
33
+ ) {
34
+ const [activeUrl, setActiveUrl] = useState(
35
+ () => (typeof window !== 'undefined' ? window.location.href : ''),
36
+ subSlot(slot, 'url'),
37
+ );
38
+ const scrollPos = useRef(0, subSlot(slot, 'scroll'));
39
+
40
+ const setPositionFixed = useCallback(
41
+ () => {
42
+ // All browsers on iOS will return true here.
43
+ if (!isSafari()) return;
44
+
45
+ // If previousBodyPosition is already set, don't set it again.
46
+ if (previousBodyPosition === null && isOpen && !noBodyStyles) {
47
+ previousBodyPosition = {
48
+ position: document.body.style.position,
49
+ top: document.body.style.top,
50
+ left: document.body.style.left,
51
+ height: document.body.style.height,
52
+ right: 'unset',
53
+ };
54
+
55
+ // Update the dom inside an animation frame
56
+ const { scrollX, innerHeight } = window;
57
+
58
+ document.body.style.setProperty('position', 'fixed', 'important');
59
+ Object.assign(document.body.style, {
60
+ top: `${-scrollPos.current}px`,
61
+ left: `${-scrollX}px`,
62
+ right: '0px',
63
+ height: 'auto',
64
+ });
65
+
66
+ window.setTimeout(
67
+ () =>
68
+ window.requestAnimationFrame(() => {
69
+ // Attempt to check if the bottom bar appeared due to the position change
70
+ const bottomBarHeight = innerHeight - window.innerHeight;
71
+ if (bottomBarHeight && scrollPos.current >= innerHeight) {
72
+ // Move the content further up so that the bottom bar doesn't hide it
73
+ document.body.style.top = `${-(scrollPos.current + bottomBarHeight)}px`;
74
+ }
75
+ }),
76
+ 300,
77
+ );
78
+ }
79
+ },
80
+ [isOpen, noBodyStyles],
81
+ subSlot(slot, 'fix'),
82
+ );
83
+
84
+ const restorePositionSetting = useCallback(
85
+ () => {
86
+ // All browsers on iOS will return true here.
87
+ if (!isSafari()) return;
88
+
89
+ if (previousBodyPosition !== null && !noBodyStyles) {
90
+ // Convert the position from "px" to Int
91
+ const y = -parseInt(document.body.style.top, 10);
92
+ const x = -parseInt(document.body.style.left, 10);
93
+
94
+ // Restore styles
95
+ Object.assign(document.body.style, previousBodyPosition);
96
+
97
+ window.requestAnimationFrame(() => {
98
+ if (preventScrollRestoration && activeUrl !== window.location.href) {
99
+ setActiveUrl(window.location.href);
100
+ return;
101
+ }
102
+
103
+ window.scrollTo(x, y);
104
+ });
105
+
106
+ previousBodyPosition = null;
107
+ }
108
+ },
109
+ [activeUrl, noBodyStyles, preventScrollRestoration],
110
+ subSlot(slot, 'restore'),
111
+ );
112
+
113
+ useEffect(
114
+ () => {
115
+ if (!isSafari()) return;
116
+
117
+ function onScroll() {
118
+ scrollPos.current = window.scrollY;
119
+ }
120
+
121
+ onScroll();
122
+
123
+ window.addEventListener('scroll', onScroll);
124
+
125
+ return () => {
126
+ window.removeEventListener('scroll', onScroll);
127
+ };
128
+ },
129
+ [],
130
+ subSlot(slot, 'scroll-effect'),
131
+ );
132
+
133
+ useEffect(
134
+ () => {
135
+ if (!modal) return;
136
+
137
+ return () => {
138
+ if (typeof document === 'undefined') return;
139
+
140
+ // Another drawer is opened, safe to ignore the execution
141
+ const hasDrawerOpened = !!document.querySelector('[data-vaul-drawer]');
142
+ if (hasDrawerOpened) return;
143
+
144
+ restorePositionSetting();
145
+ };
146
+ },
147
+ [modal, restorePositionSetting],
148
+ subSlot(slot, 'cleanup-effect'),
149
+ );
150
+
151
+ useEffect(
152
+ () => {
153
+ if (nested || !hasBeenOpened) return;
154
+ // This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happening
155
+ if (isOpen) {
156
+ // avoid for standalone mode (PWA)
157
+ const isStandalone = window.matchMedia('(display-mode: standalone)').matches;
158
+ !isStandalone && setPositionFixed();
159
+
160
+ if (!modal) {
161
+ window.setTimeout(() => {
162
+ restorePositionSetting();
163
+ }, 500);
164
+ }
165
+ } else {
166
+ restorePositionSetting();
167
+ }
168
+ },
169
+ [isOpen, hasBeenOpened, activeUrl, modal, nested, setPositionFixed, restorePositionSetting],
170
+ subSlot(slot, 'position-effect'),
171
+ );
172
+
173
+ return { restorePositionSetting };
174
+ }