@sanity/ui 2.6.8 → 2.7.0

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
@@ -1,6 +1,6 @@
1
1
  import { buildTheme, createColorTheme as createColorTheme$1, hexToRgb as hexToRgb$1, hslToRgb as hslToRgb$1, multiply as multiply$1, parseColor as parseColor$1, rgbToHex as rgbToHex$1, rgbToHsl as rgbToHsl$1, rgba as rgba$1, screen as screen$1, getTheme_v2, getScopedTheme } from "@sanity/ui/theme";
2
2
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
3
- import { useMemo, useState, useRef, useEffect, createContext, useContext, useSyncExternalStore, useImperativeHandle, forwardRef, useId, useCallback, Children, isValidElement, cloneElement, Component, memo, useLayoutEffect, useReducer, Fragment as Fragment$1, startTransition } from "react";
3
+ import { useMemo, useState, useRef, useEffect, useImperativeHandle, useDebugValue, useSyncExternalStore, createContext, useContext, forwardRef, useId, useCallback, Children, isValidElement, cloneElement, Component, memo, useLayoutEffect, useReducer, Fragment as Fragment$1, startTransition } from "react";
4
4
  import ReactIs, { isValidElementType } from "react-is";
5
5
  import { ThemeProvider as ThemeProvider$1, useTheme as useTheme$1, css, styled, keyframes } from "styled-components";
6
6
  import { SpinnerIcon, CheckmarkIcon, RemoveIcon, ChevronDownIcon, CloseIcon, ChevronRightIcon, ToggleArrowRightIcon } from "@sanity/icons";
@@ -171,6 +171,12 @@ function useClickOutside(listener, elementsArg = EMPTY_ARRAY, boundaryElement) {
171
171
  };
172
172
  }, [boundaryElement, listener, elements]), setElement;
173
173
  }
