ag-common 0.0.679 → 0.0.681

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.
@@ -14,6 +14,7 @@ export * from './useContextMenu';
14
14
  export * from './useElementAttribute';
15
15
  export * from './useGranularHook';
16
16
  export * from './useInterval';
17
+ export * from './useIsInViewport';
17
18
  export * from './useLocalStorage';
18
19
  export * from './useLockBodyScroll';
19
20
  export * from './useOnClickOutside';
@@ -30,6 +30,7 @@ __exportStar(require("./useContextMenu"), exports);
30
30
  __exportStar(require("./useElementAttribute"), exports);
31
31
  __exportStar(require("./useGranularHook"), exports);
32
32
  __exportStar(require("./useInterval"), exports);
33
+ __exportStar(require("./useIsInViewport"), exports);
33
34
  __exportStar(require("./useLocalStorage"), exports);
34
35
  __exportStar(require("./useLockBodyScroll"), exports);
35
36
  __exportStar(require("./useOnClickOutside"), exports);
@@ -0,0 +1,2 @@
1
+ import type { RefObject } from 'react';
2
+ export declare function useIsInViewport(ref: RefObject<HTMLElement>): boolean;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useIsInViewport = void 0;
4
+ const react_1 = require("react");
5
+ function useIsInViewport(ref) {
6
+ const [isInViewport, setIsInViewport] = (0, react_1.useState)(false);
7
+ (0, react_1.useEffect)(() => {
8
+ const observer = new IntersectionObserver((entries) => {
9
+ setIsInViewport(entries[0].isIntersecting);
10
+ });
11
+ if (ref.current) {
12
+ observer.observe(ref.current);
13
+ }
14
+ return () => {
15
+ observer.disconnect();
16
+ };
17
+ }, [ref]);
18
+ return isInViewport;
19
+ }
20
+ exports.useIsInViewport = useIsInViewport;
@@ -3,6 +3,7 @@ import React from 'react';
3
3
  interface IPos<T> {
4
4
  cursor: MouseEvent;
5
5
  data: T;
6
+ usePortal: boolean;
6
7
  parentWidth: number;
7
8
  parentHeight: number;
8
9
  x: number;
@@ -29,22 +29,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.useTooltip = void 0;
30
30
  const styled_1 = __importDefault(require("@emotion/styled"));
31
31
  const react_1 = __importStar(require("react"));
32
+ const react_dom_1 = require("react-dom");
32
33
  const Base = styled_1.default.div `
33
34
  position: absolute;
34
35
  z-index: 2;
35
36
  `;
37
+ const globalId = 'ag-tooltip-portal';
36
38
  const Comp = ({ pos, children, }) => {
37
39
  const ref = (0, react_1.createRef)();
38
40
  const [size, setSize] = (0, react_1.useState)();
39
41
  (0, react_1.useEffect)(() => {
40
- var _a;
41
- if ((_a = size === null || size === void 0 ? void 0 : size.p) !== null && _a !== void 0 ? _a : !ref.current) {
42
+ var _a, _b;
43
+ if (!ref.current) {
44
+ return;
45
+ }
46
+ const tooltipWidth = Math.max(ref.current.clientWidth, ref.current.scrollWidth);
47
+ const tooltipHeight = Math.max(ref.current.clientHeight, ref.current.scrollHeight);
48
+ if (tooltipHeight === ((_a = size === null || size === void 0 ? void 0 : size.p) === null || _a === void 0 ? void 0 : _a.tooltipHeight) ||
49
+ tooltipWidth === ((_b = size === null || size === void 0 ? void 0 : size.p) === null || _b === void 0 ? void 0 : _b.tooltipWidth) ||
50
+ tooltipHeight === 0 ||
51
+ tooltipWidth === 0) {
42
52
  return;
43
53
  }
44
54
  setSize({
45
55
  p: {
46
- tooltipWidth: ref.current.clientWidth,
47
- tooltipHeight: ref.current.clientHeight,
56
+ tooltipWidth,
57
+ tooltipHeight,
48
58
  },
49
59
  });
50
60
  }, [ref, size]);
@@ -58,36 +68,68 @@ const Comp = ({ pos, children, }) => {
58
68
  const gap = 5;
59
69
  if (size === null || size === void 0 ? void 0 : size.p) {
60
70
  left = pos.x + gap;
71
+ const newRight = pos.parentWidth - pos.x + gap;
61
72
  if (pos.x + gap + size.p.tooltipWidth > pos.parentWidth) {
62
73
  left = undefined;
63
- right = pos.parentWidth - pos.x + gap;
74
+ right = newRight;
64
75
  }
65
76
  //
66
77
  top = pos.y + gap;
67
78
  if (top + size.p.tooltipHeight > pos.parentHeight) {
68
79
  top = undefined;
69
80
  bottom = pos.parentHeight - pos.y;
70
- if (bottom + size.p.tooltipHeight > pos.parentHeight) {
71
- bottom = pos.parentHeight - size.p.tooltipHeight;
72
- }
81
+ }
82
+ if (right && right + size.p.tooltipWidth > pos.parentWidth) {
83
+ right = undefined;
84
+ left = 0;
85
+ }
86
+ if (bottom && bottom + size.p.tooltipHeight > pos.parentHeight) {
87
+ bottom = undefined;
88
+ top = 0;
73
89
  }
74
90
  }
75
- return (react_1.default.createElement(Base, { ref: ref, style: Object.assign({ left,
91
+ const Content = (react_1.default.createElement(Base, { ref: ref, style: Object.assign(Object.assign({ left,
76
92
  right,
77
93
  top,
78
- bottom }, (!(size === null || size === void 0 ? void 0 : size.p) && { zIndex: -1 })) }, children(pos.data)));
94
+ bottom, zIndex: 10 }, (pos.usePortal && { position: 'fixed' })), (!(size === null || size === void 0 ? void 0 : size.p) && { zIndex: -1 })) }, children(pos.data)));
95
+ if (pos.usePortal) {
96
+ return (0, react_dom_1.createPortal)(Content, document.querySelector(`#${globalId}`));
97
+ }
98
+ return Content;
79
99
  };
80
100
  const useTooltip = () => {
81
101
  const [pos, setPosRaw] = (0, react_1.useState)();
102
+ (0, react_1.useEffect)(() => {
103
+ if (document.querySelectorAll(`#${globalId}`).length > 0) {
104
+ return;
105
+ }
106
+ const d = document.createElement('div');
107
+ d.id = globalId;
108
+ document.body.appendChild(d);
109
+ return () => {
110
+ var _a;
111
+ (_a = document.querySelector(`#${globalId}`)) === null || _a === void 0 ? void 0 : _a.remove();
112
+ };
113
+ }, []);
82
114
  const setPos = (p) => {
83
- var _a, _b;
84
115
  if (!p) {
85
116
  setPosRaw(undefined);
86
117
  return;
87
118
  }
88
- const { top: parentTop, left: parentLeft, width: parentWidth, height: parentHeight, } = (_b = (_a = p.parent) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect()) !== null && _b !== void 0 ? _b : { width: 0, height: 0 };
89
- const x = p.element.pageX - (parentLeft !== null && parentLeft !== void 0 ? parentLeft : 0);
90
- const y = p.element.pageY - (parentTop !== null && parentTop !== void 0 ? parentTop : 0);
119
+ let parentTop = 0;
120
+ let parentLeft = 0;
121
+ let parentWidth = document.body.clientWidth;
122
+ let parentHeight = document.body.clientHeight;
123
+ if (p.parent) {
124
+ ({
125
+ top: parentTop,
126
+ left: parentLeft,
127
+ width: parentWidth,
128
+ height: parentHeight,
129
+ } = p.parent.getBoundingClientRect());
130
+ }
131
+ const x = p.element.pageX - parentLeft;
132
+ const y = p.element.pageY - parentTop;
91
133
  const p2 = {
92
134
  cursor: p.element,
93
135
  data: p.data,
@@ -95,6 +137,7 @@ const useTooltip = () => {
95
137
  parentHeight,
96
138
  x,
97
139
  y,
140
+ usePortal: !p.parent,
98
141
  };
99
142
  setPosRaw(p2);
100
143
  };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.679",
2
+ "version": "0.0.681",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",