Created
November 16, 2024 01:12
-
-
Save Vashiru/28ac8b9e565ad22e656774a2c1ec5fe9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/node_modules/react-native-intersection-observer/lib/withIO.js b/node_modules/react-native-intersection-observer/lib/withIO.js | |
index b957799..6e4e1b4 100644 | |
--- a/node_modules/react-native-intersection-observer/lib/withIO.js | |
+++ b/node_modules/react-native-intersection-observer/lib/withIO.js | |
@@ -1,21 +1,25 @@ | |
-import React, { PureComponent, createRef, } from 'react'; | |
-import { findNodeHandle, } from 'react-native'; | |
+import React, { PureComponent, createRef } from 'react'; | |
+import { findNodeHandle, View } from 'react-native'; | |
import IOContext from './IOContext'; | |
import IOManager from './IOManager'; | |
+ | |
function withIO(Comp, methods) { | |
const IOScrollableComponent = class extends PureComponent { | |
- node; | |
+ nativeRef; | |
scroller; | |
root; | |
manager; | |
contextValue; | |
+ | |
constructor(props) { | |
super(props); | |
+ | |
const self = this; | |
- this.scroller = createRef(); | |
+ this.scroller = createRef(); // For the custom component | |
+ this.nativeRef = createRef(); // For the native view reference | |
this.root = { | |
get node() { | |
- return self.node; | |
+ return self.nativeRef.current; // Attach to the native ref | |
}, | |
get horizontal() { | |
return !!self.props.horizontal; | |
@@ -42,65 +46,95 @@ function withIO(Comp, methods) { | |
zoomScale: 1, | |
}, | |
}; | |
+ | |
const manager = new IOManager({ | |
root: this.root, | |
get rootMargin() { | |
return self.props.rootMargin; | |
}, | |
}); | |
+ | |
this.manager = manager; | |
this.contextValue = { | |
manager, | |
}; | |
} | |
+ | |
componentDidMount() { | |
- this.node = findNodeHandle(this.scroller.current); | |
+ this.nativeNode = findNodeHandle(this.nativeRef.current); // Attach native node | |
+ | |
+ // Attach dynamic methods from props | |
methods.forEach((method) => { | |
this[method] = (...args) => { | |
this.scroller.current?.[method]?.(...args); | |
}; | |
}); | |
} | |
+ | |
handleContentSizeChange = (width, height) => { | |
const { contentSize } = this.root.current; | |
+ | |
if (width !== contentSize.width || height !== contentSize.height) { | |
this.root.current.contentSize = { width, height }; | |
if (width > 0 && height > 0 && this.root.onLayout) { | |
this.root.onLayout(); | |
} | |
} | |
+ | |
const { onContentSizeChange } = this.props; | |
if (onContentSizeChange) { | |
onContentSizeChange(width, height); | |
} | |
}; | |
+ | |
handleLayout = (event) => { | |
- const { nativeEvent: { layout }, } = event; | |
+ const { | |
+ nativeEvent: { layout }, | |
+ } = event; | |
const { layoutMeasurement } = this.root.current; | |
- if (layoutMeasurement.width !== layout.width || | |
- layoutMeasurement.height !== layout.height) { | |
+ | |
+ if (layoutMeasurement.width !== layout.width || layoutMeasurement.height !== layout.height) { | |
this.root.current.layoutMeasurement = layout; | |
} | |
+ | |
const { onLayout } = this.props; | |
if (onLayout) { | |
onLayout(event); | |
} | |
}; | |
+ | |
handleScroll = (event) => { | |
this.root.current = event.nativeEvent; | |
+ | |
if (this.root.onScroll) { | |
this.root.onScroll(this.root.current); | |
} | |
+ | |
const { onScroll } = this.props; | |
if (onScroll) { | |
onScroll(event); | |
} | |
}; | |
+ | |
render() { | |
- return (React.createElement(IOContext.Provider, { value: this.contextValue }, | |
- React.createElement(Comp, { scrollEventThrottle: 16, ...this.props, ref: this.scroller, onContentSizeChange: this.handleContentSizeChange, onLayout: this.handleLayout, onScroll: this.handleScroll }))); | |
+ return ( | |
+ <IOContext.Provider value={this.contextValue}> | |
+ <View ref={this.nativeRef}> | |
+ <Comp | |
+ scrollEventThrottle={16} | |
+ {...this.props} | |
+ ref={this.scroller} | |
+ onContentSizeChange={this.handleContentSizeChange} | |
+ onLayout={this.handleLayout} | |
+ onScroll={this.handleScroll} | |
+ /> | |
+ </View> | |
+ </IOContext.Provider> | |
+ ); | |
} | |
}; | |
+ | |
return IOScrollableComponent; | |
} | |
+ | |
export default withIO; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment