@hero-design/rn 8.30.3 → 8.30.4

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.
@@ -1,8 +1,8 @@
1
1
  import '@testing-library/jest-native/extend-expect';
2
- import { fireEvent } from '@testing-library/react-native';
3
2
  import React from 'react';
4
- import ActionGroup from '..';
3
+ import { fireEvent } from '@testing-library/react-native';
5
4
  import renderWithTheme from '../../../../testHelpers/renderWithTheme';
5
+ import ActionGroup from '..';
6
6
 
7
7
  describe('ActionGroup', () => {
8
8
  it.each`
@@ -40,16 +40,23 @@ describe('ActionGroup', () => {
40
40
  expect(toJSON()).toMatchSnapshot();
41
41
 
42
42
  expect(getByText('What would you like to create?')).toBeDefined();
43
+ expect(getByText('Shout out')).toBeDefined();
43
44
  expect(getByTestId('speaker-action-item')).toBeDefined();
44
45
  expect(getByTestId('target-action-item')).toBeDefined();
45
46
  expect(getByTestId('plane-action-item')).toBeDefined();
46
47
  expect(getByTestId('health-bag-action-item')).toBeDefined();
47
48
 
48
49
  if (active) {
49
- // verify backdrop appears
50
+ // verify action group appears
51
+ expect(getByTestId('action-group')).toHaveStyle({
52
+ transform: [{ translateX: 0 }],
53
+ });
50
54
  expect(getByTestId('back-drop')).toHaveProp('pointerEvents', 'auto');
51
55
  } else {
52
- // verify backdrop disappears
56
+ // verify action group disappears
57
+ expect(getByTestId('action-group')).toHaveStyle({
58
+ transform: [{ translateX: 400 }],
59
+ });
53
60
  expect(getByTestId('back-drop')).toHaveProp('pointerEvents', 'box-none');
54
61
  }
55
62
  });
@@ -1,29 +1,35 @@
1
- import React, {
2
- forwardRef,
3
- useEffect,
4
- useImperativeHandle,
5
- useRef,
6
- } from 'react';
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { Animated, Easing, Platform, View } from 'react-native';
7
3
  import type { StyleProp, ViewStyle } from 'react-native';
8
- import { Animated, Platform } from 'react-native';
9
- import type { IconName } from '../../Icon';
10
- import type { ActionItemProps } from './ActionItem';
11
4
  import ActionItem from './ActionItem';
12
5
  import {
13
- StyledActionGroupContainer,
14
6
  StyledBackdrop,
15
7
  StyledContainer,
16
8
  StyledFAB,
17
9
  StyledHeaderText,
10
+ StyledActionGroupContainer,
18
11
  } from './StyledActionGroup';
12
+ import type { IconName } from '../../Icon';
13
+ import type { ActionItemProps } from './ActionItem';
19
14
 
20
- import Box from '../../Box';
21
- import { FABHandles } from '../FAB';
22
-
23
- export type ActionGroupHandles = {
24
- showFAB: () => void;
25
- collapseFAB: () => void;
26
- hideFAB: () => void;
15
+ type ActionItemsContainerProps = {
16
+ style?: StyleProp<ViewStyle>;
17
+ items?: Array<ActionItemProps>;
18
+ };
19
+ const ActionItemsListComponent = ({
20
+ style,
21
+ items,
22
+ }: ActionItemsContainerProps) => {
23
+ return (
24
+ <View style={style}>
25
+ {items?.map((itemProp) => (
26
+ <ActionItem
27
+ key={itemProp.key || `${itemProp.icon}_${itemProp.title}`}
28
+ {...itemProp}
29
+ />
30
+ ))}
31
+ </View>
32
+ );
27
33
  };
28
34
 
29
35
  export interface ActionGroupProps {
@@ -60,137 +66,87 @@ export interface ActionGroupProps {
60
66
  /**
61
67
  * Action items of the action group.
62
68
  * */
63
- items?: Array<ActionItemProps>;
64
69
 
70
+ items?: Array<ActionItemProps>;
65
71
  /**
66
72
  * Testing id of the component.
67
73
  */
74
+
68
75
  testID?: string;
69
76
  }
70
-
71
- const ActionGroup = forwardRef<ActionGroupHandles, ActionGroupProps>(
72
- (
77
+ const ActionGroup = ({
78
+ headerTitle,
79
+ onPress,
80
+ active,
81
+ style,
82
+ items,
83
+ testID,
84
+ fabTitle,
85
+ fabIcon = 'add',
86
+ }: ActionGroupProps) => {
87
+ const tranlateXAnimation = useRef<Animated.Value>(
88
+ new Animated.Value(active ? 1 : 0)
89
+ );
90
+ useEffect(() => {
91
+ const animation = Animated.timing(tranlateXAnimation.current, {
92
+ toValue: active ? 1 : 0,
93
+ useNativeDriver: Platform.OS === 'ios' || Platform.OS === 'android',
94
+ easing: Easing.inOut(Easing.cubic),
95
+ });
96
+
97
+ animation.start();
98
+ }, [active]);
99
+
100
+ const interpolatedTranlateXAnimation = tranlateXAnimation.current.interpolate(
73
101
  {
74
- headerTitle,
75
- onPress,
76
- active,
77
- style,
78
- items,
79
- testID,
80
- fabTitle,
81
- fabIcon = 'add',
82
- },
83
- ref
84
- ) => {
85
- const fabRef = useRef<FABHandles>(null);
86
- const tranlateXAnimation = useRef<Animated.Value>(
87
- new Animated.Value(active ? 1 : 0)
88
- );
89
-
90
- const animatedValues = useRef(
91
- items?.map(() => new Animated.Value(0)) || []
92
- ).current;
93
-
94
- const translateYAnimations = animatedValues?.map((value) =>
95
- value.interpolate({
96
- inputRange: [0, 1],
97
- outputRange: active ? [100, 0] : [40, 0],
98
- })
99
- );
100
-
101
- useImperativeHandle(
102
- ref,
103
- () => ({
104
- showFAB: () => fabRef.current?.show(),
105
- collapseFAB: () => fabRef.current?.collapse(),
106
- hideFAB: () => fabRef.current?.hide(),
107
- }),
108
- [fabRef]
109
- );
110
-
111
- useEffect(() => {
112
- Animated.spring(tranlateXAnimation.current, {
113
- toValue: active ? 1 : 0,
114
- useNativeDriver: Platform.OS !== 'web',
115
- }).start();
116
-
117
- Animated.stagger(
118
- 20,
119
- animatedValues
120
- .map((value) =>
121
- Animated.spring(value, {
122
- toValue: active ? 1 : 0,
123
- useNativeDriver: Platform.OS !== 'web',
124
- })
125
- )
126
- .reverse()
127
- ).start();
128
- }, [active, animatedValues]);
129
-
130
- const interpolatedBackdropOpacityAnimation =
131
- tranlateXAnimation.current.interpolate({
132
- inputRange: [0, 1],
133
- outputRange: [0, 0.4],
134
- });
135
-
136
- const interpolatedActionGroupOpacityAnimation =
137
- tranlateXAnimation.current.interpolate({
138
- inputRange: [0, 1],
139
- outputRange: [0, 1],
140
- });
141
-
142
- return (
143
- <StyledContainer testID={testID} pointerEvents="box-none" style={style}>
144
- <StyledBackdrop
145
- pointerEvents={active ? 'auto' : 'box-none'}
146
- testID="back-drop"
147
- style={{ opacity: interpolatedBackdropOpacityAnimation }}
148
- />
149
- <StyledActionGroupContainer
150
- pointerEvents={active ? 'auto' : 'none'}
151
- testID="action-group"
152
- style={{
153
- opacity: interpolatedActionGroupOpacityAnimation,
154
- }}
155
- >
156
- {!!headerTitle && (
157
- <Animated.View
158
- style={{
159
- transform: [{ translateY: translateYAnimations[0] }],
160
- }}
161
- >
162
- <StyledHeaderText testID="header-text">
163
- {headerTitle}
164
- </StyledHeaderText>
165
- </Animated.View>
166
- )}
167
-
168
- <Box style={[style, { paddingBottom: 0 }]}>
169
- {items?.map((itemProp, index) => (
170
- <Animated.View
171
- key={itemProp.key || `${itemProp.icon}_${itemProp.title}`}
172
- style={{
173
- transform: [{ translateY: translateYAnimations[index] }],
174
- }}
175
- >
176
- <ActionItem {...itemProp} />
177
- </Animated.View>
178
- ))}
179
- </Box>
180
- </StyledActionGroupContainer>
181
-
182
- <StyledFAB
183
- testID="fab"
184
- icon={fabIcon}
185
- onPress={onPress}
186
- animated
187
- active={active}
188
- title={fabTitle}
189
- ref={fabRef}
190
- />
191
- </StyledContainer>
192
- );
193
- }
194
- );
102
+ inputRange: [0, 1],
103
+ outputRange: [400, 0],
104
+ }
105
+ );
106
+ const interpolatedBackdropOpacityAnimation =
107
+ tranlateXAnimation.current.interpolate({
108
+ inputRange: [0, 1],
109
+ outputRange: [0, 0.4],
110
+ });
111
+
112
+ const interpolatedActionGroupOpacityAnimation =
113
+ tranlateXAnimation.current.interpolate({
114
+ inputRange: [0, 1],
115
+ outputRange: [0, 1],
116
+ });
117
+
118
+ return (
119
+ <StyledContainer testID={testID} pointerEvents="box-none" style={style}>
120
+ <StyledBackdrop
121
+ pointerEvents={active ? 'auto' : 'box-none'}
122
+ testID="back-drop"
123
+ style={{ opacity: interpolatedBackdropOpacityAnimation }}
124
+ />
125
+ <StyledActionGroupContainer
126
+ pointerEvents={active ? 'auto' : 'none'}
127
+ testID="action-group"
128
+ style={{
129
+ opacity: interpolatedActionGroupOpacityAnimation,
130
+ transform: [{ translateX: interpolatedTranlateXAnimation }],
131
+ }}
132
+ >
133
+ {!!headerTitle && (
134
+ <StyledHeaderText testID="header-text">
135
+ {headerTitle}
136
+ </StyledHeaderText>
137
+ )}
138
+ <ActionItemsListComponent items={items} />
139
+ </StyledActionGroupContainer>
140
+ <StyledFAB
141
+ testID="fab"
142
+ icon={fabIcon}
143
+ onPress={onPress}
144
+ animated
145
+ active={active}
146
+ title={fabTitle}
147
+ />
148
+ </StyledContainer>
149
+ );
150
+ };
195
151
 
196
152
  export default ActionGroup;
@@ -1,7 +1,7 @@
1
1
  import React, { useEffect, useRef } from 'react';
2
- import { Animated, Platform, StyleSheet } from 'react-native';
3
- import type { IconProps } from '../Icon';
2
+ import { Animated, Easing, Platform, StyleSheet } from 'react-native';
4
3
  import { StyledFABIcon } from './StyledFAB';
4
+ import type { IconProps } from '../Icon';
5
5
 
6
6
  const AnimatedIcons = Animated.createAnimatedComponent(StyledFABIcon);
7
7
 
@@ -14,9 +14,11 @@ const AnimatedFABIcon = ({ active, ...iconProps }: Props) => {
14
14
  new Animated.Value(active ? 1 : 0)
15
15
  );
16
16
  useEffect(() => {
17
- const animation = Animated.spring(rotateAnimation.current, {
17
+ const animation = Animated.timing(rotateAnimation.current, {
18
18
  toValue: active ? 1 : 0,
19
19
  useNativeDriver: Platform.OS === 'ios' || Platform.OS === 'android',
20
+ easing: Easing.inOut(Easing.ease),
21
+ duration: 300,
20
22
  });
21
23
 
22
24
  animation.start();
@@ -1,13 +1,5 @@
1
- import React, { forwardRef, useEffect, useImperativeHandle } from 'react';
2
- import {
3
- LayoutAnimation,
4
- LayoutAnimationConfig,
5
- Platform,
6
- StyleProp,
7
- StyleSheet,
8
- UIManager,
9
- ViewStyle,
10
- } from 'react-native';
1
+ import React from 'react';
2
+ import type { StyleProp, ViewStyle } from 'react-native';
11
3
  import { useTheme } from '../../theme';
12
4
  import type { IconName } from '../Icon';
13
5
  import { AnimatedFABIcon } from './AnimatedFABIcon';
@@ -18,18 +10,6 @@ import {
18
10
  StyledIconContainer,
19
11
  } from './StyledFAB';
20
12
 
21
- if (Platform.OS === 'android') {
22
- if (UIManager.setLayoutAnimationEnabledExperimental) {
23
- UIManager.setLayoutAnimationEnabledExperimental(true);
24
- }
25
- }
26
-
27
- export type FABHandles = {
28
- show: () => void;
29
- collapse: () => void;
30
- hide: () => void;
31
- };
32
-
33
13
  export interface FABProps {
34
14
  /**
35
15
  * Name of the Icon.
@@ -74,6 +54,7 @@ const IconOnlyContent = ({
74
54
  }: {
75
55
  icon: IconName;
76
56
  animated?: boolean;
57
+
77
58
  active?: boolean;
78
59
  }) => {
79
60
  if (animated) {
@@ -104,96 +85,31 @@ const IconWithTextContent = ({
104
85
  <StyledFABText>{title}</StyledFABText>
105
86
  </>
106
87
  );
107
-
108
- const defaultAnimation: LayoutAnimationConfig = {
109
- create: {
110
- type: 'easeInEaseOut',
111
- property: 'opacity',
112
- },
113
- update: { type: 'spring', springDamping: 1 },
114
- duration: 400,
88
+ const FAB = ({
89
+ onPress,
90
+ title,
91
+ icon,
92
+ animated,
93
+ testID,
94
+ active,
95
+ style,
96
+ }: FABProps): JSX.Element => {
97
+ const isIconOnly = !title;
98
+ const theme = useTheme();
99
+ return (
100
+ <StyledFAB
101
+ underlayColor={theme.__hd__.fab.colors.buttonPressedBackground}
102
+ onPress={onPress}
103
+ style={style}
104
+ testID={testID}
105
+ >
106
+ {isIconOnly ? (
107
+ <IconOnlyContent animated={animated} active={active} icon={icon} />
108
+ ) : (
109
+ <IconWithTextContent icon={icon} title={title} />
110
+ )}
111
+ </StyledFAB>
112
+ );
115
113
  };
116
114
 
117
- const FAB = forwardRef<FABHandles, FABProps>(
118
- ({ onPress, title, icon, animated, testID, active, style }, ref) => {
119
- const theme = useTheme();
120
-
121
- const [displayState, setDisplayState] = React.useState({
122
- hideTitle: false,
123
- hideButton: false,
124
- });
125
-
126
- const [canAnimate, setCanAnimate] = React.useState(false);
127
-
128
- const isIconOnly = displayState.hideTitle || active || !title;
129
-
130
- useImperativeHandle(
131
- ref,
132
- () => ({
133
- show: () => {
134
- setDisplayState({
135
- hideButton: false,
136
- hideTitle: false,
137
- });
138
- },
139
- collapse: () => {
140
- setDisplayState({
141
- hideButton: false,
142
- hideTitle: true,
143
- });
144
- },
145
- hide: () => {
146
- setDisplayState((previousState) => ({
147
- ...previousState,
148
- hideButton: true,
149
- }));
150
- },
151
- }),
152
- [displayState]
153
- );
154
-
155
- useEffect(() => {
156
- if (canAnimate) {
157
- LayoutAnimation.configureNext(defaultAnimation);
158
- }
159
- }, [isIconOnly]);
160
-
161
- useEffect(() => {
162
- if (canAnimate) {
163
- LayoutAnimation.configureNext(defaultAnimation);
164
- }
165
- }, [displayState.hideButton]);
166
-
167
- const marginBottom = Number(StyleSheet.flatten(style)?.marginBottom) || 0;
168
-
169
- return (
170
- <StyledFAB
171
- /** Add a small timeout before executing animation to prevent flakiness on android */
172
- onLayout={() => setTimeout(() => setCanAnimate(true), 200)}
173
- underlayColor={theme.__hd__.fab.colors.buttonPressedBackground}
174
- onPress={onPress}
175
- style={[
176
- style,
177
- {
178
- bottom: displayState.hideButton
179
- ? -(marginBottom + theme.__hd__.fab.sizes.height * 2)
180
- : StyleSheet.flatten(style)?.bottom,
181
- },
182
- ]}
183
- testID={testID}
184
- >
185
- {isIconOnly ? (
186
- <IconOnlyContent
187
- animated={animated}
188
- active={active}
189
- icon={active ? 'add' : icon}
190
- />
191
- ) : (
192
- <IconWithTextContent icon={icon} title={title} />
193
- )}
194
- </StyledFAB>
195
- );
196
- }
197
- );
198
-
199
115
  export default FAB;
@@ -5,7 +5,6 @@ exports[`FAB when animated is false renders StyledFABIcon 1`] = `
5
5
  accessible={true}
6
6
  focusable={false}
7
7
  onClick={[Function]}
8
- onLayout={[Function]}
9
8
  onResponderGrant={[Function]}
10
9
  onResponderMove={[Function]}
11
10
  onResponderRelease={[Function]}
@@ -31,14 +30,9 @@ exports[`FAB when animated is false renders StyledFABIcon 1`] = `
31
30
  "shadowOpacity": 0.12,
32
31
  "shadowRadius": 8,
33
32
  },
34
- Array [
35
- Object {
36
- "backgroundColor": "#001f23",
37
- },
38
- Object {
39
- "bottom": undefined,
40
- },
41
- ],
33
+ Object {
34
+ "backgroundColor": "#001f23",
35
+ },
42
36
  ]
43
37
  }
44
38
  >
@@ -73,7 +67,6 @@ exports[`FAB when animated is true renders animatedFABIcon 1`] = `
73
67
  accessible={true}
74
68
  focusable={false}
75
69
  onClick={[Function]}
76
- onLayout={[Function]}
77
70
  onResponderGrant={[Function]}
78
71
  onResponderMove={[Function]}
79
72
  onResponderRelease={[Function]}
@@ -99,14 +92,9 @@ exports[`FAB when animated is true renders animatedFABIcon 1`] = `
99
92
  "shadowOpacity": 0.12,
100
93
  "shadowRadius": 8,
101
94
  },
102
- Array [
103
- Object {
104
- "backgroundColor": "#001f23",
105
- },
106
- Object {
107
- "bottom": undefined,
108
- },
109
- ],
95
+ Object {
96
+ "backgroundColor": "#001f23",
97
+ },
110
98
  ]
111
99
  }
112
100
  >
@@ -171,7 +159,6 @@ exports[`FAB when title has value renders correctly 1`] = `
171
159
  accessible={true}
172
160
  focusable={false}
173
161
  onClick={[Function]}
174
- onLayout={[Function]}
175
162
  onResponderGrant={[Function]}
176
163
  onResponderMove={[Function]}
177
164
  onResponderRelease={[Function]}
@@ -197,14 +184,9 @@ exports[`FAB when title has value renders correctly 1`] = `
197
184
  "shadowOpacity": 0.12,
198
185
  "shadowRadius": 8,
199
186
  },
200
- Array [
201
- Object {
202
- "backgroundColor": "#001f23",
203
- },
204
- Object {
205
- "bottom": undefined,
206
- },
207
- ],
187
+ Object {
188
+ "backgroundColor": "#001f23",
189
+ },
208
190
  ]
209
191
  }
210
192
  >
@@ -1,7 +1,7 @@
1
- import React, { createRef } from 'react';
1
+ import React from 'react';
2
2
  import { fireEvent } from '@testing-library/react-native';
3
3
  import renderWithTheme from '../../../testHelpers/renderWithTheme';
4
- import { FABHandles, theme } from '../../../index';
4
+ import { theme } from '../../../index';
5
5
  import FAB from '..';
6
6
 
7
7
  describe('FAB', () => {
@@ -66,24 +66,4 @@ describe('FAB', () => {
66
66
  expect(onPressSpy).toBeCalledTimes(1);
67
67
  });
68
68
  });
69
-
70
- describe('usage with ref', () => {
71
- it('allow controlling inner fab states by ref', () => {
72
- const fabRef = createRef<FABHandles>();
73
-
74
- const { queryByText } = renderWithTheme(
75
- <FAB ref={fabRef} title="Shout out" icon="speaker" />
76
- );
77
-
78
- expect(queryByText('Shout out')).toBeDefined();
79
- fabRef.current!.collapse();
80
- expect(queryByText('Shout out')).toBeFalsy();
81
-
82
- fabRef.current!.show();
83
- expect(queryByText('Shout out')).toBeDefined();
84
-
85
- fabRef.current!.hide();
86
- expect(queryByText('Shout out')).toBeDefined();
87
- });
88
- });
89
69
  });