react-native-molecules 0.5.0-beta.3 → 0.5.0-beta.31

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 (227) hide show
  1. package/components/Accordion/Accordion.tsx +2 -6
  2. package/components/Accordion/AccordionItem.tsx +16 -12
  3. package/components/Accordion/AccordionItemContent.tsx +6 -1
  4. package/components/Accordion/AccordionItemHeader.tsx +1 -1
  5. package/components/Accordion/utils.ts +6 -0
  6. package/components/ActivityIndicator/ActivityIndicator.tsx +6 -15
  7. package/components/Appbar/AppbarBase.tsx +18 -13
  8. package/components/Button/Button.tsx +211 -264
  9. package/components/Button/index.tsx +9 -3
  10. package/components/Button/types.ts +16 -2
  11. package/components/Button/utils.ts +230 -208
  12. package/components/Card/Card.tsx +1 -1
  13. package/components/Checkbox/Checkbox.tsx +125 -88
  14. package/components/Checkbox/CheckboxBase.ios.tsx +14 -23
  15. package/components/Checkbox/CheckboxBase.tsx +21 -137
  16. package/components/Checkbox/context.tsx +14 -0
  17. package/components/Checkbox/index.tsx +11 -4
  18. package/components/Checkbox/types.ts +63 -29
  19. package/components/Checkbox/utils.ts +25 -108
  20. package/components/Chip/Chip.tsx +41 -52
  21. package/components/Chip/utils.ts +3 -7
  22. package/components/DateField/DateField.tsx +111 -0
  23. package/components/DateField/index.tsx +6 -0
  24. package/components/{DatePickerInput/inputUtils.ts → DateField/useDateFieldState.ts} +19 -51
  25. package/components/DatePicker/DateCalendar.tsx +83 -0
  26. package/components/DatePicker/DatePickerActions.tsx +73 -0
  27. package/components/DatePicker/DatePickerModal.tsx +246 -0
  28. package/components/DatePicker/DatePickerPopover.tsx +79 -0
  29. package/components/DatePicker/DatePickerProvider.tsx +158 -0
  30. package/components/DatePicker/DatePickerTrigger.tsx +23 -0
  31. package/components/DatePicker/context.tsx +83 -0
  32. package/components/DatePicker/index.tsx +45 -0
  33. package/components/DatePicker/utils.ts +295 -0
  34. package/components/DatePickerInline/DatePickerDockedHeader.tsx +117 -0
  35. package/components/DatePickerInline/DatePickerInline.tsx +17 -16
  36. package/components/DatePickerInline/DatePickerInlineBase.tsx +11 -5
  37. package/components/DatePickerInline/DatePickerInlineHeader.tsx +50 -20
  38. package/components/DatePickerInline/Day.tsx +25 -1
  39. package/components/DatePickerInline/DayNames.tsx +13 -10
  40. package/components/DatePickerInline/DayRange.tsx +2 -4
  41. package/components/DatePickerInline/HeaderItem.tsx +44 -29
  42. package/components/DatePickerInline/Month.tsx +48 -67
  43. package/components/DatePickerInline/MonthPicker.tsx +80 -92
  44. package/components/DatePickerInline/Swiper.native.tsx +21 -4
  45. package/components/DatePickerInline/Swiper.tsx +169 -14
  46. package/components/DatePickerInline/SwiperUtils.ts +1 -1
  47. package/components/DatePickerInline/Week.tsx +6 -1
  48. package/components/DatePickerInline/YearPicker.tsx +220 -78
  49. package/components/DatePickerInline/dateUtils.tsx +18 -13
  50. package/components/DatePickerInline/store.tsx +27 -0
  51. package/components/DatePickerInline/types.ts +6 -2
  52. package/components/DatePickerInline/utils.ts +66 -29
  53. package/components/Divider/Divider.tsx +192 -0
  54. package/components/Divider/index.tsx +10 -0
  55. package/components/Drawer/Drawer.tsx +17 -6
  56. package/components/Drawer/DrawerItemGroup.tsx +3 -7
  57. package/components/ElementGroup/ElementGroup.tsx +1 -1
  58. package/components/FilePicker/FilePicker.tsx +48 -78
  59. package/components/FilePicker/index.tsx +2 -1
  60. package/components/FilePicker/utils.ts +9 -0
  61. package/components/HelperText/HelperText.tsx +0 -35
  62. package/components/Icon/iconFactory.tsx +5 -4
  63. package/components/Icon/index.tsx +1 -1
  64. package/components/Icon/types.ts +17 -6
  65. package/components/IconButton/IconButton.tsx +84 -84
  66. package/components/IconButton/index.tsx +1 -0
  67. package/components/IconButton/types.ts +10 -0
  68. package/components/IconButton/utils.ts +167 -33
  69. package/components/List/List.tsx +276 -0
  70. package/components/List/context.tsx +27 -0
  71. package/components/List/index.ts +8 -0
  72. package/components/List/types.ts +117 -0
  73. package/components/List/utils.ts +79 -0
  74. package/components/LoadingIndicator/LoadingIndicator.tsx +253 -0
  75. package/components/LoadingIndicator/LoadingIndicator.web.tsx +136 -0
  76. package/components/LoadingIndicator/index.tsx +13 -0
  77. package/components/LoadingIndicator/utils.ts +117 -0
  78. package/components/Menu/Menu.tsx +162 -39
  79. package/components/Menu/index.tsx +10 -7
  80. package/components/Menu/utils.ts +21 -70
  81. package/components/NavigationRail/NavigationRail.tsx +15 -9
  82. package/components/Popover/Popover.tsx +119 -145
  83. package/components/Popover/PopoverRoot.tsx +60 -0
  84. package/components/Popover/common.ts +54 -34
  85. package/components/Popover/index.ts +12 -1
  86. package/components/Popover/usePlatformMeasure.native.ts +90 -0
  87. package/components/Popover/usePlatformMeasure.ts +120 -0
  88. package/components/Popover/utils.ts +34 -0
  89. package/components/Portal/Portal.tsx +1 -2
  90. package/components/Radio/Radio.tsx +188 -0
  91. package/components/Radio/RadioBase.ios.tsx +69 -0
  92. package/components/Radio/RadioBase.tsx +136 -0
  93. package/components/Radio/context.tsx +23 -0
  94. package/components/Radio/index.tsx +20 -0
  95. package/components/Radio/types.ts +101 -0
  96. package/components/Radio/utils.ts +115 -0
  97. package/components/Rating/Rating.tsx +1 -1
  98. package/components/Select/Select.tsx +521 -785
  99. package/components/Select/context.tsx +81 -0
  100. package/components/Select/index.ts +26 -14
  101. package/components/Select/types.ts +65 -58
  102. package/components/Select/utils.ts +126 -0
  103. package/components/Slot/Slot.tsx +224 -0
  104. package/components/Slot/compose-refs.tsx +62 -0
  105. package/components/Slot/index.tsx +8 -0
  106. package/components/Surface/Surface.android.tsx +32 -7
  107. package/components/Surface/Surface.ios.tsx +34 -29
  108. package/components/Surface/Surface.tsx +31 -4
  109. package/components/Surface/utils.ts +44 -6
  110. package/components/Switch/Switch.ios.tsx +1 -1
  111. package/components/Switch/Switch.tsx +10 -3
  112. package/components/Tabs/TabItem.tsx +35 -58
  113. package/components/Tabs/TabLabel.tsx +5 -9
  114. package/components/Tabs/Tabs.tsx +156 -150
  115. package/components/Tabs/utils.ts +15 -2
  116. package/components/Text/textFactory.tsx +17 -5
  117. package/components/TextInput/TextInput.tsx +663 -579
  118. package/components/TextInput/index.tsx +19 -3
  119. package/components/TextInput/types.ts +77 -28
  120. package/components/TextInput/utils.ts +235 -145
  121. package/components/TimeField/TimeField.tsx +75 -0
  122. package/components/TimeField/index.tsx +6 -0
  123. package/components/TimeField/useTimeFieldState.ts +70 -0
  124. package/components/{TimePickerField/sanitizeTime.ts → TimeField/utils.ts} +77 -10
  125. package/components/TimePicker/AnalogClock.tsx +1 -1
  126. package/components/TimePicker/TimeInput.tsx +87 -42
  127. package/components/TimePicker/TimeInputs.tsx +138 -50
  128. package/components/TimePicker/TimePicker.tsx +74 -11
  129. package/components/TimePicker/TimePickerModal.tsx +186 -0
  130. package/components/TimePicker/context.tsx +17 -0
  131. package/components/TimePicker/index.tsx +15 -3
  132. package/components/TimePicker/utils.ts +93 -4
  133. package/components/Tooltip/Tooltip.tsx +42 -67
  134. package/components/Tooltip/TooltipContent.tsx +32 -5
  135. package/components/Tooltip/TooltipTrigger.tsx +21 -24
  136. package/components/Tooltip/index.tsx +1 -1
  137. package/components/TouchableRipple/TouchableRipple.native.tsx +83 -16
  138. package/components/TouchableRipple/TouchableRipple.tsx +150 -102
  139. package/components/TouchableRipple/rippleFromForegroundColor.ts +21 -0
  140. package/hocs/index.tsx +1 -1
  141. package/hocs/withKeyboardAccessibility.tsx +2 -3
  142. package/hocs/withPortal.tsx +1 -1
  143. package/hooks/index.tsx +2 -12
  144. package/hooks/useActionState.tsx +19 -8
  145. package/hooks/useContrastColor.ts +1 -2
  146. package/hooks/useFilePicker.tsx +7 -17
  147. package/hooks/useHandleNumberFormat.tsx +2 -2
  148. package/hooks/useMediaQuery.tsx +1 -2
  149. package/package.json +95 -111
  150. package/shortcuts-manager/ShortcutsManager/ShortcutsManager.tsx +6 -3
  151. package/shortcuts-manager/ShortcutsManager/utils.tsx +1 -1
  152. package/shortcuts-manager/useSetScopes/useSetScopes.tsx +1 -1
  153. package/shortcuts-manager/useShortcut/useShortcut.tsx +1 -1
  154. package/styles/shadow.ts +2 -1
  155. package/styles/themes/LightTheme.tsx +1 -1
  156. package/utils/DocumentPicker/documentPicker.ts +78 -27
  157. package/utils/DocumentPicker/types.ts +0 -1
  158. package/utils/extractSubcomponents.ts +89 -0
  159. package/utils/extractTextStyles.ts +1 -2
  160. package/utils/formatNumberWithMask/formatNumberWithMask.ts +2 -1
  161. package/utils/index.ts +0 -3
  162. package/utils/normalizeToNumberString/normalizeToNumberString.ts +1 -1
  163. package/components/DatePickerDocked/DatePickerDocked.tsx +0 -30
  164. package/components/DatePickerDocked/DatePickerDockedHeader.tsx +0 -129
  165. package/components/DatePickerDocked/index.tsx +0 -17
  166. package/components/DatePickerDocked/types.ts +0 -11
  167. package/components/DatePickerDocked/utils.ts +0 -157
  168. package/components/DatePickerInline/DatePickerContext.tsx +0 -21
  169. package/components/DatePickerInput/DatePickerInput.tsx +0 -139
  170. package/components/DatePickerInput/DatePickerInputModal.tsx +0 -48
  171. package/components/DatePickerInput/DatePickerInputWithoutModal.tsx +0 -77
  172. package/components/DatePickerInput/DateRangeInput.tsx +0 -88
  173. package/components/DatePickerInput/index.tsx +0 -10
  174. package/components/DatePickerInput/types.ts +0 -28
  175. package/components/DatePickerInput/utils.ts +0 -15
  176. package/components/DatePickerModal/AnimatedCrossView.tsx +0 -94
  177. package/components/DatePickerModal/CalendarEdit.tsx +0 -139
  178. package/components/DatePickerModal/DatePickerModal.tsx +0 -85
  179. package/components/DatePickerModal/DatePickerModalContent.tsx +0 -155
  180. package/components/DatePickerModal/DatePickerModalContentHeader.tsx +0 -213
  181. package/components/DatePickerModal/DatePickerModalHeader.tsx +0 -74
  182. package/components/DatePickerModal/DatePickerModalHeaderBackground.tsx +0 -13
  183. package/components/DatePickerModal/index.tsx +0 -16
  184. package/components/DatePickerModal/types.ts +0 -92
  185. package/components/DatePickerModal/utils.ts +0 -122
  186. package/components/DateTimePicker/DateTimePicker.tsx +0 -172
  187. package/components/DateTimePicker/index.tsx +0 -10
  188. package/components/DateTimePicker/utils.ts +0 -12
  189. package/components/HorizontalDivider/HorizontalDivider.tsx +0 -103
  190. package/components/HorizontalDivider/index.tsx +0 -9
  191. package/components/ListItem/ListItem.tsx +0 -136
  192. package/components/ListItem/ListItemDescription.tsx +0 -25
  193. package/components/ListItem/ListItemTitle.tsx +0 -25
  194. package/components/ListItem/index.tsx +0 -14
  195. package/components/ListItem/utils.ts +0 -115
  196. package/components/Menu/MenuDivider.tsx +0 -13
  197. package/components/Menu/MenuItem.tsx +0 -128
  198. package/components/Popover/Popover.native.tsx +0 -185
  199. package/components/RadioButton/RadioButton.tsx +0 -138
  200. package/components/RadioButton/RadioButtonAndroid.tsx +0 -188
  201. package/components/RadioButton/RadioButtonGroup.tsx +0 -98
  202. package/components/RadioButton/RadioButtonIOS.tsx +0 -106
  203. package/components/RadioButton/RadioButtonItem.tsx +0 -232
  204. package/components/RadioButton/index.ts +0 -22
  205. package/components/RadioButton/utils.ts +0 -165
  206. package/components/TimePickerField/TimePickerField.tsx +0 -152
  207. package/components/TimePickerField/index.tsx +0 -10
  208. package/components/TimePickerField/utils.ts +0 -94
  209. package/components/TimePickerModal/TimePickerModal.tsx +0 -115
  210. package/components/TimePickerModal/index.tsx +0 -10
  211. package/components/TimePickerModal/utils.ts +0 -47
  212. package/components/VerticalDivider/VerticalDivider.tsx +0 -100
  213. package/components/VerticalDivider/index.tsx +0 -9
  214. package/context-bridge/index.tsx +0 -87
  215. package/fast-context/index.tsx +0 -190
  216. package/hocs/typedMemo.tsx +0 -5
  217. package/hooks/useControlledValue.tsx +0 -68
  218. package/hooks/useLatest.tsx +0 -9
  219. package/hooks/useMergedRefs.ts +0 -14
  220. package/hooks/usePrevious.ts +0 -13
  221. package/hooks/useSearchable.tsx +0 -74
  222. package/hooks/useSubcomponents.tsx +0 -59
  223. package/hooks/useToggle.tsx +0 -24
  224. package/utils/color.ts +0 -22
  225. package/utils/compare/index.ts +0 -54
  226. package/utils/lodash.ts +0 -49
  227. package/utils/repository.ts +0 -53
