Skip to content
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

Add useResizeObserver #60

Merged
merged 8 commits into from
Mar 10, 2019
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ https://sandiiarov.github.io/use-events
- [useMousePosition](https://sandiiarov.github.io/use-events/#/docs-use-mouse-position)
- [useTouch](https://sandiiarov.github.io/use-events/#/docs-use-touch)
- [useWindowResize](https://sandiiarov.github.io/use-events/#/docs-use-window-resize)
- [useResizeObserver](https://sandiiarov.github.io/use-events/#/docs-use-resize-observer)

# Install

Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions __tests__/useResizeObserver.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as React from 'react';
import { render } from 'react-testing-library';
import ResizeObserver from 'resize-observer-polyfill';
import { useResizeObserver } from '../src';

jest.mock('resize-observer-polyfill');

const TestComponent = () => {
const ref = React.useRef(null);
const [width, height] = useResizeObserver(ref);
return <div ref={ref}>{width + height}</div>;
};

const resize = (width: number, height: number) => {
// @ts-ignore
ResizeObserver.mockReset();
// @ts-ignore
ResizeObserver.mockImplementation(cb => {
cb([{ contentRect: { width, height } }]);
return { observe: jest.fn, disconnect: jest.fn };
});

const { container } = render(<TestComponent />);
return container.textContent;
};

test('useResizeObserver', () => {
expect(resize(100, 100)).toBe('200');
expect(resize(200, 200)).toBe('400');
});
41 changes: 20 additions & 21 deletions __tests__/useWindowResize.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import * as React from 'react';
import { act, render } from 'react-testing-library';
import { act, renderHook } from 'react-hooks-testing-library';
import { fireEvent } from 'react-testing-library';
import { useWindowResize } from '../src';

test('useWindowResize should react on window resize events', () => {
function fireResize(width: number, height: number) {
// @ts-ignore
window.innerWidth = width;
// @ts-ignore
window.innerHeight = height;
window.dispatchEvent(new Event('resize'));
}
const resize = (width: number, height: number) => {
// @ts-ignore
window.innerWidth = width;
// @ts-ignore
window.innerHeight = height;
fireEvent(window, new Event('resize'));
};

const TestComponent = () => {
const [height, width] = useWindowResize();
return <span>{`${width}x${height}`}</span>;
};
test('useWindowResize should react on window resize event', () => {
let width, height;

const { container, rerender } = render(<TestComponent />);
act(() => fireResize(800, 600));
expect(container.firstChild.textContent).toBe('800x600');
act(() => fireResize(1024, 768));
expect(container.firstChild.textContent).toBe('1024x768');
act(() => fireResize(1440, 800));
expect(container.firstChild.textContent).toBe('1440x800');
renderHook(() => ([width, height] = useWindowResize()));

act(() => resize(100, 100));
expect(width).toBe(100);
expect(height).toBe(100);

act(() => resize(200, 200));
expect(width).toBe(200);
expect(height).toBe(200);
});
51 changes: 51 additions & 0 deletions docs/useResizeObserver.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
name: useResizeObserver
---

import { Playground } from 'docz';
import { useResizeObserver } from '../src';

# useResizeObserver

```js
import { useResizeObserver } from 'use-events';
```

```jsx
const Example = () => {
const ref = React.useRef(null);
const [width, height] = useResizeObserver(ref);

return (
<div ref={ref}>
<b>width:</b> {width}
<b>height:</b> {height}
</div>
);
};
```

<Playground>
{() => {
const ref = React.useRef(null);
const [width, height] = useResizeObserver(ref);
return (
<div
ref={ref}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
resize: 'both',
overflow: 'auto',
border: '1px solid #CED4DE',
}}
>
<b>width:</b> {width}
<b>height:</b> {height}
</div>
);

}}

</Playground>
10 changes: 6 additions & 4 deletions docs/useWindowResize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ import { useWindowResize } from 'use-events';

```jsx
const Example = () => {
const [height, width] = useWindowResize();
const [width, height] = useWindowResize();

return (
<div>
<b>height:</b> {height} <b>width:</b> {width}
<b>width:</b> {width}
<b>height:</b> {height}
</div>
);
};
```

<Playground>
{() => {
const [height, width] = useWindowResize();
const [width, height] = useWindowResize();
return (
<div>
<b>height:</b> {height} <b>width:</b> {width}
<b>width:</b> {width}
<b>height:</b> {height}
</div>
);
}}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"peerDependencies": {
"react": ">=16.8.1"
},
"dependencies": {
"resize-observer-polyfill": "1.5.1"
},
"devDependencies": {
"@babel/core": "7.3.4",
"@babel/preset-env": "7.3.4",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { default as useClickOutside } from './useClickOutside/index';
export { default as useFocus } from './useFocus/index';
export { default as useHover } from './useHover/index';
export { default as useMousePosition } from './useMousePosition/index';
export { default as useResizeObserver } from './useResizeObserver/index';
export { default as useTouch } from './useTouch/index';
export { default as useWindowResize } from './useWindowResize/index';
32 changes: 32 additions & 0 deletions src/useResizeObserver/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import ResizeObserver from 'resize-observer-polyfill';

const useResizeObserver = (
ref: React.RefObject<HTMLElement>
): [number, number] => {
const [width, setWidth] = React.useState(0);
const [height, setHeight] = React.useState(0);

React.useLayoutEffect(() => {
const { current } = ref;

const resizeObserver = new ResizeObserver(entries => {
const { width, height } = entries[0].contentRect;

setWidth(width);
setHeight(height);
});

if (current !== null) {
resizeObserver.observe(current);
}

return () => {
resizeObserver.disconnect();
};
}, []);

return [width, height];
};

export default useResizeObserver;
4 changes: 2 additions & 2 deletions src/useWindowResize/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';

const useWindowResize = (): [number, number] => {
const [height, setHeight] = React.useState(window.innerHeight);
const [width, setWidth] = React.useState(window.innerWidth);
const [height, setHeight] = React.useState(window.innerHeight);

const resize = () => {
setWidth(window.innerWidth);
Expand All @@ -17,7 +17,7 @@ const useWindowResize = (): [number, number] => {
};
}, []);

return [height, width];
return [width, height];
};

export default useWindowResize;
Loading