main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[d565449] | 1 | import { useEffect, useRef } from 'react';
|
---|
| 2 | import { off, on } from './misc/util';
|
---|
| 3 | var defaultEvents = ['mousedown', 'touchstart'];
|
---|
| 4 | var useClickAway = function (ref, onClickAway, events) {
|
---|
| 5 | if (events === void 0) { events = defaultEvents; }
|
---|
| 6 | var savedCallback = useRef(onClickAway);
|
---|
| 7 | useEffect(function () {
|
---|
| 8 | savedCallback.current = onClickAway;
|
---|
| 9 | }, [onClickAway]);
|
---|
| 10 | useEffect(function () {
|
---|
| 11 | var handler = function (event) {
|
---|
| 12 | var el = ref.current;
|
---|
| 13 | el && !el.contains(event.target) && savedCallback.current(event);
|
---|
| 14 | };
|
---|
| 15 | for (var _i = 0, events_1 = events; _i < events_1.length; _i++) {
|
---|
| 16 | var eventName = events_1[_i];
|
---|
| 17 | on(document, eventName, handler);
|
---|
| 18 | }
|
---|
| 19 | return function () {
|
---|
| 20 | for (var _i = 0, events_2 = events; _i < events_2.length; _i++) {
|
---|
| 21 | var eventName = events_2[_i];
|
---|
| 22 | off(document, eventName, handler);
|
---|
| 23 | }
|
---|
| 24 | };
|
---|
| 25 | }, [events, ref]);
|
---|
| 26 | };
|
---|
| 27 | export default useClickAway;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.