---
name: "getBoundingClientRect"
mode: "api"
versionRequirements:
  - { name: "@ray-js/ray", version: "0.5.10" }
title: "getBoundingClientRect - Get node coordinate information"
---

## getBoundingClientRect

> [VERSION] @ray-js/ray >= 0.5.10

### Description

Get the node's coordinate information

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `ref` | `NodesRef` | Yes | Node 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

##### Rect

| Property | Type | Description |
| --- | --- | --- |
| `right` | `number` | Right boundary coordinate of the node |
| `left` | `number` | Left boundary coordinate of the node |
| `top` | `number` | Top boundary coordinate of the node |
| `bottom` | `number` | Bottom boundary coordinate of the node |
| `width` | `number` | Node width |
| `height` | `number` | Node height |

### Referenced Types

##### `interface` NodesRef

| Property | Type | Description |
| --- | --- | --- |
| `boundingClientRect` | `(callback: (result: BoundingClientRectResult) => void) => SelectorQueryChain` | Get node layout position (left, right, top, bottom, width, height) |
| `scrollOffset` | `(callback: (result: ScrollOffsetResult) => void) => SelectorQueryChain` | Get node scroll position (scrollLeft, scrollTop) |
| `fields` | `(fields: Record<string, boolean>, callback: (result: Record<string, unknown>) => void) => SelectorQueryChain` | Get specified fields of the node |

##### `interface` BoundingClientRectResult

| Property | Type | Description |
| --- | --- | --- |
| `id` | `string` | Node id |
| `dataset` | `Record<string, unknown>` | Node dataset |
| `left` | `number` | Left boundary coordinate |
| `right` | `number` | Right boundary coordinate |
| `top` | `number` | Top boundary coordinate |
| `bottom` | `number` | Bottom boundary coordinate |
| `width` | `number` | Node width |
| `height` | `number` | Node height |

##### `interface` SelectorQueryChain

| Property | Type | Description |
| --- | --- | --- |
| `exec` | `(callback: (results: unknown[]) => void) => void` | Execute query |

##### `interface` ScrollOffsetResult

| Property | Type | Description |
| --- | --- | --- |
| `id` | `string` | Node id |
| `dataset` | `Record<string, unknown>` | Node dataset |
| `scrollLeft` | `number` | Horizontal scroll position |
| `scrollTop` | `number` | Vertical scroll position |


### Examples

#### Basic usage

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