Skip to content

Commit 973d719

Browse files
[0.83-stable] Adding intersection observer chnages (#15841)
* fixing compilation error * adding package.lock.json files * removing IntersectionObserverIndex from RNTester file * removing IntersectionObserver * Revert 'removing IntersectionObserver' and 'removing IntersectionObserverIndex from RNTester file' * adding IntersectionObserver from upstream * adding packages.lock.json file chnages * Change files * adding package.lock.json files
1 parent 79339c6 commit 973d719

12 files changed

Lines changed: 2444 additions & 0 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "adding IntersectionObserver from upstream",
4+
"packageName": "react-native-windows",
5+
"email": "protikbiswas@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type IntersectionObserverType from 'react-native/src/private/webapis/intersectionobserver/IntersectionObserver';
12+
13+
import {RNTesterThemeContext} from '../../components/RNTesterTheme';
14+
import * as React from 'react';
15+
import {
16+
type ElementRef,
17+
useContext,
18+
useLayoutEffect,
19+
useRef,
20+
useState,
21+
} from 'react';
22+
import {Button, ScrollView, StyleSheet, Text, View} from 'react-native';
23+
24+
declare var IntersectionObserver: Class<IntersectionObserverType>;
25+
26+
export const name = 'IntersectionObserver Benchmark';
27+
export const title = name;
28+
export const description =
29+
'Example of using IntersectionObserver to observe a large amount of UI elements';
30+
31+
export function render(): React.Node {
32+
return <IntersectionObserverBenchark />;
33+
}
34+
35+
const ROWS = 100;
36+
const COLUMNS = 5;
37+
38+
function IntersectionObserverBenchark(): React.Node {
39+
const [isObserving, setObserving] = useState(false);
40+
41+
return (
42+
<>
43+
<View style={styles.buttonContainer}>
44+
<Button
45+
title={isObserving ? 'Stop observing' : 'Start observing'}
46+
onPress={() => setObserving(observing => !observing)}
47+
/>
48+
</View>
49+
<ScrollView>
50+
{Array(ROWS)
51+
.fill(null)
52+
.map((_, row) => (
53+
<View style={styles.row} key={row}>
54+
{Array(COLUMNS)
55+
.fill(null)
56+
.map((_2, column) => (
57+
<Item
58+
index={COLUMNS * row + column}
59+
observe={isObserving}
60+
key={column}
61+
/>
62+
))}
63+
</View>
64+
))}
65+
</ScrollView>
66+
</>
67+
);
68+
}
69+
70+
function Item({index, observe}: {index: number, observe: boolean}): React.Node {
71+
const theme = useContext(RNTesterThemeContext);
72+
const ref = useRef<?ElementRef<typeof View>>();
73+
74+
useLayoutEffect(() => {
75+
const element = ref.current;
76+
77+
if (!observe || !element) {
78+
return;
79+
}
80+
81+
const observer = new IntersectionObserver(
82+
entries => {
83+
// You can inspect the actual entries here.
84+
// We don't log them by default to avoid the logs themselves to degrade
85+
// performance.
86+
},
87+
{
88+
threshold: [0, 1],
89+
},
90+
);
91+
92+
observer.observe(element);
93+
94+
return () => {
95+
observer.disconnect();
96+
};
97+
}, [observe]);
98+
99+
return (
100+
<View
101+
ref={ref}
102+
style={[
103+
styles.item,
104+
{backgroundColor: theme.SecondarySystemBackgroundColor},
105+
]}>
106+
<Text style={[styles.itemText, {color: theme.LabelColor}]}>
107+
{index + 1}
108+
</Text>
109+
</View>
110+
);
111+
}
112+
113+
const styles = StyleSheet.create({
114+
buttonContainer: {
115+
padding: 10,
116+
},
117+
row: {
118+
flexDirection: 'row',
119+
},
120+
item: {
121+
flex: 1,
122+
padding: 12,
123+
margin: 5,
124+
},
125+
itemText: {
126+
fontSize: 22,
127+
textAlign: 'center',
128+
},
129+
});

0 commit comments

Comments
 (0)