react-native-molecules 0.5.0-beta.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 (355) hide show
  1. package/components/Accordion/Accordion.tsx +89 -0
  2. package/components/Accordion/AccordionItem.tsx +94 -0
  3. package/components/Accordion/AccordionItemContent.tsx +17 -0
  4. package/components/Accordion/AccordionItemHeader.tsx +122 -0
  5. package/components/Accordion/index.tsx +36 -0
  6. package/components/Accordion/utils.ts +62 -0
  7. package/components/ActivityIndicator/ActivityIndicator.ios.tsx +1 -0
  8. package/components/ActivityIndicator/ActivityIndicator.tsx +200 -0
  9. package/components/ActivityIndicator/AnimatedSpinner.tsx +121 -0
  10. package/components/ActivityIndicator/index.tsx +17 -0
  11. package/components/Appbar/AppbarActions.tsx +13 -0
  12. package/components/Appbar/AppbarBase.tsx +60 -0
  13. package/components/Appbar/AppbarCenterAligned.tsx +13 -0
  14. package/components/Appbar/AppbarLarge.tsx +13 -0
  15. package/components/Appbar/AppbarLeft.tsx +26 -0
  16. package/components/Appbar/AppbarMedium.tsx +13 -0
  17. package/components/Appbar/AppbarRight.tsx +40 -0
  18. package/components/Appbar/AppbarSmall.tsx +13 -0
  19. package/components/Appbar/AppbarTitle.tsx +49 -0
  20. package/components/Appbar/index.tsx +46 -0
  21. package/components/Appbar/types.ts +19 -0
  22. package/components/Appbar/utils.ts +127 -0
  23. package/components/Avatar/Avatar.tsx +189 -0
  24. package/components/Avatar/index.tsx +11 -0
  25. package/components/Avatar/utils.ts +35 -0
  26. package/components/Backdrop/Backdrop.tsx +18 -0
  27. package/components/Backdrop/index.tsx +11 -0
  28. package/components/Backdrop/types.ts +3 -0
  29. package/components/Backdrop/utils.ts +21 -0
  30. package/components/Badge/Badge.tsx +29 -0
  31. package/components/Badge/index.tsx +11 -0
  32. package/components/Badge/utils.ts +38 -0
  33. package/components/Button/Button.tsx +349 -0
  34. package/components/Button/index.tsx +11 -0
  35. package/components/Button/types.ts +5 -0
  36. package/components/Button/utils.ts +416 -0
  37. package/components/Card/Card.tsx +160 -0
  38. package/components/Card/CardActions.tsx +18 -0
  39. package/components/Card/CardContent.tsx +29 -0
  40. package/components/Card/CardHeader.tsx +31 -0
  41. package/components/Card/CardHeadline.tsx +15 -0
  42. package/components/Card/CardMedia.tsx +36 -0
  43. package/components/Card/CardSubhead.tsx +15 -0
  44. package/components/Card/CardText.tsx +13 -0
  45. package/components/Card/CardTypography.tsx +113 -0
  46. package/components/Card/index.tsx +38 -0
  47. package/components/Card/types.ts +1 -0
  48. package/components/Card/utils.ts +23 -0
  49. package/components/Checkbox/Checkbox.tsx +123 -0
  50. package/components/Checkbox/CheckboxBase.ios.tsx +86 -0
  51. package/components/Checkbox/CheckboxBase.tsx +216 -0
  52. package/components/Checkbox/index.tsx +11 -0
  53. package/components/Checkbox/types.ts +72 -0
  54. package/components/Checkbox/utils.ts +210 -0
  55. package/components/Chip/Chip.tsx +416 -0
  56. package/components/Chip/index.tsx +51 -0
  57. package/components/Chip/utils.ts +100 -0
  58. package/components/DatePickerDocked/DatePickerDocked.tsx +30 -0
  59. package/components/DatePickerDocked/DatePickerDockedHeader.tsx +129 -0
  60. package/components/DatePickerDocked/index.tsx +21 -0
  61. package/components/DatePickerDocked/types.ts +11 -0
  62. package/components/DatePickerDocked/utils.ts +155 -0
  63. package/components/DatePickerInline/AutoSizer.tsx +46 -0
  64. package/components/DatePickerInline/DatePickerContext.tsx +21 -0
  65. package/components/DatePickerInline/DatePickerInline.tsx +82 -0
  66. package/components/DatePickerInline/DatePickerInlineBase.tsx +181 -0
  67. package/components/DatePickerInline/DatePickerInlineHeader.tsx +108 -0
  68. package/components/DatePickerInline/Day.tsx +88 -0
  69. package/components/DatePickerInline/DayName.tsx +17 -0
  70. package/components/DatePickerInline/DayNames.tsx +32 -0
  71. package/components/DatePickerInline/DayRange.tsx +48 -0
  72. package/components/DatePickerInline/HeaderItem.tsx +111 -0
  73. package/components/DatePickerInline/Month.tsx +233 -0
  74. package/components/DatePickerInline/MonthPicker.tsx +174 -0
  75. package/components/DatePickerInline/Swiper.native.tsx +172 -0
  76. package/components/DatePickerInline/Swiper.tsx +172 -0
  77. package/components/DatePickerInline/SwiperUtils.ts +40 -0
  78. package/components/DatePickerInline/Week.tsx +67 -0
  79. package/components/DatePickerInline/YearPicker.tsx +139 -0
  80. package/components/DatePickerInline/dateUtils.tsx +334 -0
  81. package/components/DatePickerInline/index.tsx +41 -0
  82. package/components/DatePickerInline/types.ts +104 -0
  83. package/components/DatePickerInline/utils.ts +367 -0
  84. package/components/DatePickerInput/DatePickerInput.tsx +139 -0
  85. package/components/DatePickerInput/DatePickerInputModal.tsx +48 -0
  86. package/components/DatePickerInput/DatePickerInputWithoutModal.tsx +77 -0
  87. package/components/DatePickerInput/DateRangeInput.tsx +88 -0
  88. package/components/DatePickerInput/index.tsx +14 -0
  89. package/components/DatePickerInput/inputUtils.ts +138 -0
  90. package/components/DatePickerInput/types.ts +28 -0
  91. package/components/DatePickerInput/utils.ts +16 -0
  92. package/components/DatePickerModal/AnimatedCrossView.tsx +94 -0
  93. package/components/DatePickerModal/CalendarEdit.tsx +139 -0
  94. package/components/DatePickerModal/DatePickerModal.tsx +85 -0
  95. package/components/DatePickerModal/DatePickerModalContent.tsx +155 -0
  96. package/components/DatePickerModal/DatePickerModalContentHeader.tsx +213 -0
  97. package/components/DatePickerModal/DatePickerModalHeader.tsx +74 -0
  98. package/components/DatePickerModal/DatePickerModalHeaderBackground.tsx +13 -0
  99. package/components/DatePickerModal/index.tsx +20 -0
  100. package/components/DatePickerModal/types.ts +92 -0
  101. package/components/DatePickerModal/utils.ts +121 -0
  102. package/components/DateTimePicker/DateTimePicker.tsx +172 -0
  103. package/components/DateTimePicker/index.tsx +14 -0
  104. package/components/DateTimePicker/utils.ts +13 -0
  105. package/components/Dialog/Dialog.tsx +66 -0
  106. package/components/Dialog/DialogActions.tsx +89 -0
  107. package/components/Dialog/DialogContent.tsx +37 -0
  108. package/components/Dialog/DialogIcon.tsx +69 -0
  109. package/components/Dialog/DialogTitle.tsx +68 -0
  110. package/components/Dialog/index.tsx +37 -0
  111. package/components/Dialog/utils.ts +80 -0
  112. package/components/Drawer/Collapsible/DrawerCollapsible.tsx +22 -0
  113. package/components/Drawer/Collapsible/DrawerCollapsibleItem.tsx +38 -0
  114. package/components/Drawer/Collapsible/DrawerCollapsibleItemContent.tsx +16 -0
  115. package/components/Drawer/Collapsible/DrawerCollapsibleItemHeader.tsx +48 -0
  116. package/components/Drawer/Collapsible/index.tsx +23 -0
  117. package/components/Drawer/Collapsible/utils.ts +49 -0
  118. package/components/Drawer/Drawer.tsx +43 -0
  119. package/components/Drawer/DrawerContent.tsx +35 -0
  120. package/components/Drawer/DrawerFooter.tsx +27 -0
  121. package/components/Drawer/DrawerHeader.tsx +27 -0
  122. package/components/Drawer/DrawerItem.tsx +206 -0
  123. package/components/Drawer/DrawerItemGroup.tsx +82 -0
  124. package/components/Drawer/index.tsx +47 -0
  125. package/components/Drawer/types.ts +3 -0
  126. package/components/Drawer/utils.ts +8 -0
  127. package/components/ElementGroup/ElementGroup.tsx +139 -0
  128. package/components/ElementGroup/index.tsx +11 -0
  129. package/components/ElementGroup/utils.ts +25 -0
  130. package/components/FAB/FAB.tsx +176 -0
  131. package/components/FAB/index.tsx +12 -0
  132. package/components/FAB/types.ts +1 -0
  133. package/components/FAB/utils.ts +221 -0
  134. package/components/FilePicker/FilePicker.tsx +133 -0
  135. package/components/FilePicker/index.tsx +11 -0
  136. package/components/FilePicker/utils.ts +13 -0
  137. package/components/HelperText/HelperText.tsx +139 -0
  138. package/components/HelperText/index.tsx +11 -0
  139. package/components/HelperText/utils.ts +29 -0
  140. package/components/HorizontalDivider/HorizontalDivider.tsx +101 -0
  141. package/components/HorizontalDivider/index.tsx +13 -0
  142. package/components/Icon/CrossFadeIcon.tsx +116 -0
  143. package/components/Icon/Icon.tsx +41 -0
  144. package/components/Icon/iconFactory.tsx +23 -0
  145. package/components/Icon/index.tsx +11 -0
  146. package/components/Icon/types.ts +35 -0
  147. package/components/IconButton/IconButton.tsx +218 -0
  148. package/components/IconButton/index.tsx +11 -0
  149. package/components/IconButton/types.ts +1 -0
  150. package/components/IconButton/utils.ts +325 -0
  151. package/components/If/index.tsx +13 -0
  152. package/components/InputAddon/InputAddon.tsx +27 -0
  153. package/components/InputAddon/index.tsx +11 -0
  154. package/components/InputAddon/utils.ts +33 -0
  155. package/components/Link/Link.tsx +48 -0
  156. package/components/Link/index.tsx +11 -0
  157. package/components/Link/utils.ts +37 -0
  158. package/components/ListItem/ListItem.tsx +136 -0
  159. package/components/ListItem/ListItemDescription.tsx +25 -0
  160. package/components/ListItem/ListItemTitle.tsx +25 -0
  161. package/components/ListItem/index.tsx +18 -0
  162. package/components/ListItem/utils.ts +113 -0
  163. package/components/Menu/Menu.tsx +69 -0
  164. package/components/Menu/MenuDivider.tsx +13 -0
  165. package/components/Menu/MenuItem.tsx +128 -0
  166. package/components/Menu/index.tsx +19 -0
  167. package/components/Menu/utils.ts +92 -0
  168. package/components/Modal/Modal.tsx +261 -0
  169. package/components/Modal/index.tsx +11 -0
  170. package/components/Modal/utils.ts +45 -0
  171. package/components/NavigationRail/NavigationRail.tsx +32 -0
  172. package/components/NavigationRail/NavigationRailContent.tsx +25 -0
  173. package/components/NavigationRail/NavigationRailFooter.tsx +18 -0
  174. package/components/NavigationRail/NavigationRailHeader.tsx +18 -0
  175. package/components/NavigationRail/NavigationRailItem.tsx +226 -0
  176. package/components/NavigationRail/index.tsx +35 -0
  177. package/components/NavigationRail/utils.ts +170 -0
  178. package/components/NavigationStack/NavigationStack.tsx +85 -0
  179. package/components/NavigationStack/NavigationStackItem.tsx +60 -0
  180. package/components/NavigationStack/index.tsx +20 -0
  181. package/components/NavigationStack/utils.tsx +16 -0
  182. package/components/Popover/Popover.native.tsx +185 -0
  183. package/components/Popover/Popover.tsx +198 -0
  184. package/components/Popover/common.ts +459 -0
  185. package/components/Popover/index.ts +2 -0
  186. package/components/Portal/Portal.tsx +13 -0
  187. package/components/Portal/index.tsx +12 -0
  188. package/components/RadioButton/RadioButton.tsx +138 -0
  189. package/components/RadioButton/RadioButtonAndroid.tsx +188 -0
  190. package/components/RadioButton/RadioButtonGroup.tsx +98 -0
  191. package/components/RadioButton/RadioButtonIOS.tsx +106 -0
  192. package/components/RadioButton/RadioButtonItem.tsx +232 -0
  193. package/components/RadioButton/index.ts +27 -0
  194. package/components/RadioButton/utils.ts +164 -0
  195. package/components/Rating/Rating.tsx +149 -0
  196. package/components/Rating/RatingItem.tsx +125 -0
  197. package/components/Rating/index.tsx +13 -0
  198. package/components/Rating/utils.ts +38 -0
  199. package/components/Select/Select.tsx +1038 -0
  200. package/components/Select/index.ts +14 -0
  201. package/components/Select/types.ts +115 -0
  202. package/components/StateLayer/StateLayer.tsx +12 -0
  203. package/components/StateLayer/index.tsx +11 -0
  204. package/components/StateLayer/utils.ts +17 -0
  205. package/components/Surface/BackgroundContextWrapper.tsx +27 -0
  206. package/components/Surface/Surface.android.tsx +62 -0
  207. package/components/Surface/Surface.ios.tsx +123 -0
  208. package/components/Surface/Surface.tsx +48 -0
  209. package/components/Surface/index.tsx +12 -0
  210. package/components/Surface/utils.ts +106 -0
  211. package/components/Switch/Switch.ios.tsx +67 -0
  212. package/components/Switch/Switch.tsx +278 -0
  213. package/components/Switch/index.tsx +11 -0
  214. package/components/Switch/utils.ts +283 -0
  215. package/components/Tabs/TabItem.tsx +150 -0
  216. package/components/Tabs/TabLabel.tsx +84 -0
  217. package/components/Tabs/Tabs.tsx +398 -0
  218. package/components/Tabs/index.tsx +21 -0
  219. package/components/Tabs/utils.ts +126 -0
  220. package/components/Text/Text.tsx +23 -0
  221. package/components/Text/index.tsx +2 -0
  222. package/components/Text/textFactory.tsx +33 -0
  223. package/components/TextInput/InputLabel.tsx +181 -0
  224. package/components/TextInput/TextInput.tsx +693 -0
  225. package/components/TextInput/index.tsx +16 -0
  226. package/components/TextInput/types.ts +96 -0
  227. package/components/TextInput/utils.ts +544 -0
  228. package/components/TextInputWithMask/TextInputMask.tsx +57 -0
  229. package/components/TextInputWithMask/index.tsx +11 -0
  230. package/components/TextInputWithMask/utils.ts +56 -0
  231. package/components/TimePicker/AmPmSwitcher.tsx +99 -0
  232. package/components/TimePicker/AnalogClock.tsx +165 -0
  233. package/components/TimePicker/AnalogClockHours.tsx +163 -0
  234. package/components/TimePicker/AnalogClockMinutes.tsx +68 -0
  235. package/components/TimePicker/AnimatedClockSwitcher.tsx +72 -0
  236. package/components/TimePicker/DisplayModeContext.tsx +6 -0
  237. package/components/TimePicker/TimeInput.tsx +112 -0
  238. package/components/TimePicker/TimeInputs.tsx +148 -0
  239. package/components/TimePicker/TimePicker.tsx +130 -0
  240. package/components/TimePicker/index.tsx +19 -0
  241. package/components/TimePicker/timeUtils.ts +159 -0
  242. package/components/TimePicker/utils.ts +285 -0
  243. package/components/TimePickerField/TimePickerField.tsx +152 -0
  244. package/components/TimePickerField/index.tsx +14 -0
  245. package/components/TimePickerField/sanitizeTime.ts +85 -0
  246. package/components/TimePickerField/utils.ts +95 -0
  247. package/components/TimePickerModal/TimePickerModal.tsx +115 -0
  248. package/components/TimePickerModal/index.tsx +14 -0
  249. package/components/TimePickerModal/utils.ts +48 -0
  250. package/components/Tooltip/Tooltip.tsx +137 -0
  251. package/components/Tooltip/TooltipContent.tsx +12 -0
  252. package/components/Tooltip/TooltipTrigger.tsx +94 -0
  253. package/components/Tooltip/index.tsx +20 -0
  254. package/components/Tooltip/utils.ts +21 -0
  255. package/components/TouchableRipple/TouchableRipple.native.tsx +105 -0
  256. package/components/TouchableRipple/TouchableRipple.tsx +286 -0
  257. package/components/TouchableRipple/index.tsx +14 -0
  258. package/components/TouchableRipple/utils.ts +15 -0
  259. package/components/VerticalDivider/VerticalDivider.tsx +99 -0
  260. package/components/VerticalDivider/index.tsx +13 -0
  261. package/context-bridge/index.tsx +87 -0
  262. package/core/componentsRegistry.ts +164 -0
  263. package/core/index.tsx +2 -0
  264. package/fast-context/index.tsx +190 -0
  265. package/hocs/index.tsx +5 -0
  266. package/hocs/typedMemo.tsx +5 -0
  267. package/hocs/withKeyboardAccessibility.tsx +231 -0
  268. package/hocs/withPortal.tsx +16 -0
  269. package/hooks/createPsuedoHook.tsx +50 -0
  270. package/hooks/index.tsx +29 -0
  271. package/hooks/useActionState.native.tsx +22 -0
  272. package/hooks/useActionState.tsx +34 -0
  273. package/hooks/useActive.tsx +5 -0
  274. package/hooks/useBreakpoints.tsx +7 -0
  275. package/hooks/useColorMode.tsx +17 -0
  276. package/hooks/useContrastColor.ts +15 -0
  277. package/hooks/useControlledValue.tsx +68 -0
  278. package/hooks/useFilePicker.tsx +48 -0
  279. package/hooks/useFocus.tsx +5 -0
  280. package/hooks/useHandleNumberFormat.tsx +106 -0
  281. package/hooks/useHover.tsx +5 -0
  282. package/hooks/useKeyboardDismissable.ts +66 -0
  283. package/hooks/useLatest.tsx +9 -0
  284. package/hooks/useMediaQuery.tsx +64 -0
  285. package/hooks/useMergedRefs.ts +14 -0
  286. package/hooks/usePrevious.ts +13 -0
  287. package/hooks/useQueryFilter.tsx +35 -0
  288. package/hooks/useSearchable.tsx +74 -0
  289. package/hooks/useSubcomponents.tsx +59 -0
  290. package/hooks/useTheme.ts +3 -0
  291. package/hooks/useToggle.tsx +24 -0
  292. package/package.json +114 -0
  293. package/shortcuts-manager/EventsManager.tsx +121 -0
  294. package/shortcuts-manager/ShortcutsManager/ShortcutsManager.native.tsx +9 -0
  295. package/shortcuts-manager/ShortcutsManager/ShortcutsManager.tsx +58 -0
  296. package/shortcuts-manager/ShortcutsManager/index.tsx +2 -0
  297. package/shortcuts-manager/ShortcutsManager/utils.tsx +30 -0
  298. package/shortcuts-manager/index.tsx +6 -0
  299. package/shortcuts-manager/types.ts +24 -0
  300. package/shortcuts-manager/useIsKeyPressed/index.tsx +1 -0
  301. package/shortcuts-manager/useIsKeyPressed/useIsKeyPress.tsx +9 -0
  302. package/shortcuts-manager/useSetScopes/index.tsx +1 -0
  303. package/shortcuts-manager/useSetScopes/useSetScopes.native.tsx +9 -0
  304. package/shortcuts-manager/useSetScopes/useSetScopes.tsx +25 -0
  305. package/shortcuts-manager/useShortcut/index.tsx +2 -0
  306. package/shortcuts-manager/useShortcut/types.ts +3 -0
  307. package/shortcuts-manager/useShortcut/useShortcut.native.tsx +9 -0
  308. package/shortcuts-manager/useShortcut/useShortcut.tsx +61 -0
  309. package/shortcuts-manager/utils.ts +105 -0
  310. package/styles/index.ts +4 -0
  311. package/styles/overlay.ts +69 -0
  312. package/styles/shadow.ts +21 -0
  313. package/styles/themes/DarkTheme.tsx +98 -0
  314. package/styles/themes/LightTheme.tsx +212 -0
  315. package/styles/themes/tokens.ts +248 -0
  316. package/styles/utils.ts +11 -0
  317. package/types/index.ts +129 -0
  318. package/types/theme.ts +159 -0
  319. package/utils/DocumentPicker/documentPicker.native.ts +10 -0
  320. package/utils/DocumentPicker/documentPicker.ts +76 -0
  321. package/utils/DocumentPicker/index.ts +2 -0
  322. package/utils/DocumentPicker/types.ts +28 -0
  323. package/utils/addEventListener.tsx +51 -0
  324. package/utils/backgroundContext.ts +9 -0
  325. package/utils/color.ts +22 -0
  326. package/utils/compare/index.ts +54 -0
  327. package/utils/composeEventHandlers.ts +9 -0
  328. package/utils/createNumberMask/createNumberMask.ts +98 -0
  329. package/utils/createNumberMask/index.ts +5 -0
  330. package/utils/createSyntheticEvent.ts +31 -0
  331. package/utils/date-fns.ts +7 -0
  332. package/utils/dateTimePicker.ts +5 -0
  333. package/utils/escapeRegex.ts +9 -0
  334. package/utils/extractTextStyles.ts +52 -0
  335. package/utils/formatNumberWithMask/formatNumberWithMask.ts +26 -0
  336. package/utils/formatNumberWithMask/formatWithMask.ts +119 -0
  337. package/utils/formatNumberWithMask/index.ts +6 -0
  338. package/utils/getCursorStyle/getCursorStyle.native.ts +1 -0
  339. package/utils/getCursorStyle/getCursorStyle.ts +1 -0
  340. package/utils/getCursorStyle/index.ts +1 -0
  341. package/utils/getOS/getOS.native.ts +7 -0
  342. package/utils/getOS/getOS.ts +24 -0
  343. package/utils/getOS/index.ts +1 -0
  344. package/utils/getyearRange.ts +5 -0
  345. package/utils/index.ts +20 -0
  346. package/utils/lodash.ts +50 -0
  347. package/utils/mergeRefs.ts +13 -0
  348. package/utils/normalizeBorderRadiuses.ts +24 -0
  349. package/utils/normalizeSpacings.ts +110 -0
  350. package/utils/normalizeToNumberString/index.ts +4 -0
  351. package/utils/normalizeToNumberString/normalizeToNumberString.ts +48 -0
  352. package/utils/repository.ts +103 -0
  353. package/utils/resolveColorMode.ts +9 -0
  354. package/utils/resolveStateVariant.ts +26 -0
  355. package/utils/tokenStylesParser.ts +7 -0
