forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert-dialog.tsx
300 lines (272 loc) · 7.15 KB
/
alert-dialog.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { useColorScheme } from 'nativewind';
import React from 'react';
import {
GestureResponderEvent,
Modal,
Pressable,
StyleSheet,
Text,
View,
ViewStyle,
} from 'react-native';
import { cn } from '~/lib/utils';
import { Button } from '~/components/ui/button';
import * as Slot from '~/lib/rn-primitives/slot/slot-native';
interface AlertDialogProps {
children: React.ReactNode;
closeOnOverlayPress?: boolean;
defaultOpen?: boolean;
open?: boolean;
setOpen?: React.Dispatch<React.SetStateAction<boolean>>;
}
interface AlertDialogContext {
visible: boolean;
setVisible: React.Dispatch<React.SetStateAction<boolean>>;
closeOnOverlayPress: boolean;
}
const AlertDialogContext = React.createContext<AlertDialogContext>(
{} as AlertDialogContext
);
const AlertDialog = React.forwardRef<
React.ElementRef<typeof View>,
React.ComponentPropsWithoutRef<typeof View> & AlertDialogProps
>(
(
{
open,
setOpen,
closeOnOverlayPress = false,
defaultOpen = false,
...props
},
ref
) => {
const [visible, setVisible] = React.useState(defaultOpen ?? false);
return (
<AlertDialogContext.Provider
value={{
visible: open ?? visible,
setVisible: setOpen ?? setVisible,
closeOnOverlayPress,
}}
>
<View ref={ref} {...props} />
</AlertDialogContext.Provider>
);
}
);
AlertDialog.displayName = 'AlertDialog';
function useAlertDialogContext() {
const context = React.useContext(AlertDialogContext);
if (!context) {
throw new Error(
'AlertDialog compound components cannot be rendered outside the AlertDialog component'
);
}
return context;
}
const AlertDialogTrigger = React.forwardRef<
React.ElementRef<typeof Button>,
React.ComponentPropsWithoutRef<typeof Button> & {
asChild?: boolean;
}
>(({ onPress, asChild = false, ...props }, ref) => {
const { setVisible } = useAlertDialogContext();
function handleOnPress(event: GestureResponderEvent) {
setVisible(true);
onPress?.(event);
}
const Trigger = asChild ? Slot.Pressable : Button;
return <Trigger ref={ref} onPress={handleOnPress} {...props} />;
});
AlertDialogTrigger.displayName = 'AlertDialogTrigger';
const AlertDialogContent = React.forwardRef<
React.ElementRef<typeof Modal>,
React.ComponentPropsWithoutRef<typeof Modal> & { overlayClass?: string }
>(
(
{
className,
children,
animationType = 'fade',
style: styleProp,
overlayClass,
...props
},
ref
) => {
const { colorScheme } = useColorScheme();
const { visible, setVisible, closeOnOverlayPress } =
useAlertDialogContext();
const [style, setStyle] = React.useState<ViewStyle>(
StyleSheet.flatten(styleProp)
);
React.useEffect(() => {
setStyle(
StyleSheet.flatten([
colorScheme === 'dark' ? styles.shadowDark : styles.shadowLight,
styleProp,
])
);
}, [styleProp, colorScheme]);
return (
<Modal
ref={ref}
animationType={animationType}
transparent={true}
visible={visible}
onRequestClose={() => {
setVisible((prev) => !prev);
}}
statusBarTranslucent
{...props}
>
<Pressable
onPressOut={
closeOnOverlayPress
? () => {
setVisible(false);
}
: undefined
}
className={cn(
'flex-1 justify-center items-center p-2',
animationType !== 'slide' && 'bg-zinc-50/80 dark:bg-zinc-900/80',
overlayClass
)}
>
<Pressable
style={style}
className={cn(
'bg-background rounded-2xl p-8 border border-border',
className
)}
role={'alertdialog'}
>
{children}
</Pressable>
</Pressable>
</Modal>
);
}
);
AlertDialogContent.displayName = 'AlertDialogContent';
const AlertDialogHeader = React.forwardRef<
React.ElementRef<typeof View>,
React.ComponentPropsWithoutRef<typeof View>
>(({ className, ...props }, ref) => {
return <View className={cn('gap-2', className)} ref={ref} {...props} />;
});
AlertDialogHeader.displayName = 'AlertDialogHeader';
const AlertDialogTitle = React.forwardRef<
React.ElementRef<typeof Text>,
React.ComponentPropsWithoutRef<typeof Text>
>(({ className, ...props }, ref) => {
return (
<Text
className={cn('text-xl text-foreground font-semibold', className)}
ref={ref}
role='heading'
{...props}
/>
);
});
AlertDialogTitle.displayName = 'AlertDialogTitle';
const AlertDialogDescription = React.forwardRef<
React.ElementRef<typeof Text>,
React.ComponentPropsWithoutRef<typeof Text>
>(({ className, ...props }, ref) => {
return (
<Text
className={cn('text-lg text-muted-foreground', className)}
ref={ref}
{...props}
/>
);
});
AlertDialogDescription.displayName = 'AlertDialogDescription';
const AlertDialogFooter = React.forwardRef<
React.ElementRef<typeof View>,
React.ComponentPropsWithoutRef<typeof View>
>(({ className, ...props }, ref) => {
return (
<View
className={cn('flex-row justify-end gap-3 pt-8', className)}
ref={ref}
{...props}
/>
);
});
AlertDialogFooter.displayName = 'AlertDialogFooter';
const AlertDialogCancel = React.forwardRef<
React.ElementRef<typeof Button>,
React.ComponentPropsWithoutRef<typeof Button> & {
asChild?: boolean;
}
>(({ variant = 'outline', asChild = false, ...props }, ref) => {
const { setVisible } = useAlertDialogContext();
const Trigger = asChild ? Slot.Pressable : Button;
return (
<Trigger
variant={variant}
onPress={() => {
setVisible(false);
}}
ref={ref}
{...props}
/>
);
});
AlertDialogCancel.displayName = 'AlertDialogCancel';
type ButtonProps = React.ComponentPropsWithoutRef<typeof Button>;
const AlertDialogAction = React.forwardRef<
React.ElementRef<typeof Pressable>,
Omit<ButtonProps, 'onPress'> & {
asChild?: boolean;
onPress?:
| ((event: GestureResponderEvent) => void)
| ((event: GestureResponderEvent) => Promise<void>);
}
>(({ onPress, asChild, ...props }, ref) => {
const { setVisible } = useAlertDialogContext();
async function onPressAction(ev: GestureResponderEvent) {
await onPress?.(ev);
setVisible(false);
}
const Trigger = asChild ? Slot.Pressable : Button;
return <Trigger onPress={onPressAction} ref={ref} {...props} />;
});
AlertDialogAction.displayName = 'AlertDialogAction';
export {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
};
const styles = StyleSheet.create({
shadowLight: {
shadowColor: '#000000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.1,
shadowRadius: 8,
elevation: 5,
},
shadowDark: {
shadowColor: '#000000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 8,
elevation: 5,
},
});