@cdx-ui/primitives 0.0.1-alpha.2 → 0.0.1-alpha.20

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 (273) hide show
  1. package/lib/commonjs/avatar/context.js +11 -0
  2. package/lib/commonjs/avatar/context.js.map +1 -0
  3. package/lib/commonjs/avatar/createAvatarBadge.js +20 -0
  4. package/lib/commonjs/avatar/createAvatarBadge.js.map +1 -0
  5. package/lib/commonjs/avatar/createAvatarIcon.js +30 -0
  6. package/lib/commonjs/avatar/createAvatarIcon.js.map +1 -0
  7. package/lib/commonjs/avatar/createAvatarImage.js +51 -0
  8. package/lib/commonjs/avatar/createAvatarImage.js.map +1 -0
  9. package/lib/commonjs/avatar/createAvatarRoot.js +34 -0
  10. package/lib/commonjs/avatar/createAvatarRoot.js.map +1 -0
  11. package/lib/commonjs/avatar/createAvatarText.js +42 -0
  12. package/lib/commonjs/avatar/createAvatarText.js.map +1 -0
  13. package/lib/commonjs/avatar/index.js +30 -0
  14. package/lib/commonjs/avatar/index.js.map +1 -0
  15. package/lib/commonjs/avatar/types.js +6 -0
  16. package/lib/commonjs/avatar/types.js.map +1 -0
  17. package/lib/commonjs/button/createButtonRoot.js +3 -3
  18. package/lib/commonjs/button/createButtonRoot.js.map +1 -1
  19. package/lib/commonjs/button/index.js +18 -19
  20. package/lib/commonjs/button/index.js.map +1 -1
  21. package/lib/commonjs/checkbox/context.js +14 -0
  22. package/lib/commonjs/checkbox/context.js.map +1 -0
  23. package/lib/commonjs/checkbox/createCheckboxGroup.js +44 -0
  24. package/lib/commonjs/checkbox/createCheckboxGroup.js.map +1 -0
  25. package/lib/commonjs/checkbox/createCheckboxIcon.js +29 -0
  26. package/lib/commonjs/checkbox/createCheckboxIcon.js.map +1 -0
  27. package/lib/commonjs/checkbox/createCheckboxIndicator.js +47 -0
  28. package/lib/commonjs/checkbox/createCheckboxIndicator.js.map +1 -0
  29. package/lib/commonjs/checkbox/createCheckboxLabel.js +46 -0
  30. package/lib/commonjs/checkbox/createCheckboxLabel.js.map +1 -0
  31. package/lib/commonjs/checkbox/createCheckboxRoot.js +99 -0
  32. package/lib/commonjs/checkbox/createCheckboxRoot.js.map +1 -0
  33. package/lib/commonjs/checkbox/createCheckboxRoot.web.js +90 -0
  34. package/lib/commonjs/checkbox/createCheckboxRoot.web.js.map +1 -0
  35. package/lib/commonjs/checkbox/index.js +30 -0
  36. package/lib/commonjs/checkbox/index.js.map +1 -0
  37. package/lib/commonjs/checkbox/types.js +6 -0
  38. package/lib/commonjs/checkbox/types.js.map +1 -0
  39. package/lib/commonjs/checkbox/useCheckboxRoot.js +82 -0
  40. package/lib/commonjs/checkbox/useCheckboxRoot.js.map +1 -0
  41. package/lib/commonjs/index.js +49 -1
  42. package/lib/commonjs/index.js.map +1 -1
  43. package/lib/commonjs/input/index.js +13 -19
  44. package/lib/commonjs/input/index.js.map +1 -1
  45. package/lib/commonjs/link/LinkProvider.js +28 -0
  46. package/lib/commonjs/link/LinkProvider.js.map +1 -0
  47. package/lib/commonjs/link/createLink.js +74 -0
  48. package/lib/commonjs/link/createLink.js.map +1 -0
  49. package/lib/commonjs/link/index.js +27 -0
  50. package/lib/commonjs/link/index.js.map +1 -0
  51. package/lib/commonjs/link/types.js +6 -0
  52. package/lib/commonjs/link/types.js.map +1 -0
  53. package/lib/commonjs/link/useLink.js +56 -0
  54. package/lib/commonjs/link/useLink.js.map +1 -0
  55. package/lib/commonjs/overlay/OverlayContainer.js +6 -4
  56. package/lib/commonjs/overlay/OverlayContainer.js.map +1 -1
  57. package/lib/commonjs/select/context.js +8 -43
  58. package/lib/commonjs/select/context.js.map +1 -1
  59. package/lib/commonjs/select/createSelectItem.js +1 -1
  60. package/lib/commonjs/select/createSelectItem.js.map +1 -1
  61. package/lib/commonjs/select/createSelectRoot.js +1 -1
  62. package/lib/commonjs/select/createSelectRoot.js.map +1 -1
  63. package/lib/commonjs/select/createSelectTrigger.js +2 -8
  64. package/lib/commonjs/select/createSelectTrigger.js.map +1 -1
  65. package/lib/commonjs/switch/createSwitchRoot.js +73 -0
  66. package/lib/commonjs/switch/createSwitchRoot.js.map +1 -0
  67. package/lib/commonjs/switch/index.js +15 -0
  68. package/lib/commonjs/switch/index.js.map +1 -0
  69. package/lib/commonjs/switch/types.js +6 -0
  70. package/lib/commonjs/switch/types.js.map +1 -0
  71. package/lib/commonjs/types.js +2 -0
  72. package/lib/commonjs/types.js.map +1 -0
  73. package/lib/commonjs/utils/dataAttributes.web.js +1 -4
  74. package/lib/commonjs/utils/dataAttributes.web.js.map +1 -1
  75. package/lib/module/avatar/context.js +5 -0
  76. package/lib/module/avatar/context.js.map +1 -0
  77. package/lib/module/{input/createInputIcon.js → avatar/createAvatarBadge.js} +3 -3
  78. package/lib/module/avatar/createAvatarBadge.js.map +1 -0
  79. package/lib/module/avatar/createAvatarIcon.js +25 -0
  80. package/lib/module/avatar/createAvatarIcon.js.map +1 -0
  81. package/lib/module/avatar/createAvatarImage.js +46 -0
  82. package/lib/module/avatar/createAvatarImage.js.map +1 -0
  83. package/lib/module/avatar/createAvatarRoot.js +29 -0
  84. package/lib/module/avatar/createAvatarRoot.js.map +1 -0
  85. package/lib/module/avatar/createAvatarText.js +37 -0
  86. package/lib/module/avatar/createAvatarText.js.map +1 -0
  87. package/lib/module/avatar/index.js +26 -0
  88. package/lib/module/avatar/index.js.map +1 -0
  89. package/lib/module/avatar/types.js +4 -0
  90. package/lib/module/avatar/types.js.map +1 -0
  91. package/lib/module/button/createButtonRoot.js +3 -3
  92. package/lib/module/button/createButtonRoot.js.map +1 -1
  93. package/lib/module/button/index.js +18 -19
  94. package/lib/module/button/index.js.map +1 -1
  95. package/lib/module/checkbox/context.js +7 -0
  96. package/lib/module/checkbox/context.js.map +1 -0
  97. package/lib/module/checkbox/createCheckboxGroup.js +39 -0
  98. package/lib/module/checkbox/createCheckboxGroup.js.map +1 -0
  99. package/lib/module/checkbox/createCheckboxIcon.js +24 -0
  100. package/lib/module/checkbox/createCheckboxIcon.js.map +1 -0
  101. package/lib/module/checkbox/createCheckboxIndicator.js +42 -0
  102. package/lib/module/checkbox/createCheckboxIndicator.js.map +1 -0
  103. package/lib/module/checkbox/createCheckboxLabel.js +41 -0
  104. package/lib/module/checkbox/createCheckboxLabel.js.map +1 -0
  105. package/lib/module/checkbox/createCheckboxRoot.js +94 -0
  106. package/lib/module/checkbox/createCheckboxRoot.js.map +1 -0
  107. package/lib/module/checkbox/createCheckboxRoot.web.js +85 -0
  108. package/lib/module/checkbox/createCheckboxRoot.web.js.map +1 -0
  109. package/lib/module/checkbox/index.js +26 -0
  110. package/lib/module/checkbox/index.js.map +1 -0
  111. package/lib/module/checkbox/types.js +4 -0
  112. package/lib/module/checkbox/types.js.map +1 -0
  113. package/lib/module/checkbox/useCheckboxRoot.js +78 -0
  114. package/lib/module/checkbox/useCheckboxRoot.js.map +1 -0
  115. package/lib/module/index.js +5 -1
  116. package/lib/module/index.js.map +1 -1
  117. package/lib/module/input/index.js +12 -17
  118. package/lib/module/input/index.js.map +1 -1
  119. package/lib/module/link/LinkProvider.js +23 -0
  120. package/lib/module/link/LinkProvider.js.map +1 -0
  121. package/lib/module/link/createLink.js +68 -0
  122. package/lib/module/link/createLink.js.map +1 -0
  123. package/lib/module/link/index.js +6 -0
  124. package/lib/module/link/index.js.map +1 -0
  125. package/lib/module/link/types.js +4 -0
  126. package/lib/module/link/types.js.map +1 -0
  127. package/lib/module/link/useLink.js +52 -0
  128. package/lib/module/link/useLink.js.map +1 -0
  129. package/lib/module/overlay/OverlayContainer.js +7 -5
  130. package/lib/module/overlay/OverlayContainer.js.map +1 -1
  131. package/lib/module/select/context.js +4 -42
  132. package/lib/module/select/context.js.map +1 -1
  133. package/lib/module/select/createSelectItem.js +2 -2
  134. package/lib/module/select/createSelectItem.js.map +1 -1
  135. package/lib/module/select/createSelectRoot.js +2 -2
  136. package/lib/module/select/createSelectRoot.js.map +1 -1
  137. package/lib/module/select/createSelectTrigger.js +3 -9
  138. package/lib/module/select/createSelectTrigger.js.map +1 -1
  139. package/lib/module/switch/createSwitchRoot.js +67 -0
  140. package/lib/module/switch/createSwitchRoot.js.map +1 -0
  141. package/lib/module/switch/index.js +11 -0
  142. package/lib/module/switch/index.js.map +1 -0
  143. package/lib/module/switch/types.js +4 -0
  144. package/lib/module/switch/types.js.map +1 -0
  145. package/lib/module/types.js +2 -0
  146. package/lib/module/types.js.map +1 -0
  147. package/lib/module/utils/dataAttributes.web.js +1 -4
  148. package/lib/module/utils/dataAttributes.web.js.map +1 -1
  149. package/lib/typescript/avatar/context.d.ts +6 -0
  150. package/lib/typescript/avatar/context.d.ts.map +1 -0
  151. package/lib/typescript/avatar/createAvatarBadge.d.ts +4 -0
  152. package/lib/typescript/avatar/createAvatarBadge.d.ts.map +1 -0
  153. package/lib/typescript/avatar/createAvatarIcon.d.ts +4 -0
  154. package/lib/typescript/avatar/createAvatarIcon.d.ts.map +1 -0
  155. package/lib/typescript/avatar/createAvatarImage.d.ts +4 -0
  156. package/lib/typescript/avatar/createAvatarImage.d.ts.map +1 -0
  157. package/lib/typescript/avatar/createAvatarRoot.d.ts +4 -0
  158. package/lib/typescript/avatar/createAvatarRoot.d.ts.map +1 -0
  159. package/lib/typescript/avatar/createAvatarText.d.ts +4 -0
  160. package/lib/typescript/avatar/createAvatarText.d.ts.map +1 -0
  161. package/lib/typescript/avatar/index.d.ts +11 -0
  162. package/lib/typescript/avatar/index.d.ts.map +1 -0
  163. package/lib/typescript/avatar/types.d.ts +33 -0
  164. package/lib/typescript/avatar/types.d.ts.map +1 -0
  165. package/lib/typescript/button/context.d.ts +1 -1
  166. package/lib/typescript/button/context.d.ts.map +1 -1
  167. package/lib/typescript/button/createButtonRoot.d.ts +1 -1
  168. package/lib/typescript/button/createButtonRoot.d.ts.map +1 -1
  169. package/lib/typescript/button/index.d.ts +1 -1
  170. package/lib/typescript/button/index.d.ts.map +1 -1
  171. package/lib/typescript/button/types.d.ts +0 -7
  172. package/lib/typescript/button/types.d.ts.map +1 -1
  173. package/lib/typescript/checkbox/context.d.ts +18 -0
  174. package/lib/typescript/checkbox/context.d.ts.map +1 -0
  175. package/lib/typescript/checkbox/createCheckboxGroup.d.ts +3 -0
  176. package/lib/typescript/checkbox/createCheckboxGroup.d.ts.map +1 -0
  177. package/lib/typescript/checkbox/createCheckboxIcon.d.ts +3 -0
  178. package/lib/typescript/checkbox/createCheckboxIcon.d.ts.map +1 -0
  179. package/lib/typescript/checkbox/createCheckboxIndicator.d.ts +5 -0
  180. package/lib/typescript/checkbox/createCheckboxIndicator.d.ts.map +1 -0
  181. package/lib/typescript/checkbox/createCheckboxLabel.d.ts +5 -0
  182. package/lib/typescript/checkbox/createCheckboxLabel.d.ts.map +1 -0
  183. package/lib/typescript/checkbox/createCheckboxRoot.d.ts +2 -0
  184. package/lib/typescript/checkbox/createCheckboxRoot.d.ts.map +1 -0
  185. package/lib/typescript/checkbox/createCheckboxRoot.web.d.ts +2 -0
  186. package/lib/typescript/checkbox/createCheckboxRoot.web.d.ts.map +1 -0
  187. package/lib/typescript/checkbox/index.d.ts +11 -0
  188. package/lib/typescript/checkbox/index.d.ts.map +1 -0
  189. package/lib/typescript/checkbox/types.d.ts +57 -0
  190. package/lib/typescript/checkbox/types.d.ts.map +1 -0
  191. package/lib/typescript/checkbox/useCheckboxRoot.d.ts +147 -0
  192. package/lib/typescript/checkbox/useCheckboxRoot.d.ts.map +1 -0
  193. package/lib/typescript/index.d.ts +6 -1
  194. package/lib/typescript/index.d.ts.map +1 -1
  195. package/lib/typescript/input/index.d.ts +3 -3
  196. package/lib/typescript/input/index.d.ts.map +1 -1
  197. package/lib/typescript/input/types.d.ts +1 -2
  198. package/lib/typescript/input/types.d.ts.map +1 -1
  199. package/lib/typescript/link/LinkProvider.d.ts +36 -0
  200. package/lib/typescript/link/LinkProvider.d.ts.map +1 -0
  201. package/lib/typescript/link/createLink.d.ts +6 -0
  202. package/lib/typescript/link/createLink.d.ts.map +1 -0
  203. package/lib/typescript/link/index.d.ts +6 -0
  204. package/lib/typescript/link/index.d.ts.map +1 -0
  205. package/lib/typescript/link/types.d.ts +45 -0
  206. package/lib/typescript/link/types.d.ts.map +1 -0
  207. package/lib/typescript/link/useLink.d.ts +9 -0
  208. package/lib/typescript/link/useLink.d.ts.map +1 -0
  209. package/lib/typescript/overlay/OverlayContainer.d.ts +5 -1
  210. package/lib/typescript/overlay/OverlayContainer.d.ts.map +1 -1
  211. package/lib/typescript/select/context.d.ts +14 -7
  212. package/lib/typescript/select/context.d.ts.map +1 -1
  213. package/lib/typescript/select/createSelectTrigger.d.ts +2 -7
  214. package/lib/typescript/select/createSelectTrigger.d.ts.map +1 -1
  215. package/lib/typescript/select/types.d.ts +1 -7
  216. package/lib/typescript/select/types.d.ts.map +1 -1
  217. package/lib/typescript/switch/createSwitchRoot.d.ts +3 -0
  218. package/lib/typescript/switch/createSwitchRoot.d.ts.map +1 -0
  219. package/lib/typescript/switch/index.d.ts +7 -0
  220. package/lib/typescript/switch/index.d.ts.map +1 -0
  221. package/lib/typescript/switch/types.d.ts +38 -0
  222. package/lib/typescript/switch/types.d.ts.map +1 -0
  223. package/lib/typescript/types.d.ts +8 -0
  224. package/lib/typescript/types.d.ts.map +1 -0
  225. package/lib/typescript/utils/dataAttributes.web.d.ts.map +1 -1
  226. package/package.json +12 -8
  227. package/src/avatar/context.tsx +5 -0
  228. package/src/avatar/createAvatarBadge.tsx +12 -0
  229. package/src/avatar/createAvatarIcon.tsx +16 -0
  230. package/src/avatar/createAvatarImage.tsx +40 -0
  231. package/src/avatar/createAvatarRoot.tsx +23 -0
  232. package/src/avatar/createAvatarText.tsx +30 -0
  233. package/src/avatar/index.ts +43 -0
  234. package/src/avatar/types.ts +77 -0
  235. package/src/button/context.tsx +1 -1
  236. package/src/button/createButtonRoot.tsx +5 -4
  237. package/src/button/index.tsx +18 -19
  238. package/src/button/types.ts +0 -8
  239. package/src/checkbox/context.tsx +18 -0
  240. package/src/checkbox/createCheckboxGroup.tsx +32 -0
  241. package/src/checkbox/createCheckboxIcon.tsx +18 -0
  242. package/src/checkbox/createCheckboxIndicator.tsx +43 -0
  243. package/src/checkbox/createCheckboxLabel.tsx +42 -0
  244. package/src/checkbox/createCheckboxRoot.tsx +100 -0
  245. package/src/checkbox/createCheckboxRoot.web.tsx +79 -0
  246. package/src/checkbox/index.ts +43 -0
  247. package/src/checkbox/types.ts +74 -0
  248. package/src/checkbox/useCheckboxRoot.ts +85 -0
  249. package/src/index.ts +6 -1
  250. package/src/input/index.tsx +15 -28
  251. package/src/input/types.ts +0 -4
  252. package/src/link/LinkProvider.tsx +50 -0
  253. package/src/link/createLink.tsx +71 -0
  254. package/src/link/index.tsx +5 -0
  255. package/src/link/types.ts +47 -0
  256. package/src/link/useLink.ts +54 -0
  257. package/src/overlay/OverlayContainer.tsx +7 -8
  258. package/src/select/context.tsx +9 -44
  259. package/src/select/createSelectItem.tsx +3 -3
  260. package/src/select/createSelectRoot.tsx +3 -3
  261. package/src/select/createSelectTrigger.tsx +6 -31
  262. package/src/select/types.ts +1 -10
  263. package/src/switch/createSwitchRoot.tsx +72 -0
  264. package/src/switch/index.ts +16 -0
  265. package/src/switch/types.ts +42 -0
  266. package/src/types.ts +7 -0
  267. package/src/utils/dataAttributes.web.ts +2 -4
  268. package/lib/commonjs/input/createInputIcon.js +0 -20
  269. package/lib/commonjs/input/createInputIcon.js.map +0 -1
  270. package/lib/module/input/createInputIcon.js.map +0 -1
  271. package/lib/typescript/input/createInputIcon.d.ts +0 -4
  272. package/lib/typescript/input/createInputIcon.d.ts.map +0 -1
  273. package/src/input/createInputIcon.tsx +0 -12
