main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[d565449] | 1 | import { useEffect } from 'react';
|
---|
| 2 | import useRafState from './useRafState';
|
---|
| 3 | import { off, on } from './misc/util';
|
---|
| 4 | var useScroll = function (ref) {
|
---|
| 5 | if (process.env.NODE_ENV === 'development') {
|
---|
| 6 | if (typeof ref !== 'object' || typeof ref.current === 'undefined') {
|
---|
| 7 | console.error('`useScroll` expects a single ref argument.');
|
---|
| 8 | }
|
---|
| 9 | }
|
---|
| 10 | var _a = useRafState({
|
---|
| 11 | x: 0,
|
---|
| 12 | y: 0,
|
---|
| 13 | }), state = _a[0], setState = _a[1];
|
---|
| 14 | useEffect(function () {
|
---|
| 15 | var handler = function () {
|
---|
| 16 | if (ref.current) {
|
---|
| 17 | setState({
|
---|
| 18 | x: ref.current.scrollLeft,
|
---|
| 19 | y: ref.current.scrollTop,
|
---|
| 20 | });
|
---|
| 21 | }
|
---|
| 22 | };
|
---|
| 23 | if (ref.current) {
|
---|
| 24 | on(ref.current, 'scroll', handler, {
|
---|
| 25 | capture: false,
|
---|
| 26 | passive: true,
|
---|
| 27 | });
|
---|
| 28 | }
|
---|
| 29 | return function () {
|
---|
| 30 | if (ref.current) {
|
---|
| 31 | off(ref.current, 'scroll', handler);
|
---|
| 32 | }
|
---|
| 33 | };
|
---|
| 34 | }, [ref]);
|
---|
| 35 | return state;
|
---|
| 36 | };
|
---|
| 37 | export default useScroll;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.