ag-common 0.0.679 → 0.0.680

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,16 +29,18 @@ 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
42
  var _a;
41
- if ((_a = size === null || size === void 0 ? void 0 : size.p) !== null && _a !== void 0 ? _a : !ref.current) {
43
+ if ((((_a = size === null || size === void 0 ? void 0 : size.p) === null || _a === void 0 ? void 0 : _a.tooltipHeight) && size.p.tooltipWidth) || !ref.current) {
42
44
  return;
43
45
  }
44
46
  setSize({
@@ -64,30 +66,59 @@ const Comp = ({ pos, children, }) => {
64
66
  }
65
67
  //
66
68
  top = pos.y + gap;
67
- if (top + size.p.tooltipHeight > pos.parentHeight) {
69
+ const newBottom = pos.parentHeight - size.p.tooltipHeight;
70
+ if (top + size.p.tooltipHeight > pos.parentHeight &&
71
+ //check against really tall
72
+ newBottom > 0) {
68
73
  top = undefined;
69
74
  bottom = pos.parentHeight - pos.y;
70
75
  if (bottom + size.p.tooltipHeight > pos.parentHeight) {
71
- bottom = pos.parentHeight - size.p.tooltipHeight;
76
+ bottom = newBottom;
72
77
  }
73
78
  }
74
79
  }
75
- return (react_1.default.createElement(Base, { ref: ref, style: Object.assign({ left,
80
+ const Content = (react_1.default.createElement(Base, { ref: ref, style: Object.assign(Object.assign({ left,
76
81
  right,
77
82
  top,
78
- bottom }, (!(size === null || size === void 0 ? void 0 : size.p) && { zIndex: -1 })) }, children(pos.data)));
83
+ bottom, zIndex: 10 }, (pos.usePortal && { position: 'fixed' })), (!(size === null || size === void 0 ? void 0 : size.p) && { zIndex: -1 })) }, children(pos.data)));
84
+ if (pos.usePortal) {
85
+ return (0, react_dom_1.createPortal)(Content, document.querySelector(`#${globalId}`));
86
+ }
87
+ return Content;
79
88
  };
80
89
  const useTooltip = () => {
81
90
  const [pos, setPosRaw] = (0, react_1.useState)();
91
+ (0, react_1.useEffect)(() => {
92
+ if (document.querySelectorAll(`#${globalId}`).length > 0) {
93
+ return;
94
+ }
95
+ const d = document.createElement('div');
96
+ d.id = globalId;
97
+ document.body.appendChild(d);
98
+ return () => {
99
+ var _a;
100
+ (_a = document.querySelector(`#${globalId}`)) === null || _a === void 0 ? void 0 : _a.remove();
101
+ };
102
+ }, []);
82
103
  const setPos = (p) => {
83
- var _a, _b;
84
104
  if (!p) {
85
105
  setPosRaw(undefined);
86
106
  return;
87
107
  }
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);
108
+ let parentTop = 0;
109
+ let parentLeft = 0;
110
+ let parentWidth = document.body.clientWidth;
111
+ let parentHeight = document.body.clientHeight;
112
+ if (p.parent) {
113
+ ({
114
+ top: parentTop,
115
+ left: parentLeft,
116
+ width: parentWidth,
117
+ height: parentHeight,
118
+ } = p.parent.getBoundingClientRect());
119
+ }
120
+ const x = p.element.pageX - parentLeft;
121
+ const y = p.element.pageY - parentTop;
91
122
  const p2 = {
92
123
  cursor: p.element,
93
124
  data: p.data,
@@ -95,6 +126,7 @@ const useTooltip = () => {
95
126
  parentHeight,
96
127
  x,
97
128
  y,
129
+ usePortal: !p.parent,
98
130
  };
99
131
  setPosRaw(p2);
100
132
  };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.679",
2
+ "version": "0.0.680",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",