Files
react-native/packages/rn-tester/js/examples/MutationObserver/VisualCompletionExample/VCOverlayExample.js
T
Rubén Norte f193b1774c Enable MutationObserver in RNTester (#38101)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38101

This adds a few examples of using `MutationObserver` in React Native to RNTester.

`MutationObserver` isn't yet enabled so these shouldn't be accessible normally for the time being.

Changelog: [internal]

Reviewed By: NickGerleman

Differential Revision: D46149087

fbshipit-source-id: 8374b5915b8474a17a2f2e3277a9f409f6ab8380
2023-07-03 15:34:34 -07:00

68 lines
1.7 KiB
JavaScript

/**
* 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.
*
* @flow strict-local
* @format
*/
import type VCTracker, {VisualElement} from './VCTrackerExample';
import {useEffect, useState} from 'react';
import * as React from 'react';
import {Dimensions, StyleSheet, View} from 'react-native';
const OVERLAY_SCALE = 0.25;
export default function VCOverlayExample(props: {
vcTracker: VCTracker,
}): React.Node {
const [visualElements, setVisualElements] = useState<
$ReadOnlyArray<VisualElement>,
>([]);
useEffect(() => {
setVisualElements(props.vcTracker.getVisualElements());
props.vcTracker.onUpdateVisualElements(elements => {
setVisualElements(elements);
});
}, [props.vcTracker]);
return (
<View style={styles.overlay}>
{visualElements.map((visualElement, index) => (
<View
key={index}
style={[
styles.overlayElement,
{
top: visualElement.rect.top * OVERLAY_SCALE,
left: visualElement.rect.left * OVERLAY_SCALE,
width: visualElement.rect.width * OVERLAY_SCALE,
height: visualElement.rect.height * OVERLAY_SCALE,
},
]}
/>
))}
</View>
);
}
const styles = StyleSheet.create({
overlay: {
position: 'absolute',
bottom: 60,
right: 10,
width: OVERLAY_SCALE * Dimensions.get('window').width,
height: OVERLAY_SCALE * Dimensions.get('window').height,
backgroundColor: 'gray',
opacity: 0.9,
},
overlayElement: {
position: 'absolute',
borderWidth: 1,
borderColor: 'black',
},
});