@rific/drawer 0.1.1 → 0.2.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.
- package/README.md +60 -4
- package/dist/index.d.mts +42 -10
- package/dist/index.d.ts +42 -10
- package/dist/index.js +198 -43
- package/dist/index.mjs +213 -51
- package/package.json +8 -1
- package/src/Drawer.tsx +242 -24
- package/src/DrawerEdgeSwipe.tsx +45 -18
- package/src/createDrawer.tsx +89 -11
- package/src/geometry.ts +16 -0
- package/src/index.ts +1 -0
package/src/Drawer.tsx
CHANGED
|
@@ -1,54 +1,264 @@
|
|
|
1
1
|
import { Pressable } from '@rific/haptic-press'
|
|
2
|
-
import { ReactNode, useEffect } from 'react'
|
|
3
|
-
import { StyleSheet } from 'react-native'
|
|
2
|
+
import { ComponentType, ReactNode, useEffect, useState } from 'react'
|
|
3
|
+
import { LayoutChangeEvent, Platform, StyleProp, StyleSheet, View, ViewStyle } from 'react-native'
|
|
4
|
+
import { Gesture, GestureDetector } from 'react-native-gesture-handler'
|
|
4
5
|
import { useTheme } from 'react-native-paper'
|
|
5
|
-
import Animated, { SharedValue, useAnimatedStyle, useSharedValue, withSpring } from 'react-native-reanimated'
|
|
6
|
+
import Animated, { runOnJS, SharedValue, useAnimatedStyle, useSharedValue, withSpring, withTiming } from 'react-native-reanimated'
|
|
7
|
+
|
|
8
|
+
import { type DrawerSide, getClosedOffset, getOpenDirection, isVerticalSide } from './geometry'
|
|
6
9
|
|
|
7
10
|
const SPRING = { damping: 40, stiffness: 300, overshootClamping: true }
|
|
11
|
+
const VELOCITY_THRESHOLD = 500
|
|
12
|
+
|
|
13
|
+
// Minimal local shape of @rific/auto-paper covering only the members used below — avoids forcing
|
|
14
|
+
// TypeScript to resolve the optional peer's real types for consumers who never installed it.
|
|
15
|
+
interface AutoPaperModule {
|
|
16
|
+
BlurView: ComponentType<{ children?: ReactNode; onLayout?: (event: LayoutChangeEvent) => void; style?: StyleProp<ViewStyle> }>
|
|
17
|
+
useBlur: (override?: boolean) => boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// @rific/auto-paper is an optional peer — when absent, the panel falls back to a solid fill.
|
|
21
|
+
const AutoPaper = (() => {
|
|
22
|
+
try {
|
|
23
|
+
return require('@rific/auto-paper') as AutoPaperModule
|
|
24
|
+
} catch {
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
})()
|
|
28
|
+
|
|
29
|
+
// useBlur is a hook and must be called unconditionally on every render (rules of hooks) — this
|
|
30
|
+
// fallback stands in when @rific/auto-paper isn't installed, so a per-render branch isn't needed.
|
|
31
|
+
const useBlurFallback = (override?: boolean) => override ?? false
|
|
32
|
+
const useBlurHook = AutoPaper?.useBlur ?? useBlurFallback
|
|
8
33
|
|
|
9
34
|
export type DrawerProps = {
|
|
35
|
+
// Peak backdrop opacity once fully open (scaled down as the panel slides toward closed, same as
|
|
36
|
+
// before) — 0 renders no dimming at all, letting whatever's behind the panel stay fully visible.
|
|
37
|
+
// Independent of blockingBackdrop: a panel can stay undimmed and still block touches, or vice versa.
|
|
38
|
+
backdropOpacity?: number
|
|
39
|
+
// Backdrop still dims/blurs identically either way; this only controls whether it intercepts
|
|
40
|
+
// touches. false lets siblings behind/around the panel stay reachable while open.
|
|
41
|
+
blockingBackdrop?: boolean
|
|
42
|
+
blur?: boolean
|
|
10
43
|
// Renders edge-to-edge behind the status bar/notch/home indicator, deliberately — pad your own
|
|
11
44
|
// content with useSafeAreaInsets() (or a safe-area-aware header like @rific/scroll-view's) rather
|
|
12
45
|
// than have this component impose a strategy that might double up with one you're already using.
|
|
13
46
|
children?: ReactNode
|
|
47
|
+
// When true, the panel sizes itself to its content's natural size along the main axis instead of
|
|
48
|
+
// the fixed height/width below, and animates smoothly as that natural size changes. height/width
|
|
49
|
+
// remain the pre-measurement placeholder (used until the first layout is measured).
|
|
50
|
+
contentSize?: boolean
|
|
51
|
+
// Renders a drag handle that can be swiped to close the panel, independent of the backdrop.
|
|
52
|
+
dismissible?: boolean
|
|
53
|
+
height?: number
|
|
14
54
|
onClose: () => void
|
|
55
|
+
// Fires once the close animation actually finishes settling at closedOffset — not when `open`
|
|
56
|
+
// merely flips to false, which happens the instant something asks to close, well before the panel
|
|
57
|
+
// has visually finished sliding away. Lets a parent (e.g. DrawerProvider's isVisible) track "still
|
|
58
|
+
// on screen, mid-outro" as distinct from "asked to close," for anything that needs to stay above
|
|
59
|
+
// the panel for the full duration it's visible rather than disappearing the moment closing starts.
|
|
60
|
+
onClosed?: () => void
|
|
61
|
+
// Fires whenever the content wrapper's natural size is (re)measured — lets a parent (e.g.
|
|
62
|
+
// DrawerProvider) mirror the live size for its own math (DrawerEdgeSwipe's commit threshold).
|
|
63
|
+
onMeasure?: (size: number) => void
|
|
64
|
+
// Fires once the open animation actually finishes settling at 0 — the open-direction mirror of
|
|
65
|
+
// onClosed above. Not consumed internally by DrawerProvider (isVisible only needs the outro side),
|
|
66
|
+
// but useful for anything that should wait until the panel has visually finished sliding in, e.g.
|
|
67
|
+
// autofocusing a field inside `content` only once it's actually on screen.
|
|
68
|
+
onOpened?: () => void
|
|
15
69
|
open: boolean
|
|
16
|
-
side?:
|
|
17
|
-
|
|
70
|
+
side?: DrawerSide
|
|
71
|
+
// Whether the handle strip's own visual pill graphic draws — independent of `dismissible`, which
|
|
72
|
+
// controls the strip (and its drag-to-dismiss gesture) as a whole. false keeps the strip's hit area
|
|
73
|
+
// (and the gesture attached to it) exactly as before; only the pill itself stops rendering. There's
|
|
74
|
+
// no way to hide the strip's *gesture* while keeping the pill visible — an invisible-but-still-
|
|
75
|
+
// grabbable strip would be a hidden trap for stray touches, not a real use case.
|
|
76
|
+
showHandle?: boolean
|
|
77
|
+
translateOffset?: SharedValue<number>
|
|
18
78
|
width?: number
|
|
79
|
+
// Stacking tier for this panel's backdrop/panel/handle, for apps that may have more than one Drawer
|
|
80
|
+
// open at once (e.g. two separate createDrawer() instances). Plain View z-index ties break on
|
|
81
|
+
// render/mount order, which for nested DrawerProviders is fixed by however they happen to be
|
|
82
|
+
// declared/nested — not necessarily the order that makes sense on screen. The backdrop uses this
|
|
83
|
+
// value directly; the panel and its drag handle use zIndex + 1 / + 2, preserving their relative
|
|
84
|
+
// order within the panel itself. Give a simultaneously-open drawer that should stack on top a
|
|
85
|
+
// higher value than the one(s) underneath it.
|
|
86
|
+
zIndex?: number
|
|
19
87
|
}
|
|
20
88
|
|
|
21
|
-
export const Drawer = ({ children, onClose, open, side = 'left',
|
|
89
|
+
export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur, children, contentSize = false, dismissible = true, height = 300, onClose, onClosed, onMeasure, onOpened, open, side = 'left', showHandle = true, translateOffset: externalTranslateOffset, width = 300, zIndex = 50 }: DrawerProps) => {
|
|
22
90
|
const { colors } = useTheme()
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
91
|
+
const vertical = isVerticalSide(side)
|
|
92
|
+
const size = vertical ? height : width
|
|
93
|
+
const openDirection = getOpenDirection(side)
|
|
94
|
+
const closeDirection = -openDirection as 1 | -1
|
|
95
|
+
|
|
96
|
+
const [measuredSize, setMeasuredSize] = useState<number | null>(null)
|
|
97
|
+
const effectiveSize = contentSize ? (measuredSize ?? size) : size
|
|
98
|
+
|
|
99
|
+
const closedOffset = getClosedOffset(side, effectiveSize)
|
|
100
|
+
const internalTranslateOffset = useSharedValue(closedOffset)
|
|
101
|
+
const translateOffset = externalTranslateOffset ?? internalTranslateOffset
|
|
102
|
+
const resolvedBlur = useBlurHook(blur)
|
|
103
|
+
|
|
104
|
+
// The container's own animated size, eased toward effectiveSize whenever it changes (content-driven
|
|
105
|
+
// sizing only). Kept as a shared value — like translateOffset — rather than driven inline inside
|
|
106
|
+
// useAnimatedStyle, so it only restarts its transition when effectiveSize actually changes.
|
|
107
|
+
const animatedSize = useSharedValue(effectiveSize)
|
|
26
108
|
|
|
27
109
|
useEffect(() => {
|
|
28
|
-
|
|
29
|
-
|
|
110
|
+
if (open) {
|
|
111
|
+
translateOffset.value = withSpring(0, SPRING, (finished) => {
|
|
112
|
+
if (finished && onOpened) runOnJS(onOpened)()
|
|
113
|
+
})
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
translateOffset.value = withSpring(closedOffset, SPRING, (finished) => {
|
|
117
|
+
if (finished && onClosed) runOnJS(onClosed)()
|
|
118
|
+
})
|
|
119
|
+
// onOpened/onClosed deliberately excluded — both are plain closures (DrawerProvider passes a
|
|
120
|
+
// fresh function every render), and including them would restart this spring from its current
|
|
121
|
+
// position every time the parent re-renders for any unrelated reason.
|
|
122
|
+
}, [open, closedOffset, translateOffset])
|
|
123
|
+
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
if (contentSize) {
|
|
126
|
+
animatedSize.value = withTiming(effectiveSize)
|
|
127
|
+
}
|
|
128
|
+
}, [contentSize, effectiveSize, animatedSize])
|
|
129
|
+
|
|
130
|
+
const drawerStyle = useAnimatedStyle(() => ({ transform: [vertical ? { translateY: translateOffset.value } : { translateX: translateOffset.value }] }))
|
|
131
|
+
const backdropStyle = useAnimatedStyle(() => ({ opacity: (1 - Math.abs(translateOffset.value) / effectiveSize) * backdropOpacity }))
|
|
132
|
+
const animatedSizeStyle = useAnimatedStyle(() => (vertical ? { height: animatedSize.value } : { width: animatedSize.value }))
|
|
133
|
+
|
|
134
|
+
const positionStyle = side === 'left' ? styles.anchorLeft : side === 'right' ? styles.anchorRight : side === 'top' ? styles.anchorTop : styles.anchorBottom
|
|
135
|
+
const sizeStyle = vertical ? { height } : { width }
|
|
136
|
+
|
|
137
|
+
const handleLayout = (event: LayoutChangeEvent) => {
|
|
138
|
+
const measured = vertical ? event.nativeEvent.layout.height : event.nativeEvent.layout.width
|
|
139
|
+
setMeasuredSize(measured)
|
|
140
|
+
onMeasure?.(measured)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// contentSize=false: the inner wrapper fills the outer's fixed bounds exactly as before (untouched
|
|
144
|
+
// path). contentSize=true: the background and the content-measuring wrapper have to be two separate
|
|
145
|
+
// views. The wrapper must render at its OWN natural size, unconstrained by the outer's (animated)
|
|
146
|
+
// bounds — using absoluteFill on it would stretch it to match the outer, making onLayout report the
|
|
147
|
+
// container's current size back to itself instead of the content's true size. But the outer's clip
|
|
148
|
+
// window only catches up to a new size gradually (see the animatedSize effect above), so on any
|
|
149
|
+
// frame where content is smaller than the still-large outer, a background sized to CONTENT instead
|
|
150
|
+
// of the outer leaves a gap below it — whatever's behind the drawer shows through until the outer
|
|
151
|
+
// finishes shrinking. So the background is its own absoluteFill sibling, always exactly the outer's
|
|
152
|
+
// current (possibly mid-animation) size, and only the wrapper is sized/measured naturally — the same
|
|
153
|
+
// structure as a CSS accordion expand/collapse either way.
|
|
154
|
+
const fillStyle: StyleProp<ViewStyle> = StyleSheet.absoluteFill
|
|
155
|
+
|
|
156
|
+
const fill =
|
|
157
|
+
resolvedBlur && AutoPaper?.BlurView ? (
|
|
158
|
+
contentSize ? (
|
|
159
|
+
<>
|
|
160
|
+
<AutoPaper.BlurView style={fillStyle} />
|
|
161
|
+
<View onLayout={handleLayout}>{children}</View>
|
|
162
|
+
</>
|
|
163
|
+
) : (
|
|
164
|
+
<AutoPaper.BlurView style={fillStyle}>{children}</AutoPaper.BlurView>
|
|
165
|
+
)
|
|
166
|
+
) : contentSize ? (
|
|
167
|
+
<>
|
|
168
|
+
<View style={[fillStyle, { backgroundColor: colors.surface }]} />
|
|
169
|
+
<View onLayout={handleLayout}>{children}</View>
|
|
170
|
+
</>
|
|
171
|
+
) : (
|
|
172
|
+
<View style={[fillStyle, { backgroundColor: colors.surface }]}>{children}</View>
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
const drawerOuterStyle: StyleProp<ViewStyle> = contentSize ? [styles.drawer, positionStyle, styles.clip, { flexDirection: vertical ? 'column' : 'row' }, drawerStyle, animatedSizeStyle, { zIndex: zIndex + 1 }] : [styles.drawer, positionStyle, sizeStyle, drawerStyle, { zIndex: zIndex + 1 }]
|
|
176
|
+
|
|
177
|
+
// Dismiss-by-drag handle: a strip pinned to the edge closest to the closed direction (the edge the
|
|
178
|
+
// panel slides back toward), gesture-matched to `side` the same way DrawerEdgeSwipe is, just
|
|
179
|
+
// mirrored — it starts from the OPEN position (0) and commits toward `closedOffset` instead of the
|
|
180
|
+
// reverse. Deliberately its own hit area rather than the whole panel, so it can't steal touches from
|
|
181
|
+
// arbitrary interactive content inside `children`.
|
|
182
|
+
const clampMin = Math.min(0, closedOffset)
|
|
183
|
+
const clampMax = Math.max(0, closedOffset)
|
|
30
184
|
|
|
31
|
-
const
|
|
32
|
-
|
|
185
|
+
const handleAxisGesture = vertical
|
|
186
|
+
? Gesture.Pan()
|
|
187
|
+
.activeOffsetY(closeDirection * 10)
|
|
188
|
+
.failOffsetX([-15, 15])
|
|
189
|
+
: Gesture.Pan()
|
|
190
|
+
.activeOffsetX(closeDirection * 10)
|
|
191
|
+
.failOffsetY([-15, 15])
|
|
33
192
|
|
|
34
|
-
const
|
|
193
|
+
const handleGesture = handleAxisGesture
|
|
194
|
+
.enabled(open)
|
|
195
|
+
.onUpdate((event) => {
|
|
196
|
+
'worklet'
|
|
197
|
+
const translation = vertical ? event.translationY : event.translationX
|
|
198
|
+
translateOffset.value = Math.min(clampMax, Math.max(clampMin, translation))
|
|
199
|
+
})
|
|
200
|
+
.onEnd((event) => {
|
|
201
|
+
'worklet'
|
|
202
|
+
const translation = vertical ? event.translationY : event.translationX
|
|
203
|
+
const velocity = vertical ? event.velocityY : event.velocityX
|
|
204
|
+
const commit = translation * closeDirection > effectiveSize / 3 || velocity * closeDirection > VELOCITY_THRESHOLD
|
|
205
|
+
if (commit) {
|
|
206
|
+
translateOffset.value = withSpring(closedOffset, SPRING)
|
|
207
|
+
runOnJS(onClose)()
|
|
208
|
+
} else {
|
|
209
|
+
translateOffset.value = withSpring(0, SPRING)
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
// The whole strip along the closed-direction edge is grabbable, flush against it — not just the
|
|
214
|
+
// small pill drawn inside it — so there's nothing precise to aim for. The strip's own thickness
|
|
215
|
+
// already reads as clearance from the rest of the panel's content, so there's no separate inset
|
|
216
|
+
// spent on top of that for nothing.
|
|
217
|
+
const HANDLE_STRIP_THICKNESS = 28
|
|
218
|
+
const handleStripSizeStyle: ViewStyle = vertical ? { height: HANDLE_STRIP_THICKNESS, left: 0, right: 0 } : { bottom: 0, top: 0, width: HANDLE_STRIP_THICKNESS }
|
|
219
|
+
const handleStripEdgeStyle: ViewStyle = side === 'bottom' ? { top: 0 } : side === 'top' ? { bottom: 0 } : side === 'left' ? { right: 0 } : { left: 0 }
|
|
220
|
+
const handlePillStyle: ViewStyle = vertical ? { height: 6, width: 64 } : { height: 64, width: 6 }
|
|
221
|
+
// react-native-web reads `cursor` straight off style; native ignores the unknown key. ViewStyle (the
|
|
222
|
+
// native type these files are typed against) has no such property, hence the cast.
|
|
223
|
+
const webCursorStyle = { cursor: Platform.OS === 'web' ? 'pointer' : undefined } as ViewStyle
|
|
35
224
|
|
|
36
225
|
return (
|
|
37
226
|
<>
|
|
38
|
-
<Animated.View style={[styles.backdrop, backdropStyle]} pointerEvents={open ? 'auto' : 'none'}>
|
|
227
|
+
<Animated.View style={[styles.backdrop, { zIndex }, backdropStyle]} pointerEvents={open && blockingBackdrop ? 'auto' : 'none'}>
|
|
39
228
|
<Pressable style={StyleSheet.absoluteFill} onPress={onClose} />
|
|
40
229
|
</Animated.View>
|
|
41
|
-
<Animated.View style={
|
|
230
|
+
<Animated.View style={drawerOuterStyle}>
|
|
231
|
+
{fill}
|
|
232
|
+
{dismissible && (
|
|
233
|
+
<GestureDetector gesture={handleGesture}>
|
|
234
|
+
<View style={[styles.handleStrip, handleStripSizeStyle, handleStripEdgeStyle, webCursorStyle, { zIndex: zIndex + 2 }]}>{showHandle && <View style={[styles.handlePill, handlePillStyle, { backgroundColor: colors.onSurfaceVariant }]} />}</View>
|
|
235
|
+
</GestureDetector>
|
|
236
|
+
)}
|
|
237
|
+
</Animated.View>
|
|
42
238
|
</>
|
|
43
239
|
)
|
|
44
240
|
}
|
|
45
241
|
|
|
46
242
|
const styles = StyleSheet.create({
|
|
243
|
+
anchorBottom: {
|
|
244
|
+
bottom: 0,
|
|
245
|
+
left: 0,
|
|
246
|
+
right: 0
|
|
247
|
+
},
|
|
47
248
|
anchorLeft: {
|
|
48
|
-
|
|
249
|
+
bottom: 0,
|
|
250
|
+
left: 0,
|
|
251
|
+
top: 0
|
|
49
252
|
},
|
|
50
253
|
anchorRight: {
|
|
51
|
-
|
|
254
|
+
bottom: 0,
|
|
255
|
+
right: 0,
|
|
256
|
+
top: 0
|
|
257
|
+
},
|
|
258
|
+
anchorTop: {
|
|
259
|
+
left: 0,
|
|
260
|
+
right: 0,
|
|
261
|
+
top: 0
|
|
52
262
|
},
|
|
53
263
|
backdrop: {
|
|
54
264
|
backgroundColor: '#000',
|
|
@@ -56,13 +266,21 @@ const styles = StyleSheet.create({
|
|
|
56
266
|
left: 0,
|
|
57
267
|
position: 'absolute',
|
|
58
268
|
right: 0,
|
|
59
|
-
top: 0
|
|
60
|
-
|
|
269
|
+
top: 0
|
|
270
|
+
},
|
|
271
|
+
clip: {
|
|
272
|
+
overflow: 'hidden'
|
|
61
273
|
},
|
|
62
274
|
drawer: {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
275
|
+
position: 'absolute'
|
|
276
|
+
},
|
|
277
|
+
handlePill: {
|
|
278
|
+
borderRadius: 3,
|
|
279
|
+
opacity: 0.5
|
|
280
|
+
},
|
|
281
|
+
handleStrip: {
|
|
282
|
+
alignItems: 'center',
|
|
283
|
+
justifyContent: 'center',
|
|
284
|
+
position: 'absolute'
|
|
67
285
|
}
|
|
68
286
|
})
|
package/src/DrawerEdgeSwipe.tsx
CHANGED
|
@@ -2,50 +2,76 @@ import { StyleSheet, View } from 'react-native'
|
|
|
2
2
|
import { Gesture, GestureDetector } from 'react-native-gesture-handler'
|
|
3
3
|
import { runOnJS, SharedValue, withSpring } from 'react-native-reanimated'
|
|
4
4
|
|
|
5
|
+
import { type DrawerSide, getClosedOffset, getOpenDirection, isVerticalSide } from './geometry'
|
|
6
|
+
|
|
5
7
|
const EDGE_WIDTH = 24
|
|
6
8
|
const SPRING = { damping: 40, overshootClamping: true, stiffness: 300 }
|
|
7
9
|
const VELOCITY_THRESHOLD = 500
|
|
8
10
|
|
|
9
11
|
export type DrawerEdgeSwipeProps = {
|
|
10
12
|
enabled?: boolean
|
|
13
|
+
height?: number
|
|
11
14
|
onOpen: () => void
|
|
12
|
-
side?:
|
|
13
|
-
|
|
14
|
-
width
|
|
15
|
+
side?: DrawerSide
|
|
16
|
+
translateOffset: SharedValue<number>
|
|
17
|
+
width?: number
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
export const DrawerEdgeSwipe = ({ enabled = true, onOpen, side = 'left',
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
20
|
+
export const DrawerEdgeSwipe = ({ enabled = true, height = 300, onOpen, side = 'left', translateOffset, width = 300 }: DrawerEdgeSwipeProps) => {
|
|
21
|
+
const vertical = isVerticalSide(side)
|
|
22
|
+
const size = vertical ? height : width
|
|
23
|
+
const closedOffset = getClosedOffset(side, size)
|
|
24
|
+
const openDirection = getOpenDirection(side)
|
|
25
|
+
const clampMin = Math.min(0, closedOffset)
|
|
26
|
+
const clampMax = Math.max(0, closedOffset)
|
|
27
|
+
|
|
28
|
+
const axisGesture = vertical
|
|
29
|
+
? Gesture.Pan()
|
|
30
|
+
.activeOffsetY(side === 'top' ? 10 : -10)
|
|
31
|
+
.failOffsetX([-15, 15])
|
|
32
|
+
: Gesture.Pan()
|
|
33
|
+
.activeOffsetX(side === 'left' ? 10 : -10)
|
|
34
|
+
.failOffsetY([-15, 15])
|
|
22
35
|
|
|
23
|
-
const gesture =
|
|
36
|
+
const gesture = axisGesture
|
|
24
37
|
.enabled(enabled)
|
|
25
|
-
.activeOffsetX(side === 'left' ? 10 : -10)
|
|
26
|
-
.failOffsetY([-15, 15])
|
|
27
38
|
.onUpdate((event) => {
|
|
28
|
-
|
|
39
|
+
'worklet'
|
|
40
|
+
const translation = vertical ? event.translationY : event.translationX
|
|
41
|
+
translateOffset.value = Math.min(clampMax, Math.max(clampMin, closedOffset + translation))
|
|
29
42
|
})
|
|
30
43
|
.onEnd((event) => {
|
|
31
|
-
|
|
44
|
+
'worklet'
|
|
45
|
+
const translation = vertical ? event.translationY : event.translationX
|
|
46
|
+
const velocity = vertical ? event.velocityY : event.velocityX
|
|
47
|
+
const commit = translation * openDirection > size / 3 || velocity * openDirection > VELOCITY_THRESHOLD
|
|
32
48
|
if (commit) {
|
|
33
|
-
|
|
49
|
+
translateOffset.value = withSpring(0, SPRING)
|
|
34
50
|
runOnJS(onOpen)()
|
|
35
51
|
} else {
|
|
36
|
-
|
|
52
|
+
translateOffset.value = withSpring(closedOffset, SPRING)
|
|
37
53
|
}
|
|
38
54
|
})
|
|
39
55
|
|
|
56
|
+
const edgeStyle = vertical ? [styles.edgeHorizontal, side === 'top' ? styles.top : styles.bottom] : [styles.edgeVertical, side === 'left' ? styles.left : styles.right]
|
|
57
|
+
|
|
40
58
|
return (
|
|
41
59
|
<GestureDetector gesture={gesture}>
|
|
42
|
-
<View style={
|
|
60
|
+
<View style={edgeStyle} />
|
|
43
61
|
</GestureDetector>
|
|
44
62
|
)
|
|
45
63
|
}
|
|
46
64
|
|
|
47
65
|
const styles = StyleSheet.create({
|
|
48
|
-
|
|
66
|
+
bottom: { bottom: 0 },
|
|
67
|
+
edgeHorizontal: {
|
|
68
|
+
height: EDGE_WIDTH,
|
|
69
|
+
left: 0,
|
|
70
|
+
position: 'absolute',
|
|
71
|
+
right: 0,
|
|
72
|
+
zIndex: 5
|
|
73
|
+
},
|
|
74
|
+
edgeVertical: {
|
|
49
75
|
bottom: 0,
|
|
50
76
|
position: 'absolute',
|
|
51
77
|
top: 0,
|
|
@@ -53,5 +79,6 @@ const styles = StyleSheet.create({
|
|
|
53
79
|
zIndex: 5
|
|
54
80
|
},
|
|
55
81
|
left: { left: 0 },
|
|
56
|
-
right: { right: 0 }
|
|
82
|
+
right: { right: 0 },
|
|
83
|
+
top: { top: 0 }
|
|
57
84
|
})
|
package/src/createDrawer.tsx
CHANGED
|
@@ -1,27 +1,67 @@
|
|
|
1
|
-
import { createContext, JSX, ReactNode, useContext, useMemo, useState } from 'react'
|
|
1
|
+
import { createContext, JSX, ReactNode, useContext, useEffect, useMemo, useState } from 'react'
|
|
2
|
+
import { BackHandler } from 'react-native'
|
|
2
3
|
import { useSharedValue } from 'react-native-reanimated'
|
|
3
4
|
|
|
4
5
|
import { Drawer } from './Drawer'
|
|
5
6
|
import { DrawerEdgeSwipe } from './DrawerEdgeSwipe'
|
|
7
|
+
import { type DrawerSide, getClosedOffset, isVerticalSide } from './geometry'
|
|
6
8
|
|
|
7
9
|
export type DrawerActions = {
|
|
8
10
|
close: () => void
|
|
9
11
|
isOpen: boolean
|
|
12
|
+
// True from open() through the full close animation, only dropping to false once the panel has
|
|
13
|
+
// actually finished sliding away — unlike isOpen, which flips the instant close() is called. For
|
|
14
|
+
// anything that needs to stay above/alongside the panel for as long as it's visually on screen
|
|
15
|
+
// (e.g. a FAB row portaled above it) rather than disappearing the moment closing starts.
|
|
16
|
+
isVisible: boolean
|
|
10
17
|
open: () => void
|
|
11
18
|
}
|
|
12
19
|
|
|
13
20
|
export type CreateDrawerOptions = {
|
|
14
|
-
|
|
21
|
+
backdropOpacity?: number
|
|
22
|
+
blockingBackdrop?: boolean
|
|
23
|
+
blur?: boolean
|
|
24
|
+
// Whether the Android hardware back button closes the drawer while it's open, instead of falling
|
|
25
|
+
// through to whatever screen/navigator is behind it. No-ops on iOS/web, where the underlying
|
|
26
|
+
// BackHandler event never fires.
|
|
27
|
+
closeOnBackPress?: boolean
|
|
28
|
+
contentSize?: boolean
|
|
29
|
+
dismissible?: boolean
|
|
30
|
+
height?: number
|
|
31
|
+
showHandle?: boolean
|
|
32
|
+
side?: DrawerSide
|
|
15
33
|
width?: number
|
|
34
|
+
// Stacking tier for this drawer's backdrop/panel/handle — see Drawer's own zIndex doc for the full
|
|
35
|
+
// rationale. Matters once more than one createDrawer() instance can be open at the same time: give
|
|
36
|
+
// whichever one should render on top a higher value than the other(s), independent of which
|
|
37
|
+
// DrawerProvider happens to be declared outermost/innermost.
|
|
38
|
+
zIndex?: number
|
|
16
39
|
}
|
|
17
40
|
|
|
18
41
|
export type DrawerProviderProps = {
|
|
42
|
+
// Overrides the backdropOpacity passed to createDrawer() (if any) for this mount.
|
|
43
|
+
backdropOpacity?: number
|
|
44
|
+
// Overrides the blockingBackdrop passed to createDrawer() (if any) for this mount.
|
|
45
|
+
blockingBackdrop?: boolean
|
|
46
|
+
// Overrides the blur passed to createDrawer() (if any) for this mount; falls through to
|
|
47
|
+
// @rific/auto-paper's own useBlur() global preference when neither is set.
|
|
48
|
+
blur?: boolean
|
|
19
49
|
// Rest-of-app content the drawer's actions must be reachable from — a normal Provider children slot.
|
|
20
50
|
children?: ReactNode
|
|
51
|
+
// Overrides the closeOnBackPress passed to createDrawer() (if any) for this mount.
|
|
52
|
+
closeOnBackPress?: boolean
|
|
21
53
|
// The drawer panel's own content, rendered inside the sliding panel rather than as `children`.
|
|
22
54
|
content?: ReactNode
|
|
55
|
+
// Overrides the contentSize passed to createDrawer() (if any) for this mount.
|
|
56
|
+
contentSize?: boolean
|
|
57
|
+
// Overrides the dismissible passed to createDrawer() (if any) for this mount.
|
|
58
|
+
dismissible?: boolean
|
|
23
59
|
// Lets a screen temporarily suppress the edge-swipe gesture (e.g. while another drawer is open).
|
|
24
60
|
enabled?: boolean
|
|
61
|
+
// Overrides the showHandle passed to createDrawer() (if any) for this mount.
|
|
62
|
+
showHandle?: boolean
|
|
63
|
+
// Overrides the zIndex passed to createDrawer() (if any) for this mount.
|
|
64
|
+
zIndex?: number
|
|
25
65
|
}
|
|
26
66
|
|
|
27
67
|
export type CreateDrawerResult = {
|
|
@@ -33,28 +73,66 @@ const noop = () => {}
|
|
|
33
73
|
|
|
34
74
|
// One call per drawer instance — each gets its own Context, so a left nav drawer and a right
|
|
35
75
|
// settings drawer (say) stay fully independent without a stringly-typed id to keep them apart.
|
|
36
|
-
export const createDrawer = ({ side = 'left', width = 300 }: CreateDrawerOptions = {}): CreateDrawerResult => {
|
|
37
|
-
const DrawerActionsContext = createContext<DrawerActions>({ close: noop, isOpen: false, open: noop })
|
|
76
|
+
export const createDrawer = ({ backdropOpacity: factoryBackdropOpacity, blockingBackdrop: factoryBlockingBackdrop, blur: factoryBlur, closeOnBackPress: factoryCloseOnBackPress, contentSize: factoryContentSize, dismissible: factoryDismissible, height = 300, showHandle: factoryShowHandle, side = 'left', width = 300, zIndex: factoryZIndex }: CreateDrawerOptions = {}): CreateDrawerResult => {
|
|
77
|
+
const DrawerActionsContext = createContext<DrawerActions>({ close: noop, isOpen: false, isVisible: false, open: noop })
|
|
78
|
+
const vertical = isVerticalSide(side)
|
|
79
|
+
const size = vertical ? height : width
|
|
80
|
+
const closedOffset = getClosedOffset(side, size)
|
|
38
81
|
|
|
39
|
-
const DrawerProvider = ({ children, content, enabled = true }: DrawerProviderProps): JSX.Element => {
|
|
82
|
+
const DrawerProvider = ({ backdropOpacity: backdropOpacityOverride, blockingBackdrop: blockingBackdropOverride, blur: blurOverride, children, closeOnBackPress: closeOnBackPressOverride, content, contentSize: contentSizeOverride, dismissible: dismissibleOverride, enabled = true, showHandle: showHandleOverride, zIndex: zIndexOverride }: DrawerProviderProps): JSX.Element => {
|
|
40
83
|
const [isOpen, setIsOpen] = useState(false)
|
|
41
|
-
|
|
42
|
-
|
|
84
|
+
// Starts in lockstep with isOpen (both false) and stays true past isOpen turning false, until
|
|
85
|
+
// Drawer's onClosed reports the outro spring has actually settled — see the isVisible field above.
|
|
86
|
+
const [isVisible, setIsVisible] = useState(false)
|
|
87
|
+
const [measuredSize, setMeasuredSize] = useState<number | null>(null)
|
|
88
|
+
const translateOffset = useSharedValue(closedOffset)
|
|
89
|
+
const backdropOpacity = backdropOpacityOverride ?? factoryBackdropOpacity ?? 0.45
|
|
90
|
+
const blur = blurOverride ?? factoryBlur
|
|
91
|
+
const blockingBackdrop = blockingBackdropOverride ?? factoryBlockingBackdrop ?? true
|
|
92
|
+
const closeOnBackPress = closeOnBackPressOverride ?? factoryCloseOnBackPress ?? true
|
|
93
|
+
const contentSize = contentSizeOverride ?? factoryContentSize ?? false
|
|
94
|
+
const dismissible = dismissibleOverride ?? factoryDismissible ?? true
|
|
95
|
+
const showHandle = showHandleOverride ?? factoryShowHandle ?? true
|
|
96
|
+
const zIndex = zIndexOverride ?? factoryZIndex ?? 50
|
|
97
|
+
|
|
98
|
+
// Fed by Drawer's onMeasure, and forwarded (in place of the static option) to both Drawer and
|
|
99
|
+
// DrawerEdgeSwipe — DrawerEdgeSwipe's own commit-threshold math needs the live size too, but it
|
|
100
|
+
// stays agnostic of contentSize itself; it just receives whichever height/width value is current.
|
|
101
|
+
const effectiveSize = contentSize ? (measuredSize ?? size) : size
|
|
102
|
+
|
|
103
|
+
const open = () => {
|
|
104
|
+
setIsOpen(true)
|
|
105
|
+
setIsVisible(true)
|
|
106
|
+
}
|
|
43
107
|
|
|
44
108
|
const actions = useMemo<DrawerActions>(
|
|
45
109
|
() => ({
|
|
46
110
|
close: () => setIsOpen(false),
|
|
47
111
|
isOpen,
|
|
48
|
-
|
|
112
|
+
isVisible,
|
|
113
|
+
open
|
|
49
114
|
}),
|
|
50
|
-
[isOpen]
|
|
115
|
+
[isOpen, isVisible]
|
|
51
116
|
)
|
|
52
117
|
|
|
118
|
+
// Only subscribed while open, so the back button falls through to normal navigation the rest of
|
|
119
|
+
// the time. Returning true from the handler marks the press as consumed (RN's contract for
|
|
120
|
+
// hardwareBackPress), stopping it from also closing a screen/navigator behind the drawer.
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
if (!isOpen || !closeOnBackPress) return
|
|
123
|
+
|
|
124
|
+
const subscription = BackHandler.addEventListener('hardwareBackPress', () => {
|
|
125
|
+
setIsOpen(false)
|
|
126
|
+
return true
|
|
127
|
+
})
|
|
128
|
+
return () => subscription.remove()
|
|
129
|
+
}, [isOpen, closeOnBackPress])
|
|
130
|
+
|
|
53
131
|
return (
|
|
54
132
|
<DrawerActionsContext.Provider value={actions}>
|
|
55
133
|
{children}
|
|
56
|
-
<DrawerEdgeSwipe enabled={enabled && !isOpen}
|
|
57
|
-
<Drawer onClose={() => setIsOpen(false)} open={isOpen} side={side}
|
|
134
|
+
<DrawerEdgeSwipe enabled={enabled && !isOpen} height={vertical ? effectiveSize : height} onOpen={open} side={side} translateOffset={translateOffset} width={vertical ? width : effectiveSize} />
|
|
135
|
+
<Drawer backdropOpacity={backdropOpacity} blockingBackdrop={blockingBackdrop} blur={blur} contentSize={contentSize} dismissible={dismissible} height={vertical ? effectiveSize : height} onClose={() => setIsOpen(false)} onClosed={() => setIsVisible(false)} onMeasure={setMeasuredSize} open={isOpen} showHandle={showHandle} side={side} translateOffset={translateOffset} width={vertical ? width : effectiveSize} zIndex={zIndex}>
|
|
58
136
|
{content}
|
|
59
137
|
</Drawer>
|
|
60
138
|
</DrawerActionsContext.Provider>
|
package/src/geometry.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type DrawerSide = 'left' | 'right' | 'top' | 'bottom'
|
|
2
|
+
|
|
3
|
+
export function isVerticalSide(side: DrawerSide): boolean {
|
|
4
|
+
return side === 'top' || side === 'bottom'
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// left/top start at a negative offset (slide toward 0, i.e. positive, to open);
|
|
8
|
+
// right/bottom start at a positive offset (slide toward 0, i.e. negative, to open).
|
|
9
|
+
export function getClosedOffset(side: DrawerSide, size: number): number {
|
|
10
|
+
return side === 'left' || side === 'top' ? -size : size
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Which drag direction (along the relevant axis) counts as "opening".
|
|
14
|
+
export function getOpenDirection(side: DrawerSide): 1 | -1 {
|
|
15
|
+
return side === 'left' || side === 'top' ? 1 : -1
|
|
16
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { combineDrawerProviders, createDrawer, type CreateDrawerOptions, type CreateDrawerResult, type DrawerActions, type DrawerProviderComponent, type DrawerProviderProps } from './createDrawer'
|
|
2
2
|
export { Drawer, type DrawerProps } from './Drawer'
|
|
3
3
|
export { DrawerEdgeSwipe, type DrawerEdgeSwipeProps } from './DrawerEdgeSwipe'
|
|
4
|
+
export { type DrawerSide } from './geometry'
|