@sanity/ui 2.8.5 → 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.mjs CHANGED
@@ -178,10 +178,10 @@ function useClickOutsideEvent(listener, elementsArg = () => EMPTY_ARRAY, boundar
178
178
  const target = evt.target;
179
179
  if (!(target instanceof Node))
180
180
  return;
181
- const resolvedBoundaryElement = typeof boundaryElement == "function" ? boundaryElement() : boundaryElement;
181
+ const resolvedBoundaryElement = boundaryElement == null ? void 0 : boundaryElement();
182
182
  if (resolvedBoundaryElement && !resolvedBoundaryElement.contains(target))
183
183
  return;
184
- const elements = (Array.isArray(elementsArg) ? elementsArg : elementsArg()).flat();
184
+ const elements = elementsArg().flat();
185
185
  for (const el of elements)
186
186
  if (el && (target === el || el.contains(target)))
187
187
  return;
@@ -5334,13 +5334,13 @@ function _sortElements(rootElement, elements) {
5334
5334
  elements.sort(_sort);
5335
5335
  }
5336
5336
  function useMenuController(props) {
5337
- const { onKeyDown, originElement, shouldFocus } = props, elementsRef = useRef([]), [rootElement, setRootElement] = useState(null), [activeIndex, _setActiveIndex] = useState(-1), activeIndexRef = useRef(activeIndex), activeElement = elementsRef.current[activeIndex] || null, mounted = !!rootElement, setActiveIndex = useCallback((nextActiveIndex) => {
5337
+ const { onKeyDown, originElement, shouldFocus, rootElementRef } = props, elementsRef = useRef([]), [activeIndex, _setActiveIndex] = useState(-1), activeIndexRef = useRef(activeIndex), activeElement = useMemo(() => elementsRef.current[activeIndex] || null, [activeIndex]), mounted = !!rootElementRef.current, setActiveIndex = useCallback((nextActiveIndex) => {
5338
5338
  _setActiveIndex(nextActiveIndex), activeIndexRef.current = nextActiveIndex;
5339
5339
  }, []), mount = useCallback(
5340
5340
  (element, selected) => {
5341
5341
  if (!element) return () => {
5342
5342
  };
5343
- if (elementsRef.current.indexOf(element) === -1 && (elementsRef.current.push(element), _sortElements(rootElement, elementsRef.current)), selected) {
5343
+ if (elementsRef.current.indexOf(element) === -1 && (elementsRef.current.push(element), _sortElements(rootElementRef.current, elementsRef.current)), selected) {
5344
5344
  const selectedIndex = elementsRef.current.indexOf(element);
5345
5345
  setActiveIndex(selectedIndex);
5346
5346
  }
@@ -5349,7 +5349,7 @@ function useMenuController(props) {
5349
5349
  idx > -1 && elementsRef.current.splice(idx, 1);
5350
5350
  };
5351
5351
  },
5352
- [rootElement, setActiveIndex]
5352
+ [rootElementRef, setActiveIndex]
5353
5353
  ), handleKeyDown = useCallback(
5354
5354
  (event) => {
5355
5355
  if (event.key === "Tab") {
@@ -5404,44 +5404,40 @@ function useMenuController(props) {
5404
5404
  },
5405
5405
  [setActiveIndex]
5406
5406
  ), handleItemMouseLeave = useCallback(() => {
5407
- setActiveIndex(-2), rootElement == null || rootElement.focus();
5408
- }, [setActiveIndex, rootElement]);
5407
+ var _a;
5408
+ setActiveIndex(-2), (_a = rootElementRef.current) == null || _a.focus();
5409
+ }, [rootElementRef, setActiveIndex]);
5409
5410
  return useEffect(() => {
5410
5411
  if (!mounted) return;
5411
- const rafId = window.requestAnimationFrame(() => {
5412
- const _activeIndex = activeIndexRef.current;
5413
- if (_activeIndex === -1) {
5412
+ const rafId = requestAnimationFrame(() => {
5413
+ if (activeIndex === -1) {
5414
5414
  if (shouldFocus === "first") {
5415
5415
  const el = _getFocusableElements(elementsRef.current)[0];
5416
5416
  if (el) {
5417
5417
  const currentIndex = elementsRef.current.indexOf(el);
5418
- setActiveIndex(currentIndex), activeIndexRef.current = currentIndex;
5418
+ setActiveIndex(currentIndex);
5419
5419
  }
5420
5420
  }
5421
5421
  if (shouldFocus === "last") {
5422
5422
  const focusableElements = _getFocusableElements(elementsRef.current), el = focusableElements[focusableElements.length - 1];
5423
5423
  if (el) {
5424
5424
  const currentIndex = elementsRef.current.indexOf(el);
5425
- setActiveIndex(currentIndex), activeIndexRef.current = currentIndex;
5425
+ setActiveIndex(currentIndex);
5426
5426
  }
5427
5427
  }
5428
5428
  return;
5429
5429
  }
5430
- const element = elementsRef.current[_activeIndex] || null;
5430
+ const element = elementsRef.current[activeIndex] || null;
5431
5431
  element == null || element.focus();
5432
5432
  });
5433
- return () => {
5434
- window.cancelAnimationFrame(rafId);
5435
- };
5433
+ return () => cancelAnimationFrame(rafId);
5436
5434
  }, [activeIndex, mounted, setActiveIndex, shouldFocus]), {
5437
5435
  activeElement,
5438
5436
  activeIndex,
5439
5437
  handleItemMouseEnter,
5440
5438
  handleItemMouseLeave,
5441
5439
  handleKeyDown,
5442
- mount,
5443
- rootElement,
5444
- setRootElement
5440
+ mount
5445
5441
  };
5446
5442
  }
5447
5443
  const Root$5 = styled(Box)`
@@ -5473,34 +5469,23 @@ const Root$5 = styled(Box)`
5473
5469
  handleItemMouseEnter,
5474
5470
  handleItemMouseLeave,
5475
5471
  handleKeyDown,
5476
- mount,
5477
- rootElement,
5478
- setRootElement
5479
- } = useMenuController({ onKeyDown, originElement, shouldFocus }), handleRefChange = useCallback(
5472
+ mount
5473
+ } = useMenuController({ onKeyDown, originElement, shouldFocus, rootElementRef: ref }), unregisterElementRef = useRef(null), handleRefChange = useCallback(
5480
5474
  (el) => {
5481
- setRootElement(el), ref.current = el;
5475
+ unregisterElementRef.current && (unregisterElementRef.current(), unregisterElementRef.current = null), ref.current = el, ref.current && registerElement && (unregisterElementRef.current = registerElement(ref.current));
5482
5476
  },
5483
- [setRootElement]
5477
+ [registerElement]
5484
5478
  );
5485
5479
  useEffect(() => {
5486
5480
  onItemSelect && onItemSelect(activeIndex);
5487
- }, [activeIndex, onItemSelect]), useClickOutside(
5488
- useCallback(
5489
- (event) => isTopLayer && onClickOutside && onClickOutside(event),
5490
- [isTopLayer, onClickOutside]
5491
- ),
5492
- [rootElement]
5493
- ), useGlobalKeyDown(
5481
+ }, [activeIndex, onItemSelect]), useClickOutsideEvent(isTopLayer && onClickOutside, () => [ref.current]), useGlobalKeyDown(
5494
5482
  useCallback(
5495
5483
  (event) => {
5496
5484
  isTopLayer && event.key === "Escape" && (event.stopPropagation(), onEscape && onEscape());
5497
5485
  },
5498
5486
  [isTopLayer, onEscape]
5499
5487
  )
5500
- ), useEffect(() => {
5501
- if (!(!rootElement || !registerElement))
5502
- return registerElement(rootElement);
5503
- }, [registerElement, rootElement]);
5488
+ );
5504
5489
  const value = useMemo(
5505
5490
  () => ({
5506
5491
  version: 0,
@@ -5607,9 +5592,7 @@ const Root$5 = styled(Box)`
5607
5592
  [menuElements]
5608
5593
  ), handleItemClick = useCallback(() => {
5609
5594
  setOpen(!1), !disableRestoreFocusOnClose && buttonElement && buttonElement.focus();
5610
- }, [buttonElement, disableRestoreFocusOnClose]), registerElement = useCallback((el) => (setChildMenuElements((els) => els.concat([el])), () => {
5611
- setChildMenuElements((els) => els.filter((_el) => _el !== el));
5612
- }), []), menuProps = useMemo(
5595
+ }, [buttonElement, disableRestoreFocusOnClose]), registerElement = useCallback((el) => (setChildMenuElements((els) => els.concat([el])), () => setChildMenuElements((els) => els.filter((_el) => _el !== el))), []), menuProps = useMemo(
5613
5596
  () => ({
5614
5597
  "aria-labelledby": id,
5615
5598
  onBlurCapture: handleBlur,
@@ -5813,7 +5796,7 @@ function MenuGroup(props) {
5813
5796
  onItemClick,
5814
5797
  onItemMouseEnter = menu.onMouseEnter,
5815
5798
  registerElement
5816
- } = menu, [rootElement, setRootElement] = useState(null), [open, setOpen] = useState(!1), shouldFocusRef = useRef(null), active = !!activeElement && activeElement === rootElement, [withinMenu, setWithinMenu] = useState(!1), handleMouseEnter = useCallback(
5799
+ } = menu, [rootElement, setRootElement] = useState(null), [open, setOpen] = useState(!1), [shouldFocus, setShouldFocus] = useState(null), active = !!activeElement && activeElement === rootElement, [withinMenu, setWithinMenu] = useState(!1), handleMouseEnter = useCallback(
5817
5800
  (event) => {
5818
5801
  setWithinMenu(!1), onItemMouseEnter(event), setOpen(!0);
5819
5802
  },
@@ -5827,19 +5810,21 @@ function MenuGroup(props) {
5827
5810
  [rootElement]
5828
5811
  ), handleClick = useCallback(
5829
5812
  (event) => {
5830
- onClick && onClick(event), shouldFocusRef.current = "first", setOpen(!0), requestAnimationFrame(() => {
5831
- shouldFocusRef.current = null;
5832
- });
5813
+ onClick == null || onClick(event), setShouldFocus("first"), setOpen(!0);
5833
5814
  },
5834
5815
  [onClick]
5835
5816
  ), handleChildItemClick = useCallback(() => {
5836
- setOpen(!1), onItemClick && onItemClick();
5817
+ setOpen(!1), onItemClick == null || onItemClick();
5837
5818
  }, [onItemClick]), handleMenuMouseEnter = useCallback(() => setWithinMenu(!0), []);
5838
5819
  useEffect(() => mount(rootElement), [mount, rootElement]), useEffect(() => {
5839
5820
  active || setOpen(!1);
5840
5821
  }, [active]), useEffect(() => {
5841
5822
  open || setWithinMenu(!1);
5842
- }, [open]);
5823
+ }, [open]), useEffect(() => {
5824
+ if (!shouldFocus) return;
5825
+ const rafId = requestAnimationFrame(() => setShouldFocus(null));
5826
+ return () => cancelAnimationFrame(rafId);
5827
+ }, [shouldFocus]);
5843
5828
  const childMenu = /* @__PURE__ */ jsx(
5844
5829
  Menu,
5845
5830
  {
@@ -5849,15 +5834,13 @@ function MenuGroup(props) {
5849
5834
  onKeyDown: handleMenuKeyDown,
5850
5835
  onMouseEnter: handleMenuMouseEnter,
5851
5836
  registerElement,
5852
- shouldFocus: shouldFocusRef.current,
5837
+ shouldFocus,
5853
5838
  children
5854
5839
  }
5855
5840
  ), handleKeyDown = useCallback((event) => {
5856
5841
  const target = event.currentTarget;
5857
5842
  if (document.activeElement === target && event.key === "ArrowRight") {
5858
- shouldFocusRef.current = "first", setOpen(!0), setWithinMenu(!0), requestAnimationFrame(() => {
5859
- shouldFocusRef.current = null;
5860
- });
5843
+ setShouldFocus("first"), setOpen(!0), setWithinMenu(!0);
5861
5844
  return;
5862
5845
  }
5863
5846
  }, []);
@@ -6176,7 +6159,7 @@ const CustomInline = styled(Inline)`
6176
6159
  }
6177
6160
  100% {
6178
6161
  width: 100%;
6179
- }
6162
+ }
6180
6163
  `, LOADING_BAR_HEIGHT = 2;
6181
6164
  function rootStyles(props) {
6182
6165
  const { color } = getTheme_v2$1(props.theme), loadingBarColor = color.button.default[props.tone].enabled.bg;