@@ -1,56 +1,21 @@
1
- import { createContext, useContext } from 'react';
2
- import type { InteractionState, SelectContextValue, SelectItemInteractionState } from './types';
1
+ import { createContext } from '@cdx-ui/utils';
2
+ import type { InteractionState } from '../types';
3
+ import type { SelectContextValue, SelectItemInteractionState } from './types';
3
4
 
4
- const defaultContextValue: SelectContextValue = {
5
- open: false,
6
- items: [],
7
- setOpen: () => {},
8
- value: undefined,
9
- onValueChange: () => {},
10
- disabled: false,
11
- required: false,
12
- invalid: false,
13
- readOnly: false,
14
- native: false,
15
- triggerRef: { current: null },
16
- contentId: '',
17
- triggerId: '',
18
- activeValue: undefined,
19
- setActiveValue: () => {}, // TODO: Is this needed? At the very least it could be moved to SelectContent
20
- accessibilityLabel: undefined,
21
- };
22
-
23
- export const SelectContext = createContext<SelectContextValue>(defaultContextValue);
24
-
25
- export const useSelectContext = () => useContext(SelectContext);
5
+ export const [SelectProvider, useSelectContext] =
6
+ createContext<SelectContextValue>('SelectContext');
26
7
 
27
8
  /**
28
9
  * Propagates the trigger's interaction state (hover, focus, active, etc.) to
29
10
  * child components like SelectValue and SelectIcon so they can apply matching
30
11
  * data-attribute styles.
31
12
  */
