@adamjanicki/ui 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,37 +1,38 @@
1
- import { cloneElement, useEffect, useRef } from "react";
1
+ import { cloneElement, useCallback, useEffect, useRef } from "react";
2
2
  var ClickOutside = function (_a) {
3
3
  var children = _a.children, onClickOutside = _a.onClickOutside;
4
- var ref = useRef(null);
4
+ var ref = useRef();
5
5
  var bubbledRef = useRef(false);
6
6
  var startedRef = useRef(false);
7
7
  useEffect(function () {
8
- // Set startedRef after the current synchronous code has executed
9
- var timeoutId = setTimeout(function () {
8
+ setTimeout(function () {
10
9
  startedRef.current = true;
11
10
  }, 0);
12
- var handleClickOutside = function (event) {
13
- var bubbledUp = bubbledRef.current;
14
- bubbledRef.current = false;
15
- if (!startedRef.current || !ref.current || bubbledUp)
16
- return;
17
- var isOnEventPath = event.composedPath().includes(ref.current);
18
- if (!isOnEventPath) {
19
- onClickOutside(event);
20
- }
21
- };
22
- document.addEventListener("click", handleClickOutside);
23
11
  return function () {
24
- clearTimeout(timeoutId);
25
12
  startedRef.current = false;
26
- document.removeEventListener("click", handleClickOutside);
27
13
  };
14
+ }, []);
15
+ var handleClickOutside = useCallback(function (event) {
16
+ var bubbledUp = bubbledRef.current;
17
+ bubbledRef.current = false;
18
+ if (!startedRef.current || !ref.current || bubbledUp)
19
+ return;
20
+ var isOnEventPath = event.composedPath().includes(ref.current);
21
+ !isOnEventPath && onClickOutside(event);
28
22
  }, [onClickOutside]);
23
+ useEffect(function () {
24
+ document.addEventListener("click", handleClickOutside);
25
+ return function () { return document.removeEventListener("click", handleClickOutside); };
26
+ }, [handleClickOutside]);
29
27
  return cloneElement(children, {
30
28
  ref: ref,
31
29
  onClick: function (event) {
32
30
  var _a, _b;
31
+ // point of this is to let us know that click bubbled up
32
+ // from the child element, so we can ignore it if it
33
+ // happens on the element itself
33
34
  bubbledRef.current = true;
34
- (_b = (_a = children.props).onClick) === null || _b === void 0 ? void 0 : _b.call(_a, event);
35
+ (_b = (_a = children.props).onclick) === null || _b === void 0 ? void 0 : _b.call(_a, event);
35
36
  },
36
37
  });
37
38
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamjanicki/ui",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Basic UI components and hooks for React in TypeScript",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",