-
Notifications
You must be signed in to change notification settings - Fork 282
fix(web): invalidate position on ancestor scroll #472
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>(); | ||
|
||
|
@@ -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) => { | ||
const isAlreadyInvalidated = !containerPositionInvalidated.current; | ||
if ( | ||
isAlreadyInvalidated && | ||
containerRef.current && | ||
// @ts-ignore | ||
e.target.contains(containerRef.current) | ||
) { | ||
invalidateContainerPosition(); | ||
} | ||
}; | ||
//@ts-ignore | ||
window.addEventListener('resize', onResize); | ||
window.addEventListener('resize', invalidateContainerPosition); | ||
//@ts-ignore | ||
document.addEventListener('scroll', onDocumentScroll, {capture: true}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Perhaps it would be even better to create a unique hook (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jspizziri I've put up #475 to deal with the |
||
|
||
return () => { | ||
//@ts-ignore | ||
window.removeEventListener('resize', onResize); | ||
window.removeEventListener('resize', invalidateContainerPosition); | ||
|
||
//@ts-ignore | ||
document.removeEventListener('scroll', onDocumentScroll, { | ||
capture: true, | ||
}); | ||
}; | ||
}, []); | ||
}, [containerRef]); | ||
|
||
const containerStyle = StyleSheet.compose( | ||
{ | ||
|
@@ -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; | ||
|
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jspizziri
resize
event was pre-existing and I had no intention of changing it as part of this bugfix. I merely renamedonResize
toinvalidateContainerPosition
to better communicate what it does (and reuse it in theonScroll
handler).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)