32
- export const SelectTriggerContext = createContext<InteractionState>({
33
- hover: false,
34
- focus: false,
35
- active: false,
36
- disabled: false,
37
- focusVisible: false,
38
- });
39
-
40
- export const useSelectTriggerContext = () => useContext(SelectTriggerContext);
13
+ export const [SelectTriggerProvider, useSelectTriggerContext] =
14
+ createContext<InteractionState>('SelectTriggerContext');
41
15
 
42
16
  /**
43
17
  * Propagates the item's interaction and selection state (hover, focus, active,
44
18
  * highlighted, checked, etc.) to child components like SelectItemLabel.
45
19
  */
46
- export const SelectItemContext = createContext<SelectItemInteractionState>({
47
- hover: false,
48
- focus: false,
49
- active: false,
50
- disabled: false,
51
- focusVisible: false,
52
- highlighted: false,
53
- checked: false,
54
- });
55
-
56
- export const useSelectItemContext = () => useContext(SelectItemContext);
20
+ export const [SelectItemProvider, useSelectItemContext] =
21
+ createContext<SelectItemInteractionState>('SelectItemContext');
@@ -4,7 +4,7 @@ import { composeEventHandlers } from '@cdx-ui/utils';
4
4
  import { useFocus, useFocusRing } from '@react-native-aria/focus';
