|
1 | | -import { AnimatePresence, motion, useDragControls, useMotionValue, useTransform } from 'motion/react' |
2 | | -import { createContext, ReactNode, useCallback, useContext, useMemo, useState } from 'react' |
3 | | -import { animate } from 'motion' |
4 | | -import { CssVar } from '../types/Theme' |
| 1 | +import { ReactNode, useCallback, useMemo } from 'react' |
| 2 | +import { BottomSheet, useOverlayStack } from '@concrnt/ui' |
5 | 3 | import { useKeyboard } from './Keyboard' |
6 | 4 |
|
7 | | -interface DrawerContextState { |
| 5 | +interface DrawerState { |
8 | 6 | open: (content: ReactNode) => void |
9 | 7 | close: () => void |
10 | 8 | } |
11 | 9 |
|
12 | | -interface Props { |
13 | | - children: React.ReactNode |
| 10 | +interface DrawerShellProps { |
| 11 | + onDismiss: () => void |
| 12 | + children: ReactNode |
14 | 13 | } |
15 | 14 |
|
16 | | -const DrawerContext = createContext<DrawerContextState>({ |
17 | | - open: () => {}, |
18 | | - close: () => {} |
19 | | -}) |
20 | | - |
21 | | -export const DrawerProvider = (props: Props) => { |
22 | | - const y = useMotionValue(0) |
23 | | - const dragControls = useDragControls() |
| 15 | +const DrawerShell = (props: DrawerShellProps) => { |
| 16 | + const keyboard = useKeyboard() |
24 | 17 |
|
25 | | - const [content, setContent] = useState<ReactNode>(null) |
| 18 | + return ( |
| 19 | + <BottomSheet height={window.innerHeight * 0.9} keyboardInset={keyboard} onDismiss={props.onDismiss}> |
| 20 | + {props.children} |
| 21 | + </BottomSheet> |
| 22 | + ) |
| 23 | +} |
26 | 24 |
|
27 | | - const height = window.innerHeight * 0.9 |
28 | | - const backdropOpacity = useTransform(y, [0, height], [0.5, 0]) |
| 25 | +export const useDrawer = (): DrawerState => { |
| 26 | + const stack = useOverlayStack() |
29 | 27 |
|
30 | 28 | const open = useCallback( |
31 | | - (c: ReactNode) => { |
32 | | - y.set(height) |
33 | | - setContent(c) |
| 29 | + (content: ReactNode) => { |
| 30 | + stack.push({ |
| 31 | + kind: 'drawer', |
| 32 | + render: (close) => <DrawerShell onDismiss={close}>{content}</DrawerShell> |
| 33 | + }) |
34 | 34 | }, |
35 | | - [height, y] |
| 35 | + [stack] |
36 | 36 | ) |
37 | 37 |
|
38 | 38 | const close = useCallback(() => { |
39 | | - setContent(null) |
40 | | - }, []) |
| 39 | + stack.closeKind('drawer') |
| 40 | + }, [stack]) |
41 | 41 |
|
42 | | - const value = useMemo( |
| 42 | + return useMemo( |
43 | 43 | () => ({ |
44 | 44 | open, |
45 | 45 | close |
46 | 46 | }), |
47 | 47 | [open, close] |
48 | 48 | ) |
49 | | - |
50 | | - const keyboard = useKeyboard() |
51 | | - |
52 | | - return ( |
53 | | - <> |
54 | | - <DrawerContext.Provider value={value}>{props.children}</DrawerContext.Provider> |
55 | | - <AnimatePresence> |
56 | | - {content && ( |
57 | | - <> |
58 | | - <motion.div |
59 | | - style={{ |
60 | | - position: 'absolute', |
61 | | - inset: 0, |
62 | | - background: 'black', |
63 | | - opacity: backdropOpacity |
64 | | - }} |
65 | | - onClick={() => { |
66 | | - close() |
67 | | - }} |
68 | | - /> |
69 | | - <motion.div |
70 | | - style={{ |
71 | | - backgroundColor: CssVar.contentBackground, |
72 | | - color: CssVar.contentText, |
73 | | - position: 'absolute', |
74 | | - bottom: 0, |
75 | | - left: 0, |
76 | | - right: 0, |
77 | | - paddingBottom: 'env(safe-area-inset-bottom)', |
78 | | - borderRadius: `${CssVar.round(1)} ${CssVar.round(1)} 0 0`, |
79 | | - height, |
80 | | - y |
81 | | - }} |
82 | | - drag="y" |
83 | | - dragControls={dragControls} |
84 | | - dragListener={false} |
85 | | - dragConstraints={{ top: 0, bottom: height }} |
86 | | - dragElastic={0} |
87 | | - dragMomentum={false} |
88 | | - initial={{ y: height }} |
89 | | - animate={{ y: 0 }} |
90 | | - transition={{ type: 'tween', ease: 'easeOut', duration: 0.2 }} |
91 | | - exit={{ y: height }} |
92 | | - onDragEnd={(_, info) => { |
93 | | - const current = y.get() |
94 | | - const v = info.velocity.y |
95 | | - const dy = info.offset.y |
96 | | - |
97 | | - const fast = Math.abs(v) > 50 |
98 | | - const far = Math.abs(dy) > height / 2 |
99 | | - |
100 | | - let shouldClose = false |
101 | | - if (fast) { |
102 | | - shouldClose = v > 0 |
103 | | - } else if (far) { |
104 | | - shouldClose = dy > 0 |
105 | | - } else { |
106 | | - shouldClose = current > height / 2 |
107 | | - } |
108 | | - |
109 | | - if (shouldClose) { |
110 | | - close() |
111 | | - } else { |
112 | | - animate(y, 0, { type: 'tween', ease: 'easeOut', duration: 0.2 }) |
113 | | - } |
114 | | - }} |
115 | | - > |
116 | | - <div |
117 | | - style={{ |
118 | | - display: 'flex', |
119 | | - justifyContent: 'center', |
120 | | - padding: `${CssVar.space(2)} 0`, |
121 | | - position: 'relative', |
122 | | - touchAction: 'none' |
123 | | - }} |
124 | | - onPointerDown={(e) => { |
125 | | - dragControls.start(e) |
126 | | - }} |
127 | | - > |
128 | | - {/* ハンドルの見た目は変えず、当たり判定を縦方向に拡張する透明レイヤー */} |
129 | | - <div |
130 | | - style={{ |
131 | | - position: 'absolute', |
132 | | - top: `-${CssVar.space(4)}`, |
133 | | - bottom: `-${CssVar.space(4)}`, |
134 | | - left: 0, |
135 | | - right: 0 |
136 | | - }} |
137 | | - /> |
138 | | - <div |
139 | | - style={{ |
140 | | - width: '30px', |
141 | | - height: '6px', |
142 | | - borderRadius: CssVar.round(0.5), |
143 | | - backgroundColor: CssVar.divider |
144 | | - }} |
145 | | - /> |
146 | | - </div> |
147 | | - <div |
148 | | - style={{ |
149 | | - overflow: 'auto', |
150 | | - flex: 1, |
151 | | - height: '100%' |
152 | | - }} |
153 | | - > |
154 | | - {content} |
155 | | - <div |
156 | | - style={{ |
157 | | - height: `${keyboard.height}px`, |
158 | | - transition: `height ${keyboard.duration}s ease-out` |
159 | | - }} |
160 | | - /> |
161 | | - </div> |
162 | | - </motion.div> |
163 | | - </> |
164 | | - )} |
165 | | - </AnimatePresence> |
166 | | - </> |
167 | | - ) |
168 | | -} |
169 | | - |
170 | | -export const useDrawer = (): DrawerContextState => { |
171 | | - return useContext(DrawerContext) |
172 | 49 | } |
0 commit comments