# @react-hook/size

> A React hook for measuring the size of HTML elements including when they change

Latest version **2.1.2** (published 2021-06-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @react-hook/size
pnpm add @react-hook/size
yarn add @react-hook/size
bun add @react-hook/size
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2021-06-21 |
| First published | 2020-05-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 152 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1529 |
| Author | Jared Lunde |
| Maintainers | jaredlunde |
| Keywords | react, react hook, react size hook, size hook, use size, use measure, measure hook |

## Links

- npm: https://www.npmjs.com/package/@react-hook/size
- Repository: https://github.com/jaredLunde/react-hook
- Homepage: https://github.com/jaredLunde/react-hook/tree/master/packagess/#readme
- Issues: https://github.com/jaredLunde/react-hook/issues
- npm.io page: https://npm.io/package/@react-hook/size

## Dependencies (2)

- [@react-hook/resize-observer](https://npm.io/package/@react-hook/resize-observer.md) ^1.2.1
- [@react-hook/passive-layout-effect](https://npm.io/package/@react-hook/passive-layout-effect.md) ^1.2.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.1.2 (latest) — 2021-06-21
- 2.1.1 — 2020-09-02
- 2.1.0 — 2020-06-07
- 2.0.1 — 2020-06-04
- 2.0.0 — 2020-05-27
- 1.0.0 — 2020-05-27
- 0.0.1 — 2020-05-27

## README

<hr>
<div align="center">
  <h1 align="center">
    useSize()
  </h1>
</div>

<p align="center">
  <a href="https://bundlephobia.com/result?p=@react-hook/size">
    <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@react-hook/size?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Types" href="https://www.npmjs.com/package/@react-hook/size">
    <img alt="Types" src="https://img.shields.io/npm/types/@react-hook/size?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Build status" href="https://travis-ci.com/jaredLunde/react-hook">
    <img alt="Build status" src="https://img.shields.io/travis/com/jaredLunde/react-hook?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="NPM version" href="https://www.npmjs.com/package/@react-hook/size">
    <img alt="NPM Version" src="https://img.shields.io/npm/v/@react-hook/size?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="License" href="https://jaredlunde.mit-license.org/">
    <img alt="MIT License" src="https://img.shields.io/npm/l/@react-hook/size?style=for-the-badge&labelColor=24292e">
  </a>
</p>

<pre align="center">npm i @react-hook/size</pre>
<hr>

A React hook for measuring the size of HTML elements including when they change

## Features

- [x] Determines the size of the element including any padding, borders, and scroll bars.
- [x] The size reported by the hook updates each time the size of the element changes.
- [x] Uses a single `ResizeObserver` for tracking all elements used by the hooks.
      [This approach is astoundingly more performant](https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/z6ienONUb5A/F5-VcUZtBAAJ)
      than using a `ResizeObserver` per element which most hook implementations do.
- [x] Uses [`@juggle/resize-observer`](https://github.com/juggle/resize-observer) as a ponyfill when `ResizeObserver` isn't supported
      by the current browser.
- [x] Automatically unobserves the target element when the hook unmounts.

## Quick Start

[Check out an example on **CodeSandbox**](https://codesandbox.io/s/react-hooksize-example-vbpnl)

```jsx harmony
import * as React from 'react'
import useSize from '@react-hook/size'

const Component = () => {
  const target = React.useRef(null)
  const [width, height] = useSize(target)

  return (
    <div ref={target}>
      <div>Width: {width}</div>
      <div>Height: {height}</div>
    </div>
  )
}
```

## API

### useSize(target, options?)

| Argument | Type                                                 | Required? | Description                                               |
| -------- | ---------------------------------------------------- | --------- | --------------------------------------------------------- |
| target   | <code>React.RefObject<T> &#124; T &#124; null</code> | Yes       | A React ref created by `useRef()` or an HTML element      |
| options  | [`UseSizeOptions`](#usesizeoptions)                  | No        | Options for SSR. See [`UseSizeOptions`](#usesizeoptions). |

### UseSizeOptions

```ts
export interface UseSizeOptions {
  // The initial width to set into state.
  // This is useful for SSR environments.
  initialWidth: 0
  // The initial height to set into state.
  // This is useful for SSR environments.
  initialHeight: 0
}
```

## LICENSE

MIT

---
_Source: https://npm.io/package/@react-hook/size · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
