Skip to content

Commit 22c8a8d

Browse files
fix(web): align step markers with thumb position using thumbSize prop (#751)
* fix(web): align step markers with thumb position using thumbSize prop - Pass thumbSize prop to StepsIndicator component - Use actual thumbSize value instead of hardcoded constant - Add WebProps type for proper TypeScript typing - Fix step marker alignment on web platform edges Fixes #743 * chore(web): adjust styling, remove thumbSize prop * chore(docs): reverted README changes * Remove `thumbSize` from props of `StepsIndicator` There's no scenario in which, currently, `StepsIndicator` component would be used with any custom `thumbSize` provided. So for now, let's just remove it and replace it's only call with `constants.THUMB_SIZE` * Fix the `thumbSize` removal from props Remove the removed `thumbSize` from deps of styles memo. * Remove `thumbSize` default from props * Remove optional `thumbSize` from props --------- Co-authored-by: Bartosz Klonowski <[email protected]>
1 parent 25c1d78 commit 22c8a8d

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

example-web/src/Examples.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,19 @@ export const examples: Props[] = [
232232
);
233233
},
234234
},
235+
// Check the fix for the issue #743
236+
{
237+
title: 'With step numbers',
238+
render() {
239+
return (
240+
<SliderExample
241+
minimumValue={1}
242+
maximumValue={5}
243+
step={1}
244+
renderStepNumber={true}
245+
style={[styles.slider, { height: 70 }]}
246+
/>
247+
);
248+
},
249+
},
235250
];

package/src/RNCSliderNativeComponent.web.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from 'react-native';
1212
//@ts-ignore
1313
import type {ImageSource} from 'react-native/Libraries/Image/ImageSource';
14+
import {constants} from './utils/constants';
1415

1516
type Event = Readonly<{
1617
nativeEvent: {
@@ -40,7 +41,6 @@ export interface Props {
4041
inverted: boolean;
4142
disabled: boolean;
4243
trackHeight: number;
43-
thumbSize: number;
4444
thumbImage?: ImageSource;
4545
onRNCSliderSlidingStart: (event: Event) => void;
4646
onRNCSliderSlidingComplete: (event: Event) => void;
@@ -66,7 +66,6 @@ const RCTSliderWebComponent = React.forwardRef(
6666
inverted = false,
6767
disabled = false,
6868
trackHeight = 4,
69-
thumbSize = 20,
7069
thumbImage,
7170
onRNCSliderSlidingStart = (_: Event) => {},
7271
onRNCSliderSlidingComplete = (_: Event) => {},
@@ -234,6 +233,7 @@ const RCTSliderWebComponent = React.forwardRef(
234233
flexGrow: maxPercent,
235234
};
236235

236+
const thumbSize = constants.THUMB_SIZE;
237237
const thumbViewStyle = [
238238
{
239239
width: thumbSize,

package/src/components/StepsIndicator.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {FC, Fragment, useCallback, useMemo} from 'react';
2-
import {View} from 'react-native';
2+
import {Platform, View} from 'react-native';
33
import {StepNumber} from './StepNumber';
44
import {MarkerProps, SliderTrackMark} from './TrackMark';
55
//@ts-ignore
@@ -32,13 +32,35 @@ export const StepsIndicator = ({
3232
: constants.STEP_NUMBER_TEXT_FONT_BIG,
3333
};
3434
}, [options.length]);
35+
36+
const platformDependentStyles = useMemo(() => {
37+
const isWeb = Platform.OS === 'web';
38+
return {
39+
stepIndicatorContainerStyle: isWeb
40+
? styles.stepsIndicator
41+
: {
42+
...styles.stepsIndicator,
43+
marginHorizontal: sliderWidth * constants.MARGIN_HORIZONTAL_PADDING,
44+
},
45+
stepIndicatorElementStyle: isWeb
46+
? {
47+
...styles.stepIndicatorElement,
48+
width: constants.THUMB_SIZE,
49+
justifyContent: 'space-between' as const,
50+
}
51+
: styles.stepIndicatorElement,
52+
};
53+
}, [sliderWidth]);
54+
3555
const values = isLTR ? options.reverse() : options;
3656

3757
const renderStepIndicator = useCallback(
3858
(i: number, index: number) => {
3959
return (
4060
<Fragment key={index}>
41-
<View style={styles.stepIndicatorElement} key={`${index}-View`}>
61+
<View
62+
style={platformDependentStyles.stepIndicatorElementStyle}
63+
key={`${index}-View`}>
4264
<SliderTrackMark
4365
key={`${index}-SliderTrackMark`}
4466
isTrue={currentValue === i}
@@ -67,16 +89,14 @@ export const StepsIndicator = ({
6789
thumbImage,
6890
renderStepNumber,
6991
stepNumberFontStyle,
92+
platformDependentStyles.stepIndicatorElementStyle,
7093
],
7194
);
7295

7396
return (
7497
<View
7598
pointerEvents="none"
76-
style={[
77-
styles.stepsIndicator,
78-
{marginHorizontal: sliderWidth * constants.MARGIN_HORIZONTAL_PADDING},
79-
]}>
99+
style={platformDependentStyles.stepIndicatorContainerStyle}>
80100
{values.map((i, index) => renderStepIndicator(i, index))}
81101
</View>
82102
);

package/src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {Platform} from 'react-native';
33
export const constants = {
44
SLIDER_DEFAULT_INITIAL_VALUE: 0,
55
MARGIN_HORIZONTAL_PADDING: 0.05,
6+
// Default thumb size for web platform (used in step indicator positioning)
7+
THUMB_SIZE: 20,
68
STEP_NUMBER_TEXT_FONT_SMALL: 8,
79
STEP_NUMBER_TEXT_FONT_BIG: 12,
810
LIMIT_MIN_VALUE: Number.MIN_SAFE_INTEGER,

0 commit comments

Comments
 (0)