174
+ function useCustomValidity(ref, customValidity) {
175
+ useEffect(() => {
176
+ var _a;
177
+ (_a = ref.current) == null || _a.setCustomValidity(customValidity || "");
178
+ }, [customValidity, ref]);
179
+ }
174
180
  var resizeObservers = [], hasActiveObservations = function() {
175
181
  return resizeObservers.some(function(ro) {
176
182
  return ro.activeTargets.length > 0;
@@ -504,9 +510,27 @@ function useElementRect(element) {
504
510
  const elementSize = useElementSize(element);
505
511
  return (elementSize == null ? void 0 : elementSize._contentRect) || null;
506
512
  }
513
+ function useForwardedRef(ref) {
514
+ const innerRef = useRef(null);
515
+ return useImperativeHandle(ref, () => innerRef.current), innerRef;
516
+ }
507
517
  function useGlobalKeyDown(onKeyDown) {
508
518
  return useEffect(() => (addEventListener("keydown", onKeyDown), () => removeEventListener("keydown", onKeyDown)), [onKeyDown]);
509
519
  }
520
+ function useMatchMedia(mediaQueryString, getServerSnapshot2) {
521
+ const { subscribe: subscribe2, getSnapshot } = useMemo(() => {
522
+ let MEDIA_QUERY_CACHE;
523
+ const getMatchMedia = () => (MEDIA_QUERY_CACHE || (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE);
524
+ return {
525
+ subscribe: (onStoreChange) => {
526
+ const matchMedia = getMatchMedia();
527
+ return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
528
+ },
529
+ getSnapshot: () => getMatchMedia().matches
530
+ };
531
+ }, [mediaQueryString]);
532
+ return useDebugValue(mediaQueryString), useSyncExternalStore(subscribe2, getSnapshot, getServerSnapshot2);
533
+ }
510
534
  function getGlobalScope() {
511
535
  if (typeof globalThis < "u") return globalThis;
512
536
  if (typeof window < "u") return window;
@@ -516,9 +540,13 @@ function getGlobalScope() {
516
540
  }
517
541
  const globalScope = getGlobalScope();
518
542
  function createGlobalScopedContext(key2, defaultValue) {
519
- return typeof document > "u" ? createContext(defaultValue) : (globalScope[key2] = globalScope[key2] || createContext(defaultValue), globalScope[key2]);
543
+ const symbol = Symbol.for(key2);
544
+ return typeof document > "u" ? createContext(defaultValue) : (globalScope[symbol] = globalScope[symbol] || createContext(defaultValue), globalScope[symbol]);
520
545
  }
521
- const key$8 = Symbol.for("@sanity/ui/context/theme"), ThemeContext = createGlobalScopedContext(key$8, null);
546
+ const ThemeContext = createGlobalScopedContext(
547
+ "@sanity/ui/context/theme",
548
+ null
549
+ );
522
550
  function ThemeProvider(props) {
523
551
  const parentTheme = useContext(ThemeContext), {
524
552
  children,
@@ -549,7 +577,6 @@ function useTheme() {
549
577
  function useTheme_v2() {
550
578
  return getTheme_v2(useTheme$1());
551
579
  }
552
- const MEDIA_STORE_CACHE = /* @__PURE__ */ new WeakMap();
553
580
  function _getMediaQuery(media, index) {
554
581
  return index === 0 ? `screen and (max-width: ${media[index] - 1}px)` : index === media.length ? `screen and (min-width: ${media[index - 1]}px)` : `screen and (min-width: ${media[index - 1]}px) and (max-width: ${media[index] - 1}px)`;
555
582
  }
@@ -584,57 +611,18 @@ function _createMediaStore(media) {
584
611
  };
585
612
  } };
586
613
  }
587
- function getServerSnapshot$2() {
614
+ function getServerSnapshot() {
588
615
  return 0;
589
616
  }
590
617
  function useMediaIndex() {
591
- const { media } = useTheme_v2();
592
- let store = MEDIA_STORE_CACHE.get(media);
593
- return store || (store = _createMediaStore(media), MEDIA_STORE_CACHE.set(media, store)), useSyncExternalStore(store.subscribe, store.getSnapshot, getServerSnapshot$2);
594
- }
595
- let MEDIA_QUERY_CACHE$1;
596
- function getMatchMedia$1() {
597
- return MEDIA_QUERY_CACHE$1 || (MEDIA_QUERY_CACHE$1 = window.matchMedia("(prefers-color-scheme: dark)")), MEDIA_QUERY_CACHE$1;
618
+ const { media } = useTheme_v2(), store = useMemo(() => _createMediaStore(media), [media]);
619
+ return useSyncExternalStore(store.subscribe, store.getSnapshot, getServerSnapshot);
598
620
  }
599
- function subscribe$2(onStoreChange) {
600
- const matchMedia = getMatchMedia$1();
601
- return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
621
+ function usePrefersDark(getServerSnapshot2 = () => !1) {
622
+ return useMatchMedia("(prefers-color-scheme: dark)", getServerSnapshot2);
602
623
  }
603
- function getSnapshot$1() {
604
- return getMatchMedia$1().matches;
605
- }
606
- function getServerSnapshot$1() {
607
- return !1;
608
- }
609
- function usePrefersDark() {
610
- return useSyncExternalStore(subscribe$2, getSnapshot$1, getServerSnapshot$1);
611
- }
612
- let MEDIA_QUERY_CACHE;
613
- function getMatchMedia() {
614
- return MEDIA_QUERY_CACHE || (MEDIA_QUERY_CACHE = window.matchMedia("(prefers-reduced-motion: reduce)")), MEDIA_QUERY_CACHE;
615
- }
616
- function subscribe$1(onStoreChange) {
617
- const matchMedia = getMatchMedia();
618
- return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
619
- }
620
- function getSnapshot() {
621
- return getMatchMedia().matches;
622
- }
623
- function getServerSnapshot() {
624
- return !1;
625
- }
626
- function usePrefersReducedMotion() {
627
- return useSyncExternalStore(subscribe$1, getSnapshot, getServerSnapshot);
628
- }
629
- function useForwardedRef(ref) {
630
- const innerRef = useRef(null);
631
- return useImperativeHandle(ref, () => innerRef.current), innerRef;
632
- }
633
- function useCustomValidity(ref, customValidity) {
634
- useEffect(() => {
635
- var _a;
636
- (_a = ref.current) == null || _a.setCustomValidity(customValidity || "");
637
- }, [customValidity, ref]);
624
+ function usePrefersReducedMotion(getServerSnapshot2 = () => !1) {
625
+ return useMatchMedia("(prefers-reduced-motion: reduce)", getServerSnapshot2);
638
626
  }
639
627
  function responsiveBorderStyle() {
640
628
  return [border, borderTop, borderRight, borderBottom, borderLeft];
@@ -2813,8 +2801,8 @@ const Root$k = styled.div(
2813
2801
  /* @__PURE__ */ jsx(StrokePath, { d: strokePath, mask: "url(#stroke-mask)", strokeWidth: strokeWidth * 2 }),
2814
2802
  /* @__PURE__ */ jsx(ShapePath, { d: fillPath })
2815
2803
  ] }) });
2816
- }), key$7 = Symbol.for("@sanity/ui/context/boundaryElement"), BoundaryElementContext = createGlobalScopedContext(
2817
- key$7,
2804
+ }), BoundaryElementContext = createGlobalScopedContext(
2805
+ "@sanity/ui/context/boundaryElement",
2818
2806
  null
2819
2807
  );
2820
2808
  function BoundaryElementProvider(props) {
@@ -2903,7 +2891,10 @@ function getLayerContext(contextValue) {
2903
2891
  return contextValue;
2904
2892
  throw new Error("could not get layer context");
2905
2893
  }
2906
- const key$6 = Symbol.for("@sanity/ui/context/layer"), LayerContext = createGlobalScopedContext(key$6, null);
2894
+ const LayerContext = createGlobalScopedContext(
2895
+ "@sanity/ui/context/layer",
2896
+ null
2897
+ );
2907
2898
  function useLayer() {
2908
2899
  const value = useContext(LayerContext);
2909
2900
  if (!value)
@@ -3038,7 +3029,7 @@ const Root$j = styled.div({ position: "relative" }), LayerChildren = forwardRef(
3038
3029
  }), Layer = forwardRef(function(props, ref) {
3039
3030
  const { children, zOffset = 1, ...restProps } = props;
3040
3031
  return /* @__PURE__ */ jsx(LayerProvider, { zOffset, children: /* @__PURE__ */ jsx(LayerChildren, { ...restProps, ref, children }) });
3041
- }), key$5 = Symbol.for("@sanity/ui/context/portal"), elementKey = Symbol.for("@sanity/ui/context/portal/element");
3032
+ }), key = "@sanity/ui/context/portal", elementKey = Symbol.for(`${key}/element`);
3042
3033
  globalScope[elementKey] = null;
3043
3034
  const defaultContextValue = {
3044
3035
  version: 0,
@@ -3046,7 +3037,7 @@ const defaultContextValue = {
3046
3037
  get element() {
3047
3038
  return typeof document > "u" ? null : (globalScope[elementKey] || (globalScope[elementKey] = document.createElement("div"), globalScope[elementKey].setAttribute("data-portal", ""), document.body.appendChild(globalScope[elementKey])), globalScope[elementKey]);
3048
3039
  }
3049
- }, PortalContext = createGlobalScopedContext(key$5, defaultContextValue);
3040
+ }, PortalContext = createGlobalScopedContext(key, defaultContextValue);
3050
3041
  function usePortal() {
3051
3042
  const value = useContext(PortalContext);
3052
3043
  if (!value)
@@ -3060,16 +3051,6 @@ function Portal(props) {
3060
3051
  const { children, __unstable_name: name } = props, portal = usePortal(), portalElement = (name ? portal.elements && portal.elements[name] : portal.element) || ((_a = portal.elements) == null ? void 0 : _a.default);
3061
3052
  return portalElement ? createPortal(children, portalElement) : null;
3062
3053
  }
3063
- function useUnique(value) {
3064
- const valueRef = useRef(value);
3065
- return _isEqual(valueRef.current, value) || (valueRef.current = value), valueRef.current;
3066
- }
3067
- function _isEqual(objA, objB) {
3068
- if (!objA || !objB)
3069
- return objA === objB;
3070
- const keysA = Object.keys(objA), keysB = Object.keys(objB);
3071
- return keysA.length !== keysB.length ? !1 : keysA.every((key2) => objA[key2] === objB[key2]);
3072
- }
3073
3054
  function PortalProvider(props) {
3074
3055
  const { boundaryElement, children, element, __unstable_elements: elementsProp } = props, elements = useUnique(elementsProp), fallbackElement = useSyncExternalStore(
3075
3056
  emptySubscribe,
@@ -3084,7 +3065,18 @@ function PortalProvider(props) {
3084
3065
  return /* @__PURE__ */ jsx(PortalContext.Provider, { value, children });
3085
3066
  }
3086
3067
  const emptySubscribe = () => () => {
3087
- }, Root$i = styled.div`
3068
+ };
3069
+ function useUnique(value) {
3070
+ const valueRef = useRef(value);
3071
+ return _isEqual(valueRef.current, value) || (valueRef.current = value), valueRef.current;
3072
+ }
3073
+ function _isEqual(objA, objB) {
3074
+ if (!objA || !objB)
3075
+ return objA === objB;
3076
+ const keysA = Object.keys(objA), keysB = Object.keys(objB);
3077
+ return keysA.length !== keysB.length ? !1 : keysA.every((key2) => objA[key2] === objB[key2]);
3078
+ }
3079
+ const Root$i = styled.div`
3088
3080
  display: block;
3089
3081
  width: 0;
3090
3082
  height: 0;
@@ -4318,7 +4310,10 @@ const DEFAULT_TOOLTIP_ARROW_WIDTH = 15, DEFAULT_TOOLTIP_ARROW_HEIGHT = 6, DEFAUL
4318
4310
  })
4319
4311
  );
4320
4312
  TooltipCard.displayName = "TooltipCard";
4321
- const key$4 = Symbol.for("@sanity/ui/context/tooltipDelayGroup"), TooltipDelayGroupContext = createGlobalScopedContext(key$4, null);
4313
+ const TooltipDelayGroupContext = createGlobalScopedContext(
4314
+ "@sanity/ui/context/tooltipDelayGroup",
4315
+ null
4316
+ );
4322
4317
  function useTooltipDelayGroup() {
4323
4318
  return useContext(TooltipDelayGroupContext);
4324
4319
  }
@@ -5053,9 +5048,10 @@ function animationDialogStyle(props) {
5053
5048
  }
5054
5049
  ` : css``;
5055
5050
  }
5056
- const key$3 = Symbol.for("@sanity/ui/context/dialog"), DialogContext = createGlobalScopedContext(key$3, {
5057
- version: 0
5058
- });
5051
+ const DialogContext = createGlobalScopedContext(
5052
+ "@sanity/ui/context/dialog",
5053
+ { version: 0 }
5054
+ );
5059
5055
  function useDialog() {
5060
5056
  return useContext(DialogContext);
5061
5057
  }
@@ -5281,7 +5277,10 @@ const Root$6 = styled.kbd`
5281
5277
  `, Hotkeys = forwardRef(function(props, ref) {
5282
5278
  const { fontSize: fontSize2, keys, padding, radius, space: spaceProp = 0.5, ...restProps } = props, space = useArrayProp(spaceProp);
5283
5279
  return !keys || keys.length === 0 ? /* @__PURE__ */ jsx(Fragment, {}) : /* @__PURE__ */ jsx(Root$6, { "data-ui": "Hotkeys", ...restProps, ref, children: /* @__PURE__ */ jsx(Inline, { as: "span", space, children: keys.map((key2, i) => /* @__PURE__ */ jsx(Key, { fontSize: fontSize2, padding, radius, children: key2 }, i)) }) });
5284
- }), key$2 = Symbol.for("@sanity/ui/context/menu"), MenuContext = createGlobalScopedContext(key$2, null);
5280
+ }), MenuContext = createGlobalScopedContext(
5281
+ "@sanity/ui/context/menu",
5282
+ null
5283
+ );
5285
5284
  function _isFocusable(element) {
5286
5285
  return isHTMLAnchorElement(element) && element.getAttribute("data-disabled") !== "true" || isHTMLButtonElement(element) && !element.disabled;
5287
5286
  }
@@ -6255,7 +6254,10 @@ function useMounted() {
6255
6254
  );
6256
6255
  }
6257
6256
  const subscribe = () => () => {
6258
- }, key$1 = Symbol.for("@sanity/ui/context/toast"), ToastContext = createGlobalScopedContext(key$1, null), Root$1 = styled(Layer)`
6257
+ }, ToastContext = createGlobalScopedContext(
6258
+ "@sanity/ui/context/toast",
6259
+ null
6260
+ ), Root$1 = styled(Layer)`
6259
6261
  position: fixed;
6260
6262
  top: 0;
6261
6263
  left: 0;
@@ -6414,7 +6416,10 @@ function _focusItemElement(el) {
6414
6416
  firstChild && firstChild instanceof HTMLElement && firstChild.focus();
6415
6417
  }
6416
6418
  }
6417
- const key = Symbol.for("@sanity/ui/context/tree"), TreeContext = createGlobalScopedContext(key, null), Tree = memo(
6419
+ const TreeContext = createGlobalScopedContext(
6420
+ "@sanity/ui/context/tree",
6421
+ null
6422
+ ), Tree = memo(
6418
6423
  forwardRef(function(props, forwardedRef) {
6419
6424
  const { children, space = 1, onFocus, ...restProps } = props, ref = useRef(null), [focusedElement, setFocusedElement] = useState(null), focusedElementRef = useRef(focusedElement), path = useMemo(() => [], []), [itemElements, setItemElements] = useState([]), [state, setState] = useState({}), stateRef = useRef(state);
6420
6425
  useImperativeHandle(
@@ -6867,6 +6872,7 @@ export {
6867
6872
  useForwardedRef,
6868
6873
  useGlobalKeyDown,
6869
6874
  useLayer,
6875
+ useMatchMedia,
6870
6876
  useMediaIndex,
6871
6877
  usePortal,
6872
6878
  usePrefersDark,