forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapsible.tsx
150 lines (137 loc) · 3.67 KB
/
collapsible.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
import { VariantProps } from 'class-variance-authority';
import React from 'react';
import { GestureResponderEvent, Pressable, View } from 'react-native';
import Animated, { FadeInDown, FadeOutUp } from 'react-native-reanimated';
import { buttonVariants } from '~/components/ui/button';
import * as Slot from '~/lib/rn-primitives/slot/slot-native';
import { cn } from '~/lib/utils';
interface CollapsibleProps {
open?: boolean;
setOpen?: React.Dispatch<React.SetStateAction<boolean>>;
defaultOpen?: boolean;
disabled?: boolean;
}
interface CollapsibleContext {
visible: boolean;
setVisible: React.Dispatch<React.SetStateAction<boolean>>;
nativeID: string;
disabled: boolean;
}
const CollapsibleContext = React.createContext({} as CollapsibleContext);
const Collapsible = React.forwardRef<
React.ElementRef<typeof View>,
React.ComponentPropsWithoutRef<typeof View> & CollapsibleProps
>(
(
{ open, setOpen, defaultOpen, className, disabled = false, ...props },
ref
) => {
const [visible, setVisible] = React.useState(defaultOpen ?? false);
const nativeID = React.useId();
return (
<CollapsibleContext.Provider
value={{
nativeID,
visible: open ?? visible,
setVisible: setOpen ?? setVisible,
disabled: disabled ?? false,
}}
>
<View
ref={ref}
role='presentation'
className={cn('gap-3', className)}
{...props}
/>
</CollapsibleContext.Provider>
);
}
);
function useCollapsibleContext() {
const context = React.useContext(CollapsibleContext);
if (!context) {
throw new Error(
'Collapsible compound components cannot be rendered outside the Collapsible component'
);
}
return context;
}
const CollapsibleHeader = React.forwardRef<
React.ElementRef<typeof View>,
React.ComponentPropsWithoutRef<typeof View>
>(({ className, ...props }, ref) => {
return (
<View
role='heading'
ref={ref}
className={cn(
'flex-row items-center justify-between gap-3 px-4',
className
)}
{...props}
/>
);
});
const CollapsibleTrigger = React.forwardRef<
React.ElementRef<typeof Pressable>,
React.ComponentPropsWithoutRef<typeof Pressable> &
VariantProps<typeof buttonVariants> & {
asChild?: boolean;
}
>(
(
{
className,
onPress,
variant = 'outline',
size = 'sm',
asChild = false,
...props
},
ref
) => {
const { nativeID, visible, setVisible, disabled } = useCollapsibleContext();
function handleOnPress(event: GestureResponderEvent) {
setVisible((prev) => !prev);
onPress?.(event);
}
const Trigger = asChild ? Slot.Pressable : Pressable;
return (
<Trigger
key={`collapsible-trigger-${nativeID}`}
onPress={handleOnPress}
disabled={disabled}
aria-expanded={visible}
nativeID={nativeID}
ref={ref}
className={cn(buttonVariants({ variant, size, className }))}
{...props}
/>
);
}
);
const CollapsibleContent = React.forwardRef<
React.ElementRef<typeof Animated.View>,
React.ComponentPropsWithoutRef<typeof Animated.View>
>(({ className, ...props }, ref) => {
const { nativeID, visible } = useCollapsibleContext();
if (!visible) return null;
return (
<Animated.View
entering={FadeInDown}
exiting={FadeOutUp.duration(150)}
role='summary'
ref={ref}
className={cn('gap-3', className)}
key={`collapsible-content-${nativeID}`}
aria-labelledbyledBy={nativeID}
{...props}
/>
);
});
export {
Collapsible,
CollapsibleContent,
CollapsibleHeader,
CollapsibleTrigger,
};