-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.native.js
184 lines (162 loc) · 4.5 KB
/
index.native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* External dependencies
*/
import {
View,
TouchableWithoutFeedback,
Text,
Platform,
Animated,
Easing,
} from 'react-native';
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { performLayoutAnimation } from '../layout-animation';
import styles from './style.scss';
const ANIMATION_DURATION = 200;
const isIOS = Platform.OS === 'ios';
const Segment = ( { isSelected, title, onPress, onLayout, ...props } ) => {
const isSelectedIOS = isIOS && isSelected;
const segmentStyle = [ styles.segment, isIOS && styles.segmentIOS ];
const textStyle = usePreferredColorSchemeStyle(
styles.buttonTextDefault,
styles.buttonTextDefaultDark
);
const selectedTextStyle = usePreferredColorSchemeStyle(
styles.buttonTextSelected,
styles.buttonTextSelectedDark
);
const shadowStyle = usePreferredColorSchemeStyle( styles.shadowIOS, {} );
return (
<View style={ isSelectedIOS && shadowStyle }>
<TouchableWithoutFeedback onPress={ onPress }>
<View style={ segmentStyle } onLayout={ onLayout } { ...props }>
<Text
style={ [ textStyle, isSelected && selectedTextStyle ] }
maxFontSizeMultiplier={ 2 }
>
{ title }
</Text>
</View>
</TouchableWithoutFeedback>
</View>
);
};
const SegmentedControls = ( {
segments,
segmentHandler,
selectedIndex,
addonLeft,
addonRight,
} ) => {
const selectedSegmentIndex = selectedIndex || 0;
const [ activeSegmentIndex, setActiveSegmentIndex ] =
useState( selectedSegmentIndex );
const [ segmentsDimensions, setSegmentsDimensions ] = useState( {
[ activeSegmentIndex ]: { width: 0, height: 0 },
} );
const [ positionAnimationValue ] = useState( new Animated.Value( 0 ) );
useEffect( () => {
setActiveSegmentIndex( selectedSegmentIndex );
segmentHandler( segments[ selectedSegmentIndex ] );
}, [] );
useEffect( () => {
positionAnimationValue.setValue(
calculateEndValue( activeSegmentIndex )
);
}, [ segmentsDimensions ] );
const containerStyle = usePreferredColorSchemeStyle(
styles.container,
styles.containerDark
);
function performSwatchAnimation( index ) {
Animated.timing( positionAnimationValue, {
toValue: calculateEndValue( index ),
duration: ANIMATION_DURATION,
easing: Easing.ease,
useNativeDriver: false,
} ).start();
}
function calculateEndValue( index ) {
const { paddingLeft: offset } = isIOS
? styles.containerIOS
: styles.container;
const widths = Object.values( segmentsDimensions ).map(
( dimension ) => dimension.width
);
const widthsDistance = widths.slice( 0, index );
const widthsDistanceSum = widthsDistance.reduce(
( sum, n ) => sum + n,
0
);
const endValue = index === 0 ? 0 : widthsDistanceSum;
return endValue + offset;
}
function onHandlePress( segment, index ) {
performLayoutAnimation( ANIMATION_DURATION );
setActiveSegmentIndex( index );
segmentHandler( segment );
performSwatchAnimation( index );
}
function segmentOnLayout( event, index ) {
const { width, height } = event.nativeEvent.layout;
setSegmentsDimensions( {
...segmentsDimensions,
[ index ]: { width, height },
} );
}
const selectedStyle = usePreferredColorSchemeStyle(
styles.selected,
styles.selectedDark
);
const width = segmentsDimensions[ activeSegmentIndex ].width;
const height = segmentsDimensions[ activeSegmentIndex ].height;
const outlineStyle = [ styles.outline, isIOS && styles.outlineIOS ];
return (
<View style={ styles.row }>
<View style={ styles.flex }>{ addonLeft }</View>
<View style={ [ containerStyle, isIOS && styles.containerIOS ] }>
{ segments.map( ( segment, index ) => {
return (
<Segment
title={ segment }
onPress={ () => onHandlePress( segment, index ) }
isSelected={ activeSegmentIndex === index }
key={ index }
onLayout={ ( event ) =>
segmentOnLayout( event, index )
}
accessibilityState={ {
selected: activeSegmentIndex === index,
} }
accessibilityRole={ 'button' }
accessibilityLabel={ segment }
accessibilityHint={ `${ index + 1 } on ${
segments.length
}` }
/>
);
} ) }
<Animated.View
style={ [
{
width,
left: positionAnimationValue,
height,
},
selectedStyle,
outlineStyle,
] }
/>
</View>
<View style={ styles.flex }>{ addonRight }</View>
</View>
);
};
export default SegmentedControls;