Skip to content

Commit 8dadfab

Browse files
authored
Merge pull request #13 from richardvclam/feature/fade-divider
Feature/fade divider
2 parents bbed335 + 943a1b2 commit 8dadfab

File tree

5 files changed

+77
-29
lines changed

5 files changed

+77
-29
lines changed

src/Divider/Divider.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from 'react';
2+
import { View } from 'react-native';
3+
import Animated, { Easing } from 'react-native-reanimated';
4+
import { timing } from 'react-native-redash';
5+
6+
import styles from './DividerStyles';
7+
8+
export interface DividerProps {
9+
hide?: boolean;
10+
}
11+
12+
function _Divider({ hide = false }: DividerProps): JSX.Element {
13+
const opacity = React.useRef(new Animated.Value<number>(hide ? 0 : 1));
14+
15+
Animated.useCode(() => {
16+
return Animated.set(
17+
opacity.current,
18+
timing({
19+
from: hide ? 0 : 1,
20+
to: hide ? 1 : 0,
21+
easing: Easing.linear,
22+
duration: 200,
23+
}),
24+
);
25+
}, [hide]);
26+
27+
return (
28+
<Animated.View
29+
style={[styles.dividerContainer, { opacity: opacity.current }]}
30+
>
31+
<View style={styles.divider} />
32+
</Animated.View>
33+
);
34+
}
35+
36+
export const Divider = React.memo(_Divider);

src/Divider/DividerStyles.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { StyleSheet } from 'react-native';
2+
3+
const styles = StyleSheet.create({
4+
dividerContainer: {
5+
paddingTop: 7,
6+
paddingBottom: 7,
7+
zIndex: 0,
8+
},
9+
divider: {
10+
height: '100%',
11+
width: 1,
12+
borderWidth: 0,
13+
backgroundColor: 'rgba(120, 120, 120, 0.2)',
14+
},
15+
});
16+
17+
export default styles;

src/Divider/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Divider';

src/SegmentedControl/SegmentedControl.tsx

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import Animated, { Easing } from 'react-native-reanimated';
88
import { timing } from 'react-native-redash';
99

10+
import { Divider } from '../Divider';
1011
import { Segment, SegmentProps } from '../Segment';
1112
import { SegmentedContext } from '../SegmentedContext';
1213
import { clamp } from '../utils';
@@ -133,6 +134,8 @@ export const SegmentedControl = ({
133134
handleChangeValue(name);
134135
};
135136

137+
const currentIndex = _map?.[_activeName || ''] ?? -1;
138+
136139
return (
137140
<SegmentedContext.Provider
138141
value={{
@@ -169,24 +172,26 @@ export const SegmentedControl = ({
169172
/>
170173
)}
171174

172-
{values.map((child, index) => (
173-
<React.Fragment key={child.props.name}>
174-
{index > 0 && (
175-
<View style={styles.dividerContainer}>
176-
<View style={styles.divider} />
177-
</View>
178-
)}
179-
{{
180-
...child,
181-
props: {
182-
disabled,
183-
inactiveTintColor,
184-
activeTintColor,
185-
...child.props,
186-
},
187-
}}
188-
</React.Fragment>
189-
))}
175+
{values.map((child, index) => {
176+
return (
177+
<React.Fragment key={child.props.name}>
178+
{index > 0 && (
179+
<Divider
180+
hide={currentIndex !== index && currentIndex !== index - 1}
181+
/>
182+
)}
183+
{{
184+
...child,
185+
props: {
186+
disabled,
187+
inactiveTintColor,
188+
activeTintColor,
189+
...child.props,
190+
},
191+
}}
192+
</React.Fragment>
193+
);
194+
})}
190195
</View>
191196
</PanGestureHandler>
192197
</SegmentedContext.Provider>

src/SegmentedControl/SegmentedControlStyles.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ const styles = StyleSheet.create({
2828
shadowOpacity: 0.5,
2929
shadowRadius: 2,
3030
},
31-
dividerContainer: {
32-
paddingTop: 7,
33-
paddingBottom: 7,
34-
zIndex: 0,
35-
},
36-
divider: {
37-
height: '100%',
38-
width: 1,
39-
borderWidth: 0,
40-
backgroundColor: 'rgba(120, 120, 120, 0.2)',
41-
},
4231
});
4332

4433
export default styles;

0 commit comments

Comments
 (0)