@utilitywarehouse/hearth-react-native 0.26.0 → 0.27.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 (36) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-lint.log +13 -13
  3. package/CHANGELOG.md +40 -0
  4. package/build/components/Banner/Banner.js +12 -1
  5. package/build/components/PillGroup/Pill.js +0 -1
  6. package/build/components/PillGroup/PillGroup.js +4 -1
  7. package/build/components/SegmentedControl/SegmentedControl.context.d.ts +14 -0
  8. package/build/components/SegmentedControl/SegmentedControl.context.js +9 -0
  9. package/build/components/SegmentedControl/SegmentedControl.d.ts +6 -0
  10. package/build/components/SegmentedControl/SegmentedControl.js +196 -0
  11. package/build/components/SegmentedControl/SegmentedControl.props.d.ts +18 -0
  12. package/build/components/SegmentedControl/SegmentedControl.props.js +1 -0
  13. package/build/components/SegmentedControl/SegmentedControlOption.d.ts +18 -0
  14. package/build/components/SegmentedControl/SegmentedControlOption.js +122 -0
  15. package/build/components/SegmentedControl/SegmentedControlOption.props.d.ts +12 -0
  16. package/build/components/SegmentedControl/SegmentedControlOption.props.js +1 -0
  17. package/build/components/SegmentedControl/index.d.ts +4 -0
  18. package/build/components/SegmentedControl/index.js +2 -0
  19. package/build/components/index.d.ts +1 -0
  20. package/build/components/index.js +1 -0
  21. package/docs/changelog.mdx +136 -0
  22. package/docs/components/AllComponents.web.tsx +14 -0
  23. package/package.json +4 -4
  24. package/src/components/Banner/Banner.tsx +12 -1
  25. package/src/components/PillGroup/Pill.tsx +0 -1
  26. package/src/components/PillGroup/PillGroup.tsx +4 -0
  27. package/src/components/SegmentedControl/SegmentedControl.context.ts +22 -0
  28. package/src/components/SegmentedControl/SegmentedControl.docs.mdx +90 -0
  29. package/src/components/SegmentedControl/SegmentedControl.figma.tsx +40 -0
  30. package/src/components/SegmentedControl/SegmentedControl.props.ts +20 -0
  31. package/src/components/SegmentedControl/SegmentedControl.stories.tsx +77 -0
  32. package/src/components/SegmentedControl/SegmentedControl.tsx +257 -0
  33. package/src/components/SegmentedControl/SegmentedControlOption.props.ts +14 -0
  34. package/src/components/SegmentedControl/SegmentedControlOption.tsx +213 -0
  35. package/src/components/SegmentedControl/index.ts +4 -0
  36. package/src/components/index.ts +1 -0
