@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.js CHANGED
@@ -166,6 +166,12 @@ function useClickOutside(listener, elementsArg = EMPTY_ARRAY, boundaryElement) {
166
166
  };
167
167
  }, [boundaryElement, listener, elements]), setElement;
168
168
  }
169
+ function useCustomValidity(ref, customValidity) {
170
+ react.useEffect(() => {
171
+ var _a;
172
+ (_a = ref.current) == null || _a.setCustomValidity(customValidity || "");
173
+ }, [customValidity, ref]);
174
+ }
169
175
  var resizeObservers = [], hasActiveObservations = function() {
170
176
  return resizeObservers.some(function(ro) {
171
177
  return ro.activeTargets.length > 0;
@@ -499,9 +505,27 @@ function useElementRect(element) {
499
505
  const elementSize = useElementSize(element);
500
506
  return (elementSize == null ? void 0 : elementSize._contentRect) || null;
501
507
  }
508
+ function useForwardedRef(ref) {
509
+ const innerRef = react.useRef(null);
510
+ return react.useImperativeHandle(ref, () => innerRef.current), innerRef;
511
+ }
502
512
  function useGlobalKeyDown(onKeyDown) {
503
513
  return react.useEffect(() => (addEventListener("keydown", onKeyDown), () => removeEventListener("keydown", onKeyDown)), [onKeyDown]);
504
514
  }
515
+ function useMatchMedia(mediaQueryString, getServerSnapshot2) {
516
+ const { subscribe: subscribe2, getSnapshot } = react.useMemo(() => {
517
+ let MEDIA_QUERY_CACHE;
518
+ const getMatchMedia = () => (MEDIA_QUERY_CACHE || (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE);
519
+ return {
520
+ subscribe: (onStoreChange) => {
521
+ const matchMedia = getMatchMedia();
522
+ return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
523
+ },
524
+ getSnapshot: () => getMatchMedia().matches
525
+ };
526
+ }, [mediaQueryString]);
527
+ return react.useDebugValue(mediaQueryString), react.useSyncExternalStore(subscribe2, getSnapshot, getServerSnapshot2);
528
+ }
505
529
  function getGlobalScope() {
506
530
  if (typeof globalThis < "u") return globalThis;
507
531
  if (typeof window < "u") return window;
@@ -511,9 +535,13 @@ function getGlobalScope() {
511
535
  }
512
536
  const globalScope = getGlobalScope();
513
537
  function createGlobalScopedContext(key2, defaultValue) {
514
- return typeof document > "u" ? react.createContext(defaultValue) : (globalScope[key2] = globalScope[key2] || react.createContext(defaultValue), globalScope[key2]);
538
+ const symbol = Symbol.for(key2);
539
+ return typeof document > "u" ? react.createContext(defaultValue) : (globalScope[symbol] = globalScope[symbol] || react.createContext(defaultValue), globalScope[symbol]);
515
540
  }
516
- const key$8 = Symbol.for("@sanity/ui/context/theme"), ThemeContext = createGlobalScopedContext(key$8, null);
541
+ const ThemeContext = createGlobalScopedContext(
542
+ "@sanity/ui/context/theme",
543
+ null
544
+ );
517
545
  function ThemeProvider(props) {
518
546
  const parentTheme = react.useContext(ThemeContext), {
519
547
  children,
@@ -544,7 +572,6 @@ function useTheme() {
544
572
  function useTheme_v2() {
545
573
  return theme.getTheme_v2(styledComponents.useTheme());
546
574
  }
547
- const MEDIA_STORE_CACHE = /* @__PURE__ */ new WeakMap();
548
575
  function _getMediaQuery(media, index) {
549
576
  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)`;
550
577
  }
@@ -579,57 +606,18 @@ function _createMediaStore(media) {
579
606
  };
580
607
  } };
581
608
  }
582
- function getServerSnapshot$2() {
609
+ function getServerSnapshot() {
583
610
  return 0;
584
611
  }
585
612
  function useMediaIndex() {
586
- const { media } = useTheme_v2();
587
- let store = MEDIA_STORE_CACHE.get(media);
588
- return store || (store = _createMediaStore(media), MEDIA_STORE_CACHE.set(media, store)), react.useSyncExternalStore(store.subscribe, store.getSnapshot, getServerSnapshot$2);
589
- }
590
- let MEDIA_QUERY_CACHE$1;
591
- function getMatchMedia$1() {
592
- return MEDIA_QUERY_CACHE$1 || (MEDIA_QUERY_CACHE$1 = window.matchMedia("(prefers-color-scheme: dark)")), MEDIA_QUERY_CACHE$1;
613
+ const { media } = useTheme_v2(), store = react.useMemo(() => _createMediaStore(media), [media]);
614
+ return react.useSyncExternalStore(store.subscribe, store.getSnapshot, getServerSnapshot);
593
615
  }
594
- function subscribe$2(onStoreChange) {
595
- const matchMedia = getMatchMedia$1();
596
- return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
616
+ function usePrefersDark(getServerSnapshot2 = () => !1) {
617
+ return useMatchMedia("(prefers-color-scheme: dark)", getServerSnapshot2);
597
618
  }
598
- function getSnapshot$1() {
599
- return getMatchMedia$1().matches;
600
- }
601
- function getServerSnapshot$1() {
602
- return !1;
603
- }
604
- function usePrefersDark() {
605
- return react.useSyncExternalStore(subscribe$2, getSnapshot$1, getServerSnapshot$1);
606
- }
607
- let MEDIA_QUERY_CACHE;
608
- function getMatchMedia() {
609
- return MEDIA_QUERY_CACHE || (MEDIA_QUERY_CACHE = window.matchMedia("(prefers-reduced-motion: reduce)")), MEDIA_QUERY_CACHE;
610
- }
611
- function subscribe$1(onStoreChange) {
612
- const matchMedia = getMatchMedia();
613
- return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
614
- }
615
- function getSnapshot() {
616
- return getMatchMedia().matches;
617
- }
618
- function getServerSnapshot() {
619
- return !1;
620
- }
621
- function usePrefersReducedMotion() {
622
- return react.useSyncExternalStore(subscribe$1, getSnapshot, getServerSnapshot);
623
- }
624
- function useForwardedRef(ref) {
625
- const innerRef = react.useRef(null);
626
- return react.useImperativeHandle(ref, () => innerRef.current), innerRef;
627
- }
628
- function useCustomValidity(ref, customValidity) {
629
- react.useEffect(() => {
630
- var _a;
631
- (_a = ref.current) == null || _a.setCustomValidity(customValidity || "");
632
- }, [customValidity, ref]);
619
+ function usePrefersReducedMotion(getServerSnapshot2 = () => !1) {
620
+ return useMatchMedia("(prefers-reduced-motion: reduce)", getServerSnapshot2);
633
621
  }
634
622
  function responsiveBorderStyle() {
635
623
  return [border, borderTop, borderRight, borderBottom, borderLeft];
@@ -2808,8 +2796,8 @@ const Root$k = styledComponents.styled.div(
2808
2796
  /* @__PURE__ */ jsxRuntime.jsx(StrokePath, { d: strokePath, mask: "url(#stroke-mask)", strokeWidth: strokeWidth * 2 }),
2809
2797
  /* @__PURE__ */ jsxRuntime.jsx(ShapePath, { d: fillPath })
2810
2798
  ] }) });
2811
- }), key$7 = Symbol.for("@sanity/ui/context/boundaryElement"), BoundaryElementContext = createGlobalScopedContext(
2812
- key$7,
2799
+ }), BoundaryElementContext = createGlobalScopedContext(
2800
+ "@sanity/ui/context/boundaryElement",
2813
2801
  null
2814
2802
  );
2815
2803
  function BoundaryElementProvider(props) {
@@ -2898,7 +2886,10 @@ function getLayerContext(contextValue) {
2898
2886
  return contextValue;
2899
2887
  throw new Error("could not get layer context");
2900
2888
  }
2901
- const key$6 = Symbol.for("@sanity/ui/context/layer"), LayerContext = createGlobalScopedContext(key$6, null);
2889
+ const LayerContext = createGlobalScopedContext(
2890
+ "@sanity/ui/context/layer",
2891
+ null
2892
+ );
2902
2893
  function useLayer() {
2903
2894
  const value = react.useContext(LayerContext);
2904
2895
  if (!value)
@@ -3033,7 +3024,7 @@ const Root$j = styledComponents.styled.div({ position: "relative" }), LayerChild
3033
3024
  }), Layer = react.forwardRef(function(props, ref) {
3034
3025
  const { children, zOffset = 1, ...restProps } = props;
3035
3026
  return /* @__PURE__ */ jsxRuntime.jsx(LayerProvider, { zOffset, children: /* @__PURE__ */ jsxRuntime.jsx(LayerChildren, { ...restProps, ref, children }) });
3036
- }), key$5 = Symbol.for("@sanity/ui/context/portal"), elementKey = Symbol.for("@sanity/ui/context/portal/element");
3027
+ }), key = "@sanity/ui/context/portal", elementKey = Symbol.for(`${key}/element`);
3037
3028
  globalScope[elementKey] = null;
3038
3029
  const defaultContextValue = {
3039
3030
  version: 0,
@@ -3041,7 +3032,7 @@ const defaultContextValue = {
3041
3032
  get element() {
3042
3033
  return typeof document > "u" ? null : (globalScope[elementKey] || (globalScope[elementKey] = document.createElement("div"), globalScope[elementKey].setAttribute("data-portal", ""), document.body.appendChild(globalScope[elementKey])), globalScope[elementKey]);
3043
3034
  }
3044
- }, PortalContext = createGlobalScopedContext(key$5, defaultContextValue);
3035
+ }, PortalContext = createGlobalScopedContext(key, defaultContextValue);
3045
3036
  function usePortal() {
3046
3037
  const value = react.useContext(PortalContext);
3047
3038
  if (!value)
@@ -3055,16 +3046,6 @@ function Portal(props) {
3055
3046
  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);
3056
3047
  return portalElement ? reactDom.createPortal(children, portalElement) : null;
3057
3048
  }
3058
- function useUnique(value) {
3059
- const valueRef = react.useRef(value);
3060
- return _isEqual(valueRef.current, value) || (valueRef.current = value), valueRef.current;
3061
- }
3062
- function _isEqual(objA, objB) {
3063
- if (!objA || !objB)
3064
- return objA === objB;
3065
- const keysA = Object.keys(objA), keysB = Object.keys(objB);
3066
- return keysA.length !== keysB.length ? !1 : keysA.every((key2) => objA[key2] === objB[key2]);
3067
- }
3068
3049
  function PortalProvider(props) {
3069
3050
  const { boundaryElement, children, element, __unstable_elements: elementsProp } = props, elements = useUnique(elementsProp), fallbackElement = react.useSyncExternalStore(
3070
3051
  emptySubscribe,
@@ -3079,7 +3060,18 @@ function PortalProvider(props) {
3079
3060
  return /* @__PURE__ */ jsxRuntime.jsx(PortalContext.Provider, { value, children });
3080
3061
  }
3081
3062
  const emptySubscribe = () => () => {
3082
- }, Root$i = styledComponents.styled.div`
3063
+ };
3064
+ function useUnique(value) {
3065
+ const valueRef = react.useRef(value);
3066
+ return _isEqual(valueRef.current, value) || (valueRef.current = value), valueRef.current;
3067
+ }
3068
+ function _isEqual(objA, objB) {
3069
+ if (!objA || !objB)
3070
+ return objA === objB;
3071
+ const keysA = Object.keys(objA), keysB = Object.keys(objB);
3072
+ return keysA.length !== keysB.length ? !1 : keysA.every((key2) => objA[key2] === objB[key2]);
3073
+ }
3074
+ const Root$i = styledComponents.styled.div`
3083
3075
  display: block;
3084
3076
  width: 0;
3085
3077
  height: 0;
@@ -4313,7 +4305,10 @@ const DEFAULT_TOOLTIP_ARROW_WIDTH = 15, DEFAULT_TOOLTIP_ARROW_HEIGHT = 6, DEFAUL
4313
4305
  })
4314
4306
  );
4315
4307
  TooltipCard.displayName = "TooltipCard";
4316
- const key$4 = Symbol.for("@sanity/ui/context/tooltipDelayGroup"), TooltipDelayGroupContext = createGlobalScopedContext(key$4, null);
4308
+ const TooltipDelayGroupContext = createGlobalScopedContext(
4309
+ "@sanity/ui/context/tooltipDelayGroup",
4310
+ null
4311
+ );
4317
4312
  function useTooltipDelayGroup() {
4318
4313
  return react.useContext(TooltipDelayGroupContext);
4319
4314
  }
@@ -5048,9 +5043,10 @@ function animationDialogStyle(props) {
5048
5043
  }
5049
5044
  ` : styledComponents.css``;
5050
5045
  }
5051
- const key$3 = Symbol.for("@sanity/ui/context/dialog"), DialogContext = createGlobalScopedContext(key$3, {
5052
- version: 0
5053
- });
5046
+ const DialogContext = createGlobalScopedContext(
5047
+ "@sanity/ui/context/dialog",
5048
+ { version: 0 }
5049
+ );
5054
5050
  function useDialog() {
5055
5051
  return react.useContext(DialogContext);
5056
5052
  }
@@ -5276,7 +5272,10 @@ const Root$6 = styledComponents.styled.kbd`
5276
5272
  `, Hotkeys = react.forwardRef(function(props, ref) {
5277
5273
  const { fontSize: fontSize2, keys, padding, radius, space: spaceProp = 0.5, ...restProps } = props, space = useArrayProp(spaceProp);
5278
5274
  return !keys || keys.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {}) : /* @__PURE__ */ jsxRuntime.jsx(Root$6, { "data-ui": "Hotkeys", ...restProps, ref, children: /* @__PURE__ */ jsxRuntime.jsx(Inline, { as: "span", space, children: keys.map((key2, i) => /* @__PURE__ */ jsxRuntime.jsx(Key, { fontSize: fontSize2, padding, radius, children: key2 }, i)) }) });
5279
- }), key$2 = Symbol.for("@sanity/ui/context/menu"), MenuContext = createGlobalScopedContext(key$2, null);
5275
+ }), MenuContext = createGlobalScopedContext(
5276
+ "@sanity/ui/context/menu",
5277
+ null
5278
+ );
5280
5279
  function _isFocusable(element) {
5281
5280
  return isHTMLAnchorElement(element) && element.getAttribute("data-disabled") !== "true" || isHTMLButtonElement(element) && !element.disabled;
5282
5281
  }
@@ -6250,7 +6249,10 @@ function useMounted() {
6250
6249
  );
6251
6250
  }
6252
6251
  const subscribe = () => () => {
6253
- }, key$1 = Symbol.for("@sanity/ui/context/toast"), ToastContext = createGlobalScopedContext(key$1, null), Root$1 = styledComponents.styled(Layer)`
6252
+ }, ToastContext = createGlobalScopedContext(
6253
+ "@sanity/ui/context/toast",
6254
+ null
6255
+ ), Root$1 = styledComponents.styled(Layer)`
6254
6256
  position: fixed;
6255
6257
  top: 0;
6256
6258
  left: 0;
@@ -6409,7 +6411,10 @@ function _focusItemElement(el) {
6409
6411
  firstChild && firstChild instanceof HTMLElement && firstChild.focus();
6410
6412
  }
6411
6413
  }
6412
- const key = Symbol.for("@sanity/ui/context/tree"), TreeContext = createGlobalScopedContext(key, null), Tree = react.memo(
6414
+ const TreeContext = createGlobalScopedContext(
6415
+ "@sanity/ui/context/tree",
6416
+ null
6417
+ ), Tree = react.memo(
6413
6418
  react.forwardRef(function(props, forwardedRef) {
6414
6419
  const { children, space = 1, onFocus, ...restProps } = props, ref = react.useRef(null), [focusedElement, setFocusedElement] = react.useState(null), focusedElementRef = react.useRef(focusedElement), path = react.useMemo(() => [], []), [itemElements, setItemElements] = react.useState([]), [state, setState] = react.useState({}), stateRef = react.useRef(state);
6415
6420
  react.useImperativeHandle(
@@ -6861,6 +6866,7 @@ exports.useElementSize = useElementSize;
6861
6866
  exports.useForwardedRef = useForwardedRef;
6862
6867
  exports.useGlobalKeyDown = useGlobalKeyDown;
6863
6868
  exports.useLayer = useLayer;
6869
+ exports.useMatchMedia = useMatchMedia;
6864
6870
  exports.useMediaIndex = useMediaIndex;
6865
6871
  exports.usePortal = usePortal;
6866
6872
  exports.usePrefersDark = usePrefersDark;