react-core-ts 2.1.42 → 2.1.44

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,99 +1,65 @@
1
- import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
1
+ import React, { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
2
2
  import ReactDOM from 'react-dom';
3
+ import { getPortalTooltipCoords } from './getPosition';
3
4
 
4
5
  const ExpandableToolTip: React.FC<any> = ({ title, children, enabled }) => {
5
6
  const [dropdownPosition, setDropdownPosition] = useState('bottom-position');
6
- const dropRef = useRef(null);
7
+ const dropRef = useRef<HTMLSpanElement | null>(null);
7
8
  const menuButtonRef = useRef<HTMLDivElement>(null);
8
9
  const [visible, setVisible] = useState(false);
9
10
  const [tooltipStyle, setTooltipStyle] = useState({
10
11
  top: 0,
11
12
  left: 0,
12
- width: 0,
13
+ arrowOffset: 50,
13
14
  });
14
- const adjustDropdownPosition = (): string => {
15
- if (menuButtonRef.current) {
16
- const inputBoxRect = menuButtonRef.current?.getBoundingClientRect();
17
- const viewportHeight = window.innerHeight;
18
15
 
19
- const spaceAbove = inputBoxRect.top;
20
- const spaceBelow = viewportHeight - inputBoxRect.bottom;
21
- if (spaceAbove > spaceBelow) {
22
- if (spaceAbove > 90 && spaceBelow < 120) {
23
- return 'top-position';
24
- } else {
25
- return 'bottom-position';
26
- }
27
- } else {
28
- const diff: number = spaceBelow - spaceAbove;
29
- if (spaceAbove > 90 && spaceBelow > 90 && diff < 90) {
30
- return 'top-position';
31
- } else {
32
- return 'bottom-position';
33
- }
34
- }
35
- }
36
- };
37
-
38
- const handleMouseEnter = (e: React.MouseEvent) => {
39
- if (menuButtonRef.current) {
40
- const dropPosition = adjustDropdownPosition();
41
- setDropdownPosition(dropPosition);
42
- const rect = (e.target as HTMLElement).getBoundingClientRect();
43
- let top = rect.bottom + window.scrollY;
44
- let left = rect.left + window.scrollX;
45
- const height = rect.height;
46
- const width = dropRef?.current?.offsetWidth;
47
- if (dropPosition === 'bottom-position') {
48
- top = top + height / 2 - 5;
49
- } else {
50
- top = top - height * 2 + 15;
51
- }
52
-
53
- setTooltipStyle({
54
- top: top,
55
- left: left,
56
- width: width,
57
- });
58
- setVisible(true);
59
- }
16
+ const handleMouseEnter = () => {
17
+ setVisible(true);
60
18
  };
61
19
 
62
20
  const handleMouseLeave = () => {
63
21
  setVisible(false);
64
22
  };
65
23
 
66
- useLayoutEffect(() => {
67
- const updateTooltipPosition = () => {
68
- if (dropRef.current && visible && tooltipStyle) {
69
- const tooltipHeight = dropRef.current.offsetHeight;
70
- const tooltipWidth = dropRef.current.offsetWidth;
71
- const boxWidth = menuButtonRef.current.offsetWidth;
72
- setTooltipStyle((prevStyle) => ({
73
- ...prevStyle,
74
- left:
75
- prevStyle.left + boxWidth / 2 - tooltipWidth / 2 < 0
76
- ? 20
77
- : prevStyle.left + boxWidth / 2 - tooltipWidth / 2,
78
- width: tooltipWidth,
79
- top:
80
- dropdownPosition === 'bottom-position'
81
- ? prevStyle.top
82
- : prevStyle.top - tooltipHeight,
83
- }));
24
+ const updatePosition = useCallback(() => {
25
+ if (!menuButtonRef.current || !dropRef.current) return;
26
+
27
+ const coords = getPortalTooltipCoords(
28
+ menuButtonRef.current,
29
+ dropRef.current
30
+ );
31
+ setDropdownPosition(coords.placement);
32
+ setTooltipStyle((prev) => {
33
+ if (
34
+ prev.top === coords.top &&
35
+ prev.left === coords.left &&
36
+ prev.arrowOffset === coords.arrowOffset
37
+ ) {
38
+ return prev;
84
39
  }
85
- };
40
+ return {
41
+ top: coords.top,
42
+ left: coords.left,
43
+ arrowOffset: coords.arrowOffset,
44
+ };
45
+ });
46
+ }, []);
47
+
48
+ useLayoutEffect(() => {
49
+ if (!visible) return;
86
50
 
87
- if (visible) {
88
- updateTooltipPosition();
89
- }
51
+ updatePosition();
52
+ const frame = window.requestAnimationFrame(updatePosition);
90
53
 
91
- window.addEventListener('resize', updateTooltipPosition);
54
+ window.addEventListener('resize', updatePosition);
55
+ window.addEventListener('scroll', updatePosition, true);
92
56
 
93
57
  return () => {
94
- window.removeEventListener('resize', updateTooltipPosition);
58
+ window.cancelAnimationFrame(frame);
59
+ window.removeEventListener('resize', updatePosition);
60
+ window.removeEventListener('scroll', updatePosition, true);
95
61
  };
96
- }, [visible, dropdownPosition]);
62
+ }, [visible, updatePosition]);
97
63
 
98
64
  useEffect(() => {
99
65
  const handleClickOutside = (event: MouseEvent) => {
@@ -122,19 +88,23 @@ const ExpandableToolTip: React.FC<any> = ({ title, children, enabled }) => {
122
88
  tooltipStyle &&
123
89
  ReactDOM.createPortal(
124
90
  <span
125
- ref={(node) => (dropRef.current = node)}
91
+ ref={(node) => {
92
+ dropRef.current = node;
93
+ }}
126
94
  className={`tooltiptext custom_tooltip_style_class ${
127
95
  dropdownPosition == 'bottom-position' ? 'down' : 'up'
128
96
  }`}
129
- style={{
130
- top: tooltipStyle.top,
131
- left: tooltipStyle.left,
132
- opacity: 1,
133
- width: tooltipStyle.width,
134
- minWidth: '60px',
135
- maxWidth: '200px',
136
- visibility: 'visible',
137
- }}
97
+ style={
98
+ {
99
+ top: tooltipStyle.top,
100
+ left: tooltipStyle.left,
101
+ opacity: 1,
102
+ minWidth: '60px',
103
+ maxWidth: '200px',
104
+ visibility: 'visible',
105
+ '--tooltip-arrow-left': `${tooltipStyle.arrowOffset}px`,
106
+ } as React.CSSProperties
107
+ }
138
108
  >
139
109
  <span className="tooltip-text-container">{title}</span>
140
110
  </span>,
@@ -59,3 +59,65 @@ export const applyToolTipPositionClass = (
59
59
  textFieldElement.classList.add(positionClass);
60
60
  }
61
61
  };
62
+
63
+ export type PortalTooltipPlacement = 'top-position' | 'bottom-position';
64
+
65
+ export type PortalTooltipCoords = {
66
+ top: number;
67
+ left: number;
68
+ arrowOffset: number;
69
+ placement: PortalTooltipPlacement;
70
+ };
71
+
72
+ const VIEWPORT_PAD = 8;
73
+ const TOOLTIP_GAP = 8;
74
+ const ARROW_HALF = 5;
75
+
76
+ /** Center a portaled tooltip on its trigger and keep the arrow on the trigger. */
77
+ export const getPortalTooltipCoords = (
78
+ anchorEl: HTMLElement,
79
+ tooltipEl: HTMLElement
80
+ ): PortalTooltipCoords => {
81
+ const anchor = anchorEl.getBoundingClientRect();
82
+ const tooltipWidth = Math.max(tooltipEl.offsetWidth, 60);
83
+ const tooltipHeight = tooltipEl.offsetHeight;
84
+
85
+ const spaceAbove = anchor.top;
86
+ const spaceBelow = window.innerHeight - anchor.bottom;
87
+
88
+ let placement: PortalTooltipPlacement = 'bottom-position';
89
+ if (spaceBelow >= tooltipHeight + 20) {
90
+ placement = 'bottom-position';
91
+ } else if (spaceAbove >= tooltipHeight + 20) {
92
+ placement = 'top-position';
93
+ } else {
94
+ placement = spaceBelow > spaceAbove ? 'bottom-position' : 'top-position';
95
+ }
96
+
97
+ const top =
98
+ placement === 'bottom-position'
99
+ ? anchor.bottom + TOOLTIP_GAP + window.scrollY
100
+ : anchor.top - tooltipHeight - TOOLTIP_GAP + window.scrollY;
101
+
102
+ const triggerCenterX = anchor.left + anchor.width / 2;
103
+ const minLeft = VIEWPORT_PAD;
104
+ const maxLeft = window.innerWidth - tooltipWidth - VIEWPORT_PAD;
105
+ let left =
106
+ maxLeft < minLeft
107
+ ? minLeft
108
+ : Math.min(Math.max(triggerCenterX - tooltipWidth / 2, minLeft), maxLeft);
109
+
110
+ const minArrow = ARROW_HALF + 6;
111
+ const maxArrow = Math.max(minArrow, tooltipWidth - ARROW_HALF - 6);
112
+ const arrowOffset = Math.min(
113
+ Math.max(triggerCenterX - left, minArrow),
114
+ maxArrow
115
+ );
116
+
117
+ return {
118
+ top,
119
+ left: left + window.scrollX,
120
+ arrowOffset,
121
+ placement,
122
+ };
123
+ };
@@ -1 +0,0 @@
1
- export {};
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const stylesContent_1 = require("./generated/stylesContent");
4
- const STYLE_ID = 'react-core-ts-styles';
5
- (function injectReactCoreTsStyles() {
6
- if (typeof document === 'undefined')
7
- return;
8
- if (document.getElementById(STYLE_ID))
9
- return;
10
- const style = document.createElement('style');
11
- style.id = STYLE_ID;
12
- style.setAttribute('data-react-core-ts', '');
13
- style.textContent = stylesContent_1.STYLES;
14
- document.head.appendChild(style);
15
- })();
16
- //# sourceMappingURL=injectStyles.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"injectStyles.js","sourceRoot":"","sources":["../src/injectStyles.ts"],"names":[],"mappings":";;AAAA,6DAAmD;AAEnD,MAAM,QAAQ,GAAG,sBAAsB,CAAC;AAExC,CAAC,SAAS,uBAAuB;IAC/B,IAAI,OAAO,QAAQ,KAAK,WAAW;QAAE,OAAO;IAC5C,IAAI,QAAQ,CAAC,cAAc,CAAC,QAAQ,CAAC;QAAE,OAAO;IAE9C,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAC9C,KAAK,CAAC,EAAE,GAAG,QAAQ,CAAC;IACpB,KAAK,CAAC,YAAY,CAAC,oBAAoB,EAAE,EAAE,CAAC,CAAC;IAC7C,KAAK,CAAC,WAAW,GAAG,sBAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;AACnC,CAAC,CAAC,EAAE,CAAC"}