@@ -0,0 +1,257 @@
1
+ import { Children, isValidElement, useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+ import { View } from 'react-native';
3
+ import Animated, {
4
+ Easing,
5
+ useAnimatedStyle,
6
+ useReducedMotion,
7
+ useSharedValue,
8
+ withTiming,
9
+ } from 'react-native-reanimated';
10
+ import { StyleSheet } from 'react-native-unistyles';
11
+ import { useStyleProps } from '../../hooks';
12
+ import { SegmentedControlContext } from './SegmentedControl.context';
13
+ import type SegmentedControlProps from './SegmentedControl.props';
14
+
15
+ const Indicator = Animated.createAnimatedComponent(View);
16
+ const GROUP_BORDER_WIDTH = 1;
17
+
18
+ const SegmentedControl = ({
19
+ value: controlledValue,
20
+ defaultValue,
21
+ onValueChange,
22
+ size = 'sm',
23
+ disabled = false,
24
+ children,
25
+ style,
26
+ ...props
27
+ }: SegmentedControlProps) => {
28
+ const { computedStyles, remainingProps } = useStyleProps(props);
29
+ const isReducedMotion = useReducedMotion();
30
+ const indicatorPositionOffset = GROUP_BORDER_WIDTH;
31
+
32
+ const optionValues = useMemo(() => {
33
+ const values: string[] = [];
34
+
35
+ const walk = (node: any) => {
36
+ Children.forEach(node, child => {
37
+ if (!isValidElement(child)) return;
38
+
39
+ const childType: any = child.type;
40
+ const childProps: any = child.props;
41
+
42
+ if (
43
+ childType?.displayName === 'SegmentedControlOption' &&
44
+ typeof childProps?.value === 'string'
45
+ ) {
46
+ values.push(childProps.value);
47
+ }
48
+
49
+ if (childProps?.children) {
50
+ walk(childProps.children);
51
+ }
52
+ });
53
+ };
54
+
55
+ walk(children);
56
+ return values;
57
+ }, [children]);
58
+ const optionValuesKey = useMemo(() => optionValues.join('|'), [optionValues]);
59
+ const optionValuesRef = useRef<string[]>(optionValues);
60
+
61
+ useEffect(() => {
62
+ optionValuesRef.current = optionValues;
63
+ }, [optionValues]);
64
+
65
+ const getInitialValue = () => {
66
+ if (controlledValue !== undefined) return controlledValue;
67
+ if (defaultValue !== undefined) return defaultValue;
68
+ return optionValues[0];
69
+ };
70
+
71
+ const [uncontrolledValue, setUncontrolledValue] = useState<string | undefined>(getInitialValue);
72
+
73
+ useEffect(() => {
74
+ if (controlledValue !== undefined) {
75
+ setUncontrolledValue(controlledValue);
76
+ }
77
+ }, [controlledValue]);
78
+
79
+ useEffect(() => {
80
+ const currentOptionValues = optionValuesRef.current;
81
+ setUncontrolledValue(prev => {
82
+ if (!prev) return currentOptionValues[0];
83
+ if (!currentOptionValues.includes(prev)) return currentOptionValues[0];
84
+ return prev;
85
+ });
86
+ }, [optionValuesKey]);
87
+
88
+ const currentValue = controlledValue !== undefined ? controlledValue : uncontrolledValue;
89
+
90
+ const indicatorX = useSharedValue(0);
91
+ const indicatorWidth = useSharedValue(0);
92
+ const indicatorY = useSharedValue(0);
93
+ const indicatorHeight = useSharedValue(0);
94
+ const [hasIndicator, setHasIndicator] = useState(false);
95
+ const layoutsRef = useRef<Map<string, { x: number; y: number; width: number; height: number }>>(
96
+ new Map()
97
+ );
98
+ const prevValueRef = useRef<string | undefined>(undefined);
99
+ const initialisedRef = useRef(false);
100
+
101
+ const select = useCallback(
102
+ (nextValue: string) => {
103
+ if (disabled) return;
104
+ if (controlledValue === undefined) {
105
+ setUncontrolledValue(nextValue);
106
+ }
107
+ onValueChange?.(nextValue);
108
+ },
109
+ [controlledValue, disabled, onValueChange]
110
+ );
111
+
112
+ const registerOptionLayout = useCallback(
113
+ (value: string, layout: { x: number; y: number; width: number; height: number }) => {
114
+ layoutsRef.current.set(value, layout);
115
+ const activeValue = controlledValue !== undefined ? controlledValue : uncontrolledValue;
116
+ if (!activeValue || activeValue !== value) return;
117
+
118
+ if (!initialisedRef.current) {
119
+ indicatorX.value = Math.max(0, layout.x - indicatorPositionOffset);
120
+ indicatorWidth.value = layout.width;
121
+ indicatorY.value = Math.max(0, layout.y - indicatorPositionOffset);
122
+ indicatorHeight.value = layout.height;
123
+ prevValueRef.current = activeValue;
124
+ initialisedRef.current = true;
125
+ setHasIndicator(true);
126
+ return;
127
+ }
128
+
129
+ if (prevValueRef.current === activeValue) return;
130
+
131
+ const config = {
132
+ delay: 200,
133
+ duration: isReducedMotion ? 0 : 220,
134
+ easing: Easing.out(Easing.cubic),
135
+ } as const;
136
+
137
+ indicatorX.value = withTiming(Math.max(0, layout.x - indicatorPositionOffset), config);
138
+ indicatorWidth.value = withTiming(layout.width, config);
139
+ indicatorY.value = withTiming(Math.max(0, layout.y - indicatorPositionOffset), config);
140
+ indicatorHeight.value = withTiming(layout.height, config);
141
+ prevValueRef.current = activeValue;
142
+ },
143
+ [
144
+ controlledValue,
145
+ indicatorHeight,
146
+ indicatorWidth,
147
+ indicatorX,
148
+ indicatorY,
149
+ indicatorPositionOffset,
150
+ isReducedMotion,
151
+ uncontrolledValue,
152
+ ]
153
+ );
154
+
155
+ useEffect(() => {
156
+ if (!currentValue || !initialisedRef.current) return;
157
+ if (prevValueRef.current === undefined || prevValueRef.current === currentValue) return;
158
+ const layout = layoutsRef.current.get(currentValue);
159
+ if (!layout) return;
160
+ const config = {
161
+ duration: isReducedMotion ? 0 : 220,
162
+ easing: Easing.out(Easing.cubic),
163
+ } as const;
164
+
165
+ indicatorX.value = withTiming(Math.max(0, layout.x - indicatorPositionOffset), config);
166
+ indicatorWidth.value = withTiming(layout.width, config);
167
+ indicatorY.value = withTiming(Math.max(0, layout.y - indicatorPositionOffset), config);
168
+ indicatorHeight.value = withTiming(layout.height, config);
169
+ prevValueRef.current = currentValue;
170
+ }, [
171
+ currentValue,
172
+ indicatorHeight,
173
+ indicatorWidth,
174
+ indicatorX,
175
+ indicatorY,
176
+ indicatorPositionOffset,
177
+ isReducedMotion,
178
+ optionValuesKey,
179
+ ]);
180
+
181
+ const indicatorStyle = useAnimatedStyle(() => ({
182
+ transform: [{ translateX: indicatorX.value }, { translateY: indicatorY.value }],
183
+ width: indicatorWidth.value,
184
+ height: indicatorHeight.value,
185
+ }));
186
+
187
+ styles.useVariants({ disabled, size });
188
+
189
+ const contextValue = useMemo(
190
+ () => ({
191
+ value: currentValue,
192
+ select,
193
+ disabled,
194
+ size,
195
+ registerOptionLayout,
196
+ }),
197
+ [currentValue, select, disabled, size, registerOptionLayout]
198
+ );
199
+
200
+ return (
201
+ <SegmentedControlContext.Provider value={contextValue}>
202
+ <View
203
+ accessibilityRole="radiogroup"
204
+ accessibilityState={{ disabled }}
205
+ style={[styles.container, computedStyles, style]}
206
+ {...remainingProps}
207
+ >
208
+ {hasIndicator ? (
209
+ <Indicator pointerEvents="none" style={[styles.indicator, indicatorStyle]} />
210
+ ) : null}
211
+ {children}
212
+ </View>
213
+ </SegmentedControlContext.Provider>
214
+ );
215
+ };
216
+
217
+ SegmentedControl.displayName = 'SegmentedControl';
218
+
219
+ const styles = StyleSheet.create(theme => ({
220
+ container: {
221
+ flexDirection: 'row',
222
+ alignItems: 'center',
223
+ alignSelf: 'flex-start',
224
+ gap: theme.components.segmentedControl.group.gap,
225
+ height: theme.components.segmentedControl.group.height,
226
+ borderRadius: theme.components.segmentedControl.group.borderRadius,
227
+ borderWidth: theme.components.segmentedControl.group.borderWidth,
228
+ backgroundColor: theme.color.surface.neutral.subtle,
229
+ borderColor: theme.color.border.strong,
230
+ variants: {
231
+ size: {
232
+ sm: {
233
+ height: 32,
234
+ padding: 2,
235
+ },
236
+ md: {
237
+ height: theme.components.segmentedControl.group.height,
238
+ padding: 2,
239
+ },
240
+ },
241
+ disabled: {
242
+ true: {
243
+ opacity: theme.opacity.disabled,
244
+ },
245
+ },
246
+ },
247
+ },
248
+ indicator: {
249
+ position: 'absolute',
250
+ left: 0,
251
+ top: 0,
252
+ borderRadius: theme.components.segmentedControl.borderRadius,
253
+ backgroundColor: theme.color.interactive.brand.surface.strong.default,
254
+ },
255
+ }));
256
+
257
+ export default SegmentedControl;
@@ -0,0 +1,14 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { PressableProps, ViewProps } from 'react-native';
3
+
4
+ export interface SegmentedControlOptionProps extends Omit<PressableProps, 'children'> {
5
+ /** Unique option value. */
6
+ value: string;
7
+ /** Option label/content. */
8
+ children: ReactNode;
9
+ /** Disables only this option. */
10
+ disabled?: boolean;
11
+ style?: ViewProps['style'];
12
+ }
13
+
14
+ export default SegmentedControlOptionProps;
@@ -0,0 +1,213 @@
1
+ import { createPressable } from '@gluestack-ui/pressable';
2
+ import { useEffect } from 'react';
3
+ import { Platform, Pressable, View } from 'react-native';
4
+ import Animated, {
5
+ Easing,
6
+ useAnimatedStyle,
7
+ useReducedMotion,
8
+ useSharedValue,
9
+ withTiming,
10
+ } from 'react-native-reanimated';
11
+ import { StyleSheet } from 'react-native-unistyles';
12
+ import { BodyText } from '../BodyText';
13
+ import { useSegmentedControlContext } from './SegmentedControl.context';
14
+ import type SegmentedControlOptionProps from './SegmentedControlOption.props';
15
+
16
+ const AnimatedView = Animated.createAnimatedComponent(View);
17
+
18
+ const SegmentedControlOptionRoot = ({
19
+ value,
20
+ children,
21
+ accessibilityLabel,
22
+ disabled = false,
23
+ style,
24
+ states = {},
25
+ ...props
26
+ }: SegmentedControlOptionProps & { states?: { active?: boolean; disabled?: boolean } }) => {
27
+ const {
28
+ value: selectedValue,
29
+ select,
30
+ disabled: allDisabled,
31
+ size,
32
+ registerOptionLayout,
33
+ } = useSegmentedControlContext();
34
+ const { active = false } = states;
35
+ const reducedMotion = useReducedMotion();
36
+
37
+ const selected = selectedValue === value;
38
+ const isDisabled = disabled || !!allDisabled;
39
+
40
+ const selectedProgress = useSharedValue(selected ? 1 : 0);
41
+
42
+ useEffect(() => {
43
+ selectedProgress.value = withTiming(selected ? 1 : 0, {
44
+ duration: reducedMotion ? 0 : 220,
45
+ easing: Easing.out(Easing.cubic),
46
+ });
47
+ }, [reducedMotion, selected, selectedProgress]);
48
+
49
+ const regularLabelStyle = useAnimatedStyle(() => ({
50
+ opacity: 1 - selectedProgress.value,
51
+ }));
52
+
53
+ const selectedLabelStyle = useAnimatedStyle(() => ({
54
+ opacity: selectedProgress.value,
55
+ }));
56
+
57
+ styles.useVariants({ selected, disabled: isDisabled, size, active });
58
+
59
+ const onPress = () => {
60
+ if (isDisabled) return;
61
+ select(value);
62
+ };
63
+
64
+ const accessibleLabel =
65
+ typeof children === 'string' || typeof children === 'number' ? String(children) : value;
66
+
67
+ return (
68
+ <Pressable
69
+ {...props}
70
+ accessibilityRole="radio"
71
+ accessibilityState={{ checked: selected, disabled: isDisabled }}
72
+ accessibilityLabel={accessibilityLabel ?? accessibleLabel}
73
+ onPress={onPress}
74
+ onLayout={e => registerOptionLayout(value, e.nativeEvent.layout)}
75
+ disabled={isDisabled}
76
+ style={[styles.option, style]}
77
+ {...(Platform.OS === 'web'
78
+ ? ({ 'aria-label': accessibilityLabel ?? accessibleLabel } as any)
79
+ : null)}
80
+ >
81
+ <View
82
+ style={styles.labelWrap}
83
+ accessible={false}
84
+ accessibilityElementsHidden
85
+ importantForAccessibility="no-hide-descendants"
86
+ {...(Platform.OS === 'web' ? ({ 'aria-hidden': true } as any) : null)}
87
+ >
88
+ <BodyText
89
+ size="md"
90
+ weight="semibold"
91
+ style={styles.labelSizer}
92
+ accessible={false}
93
+ accessibilityElementsHidden
94
+ importantForAccessibility="no-hide-descendants"
95
+ {...(Platform.OS === 'web' ? ({ 'aria-hidden': true } as any) : null)}
96
+ >
97
+ {children}
98
+ </BodyText>
99
+ <AnimatedView
100
+ pointerEvents="none"
101
+ style={[styles.textLayer, regularLabelStyle]}
102
+ accessible={false}
103
+ accessibilityElementsHidden
104
+ importantForAccessibility="no-hide-descendants"
105
+ {...(Platform.OS === 'web' ? ({ 'aria-hidden': true } as any) : null)}
106
+ >
107
+ <BodyText size="md" weight="regular" style={styles.textRegular}>
108
+ {children}
109
+ </BodyText>
110
+ </AnimatedView>
111
+ <AnimatedView
112
+ pointerEvents="none"
113
+ style={[styles.textLayer, selectedLabelStyle]}
114
+ accessible={false}
115
+ accessibilityElementsHidden
116
+ importantForAccessibility="no-hide-descendants"
117
+ {...(Platform.OS === 'web' ? ({ 'aria-hidden': true } as any) : null)}
118
+ >
119
+ <BodyText size="md" weight="semibold" style={styles.textSelected}>
120
+ {children}
121
+ </BodyText>
122
+ </AnimatedView>
123
+ </View>
124
+ </Pressable>
125
+ );
126
+ };
127
+
128
+ const SegmentedControlOption = createPressable({ Root: SegmentedControlOptionRoot });
129
+
130
+ SegmentedControlOption.displayName = 'SegmentedControlOption';
131
+
132
+ const styles = StyleSheet.create(theme => ({
133
+ option: {
134
+ minWidth: theme.components.segmentedControl.minWidth,
135
+ height: theme.components.segmentedControl.height,
136
+ borderRadius: theme.components.segmentedControl.borderRadius,
137
+ paddingHorizontal: theme.components.segmentedControl.paddingHorizontal,
138
+ paddingVertical: theme.components.segmentedControl.paddingVertical,
139
+ justifyContent: 'center',
140
+ alignItems: 'center',
141
+ backgroundColor: 'transparent',
142
+ zIndex: 1,
143
+ variants: {
144
+ size: {
145
+ sm: {
146
+ height: 28,
147
+ paddingHorizontal: theme.space[150],
148
+ paddingVertical: 0,
149
+ },
150
+ md: {
151
+ height: 44,
152
+ paddingHorizontal: theme.components.segmentedControl.paddingHorizontal,
153
+ paddingVertical: theme.components.segmentedControl.paddingVertical,
154
+ },
155
+ },
156
+ selected: {
157
+ true: {
158
+ backgroundColor: 'transparent',
159
+ _web: {
160
+ _active: {
161
+ backgroundColor: theme.color.interactive.brand.surface.strong.active,
162
+ },
163
+ },
164
+ },
165
+ false: {
166
+ _web: {
167
+ _hover: {
168
+ backgroundColor: theme.color.interactive.neutral.surface.subtle.hover,
169
+ },
170
+ _active: {
171
+ backgroundColor: theme.color.interactive.neutral.surface.subtle.active,
172
+ },
173
+ },
174
+ },
175
+ },
176
+ active: {
177
+ true: {
178
+ backgroundColor: theme.color.interactive.neutral.surface.subtle.active,
179
+ },
180
+ },
181
+ },
182
+ },
183
+ labelWrap: {
184
+ position: 'relative',
185
+ alignItems: 'center',
186
+ justifyContent: 'center',
187
+ },
188
+ labelSizer: {
189
+ opacity: 0,
190
+ },
191
+ textLayer: {
192
+ position: 'absolute',
193
+ left: 0,
194
+ right: 0,
195
+ alignItems: 'center',
196
+ justifyContent: 'center',
197
+ },
198
+ textRegular: {
199
+ color: theme.color.text.primary,
200
+ },
201
+ textSelected: {
202
+ color: theme.color.text.inverted,
203
+ variants: {
204
+ disabled: {
205
+ true: {
206
+ opacity: 1,
207
+ },
208
+ },
209
+ },
210
+ },
211
+ }));
212
+
213
+ export default SegmentedControlOption;
@@ -0,0 +1,4 @@
1
+ export { default as SegmentedControl } from './SegmentedControl';
2
+ export type { SegmentedControlProps } from './SegmentedControl.props';
3
+ export { default as SegmentedControlOption } from './SegmentedControlOption';
4
+ export type { SegmentedControlOptionProps } from './SegmentedControlOption.props';
@@ -46,6 +46,7 @@ export * from './ProgressStepper';
46
46
  export * from './Radio';
47
47
  export * from './RadioCard';
48
48
  export * from './SectionHeader';
49
+ export * from './SegmentedControl';
49
50
  export * from './Select';
50
51
  export * from './Skeleton';
51
52
  export * from './Spinner';