Skip to content

Commit aae3124

Browse files
authored
Merge pull request #2 from kolking/active-color
Active color
2 parents a6f9f83 + 34abb41 commit aae3124

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Prop | Type | Default | Description
107107
`count` | number | | The total number of pages (required)
108108
`current` | number | `0` | The current page index
109109
`color` | string | `black` | Color of the indicators
110+
`activeColor` | string | | Optional color of the active indicator
110111
`size` | number | `6` | Size of the indicators
111112
`gap` | number | `6` | Distance between the indicators
112113
`opacity` | number | `0.6` | Opacity of inactive indicators

example/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const App = () => {
4040
const { width, height } = useWindowDimensions();
4141
const [current, setCurrent] = useState(0);
4242
const scrollX = useRef(new Animated.Value(0)).current;
43+
const animatedCurrent = useRef(Animated.divide(scrollX, width)).current;
4344

4445
const handleScrollEnd = useCallback(
4546
(e: NativeSyntheticEvent<NativeScrollEvent>) => {
@@ -75,7 +76,7 @@ const App = () => {
7576
style={styles.pageIndicator}
7677
count={pages.length}
7778
//current={current}
78-
animatedCurrent={Animated.divide(scrollX, width)}
79+
animatedCurrent={animatedCurrent}
7980
color="white"
8081
/>
8182
</View>

src/Indicator.tsx

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface IndicatorProps extends ViewProps {
1010
stroke: number;
1111
opacity: number;
1212
vertical: boolean;
13+
activeColor: string;
1314
borderRadius: number;
1415
animatedValue: Animated.Value | Animated.AnimatedInterpolation;
1516
}
@@ -23,10 +24,12 @@ const Indicator = ({
2324
stroke,
2425
opacity,
2526
vertical,
27+
activeColor,
2628
borderRadius,
2729
animatedValue,
2830
}: IndicatorProps) => {
2931
const offset = (stroke - size) / 2;
32+
const overlay = activeColor !== color;
3033

3134
const wrapperTranslate = animatedValue.interpolate({
3235
inputRange: [index - 1, index, index + 1],
@@ -43,12 +46,22 @@ const Indicator = ({
4346
[vertical ? 'marginVertical' : 'marginHorizontal']: gap / 2,
4447
opacity: animatedValue.interpolate({
4548
inputRange: [index - 1, index, index + 1],
46-
outputRange: [opacity, 1, opacity],
49+
outputRange: [opacity, overlay ? 0 : 1, opacity],
4750
extrapolate: 'clamp',
4851
}),
4952
transform: [vertical ? { translateY: wrapperTranslate } : { translateX: wrapperTranslate }],
5053
};
5154

55+
const overlayStyles: Animated.AnimatedProps<ViewStyle> = {
56+
...wrapperStyles,
57+
...StyleSheet.absoluteFillObject,
58+
opacity: animatedValue.interpolate({
59+
inputRange: [index - 1, index, index + 1],
60+
outputRange: [0, 1, 0],
61+
extrapolate: 'clamp',
62+
}),
63+
};
64+
5265
const commonStyle: Animated.AnimatedProps<ViewStyle> = {
5366
width: vertical ? size : stroke / 2,
5467
height: vertical ? stroke / 2 : size,
@@ -82,14 +95,26 @@ const Indicator = ({
8295
};
8396

8497
return (
85-
<Animated.View style={wrapperStyles}>
86-
<View style={styles.mask}>
87-
<Animated.View style={leftStyles} />
88-
</View>
89-
<View style={styles.mask}>
90-
<Animated.View style={rightStyles} />
91-
</View>
92-
</Animated.View>
98+
<View>
99+
<Animated.View style={wrapperStyles}>
100+
<View style={styles.mask}>
101+
<Animated.View style={leftStyles} />
102+
</View>
103+
<View style={styles.mask}>
104+
<Animated.View style={rightStyles} />
105+
</View>
106+
</Animated.View>
107+
{overlay && (
108+
<Animated.View style={overlayStyles}>
109+
<View style={styles.mask}>
110+
<Animated.View style={[leftStyles, { backgroundColor: activeColor }]} />
111+
</View>
112+
<View style={styles.mask}>
113+
<Animated.View style={[rightStyles, { backgroundColor: activeColor }]} />
114+
</View>
115+
</Animated.View>
116+
)}
117+
</View>
93118
);
94119
};
95120

src/PageIndicator.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface PageIndicatorProps extends ViewProps {
1414
opacity?: number;
1515
duration?: number;
1616
vertical?: boolean;
17+
activeColor?: string;
1718
borderRadius?: number;
1819
easing?: EasingFunction;
1920
animatedCurrent?: Animated.Value | Animated.AnimatedInterpolation;
@@ -29,6 +30,7 @@ export const PageIndicator = ({
2930
opacity = 0.6,
3031
duration = 500,
3132
vertical = false,
33+
activeColor = color,
3234
borderRadius = size / 2,
3335
easing = Easing.out(Easing.cubic),
3436
animatedCurrent,
@@ -60,6 +62,7 @@ export const PageIndicator = ({
6062
count={count}
6163
index={index}
6264
vertical={vertical}
65+
activeColor={activeColor}
6366
stroke={Math.max(stroke, size * 2)}
6467
opacity={clamp(opacity, 0, 1)}
6568
borderRadius={clamp(borderRadius, 0, size / 2)}

0 commit comments

Comments
 (0)