getBoundingClientRect(ref: NodesRef)

Requires:
@ray-js/ray≥0.5.10

Description

Get the node's coordinate information

Parameters

ParameterTypeRequiredDescription
YesNode reference obtained via getElementById

Return Value

Type: Promise<Rect>
Rect object containing the node's coordinates and size; returns null if the node does not exist

Examples

Basic usage

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>
  );
}