@@ -1,115 +0,0 @@
1
- import { memo, useCallback, useEffect, useMemo, useState } from 'react';
2
- import { KeyboardAvoidingView, Platform, View } from 'react-native';
3
-
4
- import { format } from '../../utils/date-fns';
5
- import { Button } from '../Button';
6
- import { IconButton } from '../IconButton';
7
- import { Modal, type ModalProps } from '../Modal';
8
- import { Portal } from '../Portal';
9
- import { Text } from '../Text';
10
- import TimePicker from '../TimePicker/TimePicker';
11
- import {
12
- clockTypes,
13
- getTimeInputTypeIcon,
14
- inputTypes,
15
- type PossibleClockTypes,
16
- type PossibleInputTypes,
17
- reverseInputTypes,
18
- } from '../TimePicker/timeUtils';
19
- import { styles } from './utils';
20
-
21
- export type Props = Omit<ModalProps, 'children'> & {
22
- is24Hour?: boolean;
23
- label?: string;
24
- uppercase?: boolean;
25
- cancelLabel?: string;
26
- confirmLabel?: string;
27
- time?: string;
28
- onConfirm: (time: string) => any;
29
- keyboardIcon?: string;
30
- clockIcon?: string;
31
- isLandscape?: boolean;
32
- };
33
-
34
- export function TimePickerModal({
35
- isOpen,
36
- onClose,
37
- onConfirm: onConfirmProp,
38
- time: timeProp,
39
- label = 'Select time',
40
- uppercase = false,
41
- cancelLabel = 'Cancel',
42
- confirmLabel = 'Ok',
43
- is24Hour,
44
- keyboardIcon = 'keyboard-outline',
45
- clockIcon = 'clock-outline',
46
- isLandscape = false,
47
- style,
48
- ...rest
49
- }: Props) {
50
- const [inputType, setInputType] = useState<PossibleInputTypes>(inputTypes.picker);
51
- const [focused, setFocused] = useState<PossibleClockTypes>(clockTypes.hours);
52
- const [time, setTime] = useState(() => timeProp || format(new Date(), 'HH:mm'));
53
-
54
- const modelStyle = useMemo(() => [styles.modalContent, style], [style]);
55
-
56
- const onFocusInput = useCallback((type: PossibleClockTypes) => setFocused(type), []);
57
-
58
- const onChange = useCallback(
59
- (params: { focused?: PossibleClockTypes | undefined; time: string }) => {
60
- if (params.focused) {
61
- setFocused(params.focused);
62
- }
63
-
64
- setTime(params.time);
65
- },
66
- [setFocused, setTime],
67
- );
68
-
69
- const onConfirm = useCallback(() => onConfirmProp(time), [onConfirmProp, time]);
70
-
71
- useEffect(() => {
72
- setTime(timeProp || format(new Date(), 'HH:mm'));
73
- }, [timeProp]);
74
-
75
- return (
76
- <Portal>
77
- <Modal {...rest} isOpen={isOpen} style={modelStyle} onClose={onClose}>
78
- <KeyboardAvoidingView
79
- style={styles.keyboardView}
80
- behavior={Platform.OS === 'ios' ? 'padding' : undefined}>
81
- <View style={styles.labelContainer}>
82
- <Text style={styles.label}>{uppercase ? label.toUpperCase() : label}</Text>
83
- </View>
84
- <View style={styles.timePickerContainer}>
85
- <TimePicker
86
- is24Hour={is24Hour}
87
- inputType={inputType}
88
- focused={focused}
89
- time={time}
90
- onTimeChange={onChange}
91
- onFocusInput={onFocusInput}
92
- isLandscape={isLandscape}
93
- />
94
- </View>
95
- <View style={styles.footer}>
96
- <IconButton
97
- name={getTimeInputTypeIcon(inputType, {
98
- keyboard: keyboardIcon,
99
- picker: clockIcon,
100
- })}
101
- onPress={() => setInputType(reverseInputTypes[inputType])}
102
- style={styles.inputTypeToggle}
103
- accessibilityLabel="toggle keyboard"
104
- />
105
- <View style={styles.fill} />
106
- <Button onPress={onClose}>{cancelLabel}</Button>
107
- <Button onPress={onConfirm}>{confirmLabel}</Button>
108
- </View>
109
- </KeyboardAvoidingView>
110
- </Modal>
111
- </Portal>
112
- );
113
- }
114
-
115
- export default memo(TimePickerModal);
@@ -1,10 +0,0 @@
1
- import { getRegisteredComponentWithFallback } from '../../core';
2
- import TimePickerModalDefault from './TimePickerModal';
3
-
4
- export const TimePickerModal = getRegisteredComponentWithFallback(
5
- 'TimePickerModal',
6
- TimePickerModalDefault,
7
- );
8
-
9
- export type { Props as TimePickerModalProps } from './TimePickerModal';
10
- export { styles } from './utils';
@@ -1,47 +0,0 @@
1
- import { StyleSheet } from 'react-native-unistyles';
2
-
3
- import { getRegisteredComponentStylesWithFallback } from '../../core';
4
-
5
- const timepickerModalStylesDefault = StyleSheet.create(theme => ({
6
- keyboardView: {
7
- justifyContent: 'center',
8
- alignItems: 'center',
9
- flex: 1,
10
- },
11
- modalContent: {
12
- minWidth: 287,
13
- width: undefined,
14
- maxWidth: undefined,
15
- flex: undefined,
16
- borderRadius: theme.shapes.corner.extraLarge,
17
- },
18
- labelContainer: {
19
- height: 28,
20
- justifyContent: 'flex-end',
21
- paddingLeft: theme.spacings['6'],
22
- paddingRight: theme.spacings['6'],
23
- alignSelf: 'flex-start',
24
- },
25
- label: {
26
- letterSpacing: 1,
27
- fontSize: theme.typescale.labelLarge.fontSize,
28
- color: theme.colors.onSurface,
29
- fontWeight: theme.typescale.labelLarge.fontWeight,
30
- },
31
- timePickerContainer: {
32
- padding: theme.spacings['6'],
33
- },
34
- footer: {
35
- flexDirection: 'row',
36
- alignItems: 'center',
37
- padding: theme.spacings['2'],
38
- width: '100%',
39
- },
40
- inputTypeToggle: { margin: theme.spacings['1'] },
41
- fill: { flex: 1 },
42
- }));
43
-
44
- export const styles = getRegisteredComponentStylesWithFallback(
45
- 'TimePickerModal',
46
- timepickerModalStylesDefault,
47
- );
@@ -1,100 +0,0 @@
1
- import { memo } from 'react';
2
- import { View, type ViewProps } from 'react-native';
3
- import { StyleSheet } from 'react-native-unistyles';
4
-
5
- import { getRegisteredComponentStylesWithFallback } from '../../core';
6
-
7
- export type Props = Omit<ViewProps, 'children'> & {
8
- /**
9
- * Top Inset of the Divider.
10
- */
11
- topInset?: number;
12
- /**
13
- * Bottom Inset of the Divider.
14
- */
15
- bottomInset?: number;
16
- /**
17
- * Whether divider should be bolded.
18
- */
19
- bold?: boolean;
20
- /**
21
- * Horizontal spacing of the Divider
22
- */
23
- spacing?: number;
24
- };
25
-
26
- /**
27
- * A divider is a thin, lightweight separator that groups content in lists and page layouts.
28
- *
29
- * <div class="screenshots">
30
- * <figure>
31
- * <img class="medium" src="screenshots/divider.png" />
32
- * </figure>
33
- * </div>
34
- *
35
- * ## Usage
36
- * ```js
37
- * import * as React from 'react';
38
- * import { View } from 'react-native';
39
- * import { Divider, Text } from 'react-native-paper';
40
- *
41
- * const MyComponent = () => (
42
- * <View>
43
- * <Text>Lemon</Text>
44
- * <Divider />
45
- * <Text>Mango</Text>
46
- * <Divider />
47
- * </View>
48
- * );
49
- *
50
- * export default MyComponent;
51
- * ```
52
- */
53
-
54
- const VerticalDivider = ({
55
- topInset = 0,
56
- bottomInset = 0,
57
- spacing = 0,
58
- style,
59
- bold = false,
60
- ...rest
61
- }: Props) => {
62
- verticalDividerStyles.useVariants({
63
- isBold: bold,
64
- });
65
-
66
- return (
67
- <View
68
- {...rest}
69
- style={[
70
- verticalDividerStyles.root,
71
- style,
72
- topInset ? { marginTop: topInset } : undefined,
73
- bottomInset ? { marginBottom: bottomInset } : undefined,
74
- spacing ? { marginHorizontal: spacing } : undefined,
75
- ]}
76
- />
77
- );
78
- };
79
-
80
- export const verticalDividerStylesDefault = StyleSheet.create(theme => ({
81
- root: {
82
- width: StyleSheet.hairlineWidth,
83
- background: theme.colors.outlineVariant,
84
-
85
- variants: {
86
- isBold: {
87
- true: {
88
- width: 1,
89
- },
90
- },
91
- },
92
- },
93
- }));
94
-
95
- export const verticalDividerStyles = getRegisteredComponentStylesWithFallback(
96
- 'VerticalDivider',
97
- verticalDividerStylesDefault,
98
- );
99
-
100
- export default memo(VerticalDivider);
@@ -1,9 +0,0 @@
1
- import { getRegisteredComponentWithFallback } from '../../core';
2
- import VerticalDividerDefault from './VerticalDivider';
3
-
4
- export const VerticalDivider = getRegisteredComponentWithFallback(
5
- 'VerticalDivider',
6
- VerticalDividerDefault,
7
- );
8
-
9
- export { type Props as VerticalDividerProps, verticalDividerStyles } from './VerticalDivider';
@@ -1,87 +0,0 @@
1
- import type { ComponentType, Context as ContextType, PropsWithChildren } from 'react';
2
- import { Fragment, useContext, useId, useMemo, useRef, useState } from 'react';
3
-
4
- import typedMemo from '../hocs/typedMemo';
5
- import { Repository } from '../utils/repository';
6
-
7
- // https://github.com/pmndrs/its-fine/blob/main/src/index.tsx
8
- // In development, React will warn about using contexts between renderers.
9
- // Hide the warning because its-fine fixes this issue
10
- // https://github.com/facebook/react/pull/12779
11
- function wrapContext<T>(context: React.Context<T>): React.Context<T> {
12
- try {
13
- return Object.defineProperties(context, {
14
- _currentRenderer: {
15
- get() {
16
- return null;
17
- },
18
- set() {},
19
- },
20
- _currentRenderer2: {
21
- get() {
22
- return null;
23
- },
24
- set() {},
25
- },
26
- });
27
- } catch (_) {
28
- return context;
29
- }
30
- }
31
-
32
- export const createContextBridge = <T extends object>(
33
- bridgeName: string,
34
- Wrapper: ComponentType<T>,
35
- contexts: ContextType<any>[] = [],
36
- ) => {
37
- const respository = new Repository<ContextType<any>[]>({
38
- name: bridgeName,
39
- onRegister: (arg, name, registry) => {
40
- return [registry[name] ?? [], arg].flat();
41
- },
42
- });
43
-
44
- return {
45
- registerContextToBridge: (updatedContexts: ContextType<any> | ContextType<any>[]) => {
46
- respository.register(
47
- 'contexts',
48
- ([] as ContextType<any>[])
49
- .concat(updatedContexts)
50
- .map(context => wrapContext(context)),
51
- );
52
- },
53
- BridgedComponent: typedMemo((props: PropsWithChildren<T> & { name?: string }) => {
54
- const { name, ...rest } = props;
55
- const contextValuesRef = useRef<any[]>([]);
56
-
57
- const id = useId();
58
-
59
- const [allContexts] = useState(() =>
60
- Array.from(new Set([...contexts, ...Object.values(respository.getAll()).flat()])),
61
- );
62
-
63
- for (const i in allContexts) {
64
- // eslint-disable-next-line react-hooks/rules-of-hooks
65
- contextValuesRef.current[i] = useContext(allContexts[i]);
66
- }
67
-
68
- const content = useMemo(() => {
69
- return allContexts.reduce((acc, Context, currentIndex) => {
70
- return (
71
- <Context.Provider value={contextValuesRef.current[currentIndex]}>
72
- {acc}
73
- </Context.Provider>
74
- );
75
- }, <>{props.children}</>);
76
- // eslint-disable-next-line react-hooks/exhaustive-deps
77
- }, [...contextValuesRef.current, allContexts, props.children]);
78
-
79
- const _key = name ? name + id : id;
80
- return (
81
- <Wrapper name={name} {...(rest as T)}>
82
- <Fragment key={_key}>{content}</Fragment>
83
- </Wrapper>
84
- );
85
- }),
86
- };
87
- };
@@ -1,190 +0,0 @@
1
- import {
2
- type Context,
3
- createContext,
4
- type ReactNode,
5
- type RefObject,
6
- useCallback,
7
- useContext,
8
- useEffect,
9
- useMemo,
10
- useRef,
11
- } from 'react';
12
- import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';
13
-
14
- import typedMemo from '../hocs/typedMemo';
15
- import { usePrevious } from '../hooks';
16
- import { shallowCompare } from '../utils';
17
-
18
- type StoreDataType = Record<string, any>;
19
-
20
- type SelectorOutputType<IStore, SelectorOutput> = (store: IStore) => SelectorOutput;
21
-
22
- type UseStoreDataReturnType<T> = {
23
- get: () => T;
24
- set: (value: (prev: T) => Partial<T>) => void;
25
- store: RefObject<T>;
26
- subscribe: (callback: () => void) => () => void;
27
- };
28
-
29
- const useStoreData = <IStore extends StoreDataType>(
30
- value: IStore,
31
- defaultValue: IStore | null,
32
- watch: boolean = false,
33
- ): UseStoreDataReturnType<IStore> => {
34
- const store = useRef<IStore>({ ...defaultValue, ...(value as IStore) });
35
- const watchRef = useRef(watch);
36
-
37
- const get = useCallback(() => store.current, [store]);
38
-
39
- const subscribers = useRef(new Set<() => void>());
40
-
41
- const set = useCallback(
42
- (callback: (prev: IStore) => Partial<IStore>) => {
43
- store.current = { ...store.current, ...callback(store.current) };
44
- subscribers.current.forEach(subscriber => subscriber());
45
- },
46
- [store],
47
- );
48
-
49
- const subscribe = useCallback((callback: () => void) => {
50
- subscribers.current.add(callback);
51
-
52
- return () => subscribers.current.delete(callback);
53
- }, []);
54
-
55
- /** The effect is required to trigger the updates on the consumers */
56
- useEffect(() => {
57
- if (!watchRef.current) return;
58
- set(prev => ({ ...prev, ...value }));
59
- }, [set, value]);
60
-
61
- /**
62
- * Cases:
63
- * 1. when the value updates, we want the data to be updated immediately
64
- * 2. the data stored in store.current may not be current with regards to the parent and the parent must have dropped the references related to the data.
65
- * 3. because store.current is a ref, on watch and change, if we update store.current, no side-effect introduced.
66
- */
67
- if (usePrevious(value).current !== value && watchRef.current) {
68
- store.current = { ...store.current, ...value };
69
- }
70
-
71
- return useMemo(
72
- () => ({
73
- get,
74
- set,
75
- subscribe,
76
- store,
77
- }),
78
- [get, set, subscribe],
79
- );
80
- };
81
-
82
- export const createFastContext = <T extends StoreDataType = {}>(
83
- defaultValue: T | null = null,
84
- watch: boolean = false,
85
- ) => {
86
- const context = createContext<UseStoreDataReturnType<T> | null>(null);
87
-
88
- return {
89
- /**
90
- * using the value from here will never cause a rerender as context is based on refs.
91
- */
92
- useStoreRef: createUseRefContext(context),
93
-
94
- /**
95
- *
96
- * the value is memoized and thus changing the value will have no effect.
97
- * use key prop to unmount and remount if necessary. alternatively use set from the context to update the value.
98
- *
99
- */
100
- Provider: createProvider<T>(
101
- context as Context<UseStoreDataReturnType<T>>,
102
- defaultValue,
103
- watch,
104
- ),
105
-
106
- /**
107
- *
108
- * @param selector
109
- * @param equalityCheck
110
- * @returns tuple, first item is return value of the selector and the second item is setter
111
- *
112
- */
113
- useContext: <SelectorOutput,>(
114
- selector: SelectorOutputType<T, SelectorOutput>,
115
- equalityCheck = shallowCompare,
116
- ) => useStore(context as Context<UseStoreDataReturnType<T>>, selector, equalityCheck),
117
-
118
- /**
119
- *
120
- * @param selector
121
- * @param equalityCheck
122
- * @returns return value of the selector
123
- *
124
- */
125
- useContextValue: <SelectorOutput,>(
126
- selector: SelectorOutputType<T, SelectorOutput>,
127
- equalityCheck = shallowCompare,
128
- ) => useStoreValue(context as Context<UseStoreDataReturnType<T>>, selector, equalityCheck),
129
- /**
130
- * context of the store. Useful for ContextBridge
131
- */
132
- Context: context,
133
- };
134
- };
135
-
136
- export const createProvider = <T extends Record<string, any> = {}>(
137
- StoreContext: Context<UseStoreDataReturnType<T>>,
138
- defaultValue: T | null,
139
- watch: boolean,
140
- ) =>
141
- typedMemo(({ value, children }: { value: T; children: ReactNode }) => {
142
- return (
143
- <StoreContext.Provider value={useStoreData<T>(value, defaultValue, watch) as any}>
144
- {children}
145
- </StoreContext.Provider>
146
- );
147
- });
148
-
149
- export const createUseRefContext = <T,>(_Context: Context<UseStoreDataReturnType<T> | null>) => {
150
- return () => {
151
- const value = useContext<UseStoreDataReturnType<T> | null>(_Context);
152
- if (value === null)
153
- throw 'Fast Context requires the value to be wrapped in a Provider with a value.';
154
-
155
- return value;
156
- };
157
- };
158
-
159
- export const useStore = <IStore extends StoreDataType, SelectorOutput>(
160
- _Context: Context<UseStoreDataReturnType<IStore>>,
161
- selector: SelectorOutputType<IStore, SelectorOutput>,
162
- equalityFn = Object.is,
163
- ): [SelectorOutput, (value: (prev: IStore) => Partial<IStore>) => void] => {
164
- const store = useContext(_Context);
165
- if (!store) {
166
- throw new Error('Store not found');
167
- }
168
-
169
- const state = useSyncExternalStoreWithSelector(
170
- store.subscribe,
171
- () => {
172
- return store.get();
173
- },
174
- undefined,
175
- snapshot => {
176
- return selector(snapshot);
177
- },
178
- equalityFn,
179
- );
180
-
181
- return [state, store.set];
182
- };
183
-
184
- export const useStoreValue = <IStore extends StoreDataType, SelectorOutput>(
185
- _Context: Context<UseStoreDataReturnType<IStore>>,
186
- selector: SelectorOutputType<IStore, SelectorOutput>,
187
- equalityFn = Object.is,
188
- ) => {
189
- return useStore(_Context, selector, equalityFn)[0];
190
- };
@@ -1,5 +0,0 @@
1
- import { memo } from 'react';
2
-
3
- const typedMemo: <T>(c: T) => T = memo;
4
-
5
- export default typedMemo;
@@ -1,68 +0,0 @@
1
- import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
-
3
- type ReturnType<T> = [T, (value: T, ...args: any[]) => void];
4
-
5
- type Args<T> = {
6
- value?: T;
7
- defaultValue?: T;
8
- onChange?: (value: T, ...args: any[]) => any;
9
- disabled?: boolean;
10
- manipulateValue?: (value: T | undefined, prevValue: T | undefined) => T;
11
- };
12
-
13
- const defaultManipulateValue = (val: any) => val;
14
-
15
- const useControlledValue = <T,>({
16
- value: valueProp,
17
- defaultValue,
18
- disabled = false,
19
- onChange,
20
- manipulateValue = defaultManipulateValue,
21
- }: Args<T>): ReturnType<T> => {
22
- const value = useMemo(
23
- () =>
24
- valueProp !== undefined
25
- ? manipulateValue(valueProp, undefined)
26
- : manipulateValue(defaultValue, undefined),
27
- [defaultValue, manipulateValue, valueProp],
28
- );
29
-
30
- const hasWarnedRef = useRef(false);
31
-
32
- const isUncontrolled = useRef(valueProp).current === undefined;
33
- const [uncontrolledValue, setValue] = useState(value);
34
-
35
- const updateValue = useCallback(
36
- (val: T, ...rest: any[]) => {
37
- if (disabled) return;
38
-
39
- if (isUncontrolled) {
40
- setValue(manipulateValue(val, uncontrolledValue));
41
- }
42
-
43
- onChange?.(
44
- manipulateValue(val, isUncontrolled ? uncontrolledValue : valueProp),
45
- ...rest,
46
- );
47
- },
48
- [disabled, isUncontrolled, manipulateValue, onChange, uncontrolledValue, valueProp],
49
- );
50
-
51
- useEffect(() => {
52
- if (hasWarnedRef.current) return;
53
- hasWarnedRef.current = true;
54
-
55
- if (valueProp !== undefined && isUncontrolled) {
56
- console.warn(
57
- 'Trying to change the value from uncontrolled to controlled can lead to inconsistencies',
58
- );
59
- }
60
- }, [isUncontrolled, valueProp]);
61
-
62
- return useMemo(
63
- () => [isUncontrolled ? uncontrolledValue : value, updateValue],
64
- [isUncontrolled, uncontrolledValue, updateValue, value],
65
- );
66
- };
67
-
68
- export default useControlledValue;
@@ -1,9 +0,0 @@
1
- import { useRef } from 'react';
2
-
3
- const useLatest = <T,>(value: T) => {
4
- const ref = useRef(value);
5
- ref.current = value;
6
- return ref;
7
- };
8
-
9
- export default useLatest;
@@ -1,14 +0,0 @@
1
- import { useCallback } from 'react';
2
-
3
- import { mergeRefs } from '../utils';
4
- import useLatest from './useLatest';
5
-
6
- export const useMergedRefs: typeof mergeRefs = refs => {
7
- const latestRefs = useLatest(refs);
8
- return useCallback(
9
- value => {
10
- mergeRefs(latestRefs.current)(value);
11
- },
12
- [latestRefs],
13
- );
14
- };
@@ -1,13 +0,0 @@
1
- import { useEffect, useRef } from 'react';
2
-
3
- const usePrevious = <T>(value: T) => {
4
- const ref = useRef<T>(value);
5
-
6
- useEffect(() => {
7
- ref.current = value;
8
- }, [value]);
9
-
10
- return ref;
11
- };
12
-
13
- export default usePrevious;