2.0.2 • Published 11 months ago

@react-hook/resize-observer v2.0.2

Weekly downloads
8,160
License
MIT
Repository
github
Last release
11 months ago

A React hook that fires a callback whenever ResizeObserver detects a change to its size.

Features

  • Uses a single ResizeObserver for tracking all elements used by the hooks. This approach is astoundingly more performant than using a ResizeObserver per element which most hook implementations do.
  • Automatically unobserves the target element when the hook unmounts.
  • You don't have to wrap your callback in useCallback() because any mutations are handled by the hook.

Quick Start

Check out an example on CodeSandbox

import * as React from 'react'
import useResizeObserver from '@react-hook/resize-observer'

const useSize = (target) => {
  const [size, setSize] = React.useState()

  React.useLayoutEffect(() => {
    setSize(target.current.getBoundingClientRect())
  }, [target])

  // Where the magic happens
  useResizeObserver(target, (entry) => setSize(entry.contentRect))
  return size
}

const App = () => {
  const target = React.useRef(null)
  const size = useSize(target)
  return (
    <pre ref={target}>
      {JSON.stringify({width: size.width, height: size.height}, null, 2)}
    </pre>
  )
}

API

useResizeObserver(target, callback, options)

function useResizeObserver<T extends Element>(
  target: React.RefObject<T> | React.ForwardedRef<T> | T | null,
  callback: UseResizeObserverCallback,
  options?: UseResizeObserverOptions
): ResizeObserver
ArgumentTypeRequired?Description
targetReact.RefObject | T | nullYesA React ref created by useRef() or an HTML element
callbackUseResizeObserverCallbackYesInvoked with a single ResizeObserverEntry any time the target resizes
optionsUseResizeObserverOptionsNoOptions for the ResizeObserver instance.

Types

UseResizeObserverCallback

export type UseResizeObserverCallback = (
  entry: ResizeObserverEntry,
  observer: ResizeObserver
) => any

UseResizeObserverOptions

export type UseResizeObserverOptions = {
  polyfill?: any
}

polyfill

A ResizeObserver polyfill implementation such as @juggle/resize-observer (this was the default in V1.x).

import useResizeObserver from '@react-hook/resize-observer'
import {ResizeObserver} from '@juggle/resize-observer'

useResizeObserver(..., ..., { polyfill: ResizeObserver })

LICENSE

MIT

un-bourguignon-library@infinitebrahmanuniverse/nolb-_react-h@xevolab/wisteria@everything-registry/sub-chunk-764@captainpants/typeytypetype-react-domtarget-gis-3d-mapsynapse-react-clientsysom-flamegraphwell-seismic-common-components@dagster-io/ui@dagster-io/ui-components@commercetools-docs/ui-kit@commercetools-frontend/application-components@devesharp/react-web@dolbyio/comms-uikit-reactjcicljci18itdkjellyfin-webtj-ui-componentstracqr-forms@barcode-bakery/barcode-react@blacklake-web/layout@brizy/ui@castlabs/prestoplay-react-components@chayns-components/corevirtual-table-reactuse-responsive-dimensionxtracta-ts-react-image-magnify@fluent-blocks/react@holdr-ui/react@hundred5/design-system@huygn/react-showroom@ihsanrabbs/design-system@flexy-design/flexy-fit@elliemae/ds-tabs@elliemae/ds-legacy-tabs@frc-web-components/react-dashboard@glenlowland/jellyfin-web@lcrh/responsive-toolbar@microsoft/arbutus.guidance@microsoft/arbutus.table-list@netlify/sdk--ui-react@nexusmods/ui-components@nerdbond/flow@nascentdigital/toolkit-react@ezcater/recipe@edsc/metadata-preview@mindfulstudio/ms-web-ui@mercoa/react@keepeek/keepicker-react@jrx2-dev/use-responsive-class@morphysm/react-chat-widget@react-hook/size@qualtrics/ui-react@productfy/graphiql-tree@quarkly/community-kit@rjfelix/userfront-react@open-cluster-management/ui-components@overmap-ai/blocks@sonatype/react-shared-components@spanic/react-interview-components@resoc/uimetasnippetsoolibreact-multiple-scrollbarreact-box-layoutreact-chart-libraryreact-collapsible-tagsreact-image-hotspotreact-infinite-tickernoodle-editorpreview-partpreview-part-test-menureact-roireact-showroomreact-untitled-uireact-svg-daglightbox.js-reactlightbox.js-react-clientlightbox.js-modifiedlexioluxury-escapesmetalib@wavebond/flow@wavv/ui@un-bourguignon/components@tunebond/flow@therjfelix/userfront-react@termsurf/vine@termsurf/hawk@textsurf/tree@textsurf/vine@userfront/toolkit@volvo-cars/connected-retail-ui@stolostron/ui-components@sweatpants/react@termsurf/crow@termsurf/leafanalyst-component-library
2.0.2

11 months ago

2.0.1

1 year ago

2.0.0

1 year ago

1.2.6

3 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago