Skip to content

Commit 0392261

Browse files
authored
Incubator slider - fix overlapping thumbs (#2502)
* IncubatorSlider - fix thums overlapping * change isPressDefault to isPressed * useGap - fix by adding 'secondary' prop to identify the range thumb * useGap - move to 'true' to align with old implementation
1 parent 825189c commit 0392261

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

demo/src/screens/incubatorScreens/IncubatorSliderScreen.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {renderBooleanOption} from '../ExampleScreenPresenter';
66
const VALUE = 20;
77
const NEGATIVE_VALUE = -30;
88
const MIN = 0;
9-
const MAX = 100;
10-
const INITIAL_MIN = 10;
11-
const INITIAL_MAX = 70;
9+
const MAX = Constants.screenWidth - 40; // horizontal margins 20
10+
const INITIAL_MIN = 30;
11+
const INITIAL_MAX = 270;
1212

1313
const IncubatorSliderScreen = () => {
1414
const [disableRTL, setDisableRTL] = useState<boolean>(false);
@@ -161,7 +161,6 @@ const IncubatorSliderScreen = () => {
161161
<Incubator.Slider
162162
ref={rangeSlider}
163163
useRange
164-
useGap
165164
onRangeChange={onRangeChange}
166165
minimumValue={MIN}
167166
maximumValue={MAX}

src/incubator/Slider/Thumb.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface Props extends ViewProps {
2323
shouldBounceToStep: boolean;
2424
stepInterpolatedValue: SharedValue<number>;
2525
gap?: number;
26+
secondary?: boolean;
2627
}
2728

2829
const SHADOW_RADIUS = 4;
@@ -43,17 +44,18 @@ const Thumb = (props: Props) => {
4344
shouldDisableRTL,
4445
shouldBounceToStep,
4546
stepInterpolatedValue,
46-
gap = 0
47+
gap = 0,
48+
secondary
4749
} = props;
4850

4951
const rtlFix = Constants.isRTL ? -1 : 1;
50-
const isPressedDefault = useSharedValue(false);
52+
const isPressed = useSharedValue(false);
5153
const thumbSize = useSharedValue({width: THUMB_SIZE, height: THUMB_SIZE});
5254
const lastOffset = useSharedValue(0);
5355

5456
const gesture = Gesture.Pan()
5557
.onBegin(() => {
56-
isPressedDefault.value = true;
58+
isPressed.value = true;
5759
lastOffset.value = offset.value;
5860
})
5961
.onUpdate(e => {
@@ -67,28 +69,30 @@ const Thumb = (props: Props) => {
6769
// adjust end edge
6870
newX = end.value;
6971
}
70-
if (newX < end.value - gap && newX > start.value + gap) {
72+
if (!secondary && newX < gap ||
73+
secondary && newX > end.value - gap ||
74+
newX < end.value - gap && newX > start.value + gap) {
7175
offset.value = newX;
7276
}
7377
})
7478
.onEnd(() => {
7579
onSeekEnd?.();
7680
})
7781
.onFinalize(() => {
78-
isPressedDefault.value = false;
82+
isPressed.value = false;
7983
if (shouldBounceToStep) {
8084
offset.value = Math.round(offset.value / stepInterpolatedValue.value) * stepInterpolatedValue.value;
8185
}
8286
});
8387
gesture.enabled(!disabled);
8488

8589
const animatedStyle = useAnimatedStyle(() => {
86-
const customStyle = isPressedDefault.value ? activeStyle?.value : defaultStyle?.value;
90+
const customStyle = isPressed.value ? activeStyle?.value : defaultStyle?.value;
8791
return {
8892
...customStyle,
8993
transform: [
9094
{translateX: (offset.value - thumbSize.value.width / 2) * rtlFix},
91-
{scale: withSpring(!disableActiveStyling && isPressedDefault.value ? 1.3 : 1)}
95+
{scale: withSpring(!disableActiveStyling && isPressed.value ? 1.3 : 1)}
9296
]
9397
};
9498
});

src/incubator/Slider/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const Slider = React.memo((props: Props) => {
5959
thumbHitSlop,
6060
disableActiveStyling,
6161
disabled,
62-
useGap,
62+
useGap = true,
6363
accessible,
6464
testID
6565
} = props;
@@ -214,7 +214,7 @@ const Slider = React.memo((props: Props) => {
214214
if (useRange) {
215215
return {
216216
transform: [{translateX: withTiming(defaultThumbOffset.value * rtlFix, {duration: 10})}],
217-
width: withTiming(rangeThumbOffset.value - defaultThumbOffset.value, {duration: 10})
217+
width: withTiming(Math.abs(rangeThumbOffset.value - defaultThumbOffset.value), {duration: 10})
218218
};
219219
} else {
220220
return {
@@ -232,6 +232,7 @@ const Slider = React.memo((props: Props) => {
232232
end={type === ThumbType.DEFAULT ? rangeThumbOffset : end}
233233
offset={type === ThumbType.DEFAULT ? defaultThumbOffset : rangeThumbOffset}
234234
gap={rangeGap}
235+
secondary={type !== ThumbType.DEFAULT}
235236
onSeekStart={onSeekStart}
236237
onSeekEnd={onSeekEnd}
237238
shouldDisableRTL={shouldDisableRTL}

0 commit comments

Comments
 (0)