|
1 | 1 | import * as React from "react"; |
2 | | -import { View, StyleSheet, StyleProp, ViewStyle } from "react-native"; |
| 2 | +import { View, StyleSheet, StyleProp, ViewStyle, Platform } from "react-native"; |
3 | 3 | import NativeSlider from "@react-native-community/slider"; |
4 | 4 | import isNumber from "lodash.isnumber"; |
5 | 5 | import toNumber from "lodash.tonumber"; |
@@ -72,6 +72,26 @@ function Slider({ |
72 | 72 | value || defaultValue |
73 | 73 | ); |
74 | 74 |
|
| 75 | + /** |
| 76 | + * 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) |
| 77 | + * |
| 78 | + * The issue is that the onLayout callback is called before the ref is initialized, which leads to an improperly initiatilzed variable |
| 79 | + * that determines the x position of the slider |
| 80 | + * |
| 81 | + * Similair issue: https://github.com/callstack/react-native-slider/issues/470 |
| 82 | + * |
| 83 | + * This workaround forces onLayout to be called twice, where the 2nd time around the ref is initialized |
| 84 | + * Done by updating the style of a child component which forces re layout |
| 85 | + */ |
| 86 | + const [tempThumbStyle, setTempThumbStyle] = React.useState<ViewStyle>({ |
| 87 | + width: 0, |
| 88 | + height: 0, |
| 89 | + }); |
| 90 | + |
| 91 | + React.useEffect(() => { |
| 92 | + setTempThumbStyle({ width: undefined, height: undefined }); |
| 93 | + }, []); |
| 94 | + |
75 | 95 | React.useEffect(() => { |
76 | 96 | if (value != null) { |
77 | 97 | setInternalValue(value); |
@@ -114,6 +134,8 @@ function Slider({ |
114 | 134 | thumbTintColor={thumbColor} |
115 | 135 | onSlidingComplete={handleSlidingComplete} |
116 | 136 | style={styles.slider} |
| 137 | + //@ts-ignore Not registered in types |
| 138 | + thumbStyle={Platform.OS === "web" ? tempThumbStyle : undefined} |
117 | 139 | /> |
118 | 140 | {rightIcon ? ( |
119 | 141 | <Icon color={rightIconThemeColor} name={rightIcon} size={24} /> |
|
0 commit comments