react-better-html 1.1.285 → 1.1.286

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.
package/dist/index.js CHANGED
@@ -7453,9 +7453,9 @@ var TooltipComponent = (0, import_react29.forwardRef)(function Tooltip({
7453
7453
  children
7454
7454
  }, ref) {
7455
7455
  const theme2 = (0, import_react_better_core26.useTheme)();
7456
+ const wrapperRef = (0, import_react29.useRef)(null);
7456
7457
  const triggerHolderRef = (0, import_react29.useRef)(null);
7457
7458
  const contentRef = (0, import_react29.useRef)(null);
7458
- const tooltipContainerRef = (0, import_react29.useRef)(null);
7459
7459
  const closeTimerRef = (0, import_react29.useRef)(void 0);
7460
7460
  const [isOpen, setIsOpen] = (0, import_react29.useState)(false);
7461
7461
  const [isOpenLate, setIsOpenLate] = (0, import_react29.useState)(false);
@@ -7468,26 +7468,8 @@ var TooltipComponent = (0, import_react29.forwardRef)(function Tooltip({
7468
7468
  if (closeTimerRef.current) clearTimeout(closeTimerRef.current);
7469
7469
  setIsOpen(true);
7470
7470
  setIsOpenLate(true);
7471
- setTimeout(() => {
7472
- if (!tooltipContainerRef.current) return;
7473
- if (!contentRef.current) return;
7474
- const clientRects = tooltipContainerRef.current.getBoundingClientRect();
7475
- if (clientRects) {
7476
- const { width, height, x, y } = clientRects;
7477
- const topOutside = y < 0;
7478
- const bottomOutside = y + height > window.innerHeight;
7479
- const leftOutside = x < 0;
7480
- const rightOutside = x + width > window.innerWidth;
7481
- if (topOutside) contentRef.current.style.transform = `translateY(${y * -1 + outsideScreenGap}px)`;
7482
- if (bottomOutside)
7483
- contentRef.current.style.transform = `translateY(${window.innerHeight - (y + height) - totalGap}px)`;
7484
- if (leftOutside) contentRef.current.style.transform = `translateX(${x * -1 + outsideScreenGap}px)`;
7485
- if (rightOutside)
7486
- contentRef.current.style.transform = `translateX(${window.innerWidth - (x + width) - totalGap}px)`;
7487
- }
7488
- }, 1);
7489
7471
  onOpen?.();
7490
- }, [disabled, onOpen, outsideScreenGap, totalGap]);
7472
+ }, [disabled, onOpen]);
7491
7473
  const closeTooltip = (0, import_react29.useCallback)(() => {
7492
7474
  setIsOpen(false);
7493
7475
  closeTimerRef.current = setTimeout(() => setIsOpenLate(false), 300);
@@ -7518,6 +7500,29 @@ var TooltipComponent = (0, import_react29.forwardRef)(function Tooltip({
7518
7500
  },
7519
7501
  [trigger, isOpen, closeTooltip]
7520
7502
  );
7503
+ const clampContentInsideViewport = (0, import_react29.useCallback)(() => {
7504
+ if (!wrapperRef.current || !contentRef.current) return;
7505
+ const wrapperRect = wrapperRef.current.getBoundingClientRect();
7506
+ const contentWidth2 = contentRef.current.offsetWidth;
7507
+ const contentHeight = contentRef.current.offsetHeight;
7508
+ let expectedLeft;
7509
+ let expectedTop;
7510
+ if (position === "top" || position === "bottom") {
7511
+ expectedTop = position === "top" ? wrapperRect.top - totalGap - contentHeight : wrapperRect.bottom + totalGap;
7512
+ expectedLeft = align === "left" ? wrapperRect.left : align === "right" ? wrapperRect.right - contentWidth2 : wrapperRect.left + wrapperRect.width / 2 - contentWidth2 / 2;
7513
+ } else {
7514
+ expectedLeft = position === "left" ? wrapperRect.left - totalGap - contentWidth2 : wrapperRect.right + totalGap;
7515
+ expectedTop = align === "top" ? wrapperRect.top : align === "bottom" ? wrapperRect.bottom - contentHeight : wrapperRect.top + wrapperRect.height / 2 - contentHeight / 2;
7516
+ }
7517
+ const getShift = (start, size, viewportSize) => {
7518
+ if (start < outsideScreenGap) return outsideScreenGap - start;
7519
+ if (start + size > viewportSize - outsideScreenGap) return viewportSize - outsideScreenGap - (start + size);
7520
+ return 0;
7521
+ };
7522
+ const shiftX = getShift(expectedLeft, contentWidth2, window.innerWidth);
7523
+ const shiftY = getShift(expectedTop, contentHeight, window.innerHeight);
7524
+ contentRef.current.style.transform = shiftX || shiftY ? `translate(${shiftX}px, ${shiftY}px)` : "";
7525
+ }, [position, align, totalGap, outsideScreenGap]);
7521
7526
  (0, import_react29.useEffect)(() => {
7522
7527
  if (trigger === "click") {
7523
7528
  document.addEventListener("mousedown", onClickOutside);
@@ -7530,6 +7535,9 @@ var TooltipComponent = (0, import_react29.forwardRef)(function Tooltip({
7530
7535
  if (!disabled) return;
7531
7536
  closeTooltip();
7532
7537
  }, [disabled]);
7538
+ (0, import_react29.useLayoutEffect)(() => {
7539
+ if (isOpen) clampContentInsideViewport();
7540
+ }, [isOpen, clampContentInsideViewport]);
7533
7541
  (0, import_react29.useImperativeHandle)(ref, () => {
7534
7542
  return {
7535
7543
  isOpen,
@@ -7546,6 +7554,7 @@ var TooltipComponent = (0, import_react29.forwardRef)(function Tooltip({
7546
7554
  onClick: onClickHolder,
7547
7555
  onMouseEnter,
7548
7556
  onMouseLeave,
7557
+ ref: wrapperRef,
7549
7558
  children: [
7550
7559
  /* @__PURE__ */ (0, import_jsx_runtime26.jsx)(
7551
7560
  Div_default,
@@ -7569,7 +7578,6 @@ var TooltipComponent = (0, import_react29.forwardRef)(function Tooltip({
7569
7578
  gap,
7570
7579
  isOpen,
7571
7580
  role: "tooltip",
7572
- ref: tooltipContainerRef,
7573
7581
  children: (isOpen || isOpenLate) && /* @__PURE__ */ (0, import_jsx_runtime26.jsxs)(Div_default, { position: "relative", ref: contentRef, children: [
7574
7582
  /* @__PURE__ */ (0, import_jsx_runtime26.jsx)(
7575
7583
  Div_default.box,