5
5
  import { useHover, usePress } from '@react-native-aria/interactions';
6
6
  import { dataAttributes } from '../utils/dataAttributes';
7
- import { SelectItemContext, useSelectContext } from './context';
7
+ import { SelectItemProvider, useSelectContext } from './context';
8
8
  import type { ISelectItemProps } from './types';
9
9
 
10
10
  export const createSelectItem = <T,>(BaseItem: React.ComponentType<T>) =>
@@ -74,7 +74,7 @@ export const createSelectItem = <T,>(BaseItem: React.ComponentType<T>) =>
74
74
  );
75
75
 
76
76
  return (
77
- <SelectItemContext.Provider value={interactionState}>
77
+ <SelectItemProvider value={interactionState}>
78
78
  <BaseItem
79
79
  ref={ref}
80
80
  role="option"
@@ -115,7 +115,7 @@ export const createSelectItem = <T,>(BaseItem: React.ComponentType<T>) =>
115
115
  >
116
116
  {children}
117
117
  </BaseItem>
118
- </SelectItemContext.Provider>
118
+ </SelectItemProvider>
119
119
  );
120
120
  },
121
121
  );
@@ -2,7 +2,7 @@ import type React from 'react';
2
2
  import { forwardRef, useCallback, useId, useMemo, useRef, useState } from 'react';
