Skip to content

Commit

Permalink
Docs + Storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
williamisnotdefined committed Jan 24, 2025
1 parent 9d6ad3c commit 2b8cd4b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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
1 change: 1 addition & 0 deletions src/useMeasure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type UseMeasureResult<E extends Element = Element> = [
E | null
];
type UseMeasureParams = {
// @ts-ignore [ResizeObserverOptions is not defined in resize-observer-polyfill]
observerOptions?: ResizeObserverOptions;
};

Expand Down
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 />);

0 comments on commit 2b8cd4b

Please sign in to comment.