@tallyui/primitives 0.1.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.
Files changed (128) hide show
  1. package/dist/index.d.ts +1370 -0
  2. package/dist/index.js +3954 -0
  3. package/package.json +47 -0
  4. package/src/__tests__/accordion.test.tsx +370 -0
  5. package/src/__tests__/alert-dialog.test.tsx +127 -0
  6. package/src/__tests__/animated.test.tsx +184 -0
  7. package/src/__tests__/checkbox.test.tsx +90 -0
  8. package/src/__tests__/collapsible.test.tsx +149 -0
  9. package/src/__tests__/combobox.test.tsx +292 -0
  10. package/src/__tests__/context-menu.test.tsx +226 -0
  11. package/src/__tests__/dialog.test.tsx +81 -0
  12. package/src/__tests__/dropdown-menu.test.tsx +119 -0
  13. package/src/__tests__/hooks.test.tsx +46 -0
  14. package/src/__tests__/hover-card.test.tsx +77 -0
  15. package/src/__tests__/image.test.tsx +53 -0
  16. package/src/__tests__/label.test.tsx +50 -0
  17. package/src/__tests__/list.test.tsx +63 -0
  18. package/src/__tests__/popover.test.tsx +63 -0
  19. package/src/__tests__/portal.test.tsx +66 -0
  20. package/src/__tests__/progress.test.tsx +70 -0
  21. package/src/__tests__/radio-group.test.tsx +164 -0
  22. package/src/__tests__/select.test.tsx +76 -0
  23. package/src/__tests__/separator.test.tsx +37 -0
  24. package/src/__tests__/slider.test.tsx +124 -0
  25. package/src/__tests__/slot.test.tsx +56 -0
  26. package/src/__tests__/switch.test.tsx +70 -0
  27. package/src/__tests__/table.test.tsx +172 -0
  28. package/src/__tests__/tabs.test.tsx +67 -0
  29. package/src/__tests__/text.test.tsx +59 -0
  30. package/src/__tests__/toast.test.tsx +137 -0
  31. package/src/__tests__/toggle-group.test.tsx +167 -0
  32. package/src/__tests__/toggle.test.tsx +60 -0
  33. package/src/__tests__/tooltip.test.tsx +77 -0
  34. package/src/__tests__/types.test-d.ts +106 -0
  35. package/src/__tests__/utils.test.ts +47 -0
  36. package/src/accordion/accordion.tsx +232 -0
  37. package/src/accordion/index.ts +12 -0
  38. package/src/accordion/types.ts +56 -0
  39. package/src/alert-dialog/alert-dialog.tsx +296 -0
  40. package/src/alert-dialog/index.ts +25 -0
  41. package/src/alert-dialog/types.ts +50 -0
  42. package/src/animated/index.ts +3 -0
  43. package/src/animated/presence.tsx +27 -0
  44. package/src/animated/types.ts +36 -0
  45. package/src/animated/use-presence.ts +44 -0
  46. package/src/checkbox/checkbox.tsx +106 -0
  47. package/src/checkbox/index.ts +2 -0
  48. package/src/checkbox/types.ts +19 -0
  49. package/src/collapsible/collapsible.tsx +135 -0
  50. package/src/collapsible/index.ts +2 -0
  51. package/src/collapsible/types.ts +25 -0
  52. package/src/combobox/combobox.tsx +325 -0
  53. package/src/combobox/index.ts +24 -0
  54. package/src/combobox/types.ts +84 -0
  55. package/src/context-menu/context-menu.tsx +654 -0
  56. package/src/context-menu/index.ts +40 -0
  57. package/src/context-menu/types.ts +117 -0
  58. package/src/dialog/dialog.tsx +260 -0
  59. package/src/dialog/index.ts +23 -0
  60. package/src/dialog/types.ts +47 -0
  61. package/src/dropdown-menu/dropdown-menu.tsx +654 -0
  62. package/src/dropdown-menu/index.ts +40 -0
  63. package/src/dropdown-menu/types.ts +117 -0
  64. package/src/hooks/index.ts +6 -0
  65. package/src/hooks/use-augmented-ref.ts +32 -0
  66. package/src/hooks/use-controllable-state.ts +87 -0
  67. package/src/hooks/use-relative-position.ts +253 -0
  68. package/src/hooks/use-relative-position.web.ts +9 -0
  69. package/src/hover-card/hover-card.tsx +271 -0
  70. package/src/hover-card/index.ts +17 -0
  71. package/src/hover-card/types.ts +50 -0
  72. package/src/image/image.tsx +29 -0
  73. package/src/image/index.ts +2 -0
  74. package/src/image/types.ts +10 -0
  75. package/src/index.ts +38 -0
  76. package/src/label/index.ts +2 -0
  77. package/src/label/label.tsx +45 -0
  78. package/src/label/types.ts +11 -0
  79. package/src/list/index.ts +2 -0
  80. package/src/list/list.tsx +54 -0
  81. package/src/list/types.ts +14 -0
  82. package/src/popover/index.ts +19 -0
  83. package/src/popover/popover.tsx +314 -0
  84. package/src/popover/types.ts +51 -0
  85. package/src/portal.tsx +60 -0
  86. package/src/progress/index.ts +2 -0
  87. package/src/progress/progress.tsx +92 -0
  88. package/src/progress/types.ts +16 -0
  89. package/src/radio-group/index.ts +2 -0
  90. package/src/radio-group/radio-group.tsx +160 -0
  91. package/src/radio-group/types.ts +31 -0
  92. package/src/select/index.ts +34 -0
  93. package/src/select/select.tsx +465 -0
  94. package/src/select/types.ts +112 -0
  95. package/src/separator/index.ts +2 -0
  96. package/src/separator/separator.tsx +32 -0
  97. package/src/separator/types.ts +8 -0
  98. package/src/slider/index.ts +2 -0
  99. package/src/slider/slider.tsx +139 -0
  100. package/src/slider/types.ts +35 -0
  101. package/src/slot.tsx +160 -0
  102. package/src/switch/index.ts +2 -0
  103. package/src/switch/switch.tsx +92 -0
  104. package/src/switch/types.ts +18 -0
  105. package/src/table/index.ts +12 -0
  106. package/src/table/table.tsx +160 -0
  107. package/src/table/types.ts +30 -0
  108. package/src/tabs/index.ts +15 -0
  109. package/src/tabs/tabs.tsx +140 -0
  110. package/src/tabs/types.ts +36 -0
  111. package/src/text/index.ts +2 -0
  112. package/src/text/text.tsx +40 -0
  113. package/src/text/types.ts +5 -0
  114. package/src/toast/index.ts +2 -0
  115. package/src/toast/toast.tsx +189 -0
  116. package/src/toast/types.ts +35 -0
  117. package/src/toggle/index.ts +2 -0
  118. package/src/toggle/toggle.tsx +59 -0
  119. package/src/toggle/types.ts +11 -0
  120. package/src/toggle-group/index.ts +2 -0
  121. package/src/toggle-group/toggle-group.tsx +123 -0
  122. package/src/toggle-group/types.ts +36 -0
  123. package/src/tooltip/index.ts +17 -0
  124. package/src/tooltip/tooltip.tsx +272 -0
  125. package/src/tooltip/types.ts +49 -0
  126. package/src/types.ts +129 -0
  127. package/src/uniwind-env.d.ts +31 -0
  128. package/src/utils.ts +67 -0
