forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggle-group.tsx
86 lines (77 loc) · 2.83 KB
/
toggle-group.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
import { VariantProps } from 'class-variance-authority';
import type { LucideIcon } from 'lucide-react-native';
import * as React from 'react';
import { toggleTextVariants, toggleVariants } from '../../components/ui/toggle';
import { TextClassContext } from './text';
import * as ToggleGroupPrimitive from '@rnr/toggle-group';
import { cn } from '../../lib/utils';
const ToggleGroupContext = React.createContext<VariantProps<typeof toggleVariants> | null>(null);
const ToggleGroup = React.forwardRef<
React.ElementRef<typeof ToggleGroupPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Root> &
VariantProps<typeof toggleVariants>
>(({ className, variant, size, children, ...props }, ref) => (
<ToggleGroupPrimitive.Root
ref={ref}
className={cn('flex flex-row items-center justify-center gap-1', className)}
{...props}
>
<ToggleGroupContext.Provider value={{ variant, size }}>{children}</ToggleGroupContext.Provider>
</ToggleGroupPrimitive.Root>
));
ToggleGroup.displayName = ToggleGroupPrimitive.Root.displayName;
function useToggleGroupContext() {
const context = React.useContext(ToggleGroupContext);
if (context === null) {
throw new Error(
'ToggleGroup compound components cannot be rendered outside the ToggleGroup component'
);
}
return context;
}
const ToggleGroupItem = React.forwardRef<
React.ElementRef<typeof ToggleGroupPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Item> &
VariantProps<typeof toggleVariants>
>(({ className, children, variant, size, ...props }, ref) => {
const context = useToggleGroupContext();
const { value } = ToggleGroupPrimitive.useRootContext();
return (
<TextClassContext.Provider
value={cn(
toggleTextVariants({ variant, size }),
ToggleGroupPrimitive.utils.getIsSelected(value, props.value)
? 'text-accent-foreground'
: 'web:group-hover:text-muted-foreground'
)}
>
<ToggleGroupPrimitive.Item
ref={ref}
className={cn(
toggleVariants({
variant: context.variant || variant,
size: context.size || size,
}),
props.disabled && 'web:pointer-events-none opacity-50',
ToggleGroupPrimitive.utils.getIsSelected(value, props.value) && 'bg-accent',
className
)}
{...props}
>
{children}
</ToggleGroupPrimitive.Item>
</TextClassContext.Provider>
);
});
ToggleGroupItem.displayName = ToggleGroupPrimitive.Item.displayName;
function ToggleGroupIcon({
className,
icon: Icon,
...props
}: React.ComponentPropsWithoutRef<LucideIcon> & {
icon: LucideIcon;
}) {
const textClass = React.useContext(TextClassContext);
return <Icon className={cn(textClass, className)} {...props} />;
}
export { ToggleGroup, ToggleGroupIcon, ToggleGroupItem };