Skip to content

[useMeasure] ResizeObserverOptions added as prop #2619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/useMeasure.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,32 @@ const Demo = () => {
};
```

## Usage with observer options
```jsx
import { useMeasure } from "react-use";

const Demo = () => {
const [ref, { x, y, width, height, top, right, bottom, left }] = useMeasure({ observerOptions: { box: 'border-box' } });

return (
<div ref={ref}>
<div>x: {x}</div>
<div>y: {y}</div>
<div>width: {width}</div>
<div>height: {height}</div>
<div>top: {top}</div>
<div>right: {right}</div>
<div>bottom: {bottom}</div>
<div>left: {left}</div>
</div>
);
};
```

This hook uses [`ResizeObserver` API][resize-observer], if you want to support
legacy browsers, consider installing [`resize-observer-polyfill`][resize-observer-polyfill]
before running your app.
In order to use observerOptions check [CanIUse](https://caniuse.com/resizeobserver).

```js
if (!window.ResizeObserver) {
Expand Down
21 changes: 15 additions & 6 deletions src/useMeasure.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { useMemo, useState } from 'react';
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
import { isBrowser, noop } from './misc/util';
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';

export type UseMeasureRect = Pick<
DOMRectReadOnly,
'x' | 'y' | 'top' | 'left' | 'right' | 'bottom' | 'height' | 'width'
>;
export type UseMeasureRef<E extends Element = Element> = (element: E) => void;
export type UseMeasureResult<E extends Element = Element> = [UseMeasureRef<E>, UseMeasureRect];
export type UseMeasureResult<E extends Element = Element> = [
UseMeasureRef<E>,
UseMeasureRect,
E | null
];
type UseMeasureParams = {
// @ts-ignore [ResizeObserverOptions is not defined in resize-observer-polyfill]
observerOptions?: ResizeObserverOptions;
};

const defaultState: UseMeasureRect = {
x: 0,
Expand All @@ -20,7 +28,8 @@ const defaultState: UseMeasureRect = {
right: 0,
};

function useMeasure<E extends Element = Element>(): UseMeasureResult<E> {
function useMeasure<E extends Element = Element>(params?: UseMeasureParams): UseMeasureResult<E> {
const { observerOptions } = { ...params };
const [element, ref] = useState<E | null>(null);
const [rect, setRect] = useState<UseMeasureRect>(defaultState);

Expand All @@ -37,15 +46,15 @@ function useMeasure<E extends Element = Element>(): UseMeasureResult<E> {

useIsomorphicLayoutEffect(() => {
if (!element) return;
observer.observe(element);
observer.observe(element, observerOptions);
return () => {
observer.disconnect();
};
}, [element]);

return [ref, rect];
return [ref, rect, element];
}

export default isBrowser && typeof (window as any).ResizeObserver !== 'undefined'
? useMeasure
: ((() => [noop, defaultState]) as typeof useMeasure);
: ((() => [noop, defaultState, null]) as typeof useMeasure);
17 changes: 17 additions & 0 deletions stories/useMeasure.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ const Demo = () => {
);
};

const DemoObserverOptions = () => {
const [ref, state] = useMeasure({ observerOptions: { box: 'border-box' } });

return (
<>
<pre>{JSON.stringify(state, null, 2)}</pre>
<div ref={ref} style={{ background: 'red' }}>
resize me
</div>
</>
);
};

storiesOf('Sensors/useMeasure', module)
.add('Docs', () => <ShowDocs md={require('../docs/useMeasure.md')} />)
.add('Demo', () => <Demo />);

storiesOf('Sensors/useMeasure', module)
.add('Docs', () => <ShowDocs md={require('../docs/useMeasure.md')} />)
.add('DemoObserverOptions', () => <DemoObserverOptions />);