Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): invalidate position on ancestor scroll #472

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion example-web/src/Examples.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useState} from 'react';
// @ts-ignore
import {Text, View, StyleSheet} from 'react-native';
import {Text, View, StyleSheet, ScrollView} from 'react-native';
// @ts-ignore
import Slider, {SliderProps} from '@react-native-community/slider';

Expand Down Expand Up @@ -201,4 +201,29 @@ export const examples: Props[] = [
return <SliderExample disabled value={0.6} />;
},
},
{
title: 'Slider in horizontal scroll view',
render() {
return (
<ScrollView
horizontal
style={{
paddingVertical: 50,
borderStyle: 'dotted',
borderWidth: 1,
flexDirection: 'row',
width: 300,
}}
contentContainerStyle={{ overflowX: 'scroll' }}
>
<View style={{ width: 400, paddingLeft: 100 }}>
<SliderExample maximumValue={100} />
<Text style={{ textAlign: 'right', width: 200 }}>
Scroll right, then slide ➔
</Text>
</View>
</ScrollView>
);
},
},
];
36 changes: 27 additions & 9 deletions package/src/RNCSliderNativeComponent.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const RCTSliderWebComponent = React.forwardRef(
const containerSize = React.useRef({width: 0, height: 0});
const containerPositionX = React.useRef(0);
const containerRef = forwardedRef || React.createRef();
const hasBeenResized = React.useRef(false);
const containerPositionInvalidated = React.useRef(false);
const [value, setValue] = React.useState(initialValue || minimumValue);
const lastInitialValue = React.useRef<number>();

Expand Down Expand Up @@ -145,18 +145,36 @@ const RCTSliderWebComponent = React.forwardRef(
}
}, [initialValue, updateValue, animatedValue]);

const onResize = () => {
hasBeenResized.current = true;
};
React.useEffect(() => {
const invalidateContainerPosition = () => {
containerPositionInvalidated.current = true;
};
const onDocumentScroll = (e: Event) => {
if (
// Avoid all other checks if already invalidated
!containerPositionInvalidated.current &&
containerRef.current &&
// @ts-ignore
e.target.contains(containerRef.current)
) {
BartoszKlonowski marked this conversation as resolved.
Show resolved Hide resolved
invalidateContainerPosition();
}
};
//@ts-ignore
window.addEventListener('resize', onResize);
window.addEventListener('resize', invalidateContainerPosition);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@motiz88 you should probably subscribe to resize events the react-native way:

https://necolas.github.io/react-native-web/docs/use-window-dimensions/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jspizziri

  1. This use of the resize event was pre-existing and I had no intention of changing it as part of this bugfix. I merely renamed onResize to invalidateContainerPosition to better communicate what it does (and reuse it in the onScroll handler).
  2. IMO, given this is platform-specific code, use of DOM primitives is completely fine. It's also not directly interchangeable with useWindowDimensions (which would tie us to the React component lifecycle in a way that might not be the most correct/efficient for this use case)

//@ts-ignore
document.addEventListener('scroll', onDocumentScroll, {capture: true});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no way to do this in a more react-native idiomatic way, my opinion would be to do something like the following at the top of the file:

// @ts-ignore
const { addEventListener, removeEventListener } = document;

...
const RCTSliderWebComponent = React.forwardRef(
...
);

That would reduce the overall number of @ts-ignore comments in the code, which aren't ideal.

Perhaps it would be even better to create a unique hook (e.g. useOnDocumentScroll that receives a callback or something), that abstracts this functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would reduce the overall number of @ts-ignore comments in the code, which aren't ideal.

I think the @ts-ignore issue (which, again, is pre-existing here) would be best solved by configuring TypeScript to properly typecheck uses of DOM APIs in .web.tsx files. This might be solved by adding /// <reference lib="dom" /> to the top of the file, but I haven't tried it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jspizziri I've put up #475 to deal with the @ts-ignores comprehensively.


return () => {
//@ts-ignore
window.removeEventListener('resize', onResize);
window.removeEventListener('resize', invalidateContainerPosition);

//@ts-ignore
document.removeEventListener('scroll', onDocumentScroll, {
capture: true,
});
};
}, []);
}, [containerRef]);

const containerStyle = StyleSheet.compose(
{
Expand Down Expand Up @@ -221,8 +239,8 @@ const RCTSliderWebComponent = React.forwardRef(
const getValueFromNativeEvent = (pageX: number) => {
const {width = 1} = containerSize.current;

if (hasBeenResized.current) {
hasBeenResized.current = false;
if (containerPositionInvalidated.current) {
containerPositionInvalidated.current = false;
updateContainerPositionX();
}
const containerX = containerPositionX.current;
Expand Down