@@ -0,0 +1,140 @@
1
+ import * as React from 'react';
2
+ import { Pressable, View, type GestureResponderEvent } from 'react-native';
3
+ import { useControllableState } from '../hooks';
4
+ import { Slot } from '../slot';
5
+ import type {
6
+ ContentProps,
7
+ ListProps,
8
+ RootContext as RootContextType,
9
+ RootProps,
10
+ TriggerProps,
11
+ } from './types';
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // Context
15
+ // ---------------------------------------------------------------------------
16
+
17
+ const RootContext = React.createContext<RootContextType | null>(null);
18
+
19
+ function useRootContext(): RootContextType {
20
+ const context = React.useContext(RootContext);
21
+ if (!context) {
22
+ throw new Error(
23
+ 'Tabs compound components cannot be rendered outside the Tabs.Root component'
24
+ );
25
+ }
26
+ return context;
27
+ }
28
+
29
+ // ---------------------------------------------------------------------------
30
+ // Root
31
+ // ---------------------------------------------------------------------------
32
+
33
+ function Root({
34
+ value: valueProp,
35
+ defaultValue,
36
+ onValueChange: onValueChangeProp,
37
+ children,
38
+ }: RootProps) {
39
+ const nativeID = React.useId();
40
+ const [value = '', onValueChange] = useControllableState({
41
+ prop: valueProp,
42
+ defaultProp: defaultValue,
43
+ onChange: onValueChangeProp,
44
+ });
45
+
46
+ return (
47
+ <RootContext.Provider value={{ value, onValueChange, nativeID }}>
48
+ {children}
49
+ </RootContext.Provider>
50
+ );
51
+ }
52
+
53
+ Root.displayName = 'TabsRoot';
54
+
55
+ // ---------------------------------------------------------------------------
56
+ // List
57
+ // ---------------------------------------------------------------------------
58
+
59
+ const List = React.forwardRef<View, ListProps>(function List(
60
+ { asChild, ...props },
61
+ ref
62
+ ) {
63
+ const Component = asChild ? Slot : View;
64
+ return <Component ref={ref} role="tablist" {...props} />;
65
+ });
66
+
67
+ List.displayName = 'TabsList';
68
+
69
+ // ---------------------------------------------------------------------------
70
+ // Trigger
71
+ // ---------------------------------------------------------------------------
72
+
73
+ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
74
+ { asChild, onPress: onPressProp, disabled, value: tabValue, ...props },
75
+ ref
76
+ ) {
77
+ const { onValueChange, value: rootValue, nativeID } = useRootContext();
78
+
79
+ const onPress = React.useCallback(
80
+ (ev: GestureResponderEvent) => {
81
+ if (disabled) return;
82
+ onValueChange(tabValue);
83
+ onPressProp?.(ev);
84
+ },
85
+ [disabled, onValueChange, tabValue, onPressProp]
86
+ );
87
+
88
+ const isActive = rootValue === tabValue;
89
+ const Component = asChild ? Slot : Pressable;
90
+
91
+ return (
92
+ <Component
93
+ ref={ref}
94
+ nativeID={`${nativeID}-tab-${tabValue}`}
95
+ aria-disabled={disabled || undefined}
96
+ aria-selected={isActive}
97
+ role="tab"
98
+ onPress={onPress}
99
+ disabled={disabled || undefined}
100
+ {...props}
101
+ />
102
+ );
103
+ });
104
+
105
+ Trigger.displayName = 'TabsTrigger';
106
+
107
+ // ---------------------------------------------------------------------------
108
+ // Content
109
+ // ---------------------------------------------------------------------------
110
+
111
+ const Content = React.forwardRef<View, ContentProps>(function Content(
112
+ { asChild, forceMount, value: tabValue, ...props },
113
+ ref
114
+ ) {
115
+ const { value: rootValue, nativeID } = useRootContext();
116
+
117
+ if (!forceMount && rootValue !== tabValue) {
118
+ return null;
119
+ }
120
+
121
+ const Component = asChild ? Slot : View;
122
+
123
+ return (
124
+ <Component
125
+ ref={ref}
126
+ aria-hidden={rootValue !== tabValue}
127
+ aria-labelledby={`${nativeID}-tab-${tabValue}`}
128
+ role="tabpanel"
129
+ {...props}
130
+ />
131
+ );
132
+ });
133
+
134
+ Content.displayName = 'TabsContent';
135
+
136
+ // ---------------------------------------------------------------------------
137
+ // Exports
138
+ // ---------------------------------------------------------------------------
139
+
140
+ export { Content, List, Root, Trigger, useRootContext };
@@ -0,0 +1,36 @@
1
+ import type {
2
+ ForceMountable,
3
+ SlottablePressableProps,
4
+ SlottableViewProps,
5
+ } from '../types';
6
+
7
+ interface RootProps {
8
+ value?: string;
9
+ defaultValue?: string;
10
+ onValueChange?: (value: string) => void;
11
+ children?: React.ReactNode;
12
+ }
13
+
14
+ interface ListProps extends SlottableViewProps {}
15
+
16
+ interface TriggerProps extends SlottablePressableProps {
17
+ value: string;
18
+ }
19
+
20
+ interface ContentProps extends SlottableViewProps, ForceMountable {
21
+ value: string;
22
+ }
23
+
24
+ interface RootContext {
25
+ value: string;
26
+ onValueChange: (value: string) => void;
27
+ nativeID: string;
28
+ }
29
+
30
+ export type {
31
+ ContentProps,
32
+ ListProps,
33
+ RootContext,
34
+ RootProps,
35
+ TriggerProps,
36
+ };
@@ -0,0 +1,2 @@
1
+ export { Root, TextClassContext } from './text';
2
+ export type { RootProps } from './types';
@@ -0,0 +1,40 @@
1
+ import * as React from 'react';
2
+ import { Text } from 'react-native';
3
+ import { Slot } from '../slot';
4
+ import type { RootProps } from './types';
5
+
6
+ // ---------------------------------------------------------------------------
7
+ // Context
8
+ // ---------------------------------------------------------------------------
9
+
10
+ const TextClassContext = React.createContext<string | undefined>(undefined);
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Root
14
+ // ---------------------------------------------------------------------------
15
+
16
+ const Root = React.forwardRef<Text, RootProps>(function Root(
17
+ { asChild, className, ...props },
18
+ ref
19
+ ) {
20
+ const textClass = React.useContext(TextClassContext);
21
+ const Component = asChild ? Slot : Text;
22
+
23
+ // Merge context className with prop className.
24
+ // The styled component layer (cn/tailwind-merge) handles deduplication.
25
+ const mergedClassName = textClass
26
+ ? className
27
+ ? `${textClass} ${className}`
28
+ : textClass
29
+ : className;
30
+
31
+ return <Component ref={ref} className={mergedClassName} {...props} />;
32
+ });
33
+
34
+ Root.displayName = 'TextRoot';
35
+
36
+ // ---------------------------------------------------------------------------
37
+ // Exports
38
+ // ---------------------------------------------------------------------------
39
+
40
+ export { Root, TextClassContext };
@@ -0,0 +1,5 @@
1
+ import type { SlottableTextProps } from '../types';
2
+
3
+ interface RootProps extends SlottableTextProps {}
4
+
5
+ export type { RootProps };
@@ -0,0 +1,2 @@
1
+ export { Root, Title, Description, Action, Close, useRootContext } from './toast';
2
+ export type { RootProps, TitleProps, DescriptionProps, ActionProps, CloseProps, RootContext } from './types';
@@ -0,0 +1,189 @@
1
+ import * as React from 'react';
2
+ import { Pressable, Text, View, type GestureResponderEvent } from 'react-native';
3
+ import { useControllableState } from '../hooks';
4
+ import { Slot } from '../slot';
5
+ import type { RootProps, TitleProps, DescriptionProps, ActionProps, CloseProps, RootContext as RootContextType } from './types';
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // Context
9
+ // ---------------------------------------------------------------------------
10
+
11
+ const RootContext = React.createContext<RootContextType | null>(null);
12
+
13
+ function useRootContext(): RootContextType {
14
+ const context = React.useContext(RootContext);
15
+ if (!context) {
16
+ throw new Error(
17
+ 'Toast compound components cannot be rendered outside the Toast.Root component'
18
+ );
19
+ }
20
+ return context;
21
+ }
22
+
23
+ // ---------------------------------------------------------------------------
24
+ // Root
25
+ // ---------------------------------------------------------------------------
26
+
27
+ const Root = React.forwardRef<View, RootProps>(function Root(
28
+ {
29
+ asChild,
30
+ open: openProp,
31
+ defaultOpen,
32
+ onOpenChange: onOpenChangeProp,
33
+ type = 'foreground',
34
+ ...props
35
+ },
36
+ ref
37
+ ) {
38
+ const [open = false, setOpen] = useControllableState({
39
+ prop: openProp,
40
+ defaultProp: defaultOpen,
41
+ onChange: onOpenChangeProp,
42
+ });
43
+
44
+ const onOpenChange = React.useCallback(
45
+ (value: boolean) => {
46
+ setOpen(value);
47
+ },
48
+ [setOpen]
49
+ );
50
+
51
+ if (!open) {
52
+ return null;
53
+ }
54
+
55
+ const Component = asChild ? Slot : View;
56
+
57
+ return (
58
+ <RootContext.Provider value={{ open, onOpenChange, type }}>
59
+ <Component
60
+ ref={ref}
61
+ role="status"
62
+ aria-live={type === 'foreground' ? 'assertive' : 'polite'}
63
+ {...props}
64
+ />
65
+ </RootContext.Provider>
66
+ );
67
+ });
68
+
69
+ Root.displayName = 'ToastRoot';
70
+
71
+ // ---------------------------------------------------------------------------
72
+ // Title
73
+ // ---------------------------------------------------------------------------
74
+
75
+ const Title = React.forwardRef<Text, TitleProps>(function Title(
76
+ { asChild, ...props },
77
+ ref
78
+ ) {
79
+ useRootContext();
80
+
81
+ const Component = asChild ? Slot : Text;
82
+
83
+ return <Component ref={ref} {...props} />;
84
+ });
85
+
86
+ Title.displayName = 'ToastTitle';
87
+
88
+ // ---------------------------------------------------------------------------
89
+ // Description
90
+ // ---------------------------------------------------------------------------
91
+
92
+ const Description = React.forwardRef<Text, DescriptionProps>(function Description(
93
+ { asChild, ...props },
94
+ ref
95
+ ) {
96
+ useRootContext();
97
+
98
+ const Component = asChild ? Slot : Text;
99
+
100
+ return <Component ref={ref} {...props} />;
101
+ });
102
+
103
+ Description.displayName = 'ToastDescription';
104
+
105
+ // ---------------------------------------------------------------------------
106
+ // Action
107
+ // ---------------------------------------------------------------------------
108
+
109
+ const Action = React.forwardRef<View, ActionProps>(function Action(
110
+ {
111
+ asChild,
112
+ altText: _altText,
113
+ disabled = false,
114
+ onPress: onPressProp,
115
+ ...props
116
+ },
117
+ ref
118
+ ) {
119
+ useRootContext();
120
+
121
+ const onPress = React.useCallback(
122
+ (ev: GestureResponderEvent) => {
123
+ if (disabled) return;
124
+ onPressProp?.(ev);
125
+ },
126
+ [disabled, onPressProp]
127
+ );
128
+
129
+ const Component = asChild ? Slot : Pressable;
130
+
131
+ return (
132
+ <Component
133
+ ref={ref}
134
+ role="button"
135
+ aria-disabled={disabled || undefined}
136
+ onPress={onPress}
137
+ disabled={disabled || undefined}
138
+ {...props}
139
+ />
140
+ );
141
+ });
142
+
143
+ Action.displayName = 'ToastAction';
144
+
145
+ // ---------------------------------------------------------------------------
146
+ // Close
147
+ // ---------------------------------------------------------------------------
148
+
149
+ const Close = React.forwardRef<View, CloseProps>(function Close(
150
+ {
151
+ asChild,
152
+ disabled = false,
153
+ onPress: onPressProp,
154
+ ...props
155
+ },
156
+ ref
157
+ ) {
158
+ const { onOpenChange } = useRootContext();
159
+
160
+ const onPress = React.useCallback(
161
+ (ev: GestureResponderEvent) => {
162
+ if (disabled) return;
163
+ onOpenChange(false);
164
+ onPressProp?.(ev);
165
+ },
166
+ [disabled, onOpenChange, onPressProp]
167
+ );
168
+
169
+ const Component = asChild ? Slot : Pressable;
170
+
171
+ return (
172
+ <Component
173
+ ref={ref}
174
+ role="button"
175
+ aria-disabled={disabled || undefined}
176
+ onPress={onPress}
177
+ disabled={disabled || undefined}
178
+ {...props}
179
+ />
180
+ );
181
+ });
182
+
183
+ Close.displayName = 'ToastClose';
184
+
185
+ // ---------------------------------------------------------------------------
186
+ // Exports
187
+ // ---------------------------------------------------------------------------
188
+
189
+ export { Root, Title, Description, Action, Close, useRootContext };
@@ -0,0 +1,35 @@
1
+ import type { SlottablePressableProps, SlottableTextProps, SlottableViewProps, PressableRef, ViewRef, TextRef } from '../types';
2
+
3
+ interface RootProps extends SlottableViewProps {
4
+ open?: boolean;
5
+ defaultOpen?: boolean;
6
+ onOpenChange?: (open: boolean) => void;
7
+ type?: 'foreground' | 'background';
8
+ children?: React.ReactNode;
9
+ ref?: ViewRef;
10
+ }
11
+
12
+ interface TitleProps extends SlottableTextProps {
13
+ ref?: TextRef;
14
+ }
15
+
16
+ interface DescriptionProps extends SlottableTextProps {
17
+ ref?: TextRef;
18
+ }
19
+
20
+ interface ActionProps extends SlottablePressableProps {
21
+ altText: string;
22
+ ref?: PressableRef;
23
+ }
24
+
25
+ interface CloseProps extends SlottablePressableProps {
26
+ ref?: PressableRef;
27
+ }
28
+
29
+ interface RootContext {
30
+ open: boolean;
31
+ onOpenChange: (open: boolean) => void;
32
+ type: 'foreground' | 'background';
33
+ }
34
+
35
+ export type { RootProps, TitleProps, DescriptionProps, ActionProps, CloseProps, RootContext };
@@ -0,0 +1,2 @@
1
+ export { Root } from './toggle';
2
+ export type { RootProps } from './types';
@@ -0,0 +1,59 @@
1
+ import * as React from 'react';
2
+ import { Pressable, View, type GestureResponderEvent } from 'react-native';
3
+ import { useControllableState } from '../hooks';
4
+ import { Slot } from '../slot';
5
+ import type { RootProps } from './types';
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // Root
9
+ // ---------------------------------------------------------------------------
10
+
11
+ const Root = React.forwardRef<View, RootProps>(function Root(
12
+ {
13
+ asChild,
14
+ pressed: pressedProp,
15
+ defaultPressed,
16
+ onPressedChange: onPressedChangeProp,
17
+ disabled = false,
18
+ onPress: onPressProp,
19
+ ...props
20
+ },
21
+ ref
22
+ ) {
23
+ const [pressed = false, setPressed] = useControllableState({
24
+ prop: pressedProp,
25
+ defaultProp: defaultPressed,
26
+ onChange: onPressedChangeProp,
27
+ });
28
+
29
+ const onPress = React.useCallback(
30
+ (ev: GestureResponderEvent) => {
31
+ if (disabled) return;
32
+ setPressed((prev) => !prev);
33
+ onPressProp?.(ev);
34
+ },
35
+ [disabled, setPressed, onPressProp]
36
+ );
37
+
38
+ const Component = asChild ? Slot : Pressable;
39
+
40
+ return (
41
+ <Component
42
+ ref={ref}
43
+ role="button"
44
+ aria-pressed={pressed}
45
+ aria-disabled={disabled || undefined}
46
+ onPress={onPress}
47
+ disabled={disabled || undefined}
48
+ {...props}
49
+ />
50
+ );
51
+ });
52
+
53
+ Root.displayName = 'ToggleRoot';
54
+
55
+ // ---------------------------------------------------------------------------
56
+ // Exports
57
+ // ---------------------------------------------------------------------------
58
+
59
+ export { Root };
@@ -0,0 +1,11 @@
1
+ import type { SlottablePressableProps, PressableRef } from '../types';
2
+
3
+ interface RootProps extends SlottablePressableProps {
4
+ pressed?: boolean;
5
+ defaultPressed?: boolean;
6
+ onPressedChange?: (pressed: boolean) => void;
7
+ disabled?: boolean;
8
+ ref?: PressableRef;
9
+ }
10
+
11
+ export type { RootProps };
@@ -0,0 +1,2 @@
1
+ export { Root, Item, useRootContext } from './toggle-group';
2
+ export type { RootProps, SingleRootProps, MultipleRootProps, ItemProps, RootContext } from './types';
@@ -0,0 +1,123 @@
1
+ import * as React from 'react';
2
+ import { Pressable, View, type GestureResponderEvent } from 'react-native';
3
+ import { useControllableState } from '../hooks';
4
+ import { Slot } from '../slot';
5
+ import { toggleGroupUtils } from '../utils';
6
+ import type {
7
+ RootProps,
8
+ ItemProps,
9
+ RootContext as RootContextType,
10
+ } from './types';
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Context
14
+ // ---------------------------------------------------------------------------
15
+
16
+ const RootContext = React.createContext<RootContextType | null>(null);
17
+
18
+ function useRootContext(): RootContextType {
19
+ const context = React.useContext(RootContext);
20
+ if (!context) {
21
+ throw new Error(
22
+ 'ToggleGroup compound components cannot be rendered outside the ToggleGroup.Root component'
23
+ );
24
+ }
25
+ return context;
26
+ }
27
+
28
+ // ---------------------------------------------------------------------------
29
+ // Root
30
+ // ---------------------------------------------------------------------------
31
+
32
+ const Root = React.forwardRef<View, RootProps>(function Root(
33
+ {
34
+ asChild,
35
+ type,
36
+ value: valueProp,
37
+ defaultValue,
38
+ onValueChange: onValueChangeProp,
39
+ disabled = false,
40
+ ...props
41
+ },
42
+ ref
43
+ ) {
44
+ const [value, setValue] = useControllableState({
45
+ prop: valueProp,
46
+ defaultProp: defaultValue,
47
+ onChange: onValueChangeProp as (state: string | string[] | undefined) => void,
48
+ });
49
+
50
+ const onValueChange = React.useCallback(
51
+ (newValue: string | string[]) => {
52
+ setValue(newValue);
53
+ },
54
+ [setValue]
55
+ );
56
+
57
+ const Component = asChild ? Slot : View;
58
+
59
+ return (
60
+ <RootContext.Provider value={{ type, value, disabled, onValueChange }}>
61
+ <Component ref={ref} role="group" {...props} />
62
+ </RootContext.Provider>
63
+ );
64
+ });
65
+
66
+ Root.displayName = 'ToggleGroupRoot';
67
+
68
+ // ---------------------------------------------------------------------------
69
+ // Item
70
+ // ---------------------------------------------------------------------------
71
+
72
+ const Item = React.forwardRef<View, ItemProps>(function Item(
73
+ {
74
+ asChild,
75
+ value: itemValue,
76
+ disabled: disabledProp = false,
77
+ onPress: onPressProp,
78
+ ...props
79
+ },
80
+ ref
81
+ ) {
82
+ const { type, disabled: rootDisabled, value, onValueChange } = useRootContext();
83
+
84
+ const isDisabled = rootDisabled || disabledProp;
85
+ const isSelected = toggleGroupUtils.getIsSelected(value, itemValue);
86
+
87
+ const onPress = React.useCallback(
88
+ (ev: GestureResponderEvent) => {
89
+ if (isDisabled) return;
90
+ if (type === 'single') {
91
+ const newValue = toggleGroupUtils.getNewSingleValue(value, itemValue);
92
+ onValueChange((newValue ?? '') as string & string[]);
93
+ } else {
94
+ const newValue = toggleGroupUtils.getNewMultipleValue(value, itemValue);
95
+ onValueChange(newValue as string[] & string);
96
+ }
97
+ onPressProp?.(ev);
98
+ },
99
+ [isDisabled, type, value, itemValue, onValueChange, onPressProp]
100
+ );
101
+
102
+ const Component = asChild ? Slot : Pressable;
103
+
104
+ return (
105
+ <Component
106
+ ref={ref}
107
+ role="button"
108
+ aria-pressed={isSelected}
109
+ aria-disabled={isDisabled || undefined}
110
+ onPress={onPress}
111
+ disabled={isDisabled || undefined}
112
+ {...props}
113
+ />
114
+ );
115
+ });
116
+
117
+ Item.displayName = 'ToggleGroupItem';
118
+
119
+ // ---------------------------------------------------------------------------
120
+ // Exports
121
+ // ---------------------------------------------------------------------------
122
+
123
+ export { Root, Item, useRootContext };
@@ -0,0 +1,36 @@
1
+ import type { SlottablePressableProps, SlottableViewProps, PressableRef, ViewRef } from '../types';
2
+
3
+ interface SingleRootProps extends SlottableViewProps {
4
+ type: 'single';
5
+ value?: string;
6
+ defaultValue?: string;
7
+ onValueChange?: (value: string) => void;
8
+ disabled?: boolean;
9
+ ref?: ViewRef;
10
+ }
11
+
12
+ interface MultipleRootProps extends SlottableViewProps {
13
+ type: 'multiple';
14
+ value?: string[];
15
+ defaultValue?: string[];
16
+ onValueChange?: (value: string[]) => void;
17
+ disabled?: boolean;
18
+ ref?: ViewRef;
19
+ }
20
+
21
+ type RootProps = SingleRootProps | MultipleRootProps;
22
+
23
+ interface ItemProps extends SlottablePressableProps {
24
+ value: string;
25
+ disabled?: boolean;
26
+ ref?: PressableRef;
27
+ }
28
+
29
+ interface RootContext {
30
+ type: 'single' | 'multiple';
31
+ value: string | string[] | undefined;
32
+ disabled: boolean;
33
+ onValueChange: (value: string | string[]) => void;
34
+ }
35
+
36
+ export type { RootProps, SingleRootProps, MultipleRootProps, ItemProps, RootContext };