/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
import {RNTesterThemeContext} from '../../components/RNTesterTheme';
import * as React from 'react';
import {
type ElementRef,
useContext,
useLayoutEffect,
useRef,
useState,
} from 'react';
import {Button, ScrollView, StyleSheet, Text, View} from 'react-native';
export const name = 'IntersectionObserver Root Threshold';
export const title = name;
export const description =
'Examples of setting threshold and rn_rootThreshold. Views will change background color if they meet their threshold.';
export function render(): React.Node {
return ;
}
/**
* Similar to the example in MDN: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
*/
function IntersectionObserverRootThreshold(): React.Node {
const theme = useContext(RNTesterThemeContext);
const [showMargin, setShowMargin] = useState(true);
return (
);
}
function ListItem(props: {
position: number,
rootThreshold: number,
threshold: number,
initialValue?: number,
description: string,
style?: ?ViewStyleProp,
}): React.Node {
const itemRef = useRef>(null);
const [intersectionRatio, setIntersectionRatio] = useState(
props.initialValue ?? 0,
);
const [intersectionRootRatio, setIntersectionRootRatio] = useState(
props.initialValue ?? 0,
);
useLayoutEffect(() => {
const itemNode = itemRef.current;
if (itemNode == null) {
return;
}
const intersectionObserver = new IntersectionObserver(
entries => {
entries.forEach(entry => {
setIntersectionRatio(entry.intersectionRatio);
// $FlowFixMe[prop-missing] - React Native specific entry property
setIntersectionRootRatio(entry.rn_intersectionRootRatio);
});
},
{threshold: props.threshold, rn_rootThreshold: props.rootThreshold},
);
// $FlowFixMe[incompatible-call]
intersectionObserver.observe(itemNode);
return () => {
intersectionObserver.disconnect();
};
}, [props.position, props.threshold, props.rootThreshold]);
return (
= props.threshold ? styles.intersecting : null,
intersectionRootRatio >= props.rootThreshold
? styles.rootIntersecting
: null,
props.style,
]}
ref={itemRef}>
{props.description}
rn_rootThreshold: {props.rootThreshold}
threshold: {props.threshold}
);
}
function IntersectionRatioIndicator(props: {
intersectionRatio: number,
intersectionRootRatio: number,
style: {top?: number, bottom?: number, left?: number, right?: number},
}): React.Node {
return (
target ratio: {`${Math.floor(props.intersectionRatio * 100)}%`}
root ratio: {`${Math.floor(props.intersectionRootRatio * 100)}%`}
);
}
const styles = StyleSheet.create({
scrollDownText: {
textAlign: 'center',
fontSize: 20,
marginTop: 20,
},
intersecting: {
backgroundColor: 'rgb(226, 237, 166)',
},
rootIntersecting: {
backgroundColor: 'rgb(237, 90, 45)',
},
margin: {
marginBottom: 700,
},
item: {
backgroundColor: 'rgb(186, 186, 186)',
borderColor: 'rgb(201, 126, 17)',
borderWidth: 2,
height: 500,
margin: 6,
alignItems: 'center',
justifyContent: 'center',
},
intersectionRatioIndicator: {
position: 'absolute',
padding: 5,
backgroundColor: 'white',
opacity: 0.7,
borderWidth: 1,
borderColor: 'black',
},
description: {
margin: 20,
fontSize: 16,
},
});