import React, { useState } from 'react';
import { View, Button, Text, getBoundingClientRect, getElementById } from '@ray-js/ray';
export default function Demo() {
const [info, setInfo] = useState('');
const handleGetRect = async () => {
const element = await getElementById('targetBox');
if (element) {
const rect = await getBoundingClientRect(element);
if (rect) {
setInfo(`Width:${rect.width} Height:${rect.height} Left:${rect.left} Top:${rect.top}`);
}
}
};
return (
<View style={{ padding: 20 }}>
<View
id="targetBox"
style={{
width: 200,
height: 100,
backgroundColor: '#1890ff',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<Text style={{ color: '#fff' }}>Target area</Text>
</View>
<Button style={{ marginTop: 10 }} onClick={handleGetRect}>
Get coordinates
</Button>
{info && <Text style={{ marginTop: 10 }}>{info}</Text>}
</View>
);
}