3
3
  import { useControllableState, useFormControl } from '@cdx-ui/utils';
4
4
  import { dataAttributes } from '../utils/dataAttributes';
5
- import { SelectContext } from './context';
5
+ import { SelectProvider } from './context';
6
6
  import type { ISelectProps } from './types';
7
7
 
8
8
  export const createSelectRoot = <T,>(BaseRoot: React.ComponentType<T>) =>
@@ -110,7 +110,7 @@ export const createSelectRoot = <T,>(BaseRoot: React.ComponentType<T>) =>
110
110
  );
111
111
 
112
112
  return (
113
- <SelectContext.Provider value={contextValue}>
113
+ <SelectProvider value={contextValue}>
114
114
  <BaseRoot
115
115
  ref={ref}
116
116
  {...(props as T)}
@@ -124,7 +124,7 @@ export const createSelectRoot = <T,>(BaseRoot: React.ComponentType<T>) =>
124
124
  >
125
125
  {children}
126
126
  </BaseRoot>
127
- </SelectContext.Provider>
127
+ </SelectProvider>
128
128
  );
129
129
  },
130
130
  );
@@ -4,8 +4,9 @@ import { type GestureResponderEvent, Platform } from 'react-native';
4
4
  import { composeEventHandlers, mergeRefs } from '@cdx-ui/utils';
