Skip to content

Commit 8f004a2

Browse files
committed
Fix dashSize for morse and train variants
1 parent b5d18e4 commit 8f004a2

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export default App;
114114
Prop | Type | Default | Description
115115
---|---|---|---
116116
`count` | number | | The total number of pages (required)
117-
`current` | number \| Animated.Value | `0` | The current page index can be either a number or an animated value obtained from the scroll position
117+
`current` | number \| Animated.Value | `0` | The current page index can be either a number or an animated value obtained from the scroll position
118118
`variant` | 'morse' \| 'beads' \| 'train' | `morse` | Pre-defined design variant
119119
`vertical` | boolean | `false` | When `true` the indicators will be stacked vertically
120120
`color` | string | `black` | Color of the indicators
@@ -138,7 +138,7 @@ Prop | Type | Default | Description
138138
Prop | Type | Default | Description
139139
---|---|---|---
140140
`size` | number | `6` | Thickness of the indicators
141-
`dashSize` | number | `size * 4` | Length of the indicators, setting it to `0` will stretch the indicators to the available width
141+
`dashSize` | number | `size * 4` | Length of the indicators, cannot be smaller then `size`
142142

143143
## Beads Variant Props
144144

src/PageIndicator.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import React, { useCallback, useEffect, useRef, useState } from 'react';
1+
import React, { useEffect, useRef } from 'react';
22
import {
33
Animated,
44
Easing,
55
EasingFunction,
6-
LayoutChangeEvent,
76
StyleProp,
87
StyleSheet,
98
View,
@@ -61,7 +60,6 @@ export const PageIndicator = ({
6160
const pixelDashSize = evenPixelRound(dashSize ?? pixelSize * 4);
6261
const pixelBorderRadius = clamp(borderRadius ?? pixelSize / 2, 0, pixelSize / 2);
6362

64-
const [trainStroke, setTrainStroke] = useState(pixelDashSize);
6563
const shouldAnimate = typeof current === 'number';
6664
const flexDirection = vertical ? 'column' : 'row';
6765
const animatedValue = useRef(
@@ -79,26 +77,18 @@ export const PageIndicator = ({
7977
}
8078
}, [shouldAnimate, animatedValue, current, count, duration, easing]);
8179

82-
const handleLayout = useCallback(
83-
(e: LayoutChangeEvent) => {
84-
if (variant === Variants.TRAIN && pixelDashSize === 0) {
85-
const { width, height } = e.nativeEvent.layout;
86-
setTrainStroke((vertical ? height : width) / count - gap);
87-
}
88-
},
89-
[variant, pixelDashSize, vertical, count, gap],
90-
);
91-
80+
const trainDashSize = Math.max(pixelDashSize, pixelSize);
81+
const morseDashSize = Math.max(pixelDashSize, pixelSize * 2);
9282
const rootStyle: StyleProp<ViewStyle> = [styles.root, style, { flexDirection }];
9383

9484
if (variant === Variants.MORSE) {
9585
rootStyle.push({
96-
[vertical ? 'height' : 'width']: pixelDashSize + pixelSize * (count - 1) + gap * count,
86+
[vertical ? 'height' : 'width']: morseDashSize + pixelSize * (count - 1) + gap * count,
9787
});
9888
}
9989

10090
return (
101-
<View {...props} style={rootStyle} pointerEvents="none" onLayout={handleLayout}>
91+
<View {...props} style={rootStyle} pointerEvents="none">
10292
{[...Array(count).keys()].map((index) =>
10393
variant === Variants.BEADS ? (
10494
<IndicatorBeads
@@ -123,7 +113,7 @@ export const PageIndicator = ({
123113
index={index}
124114
vertical={vertical}
125115
activeColor={activeColor}
126-
dashSize={trainStroke}
116+
dashSize={trainDashSize}
127117
opacity={clamp(opacity, 0, 1)}
128118
borderRadius={pixelBorderRadius}
129119
animatedValue={animatedValue}
@@ -138,7 +128,7 @@ export const PageIndicator = ({
138128
index={index}
139129
vertical={vertical}
140130
activeColor={activeColor}
141-
dashSize={Math.max(pixelDashSize, pixelSize * 2)}
131+
dashSize={morseDashSize}
142132
opacity={clamp(opacity, 0, 1)}
143133
borderRadius={pixelBorderRadius}
144134
animatedValue={animatedValue}

0 commit comments

Comments
 (0)