@@ -0,0 +1,150 @@
1
+ import {
2
+ Children,
3
+ cloneElement,
4
+ forwardRef,
5
+ isValidElement,
6
+ type JSXElementConstructor,
7
+ memo,
8
+ type ReactElement,
9
+ useCallback,
10
+ useMemo,
11
+ useRef,
12
+ useState,
13
+ } from 'react';
14
+ import { type LayoutChangeEvent, View, type ViewProps, type ViewStyle } from 'react-native';
15
+
16
+ import { useActionState } from '../../hooks';
17
+ import { resolveStateVariant } from '../../utils';
18
+ import { StateLayer } from '../StateLayer';
19
+ import { TouchableRipple, type TouchableRippleProps } from '../TouchableRipple';
20
+ import { tabsItemStyles } from './utils';
21
+
22
+ export type TabItemProps = Omit<TouchableRippleProps, 'children' | 'ref'> & {
23
+ /**
24
+ * name of the tab. This should be unique like a route name
25
+ * */
26
+ name: string;
27
+ /**
28
+ * Allows to define if TabItem is active.
29
+ * */
30
+ active?: boolean;
31
+ /**
32
+ * variant according to m3 guidelines
33
+ * */
34
+ variant?: 'primary' | 'secondary';
35
+
36
+ contentsContainerStyle?: ViewStyle;
37
+ contentsContainerProps?: Omit<ViewProps, 'children' | 'style' | 'onLayout'>;
38
+ onLayoutContent?: (e: LayoutChangeEvent) => void;
39
+ accessibilityLabel?: string;
40
+ children: ReactElement<
41
+ {
42
+ active: boolean;
43
+ hovered: boolean;
44
+ variant: 'primary' | 'secondary';
45
+ },
46
+ JSXElementConstructor<{
47
+ active: boolean;
48
+ hovered: boolean;
49
+ variant: 'primary' | 'secondary';
50
+ }>
51
+ >;
52
+ stateLayerProps?: ViewProps;
53
+ };
54
+
55
+ const TabItem = (
56
+ {
57
+ active = false,
58
+ variant = 'primary',
59
+ style,
60
+ onLayout,
61
+ onLayoutContent,
62
+ contentsContainerStyle: contentsContainerStyleProp,
63
+ contentsContainerProps,
64
+ accessibilityLabel,
65
+ children,
66
+ stateLayerProps,
67
+ ...rest
68
+ }: TabItemProps,
69
+ ref: any,
70
+ ) => {
71
+ const { hovered, actionsRef } = useActionState({ ref, actionsToListen: ['hover'] });
72
+ const [itemHeight, setItemHeight] = useState(0);
73
+
74
+ const state = resolveStateVariant({
75
+ activeAndHovered: active && hovered,
76
+ hovered,
77
+ active,
78
+ });
79
+ tabsItemStyles.useVariants({
80
+ variant,
81
+ state,
82
+ });
83
+
84
+ const { containerStyle } = useMemo(() => {
85
+ const minHeight = 48;
86
+ return {
87
+ containerStyle: [
88
+ tabsItemStyles.root,
89
+ {
90
+ minHeight: Math.max(minHeight, itemHeight),
91
+ },
92
+ style,
93
+ ],
94
+ };
95
+ }, [itemHeight, style]);
96
+
97
+ const useLayoutContentRef = useRef(onLayoutContent);
98
+ const onLayoutHandled = useCallback((e: LayoutChangeEvent) => {
99
+ useLayoutContentRef.current?.(e);
100
+ setItemHeight(e.nativeEvent.layout.height);
101
+ }, []);
102
+
103
+ const { accessibilityState, accessibilityValue } = useMemo(
104
+ () => ({
105
+ accessibilityState: { selected: active },
106
+ accessibilityValue:
107
+ typeof accessibilityLabel === 'string' ? { text: accessibilityLabel } : undefined,
108
+ }),
109
+ // eslint-disable-next-line react-hooks/exhaustive-deps
110
+ [active, accessibilityLabel, state],
111
+ );
112
+
113
+ return (
114
+ <TouchableRipple
115
+ style={containerStyle}
116
+ accessibilityRole="tab"
117
+ accessibilityState={accessibilityState}
118
+ accessibilityValue={accessibilityValue}
119
+ {...rest}
120
+ ref={actionsRef}
121
+ onLayout={onLayout}>
122
+ <>
123
+ <View
124
+ style={[tabsItemStyles.contentsContainer, contentsContainerStyleProp]}
125
+ {...contentsContainerProps}
126
+ onLayout={onLayoutHandled}>
127
+ {Children.map(children, child => {
128
+ if (!isValidElement(child)) return null;
129
+ return cloneElement(child, {
130
+ active,
131
+ hovered,
132
+ variant,
133
+ });
134
+ })}
135
+ </View>
136
+
137
+ <StateLayer
138
+ {...stateLayerProps}
139
+ style={[tabsItemStyles.stateLayer, stateLayerProps?.style]}
140
+ />
141
+ </>
142
+ </TouchableRipple>
143
+ );
144
+ };
145
+
146
+ const _TabItem = memo(forwardRef(TabItem));
147
+
148
+ _TabItem.displayName = 'Tabs_Item';
149
+
150
+ export default _TabItem;
@@ -0,0 +1,84 @@
1
+ import { type FC, memo, useMemo } from 'react';
2
+ import { type TextProps, type TextStyle } from 'react-native';
3
+
4
+ import { Icon, type IconProps, type IconType } from '../Icon';
5
+ import { Text } from '../Text';
6
+ import { tabsLabelStyles } from './utils';
7
+
8
+ const DEFAULT_ICON_SIZE = 24;
9
+
10
+ export type TabLabelProps = {
11
+ /**
12
+ * label of the tab
13
+ * */
14
+ label?: string;
15
+ labelStyle?: TextStyle;
16
+ labelProps?: Omit<TextProps, 'children' | 'style'>;
17
+ /**
18
+ * icon properties
19
+ * */
20
+ iconName?: string;
21
+ iconType?: IconType;
22
+ iconProps?: Omit<IconProps, 'name' | 'type'>;
23
+ iconStyle?: TextStyle;
24
+
25
+ activeColor?: string;
26
+
27
+ active: boolean;
28
+ hovered: boolean;
29
+
30
+ variant: 'primary' | 'secondary';
31
+ };
32
+
33
+ const TabLabel = memo((props: TabLabelProps) => {
34
+ const {
35
+ activeColor: activeColorProp,
36
+ iconName,
37
+ iconStyle: iconStyleProp,
38
+ iconType,
39
+ iconProps,
40
+ labelStyle: labelStyleProp,
41
+ labelProps,
42
+ label,
43
+ active,
44
+ } = props;
45
+
46
+ // tabsLabelStyles.useVariants({
47
+ // variant,
48
+ // state: resolveStateVariant({
49
+ // activeAndHovered: !!active && !!hovered,
50
+ // hovered,
51
+ // active,
52
+ // }),
53
+ // })
54
+
55
+ const { labelStyle, iconStyle } = useMemo(() => {
56
+ const color = active && !!activeColorProp ? { color: activeColorProp } : null;
57
+
58
+ return {
59
+ labelStyle: [tabsLabelStyles.label, labelStyleProp, color],
60
+ iconStyle: [tabsLabelStyles.icon, iconStyleProp, color],
61
+ };
62
+ }, [active, activeColorProp, iconStyleProp, labelStyleProp]);
63
+
64
+ return (
65
+ <>
66
+ {iconName && (
67
+ <Icon
68
+ style={iconStyle}
69
+ name={iconName}
70
+ type={iconType}
71
+ size={DEFAULT_ICON_SIZE}
72
+ {...iconProps}
73
+ />
74
+ )}
75
+ <Text style={labelStyle} numberOfLines={1} {...labelProps}>
76
+ {label}
77
+ </Text>
78
+ </>
79
+ );
80
+ });
81
+
82
+ TabLabel.displayName = 'TabLabel';
83
+
84
+ export default TabLabel as unknown as FC<Omit<TabLabelProps, 'active' | 'hovered' | 'variant'>>;
@@ -0,0 +1,398 @@
1
+ import {
2
+ Children,
3
+ cloneElement,
4
+ FC,
5
+ isValidElement,
6
+ memo,
7
+ ReactElement,
8
+ useCallback,
9
+ useEffect,
10
+ useMemo,
11
+ useRef,
12
+ useState,
13
+ } from 'react';
14
+ import {
15
+ Animated,
16
+ StyleProp,
17
+ ViewStyle,
18
+ ViewProps,
19
+ ScrollView as RNScrollView,
20
+ LayoutChangeEvent,
21
+ NativeSyntheticEvent,
22
+ NativeScrollEvent,
23
+ View,
24
+ ScrollView,
25
+ } from 'react-native';
26
+ import type { TabItemProps } from './TabItem';
27
+ import { useControlledValue } from '../../hooks';
28
+ import { noop } from '../../utils/lodash';
29
+ import { HorizontalDivider, type HorizontalDividerProps } from '../HorizontalDivider';
30
+ import { tabsStyles } from './utils';
31
+
32
+ export type TabsProps = ViewProps & {
33
+ /**
34
+ * child tab name
35
+ * */
36
+ value?: string;
37
+ /**
38
+ * defaultValue to preselected for uncontrolled mode
39
+ * */
40
+ defaultValue?: string;
41
+ /**
42
+ * to enable scroll
43
+ * */
44
+ scrollable?: boolean;
45
+ /**
46
+ * on name change callback.
47
+ * */
48
+ onChange?: (value: string) => void;
49
+ /**
50
+ * Disable the active indicator below.
51
+ * */
52
+ disableIndicator?: boolean;
53
+ /**
54
+ * Additional styling for tab indicator.
55
+ * */
56
+ indicatorStyle?: StyleProp<ViewStyle>;
57
+
58
+ indicatorProps?: Omit<ViewStyle, 'style'>;
59
+
60
+ dividerStyle?: ViewStyle;
61
+
62
+ dividerProps?: Omit<HorizontalDividerProps, 'style'>;
63
+
64
+ /** Define the background Variant. */
65
+ variant?: 'primary' | 'secondary';
66
+ activeColor?: string;
67
+ };
68
+
69
+ const emptyObj = {};
70
+
71
+ export const TabBase = ({
72
+ children,
73
+ value: valueProp,
74
+ defaultValue,
75
+ scrollable = false,
76
+ onChange: onChangeProp = noop,
77
+ indicatorStyle: indicatorStyleProp = emptyObj,
78
+ disableIndicator,
79
+ style,
80
+ variant = 'primary',
81
+ indicatorProps,
82
+ dividerStyle: dividerStyleProp = emptyObj,
83
+ dividerProps,
84
+ activeColor: activeColorProp,
85
+ testID,
86
+ ...rest
87
+ }: TabsProps) => {
88
+ tabsStyles.useVariants({
89
+ variant,
90
+ });
91
+
92
+ const validChildren = useMemo(
93
+ () =>
94
+ Children.toArray(children).filter(
95
+ child => isValidElement(child) && (child?.type as FC).displayName === 'Tabs_Item',
96
+ ),
97
+ [children],
98
+ );
99
+
100
+ const nameToIndexMap = useMemo(
101
+ () =>
102
+ validChildren.reduce((acc, child, currentIndex) => {
103
+ acc[(child as ReactElement).props?.name] = currentIndex;
104
+
105
+ return acc;
106
+ }, {} as Record<string, number>),
107
+ [validChildren],
108
+ );
109
+
110
+ const [value, onChange] = useControlledValue({
111
+ value: valueProp,
112
+ onChange: onChangeProp,
113
+ defaultValue: defaultValue || (validChildren[0] as ReactElement)?.props?.name,
114
+ });
115
+
116
+ const valueIndex = nameToIndexMap[value];
117
+
118
+ const positionAnimationRef = useRef(new Animated.Value(0));
119
+ const widthAnimationRef = useRef(new Animated.Value(0));
120
+ const scrollViewRef = useRef<RNScrollView>(null);
121
+ const scrollViewPosition = useRef(0);
122
+
123
+ const tabItemPositions = useRef<Array<{ width: number; contentWidth: number }>>([]);
124
+ const [tabContainerWidth, setTabContainerWidth] = useState(0);
125
+
126
+ const itemPositionsMap = useMemo(() => {
127
+ return tabItemPositions.current.reduce((acc, item, index) => {
128
+ const previousItemsWidth = tabItemPositions.current
129
+ .slice(0, index)
130
+ .reduce((totalWidth, _item) => {
131
+ totalWidth += _item.width || 0;
132
+
133
+ return totalWidth;
134
+ }, 0);
135
+
136
+ acc[index] =
137
+ variant === 'primary'
138
+ ? previousItemsWidth + (item.width - item.contentWidth) / 2
139
+ : previousItemsWidth;
140
+
141
+ return acc;
142
+ }, {} as Record<number, number>);
143
+ // to make useMemo in sync with the ref
144
+ // eslint-disable-next-line react-hooks/exhaustive-deps
145
+ }, [variant, tabItemPositions.current.length]);
146
+
147
+ const itemWidthsMap = useMemo(() => {
148
+ return tabItemPositions.current.reduce((acc, item, index) => {
149
+ acc[index] = variant === 'primary' ? item.contentWidth : item.width;
150
+
151
+ return acc;
152
+ }, {} as Record<number, number>);
153
+ // to make useMemo in sync with the ref
154
+ // eslint-disable-next-line react-hooks/exhaustive-deps
155
+ }, [variant, tabItemPositions.current.length]);
156
+
157
+ const scrollHandler = useCallback(
158
+ (currValue: number) => {
159
+ if (tabItemPositions.current.length > currValue) {
160
+ const itemStartPosition = currValue === 0 ? 0 : itemPositionsMap[currValue - 1];
161
+ const itemEndPosition = itemPositionsMap[currValue];
162
+
163
+ const scrollCurrentPosition = scrollViewPosition.current;
164
+ const tabContainerCurrentWidth = tabContainerWidth;
165
+
166
+ let scrollX = scrollCurrentPosition;
167
+
168
+ if (itemStartPosition < scrollCurrentPosition) {
169
+ scrollX += itemStartPosition - scrollCurrentPosition;
170
+ } else if (scrollCurrentPosition + tabContainerCurrentWidth < itemEndPosition) {
171
+ scrollX += itemEndPosition - (scrollCurrentPosition + tabContainerCurrentWidth);
172
+ }
173
+
174
+ scrollViewRef.current!.scrollTo({
175
+ x: scrollX,
176
+ y: 0,
177
+ animated: true,
178
+ });
179
+ }
180
+ },
181
+ [itemPositionsMap, tabContainerWidth],
182
+ );
183
+
184
+ useEffect(() => {
185
+ Animated.timing(positionAnimationRef.current, {
186
+ toValue: valueIndex,
187
+ useNativeDriver: false,
188
+ duration: 170,
189
+ }).start();
190
+
191
+ scrollable && requestAnimationFrame(() => scrollHandler(valueIndex));
192
+ }, [positionAnimationRef, scrollHandler, valueIndex, scrollable]);
193
+
194
+ useEffect(() => {
195
+ Animated.timing(widthAnimationRef.current, {
196
+ toValue: valueIndex,
197
+ useNativeDriver: false,
198
+ duration: 170,
199
+ }).start();
200
+ }, [positionAnimationRef, scrollHandler, valueIndex]);
201
+
202
+ const onScrollHandler = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
203
+ scrollViewPosition.current = event.nativeEvent.contentOffset.x;
204
+ }, []);
205
+
206
+ const transitionInterpolateWithMap = useCallback(
207
+ (obj: Record<any, number>) => {
208
+ const countItems = validChildren.length;
209
+ if (countItems < 2 || !tabItemPositions.current.length) {
210
+ // if there's only one item, use the value of that
211
+ return Object.values(obj)[0] || 0;
212
+ }
213
+ const inputRange = Array.from(Array(countItems).keys());
214
+ const outputRange = Object.values(obj);
215
+
216
+ return positionAnimationRef.current.interpolate({
217
+ inputRange,
218
+ outputRange,
219
+ });
220
+ },
221
+ [validChildren.length],
222
+ );
223
+
224
+ const indicatorTransitionInterpolate = useMemo(() => {
225
+ return transitionInterpolateWithMap(itemPositionsMap);
226
+ }, [transitionInterpolateWithMap, itemPositionsMap]);
227
+
228
+ const widthTransitionInterpolate = useMemo(() => {
229
+ return transitionInterpolateWithMap(itemWidthsMap);
230
+ }, [transitionInterpolateWithMap, itemWidthsMap]);
231
+
232
+ const { containerStyle, itemsContainerStyle, dividerStyle, indicatorStyle } = useMemo(() => {
233
+ const { indicator, itemsContainer, divider } = tabsStyles;
234
+ const { activeColor, ...restStyle } = tabsStyles.root;
235
+
236
+ return {
237
+ containerStyle: [restStyle, style],
238
+ itemsContainerStyle: itemsContainer,
239
+ dividerStyle: [divider, dividerStyleProp],
240
+ indicatorStyle: [
241
+ indicator,
242
+ {
243
+ backgroundColor: activeColorProp || activeColor,
244
+ transform: [
245
+ {
246
+ translateX: indicatorTransitionInterpolate,
247
+ },
248
+ ],
249
+ width: widthTransitionInterpolate,
250
+ },
251
+ indicatorStyleProp,
252
+ ],
253
+ };
254
+ }, [
255
+ style,
256
+ dividerStyleProp,
257
+ activeColorProp,
258
+ indicatorTransitionInterpolate,
259
+ widthTransitionInterpolate,
260
+ indicatorStyleProp,
261
+ ]);
262
+
263
+ const Container = scrollable ? ScrollView : View;
264
+ const containerProps = scrollable
265
+ ? {
266
+ horizontal: true,
267
+ ref: scrollViewRef,
268
+ onScroll: onScrollHandler,
269
+ showsHorizontalScrollIndicator: false,
270
+ style: itemsContainerStyle,
271
+ }
272
+ : {};
273
+
274
+ const onLayout = useCallback(({ nativeEvent: { layout } }: LayoutChangeEvent) => {
275
+ setTabContainerWidth(layout.width);
276
+ }, []);
277
+
278
+ const onLayoutItem = useCallback((event: LayoutChangeEvent, index: number) => {
279
+ const { width } = event.nativeEvent.layout;
280
+
281
+ const currentItemPosition = tabItemPositions.current[index];
282
+
283
+ tabItemPositions.current[index] = {
284
+ ...currentItemPosition,
285
+ width: width,
286
+ };
287
+ }, []);
288
+
289
+ const onLayoutText = useCallback((event: LayoutChangeEvent, index: number) => {
290
+ const { width } = event.nativeEvent.layout;
291
+
292
+ const currentItemPosition = tabItemPositions.current[index];
293
+
294
+ tabItemPositions.current[index] = {
295
+ ...currentItemPosition,
296
+ contentWidth: width,
297
+ };
298
+ }, []);
299
+
300
+ return (
301
+ <View
302
+ {...rest}
303
+ testID={testID}
304
+ style={containerStyle}
305
+ accessibilityRole="tablist"
306
+ onLayout={onLayout}>
307
+ <>
308
+ <Container
309
+ testID={testID && `${testID}--inner-container`}
310
+ {...containerProps}
311
+ style={itemsContainerStyle}>
312
+ {validChildren.map((child, index) => (
313
+ <ChildItem
314
+ key={(child as ReactElement).props?.name}
315
+ testID={testID && `${testID}--tab-item`}
316
+ index={index}
317
+ value={value}
318
+ child={child as ReactElement<TabItemProps>}
319
+ onChange={onChange}
320
+ onLayout={onLayoutItem}
321
+ onLayoutContent={onLayoutText}
322
+ variant={variant}
323
+ />
324
+ ))}
325
+
326
+ {!disableIndicator && (
327
+ <Animated.View
328
+ testID={testID && `${testID}--active-indicator`}
329
+ {...indicatorProps}
330
+ style={indicatorStyle}
331
+ />
332
+ )}
333
+ </Container>
334
+
335
+ <HorizontalDivider
336
+ testID={testID && `${testID}--divider`}
337
+ {...dividerProps}
338
+ style={dividerStyle}
339
+ />
340
+ </>
341
+ </View>
342
+ );
343
+ };
344
+
345
+ type ChildItemProps = {
346
+ variant: TabsProps['variant'];
347
+ child: ReactElement<TabItemProps>;
348
+ onChange: TabsProps['onChange'];
349
+ onLayout: (event: LayoutChangeEvent, index: number) => void;
350
+ onLayoutContent: (event: LayoutChangeEvent, index: number) => void;
351
+ value: string;
352
+ index: number;
353
+ testID?: string;
354
+ };
355
+
356
+ const ChildItem = memo(
357
+ ({
358
+ value,
359
+ child,
360
+ onChange,
361
+ onLayout: onLayoutProp,
362
+ onLayoutContent: onLayoutContentProp,
363
+ variant,
364
+ index,
365
+ testID,
366
+ }: ChildItemProps) => {
367
+ const name = child.props?.name;
368
+
369
+ const onPress = useCallback(() => {
370
+ onChange?.(name);
371
+ }, [name, onChange]);
372
+
373
+ const onLayout = useCallback(
374
+ (e: LayoutChangeEvent) => {
375
+ onLayoutProp(e, index);
376
+ },
377
+ [index, onLayoutProp],
378
+ );
379
+
380
+ const onLayoutContent = useCallback(
381
+ (e: LayoutChangeEvent) => {
382
+ onLayoutContentProp(e, index);
383
+ },
384
+ [index, onLayoutContentProp],
385
+ );
386
+
387
+ return cloneElement(child, {
388
+ onPress,
389
+ onLayout,
390
+ active: name === value,
391
+ onLayoutContent,
392
+ variant,
393
+ testID: child.props.testID ?? testID,
394
+ });
395
+ },
396
+ );
397
+
398
+ TabBase.displayName = 'Tabs';
@@ -0,0 +1,21 @@
1
+ import { getRegisteredComponentWithFallback, registerMoleculesComponents } from '../../core';
2
+ import type { TabItemProps } from './TabItem';
3
+ import { default as TabItem } from './TabItem';
4
+ import type { TabLabelProps } from './TabLabel';
5
+ import { default as TabLabel } from './TabLabel';
6
+ import type { TabsProps } from './Tabs';
7
+ import { TabBase } from './Tabs';
8
+
9
+ export const TabsDefault = Object.assign(TabBase, {
10
+ Item: TabItem,
11
+ Label: TabLabel,
12
+ });
13
+
14
+ registerMoleculesComponents({
15
+ Tabs: TabsDefault,
16
+ });
17
+
18
+ export const Tabs = getRegisteredComponentWithFallback('Tabs', TabsDefault);
19
+
20
+ export type { TabItemProps, TabLabelProps, TabsProps };
21
+ export { tabsItemStyles, tabsLabelStyles, tabsStyles } from './utils';