Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ import { Animated, StyleSheet, Text, View, useWindowDimensions } from 'react-nat
import { PageIndicator } from 'react-native-page-indicator';

const pages = ['Page 1', 'Page 2', 'Page 3'];
const rtl = false;

const App = () => {
const { width, height } = useWindowDimensions();
const scrollX = useRef(new Animated.Value(0)).current;
const scrollX = useRef(new Animated.Value(rtl ? width * (pages.length - 1) : 0)).current;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, this helped me save a lot of time. I just used bun patch to generate a patch for my app.

I also needed to change the default value depending on iOS/Android. For some reason on iOS the slider still works from LTR while the ScrollView works RTL, so I used 0 for iOS and windowWidth * (pageLengt-1) for Android.

Do you know how to make the indicator work RTL on iOS as well?

const animatedCurrent = useRef(Animated.divide(scrollX, width)).current;

return (
Expand Down Expand Up @@ -117,6 +118,7 @@ Prop | Type | Default | Description
`current` | number \| Animated.Value | `0` | The current page index can be either a number or an animated value obtained from the scroll position
`variant` | 'morse' \| 'beads' \| 'train' | `morse` | Pre-defined design variant
`vertical` | boolean | `false` | When `true` the indicators will be stacked vertically
`rtl` | boolean | `false` | When `true` right to left support will be enabled
`color` | string | `black` | Color of the indicators
`activeColor` | string | | Optional color of the active indicator
`gap` | number | `6` | Distance between the indicators
Expand Down
12 changes: 7 additions & 5 deletions src/IndicatorMorse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface IndicatorProps extends ViewProps {
opacity: number;
dashSize: number;
vertical: boolean;
rtl: boolean;
activeColor: string;
borderRadius: number;
animatedValue: Animated.Value | ReturnType<Animated.Value['interpolate']>;
Expand All @@ -24,6 +25,7 @@ const Indicator = ({
opacity,
dashSize,
vertical,
rtl,
activeColor,
borderRadius,
animatedValue,
Expand All @@ -46,7 +48,7 @@ const Indicator = ({
});

const wrapperStyles: Animated.AnimatedProps<ViewStyle> = {
flexDirection: vertical ? 'column' : 'row',
flexDirection: vertical ? 'column' : rtl ? 'row-reverse' :'row',
opacity: animatedValue.interpolate({
inputRange: [index - 1, index, index + 1],
outputRange: [opacity, overlay ? 0 : 1, opacity],
Expand Down Expand Up @@ -85,15 +87,15 @@ const Indicator = ({

const leftStyles: Animated.AnimatedProps<ViewStyle> = {
...commonStyle,
borderTopLeftRadius: borderRadius,
[vertical ? 'borderTopRightRadius' : 'borderBottomLeftRadius']: borderRadius,
[rtl ? 'borderTopRightRadius' : 'borderTopLeftRadius']: borderRadius,
[vertical ? 'borderTopRightRadius' : rtl ? 'borderBottomRightRadius' : 'borderBottomLeftRadius']: borderRadius,
transform: [vertical ? { translateY: translateLeft } : { translateX: translateLeft }],
};

const rightStyles: Animated.AnimatedProps<ViewStyle> = {
...commonStyle,
borderBottomRightRadius: borderRadius,
[vertical ? 'borderBottomLeftRadius' : 'borderTopRightRadius']: borderRadius,
[rtl ? 'borderBottomLeftRadius' : 'borderBottomRightRadius']: borderRadius,
[vertical ? 'borderBottomLeftRadius' : rtl ? 'borderTopLeftRadius' : 'borderTopRightRadius']: borderRadius,
transform: [vertical ? { translateY: translateRight } : { translateX: translateRight }],
};

Expand Down
7 changes: 6 additions & 1 deletion src/PageIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface PageIndicatorProps extends ViewProps {
dashSize?: number;
duration?: number;
vertical?: boolean;
rtl?: boolean;
activeColor?: string;
borderRadius?: number;
easing?: EasingFunction;
Expand All @@ -50,6 +51,7 @@ export const PageIndicator = ({
dashSize,
duration = 500,
vertical = false,
rtl = false,
activeColor = color,
borderRadius,
easing = Easing.out(Easing.cubic),
Expand All @@ -61,7 +63,7 @@ export const PageIndicator = ({
const pixelBorderRadius = clamp(borderRadius ?? pixelSize / 2, 0, pixelSize / 2);

const shouldAnimate = typeof current === 'number';
const flexDirection = vertical ? 'column' : 'row';
const flexDirection = vertical ? 'column' : rtl ? 'row-reverse' : 'row';
const animatedValue = useRef(
shouldAnimate ? new Animated.Value(clamp(current, 0, count)) : current,
).current;
Expand Down Expand Up @@ -99,6 +101,7 @@ export const PageIndicator = ({
index={index}
scale={scale}
vertical={vertical}
rtl={rtl}
activeColor={activeColor}
opacity={clamp(opacity, 0, 1)}
borderRadius={pixelBorderRadius}
Expand All @@ -112,6 +115,7 @@ export const PageIndicator = ({
color={color}
index={index}
vertical={vertical}
rtl={rtl}
activeColor={activeColor}
dashSize={trainDashSize}
opacity={clamp(opacity, 0, 1)}
Expand All @@ -127,6 +131,7 @@ export const PageIndicator = ({
count={count}
index={index}
vertical={vertical}
rtl={rtl}
activeColor={activeColor}
dashSize={morseDashSize}
opacity={clamp(opacity, 0, 1)}
Expand Down