5
5
  import { useFocus, useFocusRing } from '@react-native-aria/focus';
6
6
  import { useHover, usePress } from '@react-native-aria/interactions';
7
+ import type { InteractionState } from '../types';
7
8
  import { dataAttributes } from '../utils/dataAttributes';
8
- import { SelectTriggerContext, useSelectContext } from './context';
9
+ import { SelectTriggerProvider, useSelectContext } from './context';
9
10
  import type { ISelectTriggerProps } from './types';
10
11
 
11
12
  export const createSelectTrigger = <T,>(BaseTrigger: React.ComponentType<T>) =>
@@ -20,21 +21,7 @@ export const createSelectTrigger = <T,>(BaseTrigger: React.ComponentType<T>) =>
20
21
  isDisabled: isDisabledProp,
21
22
  ...props
22
23
  }: Omit<ISelectTriggerProps, 'children'> & {
23
- children?:
24
- | (({
25
- hovered,
26
- pressed,
27
- focused,
28
- focusVisible,
29
- disabled,
30
- }: {
31
- hovered?: boolean;
32
- pressed?: boolean;
33
- focused?: boolean;
34
- focusVisible?: boolean;
35
- disabled?: boolean;
36
- }) => React.ReactNode)
37
- | React.ReactNode;
24
+ children?: ((state: InteractionState) => React.ReactNode) | React.ReactNode;
38
25
  },
39
26
  ref?: any,
40
27
  ) => {
@@ -122,7 +109,7 @@ export const createSelectTrigger = <T,>(BaseTrigger: React.ComponentType<T>) =>
122
109
  : {};
123
110
 
124
111
  return (
125
- <SelectTriggerContext.Provider value={interactionState}>
112
+ <SelectTriggerProvider value={interactionState}>
126
113
  <BaseTrigger
127
114
  ref={mergedRef}
128
115
  role="combobox"
@@ -172,21 +159,9 @@ export const createSelectTrigger = <T,>(BaseTrigger: React.ComponentType<T>) =>
172
159
  )}
173
160
  {...webKeyboardProps}
174
161
  >
175
- {/* TODO: Align render-function children convention with Button — Button
176
- passes raw hook values (isHovered, isFocused, etc.) while this
177
- passes merged interaction state (isHoveredProp || isHovered). Pick
178
- one convention and apply to both. */}
179
- {typeof children === 'function'
180
- ? children({
181
- hovered: interactionState.hover,
182
- focused: interactionState.focus,
183
- pressed: interactionState.active,
184
- disabled: interactionState.disabled,
185
- focusVisible: interactionState.focusVisible,
186
- })
187
- : children}
162
+ {typeof children === 'function' ? children(interactionState) : children}
188
163
  </BaseTrigger>
189
- </SelectTriggerContext.Provider>
164
+ </SelectTriggerProvider>
190
165
  );
191
166
  },
192
167
  );
@@ -1,16 +1,7 @@
1
1
  import type { PropsWithoutRef, ReactNode, RefAttributes, RefObject } from 'react';
2
2
  import type { PressableProps, ViewProps } from 'react-native';
3
3
  import type { EntryOrExitLayoutType } from 'react-native-reanimated';
4
-
5
- // TODO: Consolidate with button/types.ts
6
-
7
- export interface InteractionState {
8
- hover: boolean;
9
- focus: boolean;
10
- active: boolean;
11
- disabled: boolean;
12
- focusVisible: boolean;
13
- }
4
+ import type { InteractionState } from '../types';
14
5
 
