@sanity/ui 2.8.4 → 2.8.6

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
@@ -173,10 +173,10 @@ function useClickOutsideEvent(listener, elementsArg = () => EMPTY_ARRAY, boundar
173
173
  const target = evt.target;
174
174
  if (!(target instanceof Node))
175
175
  return;
176
- const resolvedBoundaryElement = typeof boundaryElement == "function" ? boundaryElement() : boundaryElement;
176
+ const resolvedBoundaryElement = boundaryElement == null ? void 0 : boundaryElement();
177
177
  if (resolvedBoundaryElement && !resolvedBoundaryElement.contains(target))
178
178
  return;
179
- const elements = (Array.isArray(elementsArg) ? elementsArg : elementsArg()).flat();
179
+ const elements = elementsArg().flat();
180
180
  for (const el of elements)
181
181
  if (el && (target === el || el.contains(target)))
182
182
  return;
@@ -188,7 +188,7 @@ function useClickOutsideEvent(listener, elementsArg = () => EMPTY_ARRAY, boundar
188
188
  return document.addEventListener("mousedown", handleEvent), () => {
189
189
  document.removeEventListener("mousedown", handleEvent);
190
190
  };
191
- }, [hasListener, onEvent]);
191
+ }, [hasListener, onEvent]), react.useDebugValue(listener ? "MouseDown On" : "MouseDown Off");
192
192
  }
193
193
  function useCustomValidity(ref, customValidity) {
194
194
  react.useEffect(() => {
@@ -5329,13 +5329,13 @@ function _sortElements(rootElement, elements) {
5329
5329
  elements.sort(_sort);
5330
5330
  }
5331
5331
  function useMenuController(props) {
5332
- const { onKeyDown, originElement, shouldFocus } = props, elementsRef = react.useRef([]), [rootElement, setRootElement] = react.useState(null), [activeIndex, _setActiveIndex] = react.useState(-1), activeIndexRef = react.useRef(activeIndex), activeElement = elementsRef.current[activeIndex] || null, mounted = !!rootElement, setActiveIndex = react.useCallback((nextActiveIndex) => {
5332
+ const { onKeyDown, originElement, shouldFocus, rootElementRef } = props, elementsRef = react.useRef([]), [activeIndex, _setActiveIndex] = react.useState(-1), activeIndexRef = react.useRef(activeIndex), activeElement = react.useMemo(() => elementsRef.current[activeIndex] || null, [activeIndex]), mounted = !!rootElementRef.current, setActiveIndex = react.useCallback((nextActiveIndex) => {
5333
5333
  _setActiveIndex(nextActiveIndex), activeIndexRef.current = nextActiveIndex;
5334
5334
  }, []), mount = react.useCallback(
5335
5335
  (element, selected) => {
5336
5336
  if (!element) return () => {
5337
5337
  };
5338
- if (elementsRef.current.indexOf(element) === -1 && (elementsRef.current.push(element), _sortElements(rootElement, elementsRef.current)), selected) {
5338
+ if (elementsRef.current.indexOf(element) === -1 && (elementsRef.current.push(element), _sortElements(rootElementRef.current, elementsRef.current)), selected) {
5339
5339
  const selectedIndex = elementsRef.current.indexOf(element);
5340
5340
  setActiveIndex(selectedIndex);
5341
5341
  }
@@ -5344,7 +5344,7 @@ function useMenuController(props) {
5344
5344
  idx > -1 && elementsRef.current.splice(idx, 1);
5345
5345
  };
5346
5346
  },
5347
- [rootElement, setActiveIndex]
5347
+ [rootElementRef, setActiveIndex]
5348
5348
  ), handleKeyDown = react.useCallback(
5349
5349
  (event) => {
5350
5350
  if (event.key === "Tab") {
@@ -5399,44 +5399,40 @@ function useMenuController(props) {
5399
5399
  },
5400
5400
  [setActiveIndex]
5401
5401
  ), handleItemMouseLeave = react.useCallback(() => {
5402
- setActiveIndex(-2), rootElement == null || rootElement.focus();
5403
- }, [setActiveIndex, rootElement]);
5402
+ var _a;
5403
+ setActiveIndex(-2), (_a = rootElementRef.current) == null || _a.focus();
5404
+ }, [rootElementRef, setActiveIndex]);
5404
5405
  return react.useEffect(() => {
5405
5406
  if (!mounted) return;
5406
- const rafId = window.requestAnimationFrame(() => {
5407
- const _activeIndex = activeIndexRef.current;
5408
- if (_activeIndex === -1) {
5407
+ const rafId = requestAnimationFrame(() => {
5408
+ if (activeIndex === -1) {
5409
5409
  if (shouldFocus === "first") {
5410
5410
  const el = _getFocusableElements(elementsRef.current)[0];
5411
5411
  if (el) {
5412
5412
  const currentIndex = elementsRef.current.indexOf(el);
5413
- setActiveIndex(currentIndex), activeIndexRef.current = currentIndex;
5413
+ setActiveIndex(currentIndex);
5414
5414
  }
5415
5415
  }
5416
5416
  if (shouldFocus === "last") {
5417
5417
  const focusableElements = _getFocusableElements(elementsRef.current), el = focusableElements[focusableElements.length - 1];
5418
5418
  if (el) {
5419
5419
  const currentIndex = elementsRef.current.indexOf(el);
5420
- setActiveIndex(currentIndex), activeIndexRef.current = currentIndex;
5420
+ setActiveIndex(currentIndex);
5421
5421
  }
5422
5422
  }
5423
5423
  return;
5424
5424
  }
5425
- const element = elementsRef.current[_activeIndex] || null;
5425
+ const element = elementsRef.current[activeIndex] || null;
5426
5426
  element == null || element.focus();
5427
5427
  });
5428
- return () => {
5429
- window.cancelAnimationFrame(rafId);
5430
- };
5428
+ return () => cancelAnimationFrame(rafId);
5431
5429
  }, [activeIndex, mounted, setActiveIndex, shouldFocus]), {
5432
5430
  activeElement,
5433
5431
  activeIndex,
5434
5432
  handleItemMouseEnter,
5435
5433
  handleItemMouseLeave,
5436
5434
  handleKeyDown,
5437
- mount,
5438
- rootElement,
5439
- setRootElement
5435
+ mount
5440
5436
  };
5441
5437
  }
5442
5438
  const Root$5 = styledComponents.styled(Box)`
@@ -5468,34 +5464,23 @@ const Root$5 = styledComponents.styled(Box)`
5468
5464
  handleItemMouseEnter,
5469
5465
  handleItemMouseLeave,
5470
5466
  handleKeyDown,
5471
- mount,
5472
- rootElement,
5473
- setRootElement
5474
- } = useMenuController({ onKeyDown, originElement, shouldFocus }), handleRefChange = react.useCallback(
5467
+ mount
5468
+ } = useMenuController({ onKeyDown, originElement, shouldFocus, rootElementRef: ref }), unregisterElementRef = react.useRef(null), handleRefChange = react.useCallback(
5475
5469
  (el) => {
5476
- setRootElement(el), ref.current = el;
5470
+ unregisterElementRef.current && (unregisterElementRef.current(), unregisterElementRef.current = null), ref.current = el, ref.current && registerElement && (unregisterElementRef.current = registerElement(ref.current));
5477
5471
  },
5478
- [setRootElement]
5472
+ [registerElement]
5479
5473
  );
5480
5474
  react.useEffect(() => {
5481
5475
  onItemSelect && onItemSelect(activeIndex);
5482
- }, [activeIndex, onItemSelect]), useClickOutside(
5483
- react.useCallback(
5484
- (event) => isTopLayer && onClickOutside && onClickOutside(event),
5485
- [isTopLayer, onClickOutside]
5486
- ),
5487
- [rootElement]
5488
- ), useGlobalKeyDown(
5476
+ }, [activeIndex, onItemSelect]), useClickOutsideEvent(isTopLayer && onClickOutside, () => [ref.current]), useGlobalKeyDown(
5489
5477
  react.useCallback(
5490
5478
  (event) => {
5491
5479
  isTopLayer && event.key === "Escape" && (event.stopPropagation(), onEscape && onEscape());
5492
5480
  },
5493
5481
  [isTopLayer, onEscape]
5494
5482
  )
5495
- ), react.useEffect(() => {
5496
- if (!(!rootElement || !registerElement))
5497
- return registerElement(rootElement);
5498
- }, [registerElement, rootElement]);
5483
+ );
5499
5484
  const value = react.useMemo(
5500
5485
  () => ({
5501
5486
  version: 0,
@@ -5602,9 +5587,7 @@ const Root$5 = styledComponents.styled(Box)`
5602
5587
  [menuElements]
5603
5588
  ), handleItemClick = react.useCallback(() => {
5604
5589
  setOpen(!1), !disableRestoreFocusOnClose && buttonElement && buttonElement.focus();
5605
- }, [buttonElement, disableRestoreFocusOnClose]), registerElement = react.useCallback((el) => (setChildMenuElements((els) => els.concat([el])), () => {
5606
- setChildMenuElements((els) => els.filter((_el) => _el !== el));
5607
- }), []), menuProps = react.useMemo(
5590
+ }, [buttonElement, disableRestoreFocusOnClose]), registerElement = react.useCallback((el) => (setChildMenuElements((els) => els.concat([el])), () => setChildMenuElements((els) => els.filter((_el) => _el !== el))), []), menuProps = react.useMemo(
5608
5591
  () => ({
5609
5592
  "aria-labelledby": id,
5610
5593
  onBlurCapture: handleBlur,
@@ -5808,7 +5791,7 @@ function MenuGroup(props) {
5808
5791
  onItemClick,
5809
5792
  onItemMouseEnter = menu.onMouseEnter,
5810
5793
  registerElement
5811
- } = menu, [rootElement, setRootElement] = react.useState(null), [open, setOpen] = react.useState(!1), shouldFocusRef = react.useRef(null), active = !!activeElement && activeElement === rootElement, [withinMenu, setWithinMenu] = react.useState(!1), handleMouseEnter = react.useCallback(
5794
+ } = menu, [rootElement, setRootElement] = react.useState(null), [open, setOpen] = react.useState(!1), [shouldFocus, setShouldFocus] = react.useState(null), active = !!activeElement && activeElement === rootElement, [withinMenu, setWithinMenu] = react.useState(!1), handleMouseEnter = react.useCallback(
5812
5795
  (event) => {
5813
5796
  setWithinMenu(!1), onItemMouseEnter(event), setOpen(!0);
5814
5797
  },
@@ -5822,19 +5805,21 @@ function MenuGroup(props) {
5822
5805
  [rootElement]
5823
5806
  ), handleClick = react.useCallback(
5824
5807
  (event) => {
5825
- onClick && onClick(event), shouldFocusRef.current = "first", setOpen(!0), requestAnimationFrame(() => {
5826
- shouldFocusRef.current = null;
5827
- });
5808
+ onClick == null || onClick(event), setShouldFocus("first"), setOpen(!0);
5828
5809
  },
5829
5810
  [onClick]
5830
5811
  ), handleChildItemClick = react.useCallback(() => {
5831
- setOpen(!1), onItemClick && onItemClick();
5812
+ setOpen(!1), onItemClick == null || onItemClick();
5832
5813
  }, [onItemClick]), handleMenuMouseEnter = react.useCallback(() => setWithinMenu(!0), []);
5833
5814
  react.useEffect(() => mount(rootElement), [mount, rootElement]), react.useEffect(() => {
5834
5815
  active || setOpen(!1);
5835
5816
  }, [active]), react.useEffect(() => {
5836
5817
  open || setWithinMenu(!1);
5837
- }, [open]);
5818
+ }, [open]), react.useEffect(() => {
5819
+ if (!shouldFocus) return;
5820
+ const rafId = requestAnimationFrame(() => setShouldFocus(null));
5821
+ return () => cancelAnimationFrame(rafId);
5822
+ }, [shouldFocus]);
5838
5823
  const childMenu = /* @__PURE__ */ jsxRuntime.jsx(
5839
5824
  Menu,
5840
5825
  {
@@ -5844,15 +5829,13 @@ function MenuGroup(props) {
5844
5829
  onKeyDown: handleMenuKeyDown,
5845
5830
  onMouseEnter: handleMenuMouseEnter,
5846
5831
  registerElement,
5847
- shouldFocus: shouldFocusRef.current,
5832
+ shouldFocus,
5848
5833
  children
5849
5834
  }
5850
5835
  ), handleKeyDown = react.useCallback((event) => {
5851
5836
  const target = event.currentTarget;
5852
5837
  if (document.activeElement === target && event.key === "ArrowRight") {
5853
- shouldFocusRef.current = "first", setOpen(!0), setWithinMenu(!0), requestAnimationFrame(() => {
5854
- shouldFocusRef.current = null;
5855
- });
5838
+ setShouldFocus("first"), setOpen(!0), setWithinMenu(!0);
5856
5839
  return;
5857
5840
  }
5858
5841
  }, []);
@@ -6171,7 +6154,7 @@ const CustomInline = styledComponents.styled(Inline)`
6171
6154
  }
6172
6155
  100% {
6173
6156
  width: 100%;
6174
- }
6157
+ }
6175
6158
  `, LOADING_BAR_HEIGHT = 2;
6176
6159
  function rootStyles(props) {
6177
6160
  const { color } = getTheme_v2.getTheme_v2(props.theme), loadingBarColor = color.button.default[props.tone].enabled.bg;