@gustavo-valsechi/client 1.4.49 → 1.4.51

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.
@@ -39,21 +39,27 @@ var import_styles = require("./styles");
39
39
  var import_lodash = __toESM(require("lodash"));
40
40
  const TargetContext = (0, import_react.createContext)({});
41
41
  const memory = [];
42
+ const showMemory = [];
42
43
  const TargetProviderContainer = ({ children }) => {
43
44
  const [targets, setTargets] = (0, import_react.useState)([]);
45
+ const [show, setShow] = (0, import_react.useState)([]);
44
46
  (0, import_react.useEffect)(() => {
45
47
  import_lodash.default.forEach(targets, (target, index) => {
46
48
  var _a;
47
49
  const element = (_a = target.ref) == null ? void 0 : _a.current;
48
50
  if (!element) return;
49
51
  element.addEventListener("click", () => {
50
- const newTargets = [...targets];
51
- const coords = getCoords(element);
52
- newTargets.splice(index, 1, { ...target, show: !target.show, coords });
53
- setTargets(newTargets);
52
+ const isShow = import_lodash.default.find(show, (data) => data.index === index);
53
+ if (isShow) {
54
+ showMemory.slice(index, 1);
55
+ setShow(showMemory);
56
+ return;
57
+ }
58
+ showMemory.push({ index, coords: getCoords(element) });
59
+ setShow(showMemory);
54
60
  });
55
61
  });
56
- }, []);
62
+ }, [targets]);
57
63
  const getCoords = (target) => {
58
64
  const rect = target.getBoundingClientRect();
59
65
  const targetTop = rect.top + window.scrollY;
@@ -71,7 +77,7 @@ const TargetProviderContainer = ({ children }) => {
71
77
  return coords;
72
78
  };
73
79
  const add = (target) => {
74
- memory.push({ ...target, show: false, coords: { top: 0, left: 0 } });
80
+ memory.push(target);
75
81
  setTargets(memory);
76
82
  };
77
83
  const remove = (ref) => {
@@ -89,14 +95,17 @@ const TargetProviderContainer = ({ children }) => {
89
95
  children: [
90
96
  import_lodash.default.map(
91
97
  targets,
92
- (target, index) => target.show ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
93
- import_styles.Container,
94
- {
95
- style: target.coords,
96
- children: target.component
97
- },
98
- index
99
- ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {})
98
+ (target, index) => {
99
+ var _a;
100
+ return import_lodash.default.some(show, (data) => data.index === index) ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
101
+ import_styles.Container,
102
+ {
103
+ style: (_a = import_lodash.default.find(show, (data) => data.index === index)) == null ? void 0 : _a.coords,
104
+ children: target.component
105
+ },
106
+ index
107
+ ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {});
108
+ }
100
109
  ),
101
110
  children
102
111
  ]
@@ -5,21 +5,27 @@ import { Container } from "./styles";
5
5
  import _ from "lodash";
6
6
  const TargetContext = createContext({});
7
7
  const memory = [];
8
+ const showMemory = [];
8
9
  const TargetProviderContainer = ({ children }) => {
9
10
  const [targets, setTargets] = useState([]);
11
+ const [show, setShow] = useState([]);
10
12
  useEffect(() => {
11
13
  _.forEach(targets, (target, index) => {
12
14
  var _a;
13
15
  const element = (_a = target.ref) == null ? void 0 : _a.current;
14
16
  if (!element) return;
15
17
  element.addEventListener("click", () => {
16
- const newTargets = [...targets];
17
- const coords = getCoords(element);
18
- newTargets.splice(index, 1, { ...target, show: !target.show, coords });
19
- setTargets(newTargets);
18
+ const isShow = _.find(show, (data) => data.index === index);
19
+ if (isShow) {
20
+ showMemory.slice(index, 1);
21
+ setShow(showMemory);
22
+ return;
23
+ }
24
+ showMemory.push({ index, coords: getCoords(element) });
25
+ setShow(showMemory);
20
26
  });
21
27
  });
22
- }, []);
28
+ }, [targets]);
23
29
  const getCoords = (target) => {
24
30
  const rect = target.getBoundingClientRect();
25
31
  const targetTop = rect.top + window.scrollY;
@@ -37,7 +43,7 @@ const TargetProviderContainer = ({ children }) => {
37
43
  return coords;
38
44
  };
39
45
  const add = (target) => {
40
- memory.push({ ...target, show: false, coords: { top: 0, left: 0 } });
46
+ memory.push(target);
41
47
  setTargets(memory);
42
48
  };
43
49
  const remove = (ref) => {
@@ -55,14 +61,17 @@ const TargetProviderContainer = ({ children }) => {
55
61
  children: [
56
62
  _.map(
57
63
  targets,
58
- (target, index) => target.show ? /* @__PURE__ */ jsx(
59
- Container,
60
- {
61
- style: target.coords,
62
- children: target.component
63
- },
64
- index
65
- ) : /* @__PURE__ */ jsx(Fragment, {})
64
+ (target, index) => {
65
+ var _a;
66
+ return _.some(show, (data) => data.index === index) ? /* @__PURE__ */ jsx(
67
+ Container,
68
+ {
69
+ style: (_a = _.find(show, (data) => data.index === index)) == null ? void 0 : _a.coords,
70
+ children: target.component
71
+ },
72
+ index
73
+ ) : /* @__PURE__ */ jsx(Fragment, {});
74
+ }
66
75
  ),
67
76
  children
68
77
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gustavo-valsechi/client",
3
- "version": "1.4.49",
3
+ "version": "1.4.51",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",