1.2.6 • Published 2 years ago

@react-hook/resize-observer v1.2.6

Weekly downloads
8,160
License
MIT
Repository
github
Last release
2 years 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.
  • Uses @juggle/resize-observer as a ponyfill when ResizeObserver isn't supported by the current browser.
  • 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)

function useResizeObserver<T extends HTMLElement>(
  target: React.RefObject<T> | T | null,
  callback: UseResizeObserverCallback
): 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

Types

UseResizeObserverCallback

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

LICENSE

MIT

un-bourguignon-library@infinitebrahmanuniverse/nolb-_react-h@xevolab/wisteria@everything-registry/sub-chunk-764@captainpants/typeytypetype-react-domwell-seismic-common-componentstracqr-formstj-ui-componentsvirtual-table-reactitdkcredifin-testcredifin-testingluxury-escapeslightbox.js-reactcorners@hundred5/design-systemcustomize-easy-ui-componentfontdue-jsgather-content-uierda-pyroscopereact-image-hotspotreact-collapsible-tags@nexusmods/ui-components@nerdbond/flowoolib@nascentdigital/toolkit-reactcinemetricsxtracta-ts-react-image-magnify@mercoa/reactnoodle-editor@mindfulstudio/ms-web-ui@microsoft/arbutus.guidance@morphysm/react-chat-widget@microsoft/arbutus.table-list@lcrh/responsive-toolbar@lmc-eu/spirit-web-react@keepeek/keepicker-react@jrx2-dev/use-responsive-classmetalibmetasnippets@ihsanrabbs/design-system@huygn/react-showroom@open-cluster-management/ui-components@productfy/graphiql-tree@elliemae/ds-tabs@edsc/metadata-preview@fluent-blocks/react@flexy-design/flexy-fit@ezcater/recipe@resoc/ui@elliemae/ds-legacy-tabs@react-hook/size@textsurf/tree@textsurf/vine@quarkly/community-kit@qualtrics/ui-reactreact-box-layoutreact-chart-library@therjfelix/userfront-react@termsurf/crow@termsurf/vine@simu-superid/superid-datav@sitecore-discover/ui@servicetitan/forge@servicetitan/anvil2@shaderfrog/editor@rjfelix/userfront-reactreact-showroom@zus-health/ctw-component-library@un-bourguignon/componentsreact-roireact-untitled-ui@stolostron/ui-componentsreact-svg-dag@userfront/toolkit@sweatpants/react@sonatype/react-shared-components@tunebond/flow@spanic/react-interview-components@volvo-cars/connected-retail-ui@wavv/ui@wavebond/flow@blacklake-web/layout@brizy/ui@castlabs/prestoplay-react-components@chayns-components/core@dolbyio/comms-uikit-reacttarget-gis-3d-map@devesharp/react-websynapse-react-client@commercetools-frontend/application-components@commercetools-docs/ui-kit@dagster-io/ui@dagster-io/ui-components
1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago