@sanity/ui 2.0.0-alpha.20 → 2.0.0-alpha.21

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.esm.js CHANGED
@@ -5,7 +5,7 @@ import styled, { ThemeProvider as ThemeProvider$1, useTheme as useTheme$1, css,
5
5
  import { color as color$1, white as white$1, black as black$1, hues } from '@sanity/color';
6
6
  import { SpinnerIcon, CheckmarkIcon, RemoveIcon, SelectIcon, CloseIcon, ChevronDownIcon, ChevronRightIcon, ToggleArrowRightIcon } from '@sanity/icons';
7
7
  import Refractor from 'react-refractor';
8
- import { detectOverflow, flip, offset, shift, arrow, hide, useFloating, autoUpdate, size as size$2 } from '@floating-ui/react-dom';
8
+ import { detectOverflow, flip, offset, shift, arrow, hide, useFloating, autoUpdate } from '@floating-ui/react-dom';
9
9
  import { createPortal } from 'react-dom';
10
10
  import { AnimatePresence, motion } from 'framer-motion';
11
11
  const EMPTY_ARRAY = [];
@@ -7803,6 +7803,7 @@ function useDelayedState(initialState) {
7803
7803
  }, []);
7804
7804
  return [state, onStateChange];
7805
7805
  }
7806
+ const DEFAULT_TOOLTIP_PADDING = 4;
7806
7807
  const DEFAULT_FALLBACK_PLACEMENTS = {
7807
7808
  top: ["bottom", "left", "right"],
7808
7809
  "top-start": ["bottom-start", "left-start", "right-start"],
@@ -7886,9 +7887,14 @@ var __template$e = (cooked, raw) => __freeze$e(__defProp$e(cooked, "raw", {
7886
7887
  value: __freeze$e(raw || cooked.slice())
7887
7888
  }));
7888
7889
  var _a$e;
7889
- const Root$a = styled(Layer)(_a$e || (_a$e = __template$e(["\n pointer-events: none;\n"])));
7890
+ const Root$a = styled(Layer)(_a$e || (_a$e = __template$e(["\n pointer-events: none;\n max-width: ", "px;\n"])), _ref27 => {
7891
+ let {
7892
+ $maxWidth
7893
+ } = _ref27;
7894
+ return $maxWidth;
7895
+ });
7890
7896
  const Tooltip = forwardRef(function Tooltip2(props, ref) {
7891
- var _a2, _b, _c, _d, _e;
7897
+ var _a2, _b, _c, _d, _e, _f;
7892
7898
  const boundaryElementContext = useBoundaryElement();
7893
7899
  const theme = useTheme();
7894
7900
  const {
@@ -7899,7 +7905,7 @@ const Tooltip = forwardRef(function Tooltip2(props, ref) {
7899
7905
  fallbackPlacements: fallbackPlacementsProp = (_b = props.fallbackPlacements) != null ? _b : DEFAULT_FALLBACK_PLACEMENTS[(_a2 = props.placement) != null ? _a2 : "bottom"],
7900
7906
  padding = 3,
7901
7907
  placement: placementProp = "bottom",
7902
- portal,
7908
+ portal: portalProp,
7903
7909
  scheme,
7904
7910
  shadow = 2,
7905
7911
  zOffset = (_c = theme.sanity.layer) == null ? void 0 : _c.tooltip.zOffset,
@@ -7911,38 +7917,27 @@ const Tooltip = forwardRef(function Tooltip2(props, ref) {
7911
7917
  const [referenceElement, setReferenceElement] = useState(null);
7912
7918
  const arrowRef = useRef(null);
7913
7919
  const rootBoundary = "viewport";
7920
+ const portal = usePortal();
7921
+ const portalElement = typeof portalProp === "string" ? ((_d = portal.elements) == null ? void 0 : _d[portalProp]) || null : portal.element;
7922
+ const tooltipWidth = useMemo(() => {
7923
+ const availableWidths = [...(boundaryElement ? [boundaryElement.offsetWidth] : []), (portalElement == null ? void 0 : portalElement.offsetWidth) || document.body.offsetWidth];
7924
+ return Math.min(...availableWidths) - DEFAULT_TOOLTIP_PADDING * 2;
7925
+ }, [boundaryElement, portalElement == null ? void 0 : portalElement.offsetWidth]);
7914
7926
  const middleware = useMemo(() => {
7915
7927
  const ret = [];
7916
7928
  ret.push(flip({
7917
7929
  boundary: boundaryElement || void 0,
7918
7930
  fallbackPlacements,
7919
- padding: 4,
7920
- rootBoundary,
7921
- mainAxis: false
7931
+ padding: DEFAULT_TOOLTIP_PADDING,
7932
+ rootBoundary
7922
7933
  }));
7923
7934
  ret.push(offset({
7924
7935
  mainAxis: 3
7925
7936
  }));
7926
- ret.push(size$2({
7927
- apply(_ref27) {
7928
- let {
7929
- availableWidth,
7930
- availableHeight,
7931
- elements
7932
- } = _ref27;
7933
- Object.assign(elements.floating.style, {
7934
- maxWidth: "".concat(availableWidth - 4 * 2, "px"),
7935
- // the padding is `4px`
7936
- maxHeight: "".concat(availableHeight - 4 * 2, "px")
7937
- // the padding is `4px`
7938
- });
7939
- }
7940
- }));
7941
-
7942
7937
  ret.push(shift({
7943
7938
  boundary: boundaryElement || void 0,
7944
7939
  rootBoundary,
7945
- padding: 4
7940
+ padding: DEFAULT_TOOLTIP_PADDING
7946
7941
  }));
7947
7942
  ret.push(arrow({
7948
7943
  element: arrowRef,
@@ -7962,8 +7957,8 @@ const Tooltip = forwardRef(function Tooltip2(props, ref) {
7962
7957
  whileElementsMounted: autoUpdate
7963
7958
  });
7964
7959
  const staticSide = placement && FLOATING_STATIC_SIDES[placement.split("-")[0]];
7965
- const arrowX = (_d = middlewareData.arrow) == null ? void 0 : _d.x;
7966
- const arrowY = (_e = middlewareData.arrow) == null ? void 0 : _e.y;
7960
+ const arrowX = (_e = middlewareData.arrow) == null ? void 0 : _e.x;
7961
+ const arrowY = (_f = middlewareData.arrow) == null ? void 0 : _f.y;
7967
7962
  const arrowStyle = useMemo(() => {
7968
7963
  const style = {
7969
7964
  left: arrowX !== null ? arrowX : void 0,
@@ -8000,10 +7995,36 @@ const Tooltip = forwardRef(function Tooltip2(props, ref) {
8000
7995
  setIsOpen(open, standaloneDelay);
8001
7996
  }
8002
7997
  }, [isInsideGroup, delayGroupContext, openDelay, tooltipId, closeDelay, setIsOpen]);
8003
- const handleBlur = useCallback(() => handleIsOpenChange(false), [handleIsOpenChange]);
8004
- const handleFocus = useCallback(() => handleIsOpenChange(true), [handleIsOpenChange]);
8005
- const handleMouseEnter = useCallback(() => handleIsOpenChange(true), [handleIsOpenChange]);
8006
- const handleMouseLeave = useCallback(() => handleIsOpenChange(false), [handleIsOpenChange]);
7998
+ const handleBlur = useCallback(e => {
7999
+ var _a3, _b2;
8000
+ handleIsOpenChange(false);
8001
+ (_b2 = (_a3 = childProp == null ? void 0 : childProp.props) == null ? void 0 : _a3.onBlur) == null ? void 0 : _b2.call(_a3, e);
8002
+ }, [childProp == null ? void 0 : childProp.props, handleIsOpenChange]);
8003
+ const handleClick = useCallback(e => {
8004
+ var _a3, _b2;
8005
+ handleIsOpenChange(false, true);
8006
+ (_b2 = childProp == null ? void 0 : (_a3 = childProp.props).onClick) == null ? void 0 : _b2.call(_a3, e);
8007
+ }, [childProp == null ? void 0 : childProp.props, handleIsOpenChange]);
8008
+ const handleContextMenu = useCallback(e => {
8009
+ var _a3, _b2;
8010
+ handleIsOpenChange(false, true);
8011
+ (_b2 = childProp == null ? void 0 : (_a3 = childProp.props).onContextMenu) == null ? void 0 : _b2.call(_a3, e);
8012
+ }, [childProp == null ? void 0 : childProp.props, handleIsOpenChange]);
8013
+ const handleFocus = useCallback(e => {
8014
+ var _a3, _b2;
8015
+ handleIsOpenChange(true);
8016
+ (_b2 = (_a3 = childProp == null ? void 0 : childProp.props) == null ? void 0 : _a3.onFocus) == null ? void 0 : _b2.call(_a3, e);
8017
+ }, [childProp == null ? void 0 : childProp.props, handleIsOpenChange]);
8018
+ const handleMouseEnter = useCallback(e => {
8019
+ var _a3, _b2;
8020
+ handleIsOpenChange(true);
8021
+ (_b2 = (_a3 = childProp == null ? void 0 : childProp.props) == null ? void 0 : _a3.onMouseEnter) == null ? void 0 : _b2.call(_a3, e);
8022
+ }, [childProp == null ? void 0 : childProp.props, handleIsOpenChange]);
8023
+ const handleMouseLeave = useCallback(e => {
8024
+ var _a3, _b2;
8025
+ handleIsOpenChange(false);
8026
+ (_b2 = (_a3 = childProp == null ? void 0 : childProp.props) == null ? void 0 : _a3.onMouseLeave) == null ? void 0 : _b2.call(_a3, e);
8027
+ }, [childProp == null ? void 0 : childProp.props, handleIsOpenChange]);
8007
8028
  useEffect(() => {
8008
8029
  if (!isOpen) return;
8009
8030
  function handleWindowMouseMove(event) {
@@ -8062,9 +8083,11 @@ const Tooltip = forwardRef(function Tooltip2(props, ref) {
8062
8083
  onFocus: handleFocus,
8063
8084
  onMouseEnter: handleMouseEnter,
8064
8085
  onMouseLeave: handleMouseLeave,
8086
+ onClick: handleClick,
8087
+ onContextMenu: handleContextMenu,
8065
8088
  ref: setReference
8066
8089
  });
8067
- }, [childProp, handleBlur, handleFocus, handleMouseEnter, handleMouseLeave, setReference]);
8090
+ }, [childProp, handleBlur, handleClick, handleContextMenu, handleFocus, handleMouseEnter, handleMouseLeave, setReference]);
8068
8091
  if (!child) return /* @__PURE__ */jsx(Fragment, {});
8069
8092
  if (disabled) return child;
8070
8093
  const root = /* @__PURE__ */jsx(Root$a, {
@@ -8073,6 +8096,7 @@ const Tooltip = forwardRef(function Tooltip2(props, ref) {
8073
8096
  ref: setFloating,
8074
8097
  style: floatingStyles,
8075
8098
  zOffset,
8099
+ $maxWidth: tooltipWidth,
8076
8100
  children: /* @__PURE__ */jsxs(Card, {
8077
8101
  "data-ui": "Tooltip__card",
8078
8102
  "data-placement": placement,
@@ -8088,8 +8112,8 @@ const Tooltip = forwardRef(function Tooltip2(props, ref) {
8088
8112
  });
8089
8113
  return /* @__PURE__ */jsxs(Fragment, {
8090
8114
  children: [child, showTooltip && /* @__PURE__ */jsx(Fragment, {
8091
- children: portal ? /* @__PURE__ */jsx(Portal, {
8092
- __unstable_name: typeof portal === "string" ? portal : void 0,
8115
+ children: portalProp ? /* @__PURE__ */jsx(Portal, {
8116
+ __unstable_name: typeof portalProp === "string" ? portalProp : void 0,
8093
8117
  children: root
8094
8118
  }) : root
8095
8119
  })]