Skip to content

Commit 3f20f4a

Browse files
committed
Fix the case when a gap appears in the morse variant
1 parent 9f448ea commit 3f20f4a

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/PageIndicator.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ViewProps,
1010
} from 'react-native';
1111

12-
import { clamp } from './helpers';
12+
import { clamp, evenPixelRound } from './helpers';
1313
import IndicatorMorse from './IndicatorMorse';
1414
import IndicatorBeads from './IndicatorBeads';
1515
import IndicatorTrain from './IndicatorTrain';
@@ -46,16 +46,20 @@ export const PageIndicator = ({
4646
color = 'black',
4747
scale = 1.5,
4848
opacity = 0.5,
49-
dashSize = size * 4,
49+
dashSize,
5050
duration = 500,
5151
vertical = false,
5252
activeColor = color,
53-
borderRadius = size / 2,
53+
borderRadius,
5454
easing = Easing.out(Easing.cubic),
5555
style,
5656
...props
5757
}: PageIndicatorProps) => {
58-
const [trainStroke, setTrainStroke] = useState(dashSize);
58+
const pixelSize = evenPixelRound(size);
59+
const pixelDashSize = evenPixelRound(dashSize ?? pixelSize * 4);
60+
const pixelBorderRadius = clamp(borderRadius ?? pixelSize / 2, 0, pixelSize / 2);
61+
62+
const [trainStroke, setTrainStroke] = useState(pixelDashSize);
5963
const shouldAnimate = typeof current === 'number';
6064
const flexDirection = vertical ? 'column' : 'row';
6165
const animatedValue = useRef(
@@ -75,12 +79,12 @@ export const PageIndicator = ({
7579

7680
const handleLayout = useCallback(
7781
(e: LayoutChangeEvent) => {
78-
if (variant === Variants.TRAIN && dashSize === 0) {
82+
if (variant === Variants.TRAIN && pixelDashSize === 0) {
7983
const { width, height } = e.nativeEvent.layout;
8084
setTrainStroke((vertical ? height : width) / count - gap);
8185
}
8286
},
83-
[variant, dashSize, vertical, count, gap],
87+
[variant, pixelDashSize, vertical, count, gap],
8488
);
8589

8690
return (
@@ -95,14 +99,14 @@ export const PageIndicator = ({
9599
<IndicatorBeads
96100
key={index}
97101
gap={gap}
98-
size={size}
102+
size={pixelSize}
99103
color={color}
100104
index={index}
101105
scale={scale}
102106
vertical={vertical}
103107
activeColor={activeColor}
104108
opacity={clamp(opacity, 0, 1)}
105-
borderRadius={clamp(borderRadius, 0, size / 2)}
109+
borderRadius={pixelBorderRadius}
106110
animatedValue={animatedValue}
107111
/>
108112
) : variant === Variants.TRAIN ? (
@@ -116,22 +120,22 @@ export const PageIndicator = ({
116120
activeColor={activeColor}
117121
dashSize={trainStroke}
118122
opacity={clamp(opacity, 0, 1)}
119-
borderRadius={clamp(borderRadius, 0, size / 2)}
123+
borderRadius={pixelBorderRadius}
120124
animatedValue={animatedValue}
121125
/>
122126
) : (
123127
<IndicatorMorse
124128
key={index}
125129
gap={gap}
126-
size={size}
130+
size={pixelSize}
127131
color={color}
128132
count={count}
129133
index={index}
130134
vertical={vertical}
131135
activeColor={activeColor}
132-
dashSize={Math.max(dashSize, size * 2)}
136+
dashSize={Math.max(pixelDashSize, pixelSize * 2)}
133137
opacity={clamp(opacity, 0, 1)}
134-
borderRadius={clamp(borderRadius, 0, size / 2)}
138+
borderRadius={pixelBorderRadius}
135139
animatedValue={animatedValue}
136140
/>
137141
),

src/helpers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
export function clamp(value: number, min: number, max: number): number {
1+
import { PixelRatio } from 'react-native';
2+
3+
export function clamp(value: number, min: number, max: number) {
24
return Math.max(min, Math.min(value, max));
35
}
6+
7+
export function evenPixelRound(value: number) {
8+
return PixelRatio.roundToNearestPixel(value / 2) * 2;
9+
}

0 commit comments

Comments
 (0)