@vuu-ui/vuu-popups 0.8.27-debug → 0.8.28-debug

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/cjs/index.js CHANGED
@@ -440,12 +440,16 @@ var PopupComponent2 = ({
440
440
  className,
441
441
  anchorElement,
442
442
  minWidth,
443
+ offsetLeft,
444
+ offsetTop,
443
445
  placement,
444
446
  position: positionProp
445
447
  }) => {
446
448
  const { popupRef, position } = useAnchoredPosition({
447
449
  anchorElement,
448
450
  minWidth,
451
+ offsetLeft,
452
+ offsetTop,
449
453
  placement,
450
454
  position: positionProp
451
455
  });
@@ -980,6 +984,7 @@ var MenuList = ({
980
984
  const hasSubMenu = isMenuItemGroup(child);
981
985
  const subMenuShowing = hasSubMenu && childMenuShowing === itemId;
982
986
  const ariaControls = subMenuShowing ? `${id}-${itemId}` : void 0;
987
+ const ariaLabel = (label != null ? label : typeof children2 === "string") ? children2 : void 0;
983
988
  list.push(
984
989
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
985
990
  MenuItem,
@@ -998,6 +1003,7 @@ var MenuList = ({
998
1003
  "aria-controls": ariaControls,
999
1004
  "aria-haspopup": hasSubMenu || void 0,
1000
1005
  "aria-expanded": subMenuShowing || void 0,
1006
+ "aria-label": ariaLabel,
1001
1007
  children: hasSubMenu ? maybeIcon(label != null ? label : children2, withIcon, iconName) : maybeIcon(children2, withIcon, iconName)
1002
1008
  }
1003
1009
  )
@@ -1887,23 +1893,18 @@ var Prompt = ({
1887
1893
 
1888
1894
  // src/tooltip/useAnchoredPosition.ts
1889
1895
  var import_react16 = require("react");
1890
- var getPositionRelativeToAnchor2 = (anchorElement, placement, offsetLeft, offsetTop) => {
1891
- const { bottom, height, left, right, top, width } = anchorElement.getBoundingClientRect();
1892
- const midX = left + width / 2;
1893
- const midY = top + height / 2;
1894
- switch (placement) {
1895
- case "above":
1896
- return { left: midX + offsetLeft, top: top + offsetTop };
1897
- case "below":
1898
- return { left: midX + offsetLeft, top: bottom + offsetTop };
1899
- case "right":
1900
- return { left: right + offsetLeft, top: midY + offsetLeft };
1901
- case "left":
1902
- return { left: left + offsetLeft, top: midY + offsetLeft };
1903
- default:
1904
- throw Error(
1905
- "Tooltip getPositionRelativeToAnchor only supported placement values are left, right, below and right"
1906
- );
1896
+ var pointerSize = 12;
1897
+ var roomAbove = (anchor, height) => height < anchor.top;
1898
+ var roomBelow = (anchor, height) => document.body.clientHeight - anchor.bottom > height;
1899
+ var getNextPlacement = (placement) => {
1900
+ if (Array.isArray(placement)) {
1901
+ if (placement.length === 0) {
1902
+ return [void 0, placement];
1903
+ } else {
1904
+ return [placement[0], placement.slice(1)];
1905
+ }
1906
+ } else {
1907
+ return [placement, []];
1907
1908
  }
1908
1909
  };
1909
1910
  var useAnchoredPosition2 = ({
@@ -1912,19 +1913,49 @@ var useAnchoredPosition2 = ({
1912
1913
  offsetTop = 0,
1913
1914
  placement
1914
1915
  }) => {
1915
- const [position, setPosition] = (0, import_react16.useState)();
1916
+ const ref = (0, import_react16.useCallback)(
1917
+ (el) => {
1918
+ if (el && anchorElement.current) {
1919
+ const anchor = anchorElement.current.getBoundingClientRect();
1920
+ const { height, width } = el.getBoundingClientRect();
1921
+ let nextPlacement;
1922
+ let placements = placement;
1923
+ do {
1924
+ [nextPlacement, placements] = getNextPlacement(placements);
1925
+ switch (nextPlacement) {
1926
+ case "above":
1927
+ if (roomAbove(anchor, height + pointerSize)) {
1928
+ const midDiff = (width - anchor.width) / 2;
1929
+ el.style.cssText = `left:${anchor.left - midDiff}px;top:${anchor.top - height - pointerSize}px;opacity: 1;`;
1930
+ el.dataset.align = "above";
1931
+ return;
1932
+ }
1933
+ break;
1934
+ case "below":
1935
+ if (roomBelow(anchor, height + pointerSize)) {
1936
+ const midDiff = (width - anchor.width) / 2;
1937
+ el.style.cssText = `left:${anchor.left - midDiff}px;top:${anchor.bottom + pointerSize}px;opacity: 1;`;
1938
+ el.dataset.align = "below";
1939
+ return;
1940
+ }
1941
+ break;
1942
+ case "right":
1943
+ console.log("place the fucker right");
1944
+ break;
1945
+ case "left":
1946
+ console.log("place the fucker left");
1947
+ break;
1948
+ default:
1949
+ console.warn(`unklnown tooltip placement ${placement}`);
1950
+ }
1951
+ } while (nextPlacement);
1952
+ }
1953
+ },
1954
+ [anchorElement, placement]
1955
+ );
1916
1956
  (0, import_react16.useLayoutEffect)(() => {
1917
- if (anchorElement.current) {
1918
- const position2 = getPositionRelativeToAnchor2(
1919
- anchorElement.current,
1920
- placement,
1921
- offsetLeft,
1922
- offsetTop
1923
- );
1924
- setPosition(position2);
1925
- }
1926
1957
  }, [anchorElement, offsetLeft, offsetTop, placement]);
1927
- return position;
1958
+ return ref;
1928
1959
  };
1929
1960
 
1930
1961
  // src/tooltip/Tooltip.tsx
@@ -1934,26 +1965,27 @@ var classBase6 = "vuuTooltip";
1934
1965
  var Tooltip = ({
1935
1966
  anchorElement,
1936
1967
  children,
1968
+ className,
1937
1969
  id,
1938
1970
  onMouseEnter,
1939
1971
  onMouseLeave,
1940
- placement,
1972
+ placement: placementProp,
1941
1973
  status,
1942
1974
  style: styleProp
1943
1975
  }) => {
1944
- const position = useAnchoredPosition2({ anchorElement, placement });
1945
- if (position === void 0) {
1946
- return null;
1947
- }
1976
+ const ref = useAnchoredPosition2({
1977
+ anchorElement,
1978
+ placement: placementProp
1979
+ });
1948
1980
  return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(Portal, { children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
1949
1981
  "div",
1950
1982
  {
1951
- className: (0, import_clsx9.default)(classBase6, {
1983
+ className: (0, import_clsx9.default)(classBase6, className, "vuuHidden", {
1952
1984
  [`${classBase6}-error`]: status === "error"
1953
1985
  }),
1954
- "data-align": placement,
1955
1986
  id,
1956
- style: { ...styleProp, ...position },
1987
+ ref,
1988
+ style: { ...styleProp, left: 0, top: 0 },
1957
1989
  children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
1958
1990
  "span",
1959
1991
  {
@@ -1971,11 +2003,13 @@ var Tooltip = ({
1971
2003
  var import_vuu_utils9 = require("@vuu-ui/vuu-utils");
1972
2004
  var import_react17 = require("react");
1973
2005
  var useTooltip = ({
2006
+ anchorQuery = "*",
1974
2007
  id: idProp,
1975
2008
  placement = "right",
1976
2009
  tooltipContent
1977
2010
  }) => {
1978
2011
  const hideTooltipRef = (0, import_react17.useRef)();
2012
+ const isHoveringRef = (0, import_react17.useRef)(false);
1979
2013
  const anchorElementRef = (0, import_react17.useRef)(null);
1980
2014
  const mouseEnterTimerRef = (0, import_react17.useRef)();
1981
2015
  const mouseLeaveTimerRef = (0, import_react17.useRef)();
@@ -1998,52 +2032,64 @@ var useTooltip = ({
1998
2032
  var _a;
1999
2033
  (_a = hideTooltipRef.current) == null ? void 0 : _a.call(hideTooltipRef);
2000
2034
  }, []);
2001
- const showTooltip = (0, import_react17.useCallback)(() => {
2002
- const { current: anchorEl } = anchorElementRef;
2003
- if (anchorEl) {
2004
- setTooltipProps({
2005
- anchorElement: anchorElementRef,
2006
- children: tooltipContent,
2007
- id: `${id}-tooltip`,
2008
- onMouseEnter: handleMouseEnterTooltip,
2009
- onMouseLeave: handleMouseLeaveTooltip,
2010
- placement
2011
- });
2012
- document.addEventListener("keydown", escapeListener);
2035
+ const hideTooltip = (0, import_react17.useCallback)((defer = 0) => {
2036
+ if (mouseEnterTimerRef.current) {
2037
+ window.clearTimeout(mouseEnterTimerRef.current);
2038
+ mouseEnterTimerRef.current = void 0;
2039
+ } else if (hideTooltipRef.current) {
2040
+ if (defer === 0) {
2041
+ hideTooltipRef.current();
2042
+ } else {
2043
+ mouseLeaveTimerRef.current = window.setTimeout(
2044
+ hideTooltipRef.current,
2045
+ defer
2046
+ );
2047
+ }
2013
2048
  }
2014
- mouseEnterTimerRef.current = void 0;
2015
- }, [
2016
- escapeListener,
2017
- handleMouseEnterTooltip,
2018
- handleMouseLeaveTooltip,
2019
- id,
2020
- placement,
2021
- tooltipContent
2022
- ]);
2049
+ }, []);
2050
+ const showTooltip = (0, import_react17.useCallback)(
2051
+ (ref = anchorElementRef) => {
2052
+ const { current: anchorEl } = ref;
2053
+ if (anchorEl) {
2054
+ setTooltipProps({
2055
+ anchorElement: ref,
2056
+ children: tooltipContent,
2057
+ id: `${id}-tooltip`,
2058
+ onMouseEnter: handleMouseEnterTooltip,
2059
+ onMouseLeave: handleMouseLeaveTooltip,
2060
+ placement
2061
+ });
2062
+ document.addEventListener("keydown", escapeListener);
2063
+ }
2064
+ mouseEnterTimerRef.current = void 0;
2065
+ hideTooltip(isHoveringRef.current ? 3e3 : 1e3);
2066
+ },
2067
+ [
2068
+ escapeListener,
2069
+ handleMouseEnterTooltip,
2070
+ handleMouseLeaveTooltip,
2071
+ hideTooltip,
2072
+ id,
2073
+ placement,
2074
+ tooltipContent
2075
+ ]
2076
+ );
2023
2077
  const handleMouseEnter = (0, import_react17.useCallback)(
2024
2078
  (evt) => {
2025
- const el = evt.target;
2079
+ isHoveringRef.current = true;
2080
+ const el = (0, import_vuu_utils9.queryClosest)(evt.target, anchorQuery);
2026
2081
  if (el) {
2082
+ console.log(`el ${el.classList}`);
2027
2083
  anchorElementRef.current = el;
2028
2084
  mouseEnterTimerRef.current = window.setTimeout(showTooltip, 800);
2029
2085
  }
2030
2086
  },
2031
- [showTooltip]
2087
+ [anchorQuery, showTooltip]
2032
2088
  );
2033
2089
  const handleMouseLeave = (0, import_react17.useCallback)(() => {
2034
- if (anchorElementRef.current)
2035
- if (mouseEnterTimerRef.current) {
2036
- window.clearTimeout(mouseEnterTimerRef.current);
2037
- mouseEnterTimerRef.current = void 0;
2038
- } else {
2039
- if (hideTooltipRef.current) {
2040
- mouseLeaveTimerRef.current = window.setTimeout(
2041
- hideTooltipRef.current,
2042
- 200
2043
- );
2044
- }
2045
- }
2046
- }, []);
2090
+ isHoveringRef.current = false;
2091
+ hideTooltip(200);
2092
+ }, [hideTooltip]);
2047
2093
  const anchorProps = {
2048
2094
  "aria-describedby": `${id}-tooltip`,
2049
2095
  onMouseEnter: handleMouseEnter,
@@ -2051,6 +2097,8 @@ var useTooltip = ({
2051
2097
  };
2052
2098
  return {
2053
2099
  anchorProps,
2100
+ hideTooltip,
2101
+ showTooltip,
2054
2102
  tooltipProps
2055
2103
  };
2056
2104
  };