-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseRafState.story.tsx
31 lines (25 loc) · 1001 Bytes
/
useRafState.story.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useRafState, useMount } from '../src';
import ShowDocs from './util/ShowDocs';
const Demo = () => {
const [state, setState] = useRafState({ x: 0, y: 0 });
useMount(() => {
const onMouseMove = (event: MouseEvent) => {
setState({ x: event.clientX, y: event.clientY });
};
const onTouchMove = (event: TouchEvent) => {
setState({ x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY });
};
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('touchmove', onTouchMove);
return () => {
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('touchmove', onTouchMove);
};
});
return <pre>{JSON.stringify(state, null, 2)}</pre>;
};
storiesOf('State/useRafState', module)
.add('Docs', () => <ShowDocs md={require('../docs/useRafState.md')} />)
.add('Demo', () => <Demo />);