Open Torniojaws opened 1 year ago
Hello @Torniojaws!
Thanks for a detailed report. One thing I have in mind before I start analysing this: Could you check if the issue still occur if you set the height
and especially width
to the style
prop of Slider itself as well, instead of trimming it with the container View styles only?
I also had this problem when upgrading from version 3 to version 4. I have a solution that solves this that I will make a PR for.
@Torniojaws, when this happens, could you please try to change the width of the browser. Will it work after that (without reloading)?
@mathias-berg I have the same problem,the slider offset problem can be solved by changing the browser width.
But there is no reason for users to change the browser size. Secondly, our web application will be nested by iframe, so the problem will still not be solved.
@BartoszKlonowski
Not using slider style to change the width and height, and using
@mathias-berg I have the same problem,the slider offset problem can be solved by changing the browser width.
But there is no reason for users to change the browser size. Secondly, our web application will be nested by iframe, so the problem will still not be solved.
Sadly, the fix that I was working didn't fix this. Still had the problem occasionally, will look into another solution.
@BartoszKlonowski
I tried setting a width to <Slider>
itself, but seems it only "resized" the left edge offset area where moving changes the slider:
https://user-images.githubusercontent.com/5442750/212357147-7a0d9c71-a7d1-420a-9ae8-cd13b0a4286b.mov
@mathias-berg Actually yes, when I resize the browser window, it fixes the slider behaviour. Quite interesting.
The problem is because at onLayout
the containerRef
is sometimes undefined
and therefore the position is not saved and defaults to 0. But when changing the size of the window there is a listener that updated the new value and the position is saved correctly.
This is the the part of the code that is causing this since containerRef.current is not a truthy value all the times.
if ((containerRef as RefObject<View>).current) { updateContainerPositionX(); }
I find this bug making this library, unfortunately, useless
UPDATE: upgrading to expo 46->47 has fixed the issue
I'm on SDK 47 and it still exists for me
A workaround I found is to force onLayout
to be called a second time by changing the thumbStyle
prop and then reverting back.
const [tempThumbStyle, setTempThumbStyle] = React.useState<ViewStyle>({
width: 0,
height: 0,
});
React.useEffect(() => {
setTempThumbStyle({ width: undefined, height: undefined });
}, []);
And
<Slider
...
//@ts-ignore Not registered in types
thumbStyle={Platform.OS === "web" ? tempThumbStyle : undefined}
/>
Not pretty, but does the trick for now.
Still broken in latest version of expo (49.0.10)
Still occurring for me on Expo sdk 50.
@YoussefHenna's workaround worked for me.
A workaround I found is to force
onLayout
to be called a second time by changing thethumbStyle
prop and then reverting back.const [tempThumbStyle, setTempThumbStyle] = React.useState<ViewStyle>({ width: 0, height: 0, }); React.useEffect(() => { setTempThumbStyle({ width: undefined, height: undefined }); }, []);
And
<Slider ... //@ts-ignore Not registered in types thumbStyle={Platform.OS === "web" ? tempThumbStyle : undefined} />
Not pretty, but does the trick for now.
If someone looking for a solution and above don't work for you - try to use regular style
prop instead thumbStyle
. Did work for me.
import { useState, useEffect, ComponentProps } from 'react';
import { Platform, ViewStyle } from 'react-native';
import SliderLib from '@react-native-community/slider';
type Props = ComponentProps<typeof SliderLib>;
export const Slider = ({ style, ...props }: Props) => {
// https://github.com/callstack/react-native-slider/issues/470#issuecomment-1777525861
const [tempStyle, setTempStyle] = useState<ViewStyle>({
width: 0,
height: 0,
});
useEffect(() => {
setTempStyle({ width: undefined, height: undefined });
}, []);
return <SliderLib style={[Platform.OS === 'web' ? tempStyle : undefined, style]} {...props} />;
};
We found that this issue was caused by the react-navigation screen transition. The slider renders before the transition is complete, so the math to figure out the slider value based on touch is offset.
Here is our workaround: (Expo / React Navigation)
const navigation = useNavigation()
navigation.addListener('transitionEnd', () => {
if (Platform.OS === 'web') window.dispatchEvent(new Event('resize'))
})
The resize event causes the slider to re-evaluate where touches on the slider are.
Environment
(Possibly) relevant deps from package.json:
Description
I have a Slider component that works perfectly fine on browser simulated mobile phone environment (Chrome -> responsive "iPhone 6/7/8 Plus" preset). But when I use the slider on a bigger screen (Chrome -> responsive "iPad Pro" preset), the active range (where the slider starts moving with mouse drag) is far to the left from where the actual slider element is.
Let's say, the slider element is between width 750px and 900px:
Here's a video. The first part is the "click makes it max value", and then comes the slider offset:
https://user-images.githubusercontent.com/5442750/209342148-200b56a4-8bda-4a1a-a495-d398c9423b03.mov
Reproducible Demo
I've also tried setting a maxWidth for the containing
<View>
but it doesn't change the behaviour.I have also tried using
onSlidingComplete
andonSlidingStart
, but the behaviour is still the same.