15
6
  export interface SelectItemInteractionState extends InteractionState {
16
7
  highlighted: boolean;
@@ -0,0 +1,72 @@
1
+ import React, { forwardRef } from 'react';
2
+ import { useFocusRing } from '@react-native-aria/focus';
3
+ import { useHover } from '@react-native-aria/interactions';
4
+ import { useToggleState } from '@react-stately/toggle';
5
+ import { useFormControlContext, mergeRefs } from '@cdx-ui/utils';
6
+ import type { ISwitchProps } from './types';
7
+
8
+ type MergeableRef = Parameters<typeof mergeRefs>[0];
9
+
10
+ export const createSwitchRoot = <T, R = unknown>(BaseSwitch: React.ComponentType<T>) =>
11
+ forwardRef<R, ISwitchProps>(
12
+ (
13
+ {
14
+ disabled,
15
+ isDisabled,
16
+ isChecked,
17
+ isInvalid,
18
+ defaultValue,
19
+ value,
20
+ onValueChange,
21
+ ...props
22
+ }: ISwitchProps,
23
+ ref: React.ForwardedRef<R>,
24
+ ) => {
25
+ const formControlContext = useFormControlContext();
26
+ const combinedProps = { ...formControlContext, ...props };
27
+ const controlledValue = value ?? isChecked;
28
+ const state = useToggleState({
29
+ defaultSelected: defaultValue ?? false,
30
+ isSelected: controlledValue,
31
+ onChange: onValueChange,
32
+ });
33
+
34
+ const checked = controlledValue ?? state.isSelected;
35
+
36
+ const _ref = React.useRef(null);
37
+ const { isHovered } = useHover({}, _ref);
38
+ const { isFocusVisible, focusProps } = useFocusRing();
39
+
40
+ const mergedRef = mergeRefs(ref as MergeableRef, _ref);
41
+
42
+ const isDisabledResolved = disabled || isDisabled || combinedProps.isDisabled;
43
+ const isInvalidResolved = isInvalid || combinedProps.isInvalid;
44
+
45
+ return (
46
+ <BaseSwitch
47
+ states={{
48
+ hover: isHovered,
49
+ disabled: isDisabledResolved,
50
+ invalid: isInvalidResolved,
51
+ checked,
52
+ focusVisible: isFocusVisible,
53
+ }}
54
+ dataSet={{
55
+ hover: isHovered ? 'true' : 'false',
56
+ disabled: isDisabledResolved ? 'true' : 'false',
57
+ invalid: isInvalidResolved ? 'true' : 'false',
58
+ checked: checked ? 'true' : 'false',
59
+ 'focus-visible': isFocusVisible ? 'true' : 'false',
60
+ }}
61
+ disabled={isDisabledResolved}
62
+ {...focusProps}
63
+ onValueChange={(val: boolean) => {
64
+ state.setSelected(val);
65
+ }}
66
+ value={checked}
67
+ {...(combinedProps as T)}
68
+ ref={mergedRef as React.Ref<R>}
69
+ />
70
+ );
71
+ },
72
+ );
@@ -0,0 +1,16 @@
1
+ import type React from 'react';
2
+ import { createSwitchRoot } from './createSwitchRoot';
3
+ import type { ISwitchComponentType } from './types';
4
+
5
+ export type { ISwitchProps, ISwitchComponentType, IInterfaceSwitchProps } from './types';
6
+
7
+ export function createSwitch<T, R = unknown>({
8
+ Root,
9
+ }: {
10
+ Root: React.ComponentType<T>;
11
+ }): ISwitchComponentType<T, R> {
12
+ const Switch = createSwitchRoot(Root);
13
+
14
+ Switch.displayName = 'Switch';
15
+ return Switch as ISwitchComponentType<T, R>;
16
+ }
@@ -0,0 +1,42 @@
1
+ import type React from 'react';
2
+
3
+ export interface IInterfaceSwitchProps {
4
+ /**
5
+ * Callback invoked when the switch value changes.
6
+ */
7
+ onValueChange?: (value: boolean) => void;
8
+ /**
9
+ * If true, set the Switch to the checked state.
10
+ */
11
+ isChecked?: boolean;
12
+ /**
13
+ * The current value of the switch.
14
+ */
15
+ value?: boolean;
16
+ /**
17
+ * If true, the switch will be disabled.
18
+ */
19
+ isDisabled?: boolean;
20
+ /**
21
+ * If true, the switch will be disabled.
22
+ */
23
+ disabled?: boolean;
24
+ /**
25
+ * If true, the checkbox will be initially checked.
26
+ */
27
+ defaultValue?: boolean;
28
+ /**
29
+ * If true, set the switch to the invalid state.
30
+ */
31
+ isInvalid?: boolean;
32
+ /**
33
+ * If true, the switch will be hovered.
34
+ */
35
+ isHovered?: boolean;
36
+ }
37
+
38
+ export type ISwitchComponentType<T, R = unknown> = React.ForwardRefExoticComponent<
39
+ React.PropsWithoutRef<T & IInterfaceSwitchProps> & React.RefAttributes<R>
40
+ >;
41
+
42
+ export type ISwitchProps = IInterfaceSwitchProps;
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export interface InteractionState {
2
+ hover: boolean;
3
+ focus: boolean;
4
+ active: boolean;
5
+ disabled: boolean;
6
+ focusVisible: boolean;
7
+ }
@@ -17,10 +17,8 @@ export function dataAttributes(attrs: Record<string, DataAttributeValue>): {
17
17
  const dataSet: Record<string, boolean | string> = {};
18
18
  for (const key in attrs) {
19
19
  const value = attrs[key];
20
- if (value === undefined) {
21
- continue;
22
- }
23
- dataSet[key] = value;
20
+
21
+ dataSet[key] = value ?? false;
24
22
  }
25
23
  return { dataSet };
26
24
  }
@@ -1,20 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createInputIcon = void 0;
7
- var _react = require("react");
8
- var _jsxRuntime = require("react/jsx-runtime");
9
- const createInputIcon = BaseInputIcon => /*#__PURE__*/(0, _react.forwardRef)(({
10
- children,
11
- ...props
12
- }, ref) => {
13
- return /*#__PURE__*/(0, _jsxRuntime.jsx)(BaseInputIcon, {
14
- ref: ref,
15
- ...props,
16
- children: children
17
- });
18
- });
19
- exports.createInputIcon = createInputIcon;
20
- //# sourceMappingURL=createInputIcon.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_react","require","_jsxRuntime","createInputIcon","BaseInputIcon","forwardRef","children","props","ref","jsx","exports"],"sourceRoot":"../../../src","sources":["input/createInputIcon.tsx"],"mappings":";;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AAAmC,IAAAC,WAAA,GAAAD,OAAA;AAG5B,MAAME,eAAe,GAAQC,aAAqC,iBACvE,IAAAC,iBAAU,EAA2B,CAAC;EAAEC,QAAQ;EAAE,GAAGC;AAAM,CAAC,EAAEC,GAAG,KAAK;EACpE,oBACE,IAAAN,WAAA,CAAAO,GAAA,EAACL,aAAa;IAACI,GAAG,EAAEA,GAAI;IAAA,GAAMD,KAAK;IAAAD,QAAA,EAChCA;EAAQ,CACI,CAAC;AAEpB,CAAC,CAAC;AAACI,OAAA,CAAAP,eAAA,GAAAA,eAAA","ignoreList":[]}
@@ -1 +0,0 @@
1
- {"version":3,"names":["forwardRef","jsx","_jsx","createInputIcon","BaseInputIcon","children","props","ref"],"sourceRoot":"../../../src","sources":["input/createInputIcon.tsx"],"mappings":";;AACA,SAASA,UAAU,QAAQ,OAAO;AAAC,SAAAC,GAAA,IAAAC,IAAA;AAGnC,OAAO,MAAMC,eAAe,GAAQC,aAAqC,iBACvEJ,UAAU,CAA2B,CAAC;EAAEK,QAAQ;EAAE,GAAGC;AAAM,CAAC,EAAEC,GAAG,KAAK;EACpE,oBACEL,IAAA,CAACE,aAAa;IAACG,GAAG,EAAEA,GAAI;IAAA,GAAMD,KAAK;IAAAD,QAAA,EAChCA;EAAQ,CACI,CAAC;AAEpB,CAAC,CAAC","ignoreList":[]}
@@ -1,4 +0,0 @@
1
- import type React from 'react';
2
- import type { IInputIconProps } from './types';
3
- export declare const createInputIcon: <T>(BaseInputIcon: React.ComponentType<T>) => React.ForwardRefExoticComponent<IInputIconProps & React.RefAttributes<unknown>>;
4
- //# sourceMappingURL=createInputIcon.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createInputIcon.d.ts","sourceRoot":"","sources":["../../../src/input/createInputIcon.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,eAAO,MAAM,eAAe,GAAI,CAAC,EAAG,eAAe,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,oFAOrE,CAAC"}
@@ -1,12 +0,0 @@
1
- import type React from 'react';
2
- import { forwardRef } from 'react';
3
- import type { IInputIconProps } from './types';
4
-
5
- export const createInputIcon = <T,>(BaseInputIcon: React.ComponentType<T>) =>
6
- forwardRef<unknown, IInputIconProps>(({ children, ...props }, ref) => {
7
- return (
8
- <BaseInputIcon ref={ref} {...(props as T)}>
9
- {children}
10
- </BaseInputIcon>
11
- );
12
- });