Skip to content

Commit

Permalink
Merge pull request #1 from noahlaux:noahlaux/issue298
Browse files Browse the repository at this point in the history
[resize-observer] Fixes jaredLunde#298 Provide optional ResizeObserverOptions
  • Loading branch information
noahlaux authored Mar 30, 2023
2 parents 3c813da + 3a51915 commit 35336ea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/resize-observer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ const App = () => {
```ts
function useResizeObserver<T extends HTMLElement>(
target: React.RefObject<T> | T | null,
callback: UseResizeObserverCallback
callback: UseResizeObserverCallback,
options?: ResizeObserverOptions
): ResizeObserver
```

| 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 |
| callback | [`UseResizeObserverCallback`](#useresizeobservercallback) | Yes | Invoked with a single `ResizeObserverEntry` any time the `target` resizes |
| options? | [ResizeObserverOptions][1] | No | Provide options to ResizeObserver |

## Types

Expand All @@ -100,3 +102,5 @@ export type UseResizeObserverCallback = (
## LICENSE

MIT

[1]: https://w3c.github.io/csswg-drafts/resize-observer/#resize-observer-interface
15 changes: 11 additions & 4 deletions packages/resize-observer/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ResizeObserver as Polyfill,
ResizeObserverEntry,
} from '@juggle/resize-observer'
import {ResizeObserverOptions} from '@juggle/resize-observer/lib/ResizeObserverOptions'
import useLayoutEffect from '@react-hook/passive-layout-effect'
import useLatest from '@react-hook/latest'

Expand All @@ -20,10 +21,12 @@ const ResizeObserver =
* @param target A React ref created by `useRef()` or an HTML element
* @param callback Invoked with a single `ResizeObserverEntry` any time
* the `target` resizes
* @param [options] Provide options to ResizeObserver
*/
function useResizeObserver<T extends HTMLElement>(
target: React.RefObject<T> | T | null,
callback: UseResizeObserverCallback
callback: UseResizeObserverCallback,
options?: ResizeObserverOptions
): Polyfill {
const resizeObserver = getResizeObserver()
const storedCallback = useLatest(callback)
Expand All @@ -38,7 +41,7 @@ function useResizeObserver<T extends HTMLElement>(
storedCallback.current(entry, observer)
}

resizeObserver.subscribe(targetEl as HTMLElement, cb)
resizeObserver.subscribe(targetEl as HTMLElement, cb, options)

return () => {
didUnsubscribe = true
Expand Down Expand Up @@ -77,8 +80,12 @@ function createResizeObserver() {

return {
observer,
subscribe(target: HTMLElement, callback: UseResizeObserverCallback) {
observer.observe(target)
subscribe(
target: HTMLElement,
callback: UseResizeObserverCallback,
options?: ResizeObserverOptions
) {
observer.observe(target, options)
const cbs = callbacks.get(target) ?? []
cbs.push(callback)
callbacks.set(target, cbs)
Expand Down
6 changes: 5 additions & 1 deletion packages/resize-observer/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import {
ResizeObserver as Polyfill,
ResizeObserverEntry,
} from '@juggle/resize-observer'
import {ResizeObserverOptions} from '@juggle/resize-observer/lib/ResizeObserverOptions'

/**
* A React hook that fires a callback whenever ResizeObserver detects a change to its size
*
* @param target A React ref created by `useRef()` or an HTML element
* @param callback Invoked with a single `ResizeObserverEntry` any time
* the `target` resizes
* @param [options] Provide options to ResizeObserver
*/
declare function useResizeObserver<T extends HTMLElement>(
target: React.RefObject<T> | T | null,
callback: UseResizeObserverCallback
callback: UseResizeObserverCallback,
options?: ResizeObserverOptions
): Polyfill
export declare type UseResizeObserverCallback = (
entry: ResizeObserverEntry,
Expand Down

0 comments on commit 35336ea

Please sign in to comment.