Skip to content

Commit

Permalink
Fix Slider web offset issue (#806)
Browse files Browse the repository at this point in the history
* Pass in ref to native slider

* Revert "Pass in ref to native slider"

This reverts commit 9b7c7d2.

* Force `onLayout` called twice

* Add comment and some clean up
  • Loading branch information
YoussefHenna authored Oct 25, 2023
1 parent acb83b8 commit 0c7b439
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/core/src/components/Slider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { View, StyleSheet, StyleProp, ViewStyle } from "react-native";
import { View, StyleSheet, StyleProp, ViewStyle, Platform } from "react-native";
import NativeSlider from "@react-native-community/slider";
import isNumber from "lodash.isnumber";
import toNumber from "lodash.tonumber";
Expand Down Expand Up @@ -72,6 +72,26 @@ function Slider({
value || defaultValue
);

/**
* Web version of the slider relies on some logic running in the onLayout callback using a given React ref (https://github.com/callstack/react-native-slider/blob/main/package/src/RNCSliderNativeComponent.web.tsx#L320)
*
* The issue is that the onLayout callback is called before the ref is initialized, which leads to an improperly initiatilzed variable
* that determines the x position of the slider
*
* Similair issue: https://github.com/callstack/react-native-slider/issues/470
*
* This workaround forces onLayout to be called twice, where the 2nd time around the ref is initialized
* Done by updating the style of a child component which forces re layout
*/
const [tempThumbStyle, setTempThumbStyle] = React.useState<ViewStyle>({
width: 0,
height: 0,
});

React.useEffect(() => {
setTempThumbStyle({ width: undefined, height: undefined });
}, []);

React.useEffect(() => {
if (value != null) {
setInternalValue(value);
Expand Down Expand Up @@ -114,6 +134,8 @@ function Slider({
thumbTintColor={thumbColor}
onSlidingComplete={handleSlidingComplete}
style={styles.slider}
//@ts-ignore Not registered in types
thumbStyle={Platform.OS === "web" ? tempThumbStyle : undefined}
/>
{rightIcon ? (
<Icon color={rightIconThemeColor} name={rightIcon} size={24} />
Expand Down

0 comments on commit 0c7b439

Please sign in to comment.