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.mjs CHANGED
@@ -7252,7 +7252,17 @@ var Table2 = memo23(TableComponent);
7252
7252
  var Table_default = Table2;
7253
7253
 
7254
7254
  // src/components/Tooltip.tsx
7255
- import { memo as memo24, useCallback as useCallback16, useRef as useRef7, useState as useState12, useEffect as useEffect13, forwardRef as forwardRef16, useImperativeHandle as useImperativeHandle3, useMemo as useMemo10 } from "react";
7255
+ import {
7256
+ memo as memo24,
7257
+ useCallback as useCallback16,
7258
+ useRef as useRef7,
7259
+ useState as useState12,
7260
+ useEffect as useEffect13,
7261
+ useLayoutEffect,
7262
+ forwardRef as forwardRef16,
7263
+ useImperativeHandle as useImperativeHandle3,
7264
+ useMemo as useMemo10
7265
+ } from "react";
7256
7266
  import { useTheme as useTheme26 } from "react-better-core";
7257
7267
  import styled13, { css as css6 } from "styled-components";
7258
7268
  import { jsx as jsx25, jsxs as jsxs21 } from "react/jsx-runtime";
@@ -7412,9 +7422,9 @@ var TooltipComponent = forwardRef16(function Tooltip({
7412
7422
  children
7413
7423
  }, ref) {
7414
7424
  const theme2 = useTheme26();
7425
+ const wrapperRef = useRef7(null);
7415
7426
  const triggerHolderRef = useRef7(null);
7416
7427
  const contentRef = useRef7(null);
7417
- const tooltipContainerRef = useRef7(null);
7418
7428
  const closeTimerRef = useRef7(void 0);
7419
7429
  const [isOpen, setIsOpen] = useState12(false);
7420
7430
  const [isOpenLate, setIsOpenLate] = useState12(false);
@@ -7427,26 +7437,8 @@ var TooltipComponent = forwardRef16(function Tooltip({
7427
7437
  if (closeTimerRef.current) clearTimeout(closeTimerRef.current);
7428
7438
  setIsOpen(true);
7429
7439
  setIsOpenLate(true);
7430
- setTimeout(() => {
7431
- if (!tooltipContainerRef.current) return;
7432
- if (!contentRef.current) return;
7433
- const clientRects = tooltipContainerRef.current.getBoundingClientRect();
7434
- if (clientRects) {
7435
- const { width, height, x, y } = clientRects;
7436
- const topOutside = y < 0;
7437
- const bottomOutside = y + height > window.innerHeight;
7438
- const leftOutside = x < 0;
7439
- const rightOutside = x + width > window.innerWidth;
7440
- if (topOutside) contentRef.current.style.transform = `translateY(${y * -1 + outsideScreenGap}px)`;
7441
- if (bottomOutside)
7442
- contentRef.current.style.transform = `translateY(${window.innerHeight - (y + height) - totalGap}px)`;
7443
- if (leftOutside) contentRef.current.style.transform = `translateX(${x * -1 + outsideScreenGap}px)`;
7444
- if (rightOutside)
7445
- contentRef.current.style.transform = `translateX(${window.innerWidth - (x + width) - totalGap}px)`;
7446
- }
7447
- }, 1);
7448
7440
  onOpen?.();
7449
- }, [disabled, onOpen, outsideScreenGap, totalGap]);
7441
+ }, [disabled, onOpen]);
7450
7442
  const closeTooltip = useCallback16(() => {
7451
7443
  setIsOpen(false);
7452
7444
  closeTimerRef.current = setTimeout(() => setIsOpenLate(false), 300);
@@ -7477,6 +7469,29 @@ var TooltipComponent = forwardRef16(function Tooltip({
7477
7469
  },
7478
7470
  [trigger, isOpen, closeTooltip]
7479
7471
  );
7472
+ const clampContentInsideViewport = useCallback16(() => {
7473
+ if (!wrapperRef.current || !contentRef.current) return;
7474
+ const wrapperRect = wrapperRef.current.getBoundingClientRect();
7475
+ const contentWidth2 = contentRef.current.offsetWidth;
7476
+ const contentHeight = contentRef.current.offsetHeight;
7477
+ let expectedLeft;
7478
+ let expectedTop;
7479
+ if (position === "top" || position === "bottom") {
7480
+ expectedTop = position === "top" ? wrapperRect.top - totalGap - contentHeight : wrapperRect.bottom + totalGap;
7481
+ expectedLeft = align === "left" ? wrapperRect.left : align === "right" ? wrapperRect.right - contentWidth2 : wrapperRect.left + wrapperRect.width / 2 - contentWidth2 / 2;
7482
+ } else {
7483
+ expectedLeft = position === "left" ? wrapperRect.left - totalGap - contentWidth2 : wrapperRect.right + totalGap;
7484
+ expectedTop = align === "top" ? wrapperRect.top : align === "bottom" ? wrapperRect.bottom - contentHeight : wrapperRect.top + wrapperRect.height / 2 - contentHeight / 2;
7485
+ }
7486
+ const getShift = (start, size, viewportSize) => {
7487
+ if (start < outsideScreenGap) return outsideScreenGap - start;
7488
+ if (start + size > viewportSize - outsideScreenGap) return viewportSize - outsideScreenGap - (start + size);
7489
+ return 0;
7490
+ };
7491
+ const shiftX = getShift(expectedLeft, contentWidth2, window.innerWidth);
7492
+ const shiftY = getShift(expectedTop, contentHeight, window.innerHeight);
7493
+ contentRef.current.style.transform = shiftX || shiftY ? `translate(${shiftX}px, ${shiftY}px)` : "";
7494
+ }, [position, align, totalGap, outsideScreenGap]);
7480
7495
  useEffect13(() => {
7481
7496
  if (trigger === "click") {
7482
7497
  document.addEventListener("mousedown", onClickOutside);
@@ -7489,6 +7504,9 @@ var TooltipComponent = forwardRef16(function Tooltip({
7489
7504
  if (!disabled) return;
7490
7505
  closeTooltip();
7491
7506
  }, [disabled]);
7507
+ useLayoutEffect(() => {
7508
+ if (isOpen) clampContentInsideViewport();
7509
+ }, [isOpen, clampContentInsideViewport]);
7492
7510
  useImperativeHandle3(ref, () => {
7493
7511
  return {
7494
7512
  isOpen,
@@ -7505,6 +7523,7 @@ var TooltipComponent = forwardRef16(function Tooltip({
7505
7523
  onClick: onClickHolder,
7506
7524
  onMouseEnter,
7507
7525
  onMouseLeave,
7526
+ ref: wrapperRef,
7508
7527
  children: [
7509
7528
  /* @__PURE__ */ jsx25(
7510
7529
  Div_default,
@@ -7528,7 +7547,6 @@ var TooltipComponent = forwardRef16(function Tooltip({
7528
7547
  gap,
7529
7548
  isOpen,
7530
7549
  role: "tooltip",
7531
- ref: tooltipContainerRef,
7532
7550
  children: (isOpen || isOpenLate) && /* @__PURE__ */ jsxs21(Div_default, { position: "relative", ref: contentRef, children: [
7533
7551
  /* @__PURE__ */ jsx25(
7534
7552
  Div_default.box,