@sanity/ui 2.6.8 → 2.7.1-canary.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,35 @@ 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
+ function getMatchMedia() {
519
+ return MEDIA_QUERY_CACHE === void 0 && (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE;
520
+ }
521
+ return {
522
+ subscribe: (onStoreChange) => {
523
+ const matchMedia = getMatchMedia();
524
+ return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
525
+ },
526
+ getSnapshot: () => {
527
+ try {
528
+ return getMatchMedia().matches;
529
+ } catch (err) {
530
+ throw new Error(`Failed to match media query: ${mediaQueryString}`, { cause: err });
531
+ }
532
+ }
533
+ };
534
+ }, [mediaQueryString]);
535
+ return react.useDebugValue(mediaQueryString), react.useSyncExternalStore(subscribe2, getSnapshot, getServerSnapshot2);
536
+ }
505
537
  function getGlobalScope() {
506
538
  if (typeof globalThis < "u") return globalThis;
507
539
  if (typeof window < "u") return window;
@@ -511,9 +543,13 @@ function getGlobalScope() {
511
543
  }
512
544
  const globalScope = getGlobalScope();
513
545
  function createGlobalScopedContext(key2, defaultValue) {
514
- return typeof document > "u" ? react.createContext(defaultValue) : (globalScope[key2] = globalScope[key2] || react.createContext(defaultValue), globalScope[key2]);
546
+ const symbol = Symbol.for(key2);
547
+ return typeof document > "u" ? react.createContext(defaultValue) : (globalScope[symbol] = globalScope[symbol] || react.createContext(defaultValue), globalScope[symbol]);
515
548
  }
516
- const key$8 = Symbol.for("@sanity/ui/context/theme"), ThemeContext = createGlobalScopedContext(key$8, null);
549
+ const ThemeContext = createGlobalScopedContext(
550
+ "@sanity/ui/context/theme",
551
+ null
552
+ );
517
553
  function ThemeProvider(props) {
518
554
  const parentTheme = react.useContext(ThemeContext), {
519
555
  children,
@@ -544,7 +580,6 @@ function useTheme() {
544
580
  function useTheme_v2() {
545
581
  return theme.getTheme_v2(styledComponents.useTheme());
546
582
  }
547
- const MEDIA_STORE_CACHE = /* @__PURE__ */ new WeakMap();
548
583
  function _getMediaQuery(media, index) {
549
584
  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
585
  }
@@ -579,57 +614,18 @@ function _createMediaStore(media) {
579
614
  };
580
615
  } };
581
616
  }
582
- function getServerSnapshot$2() {
617
+ function getServerSnapshot() {
583
618
  return 0;
584
619
  }
585
620
  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;
593
- }
594
- function subscribe$2(onStoreChange) {
595
- const matchMedia = getMatchMedia$1();
596
- return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
597
- }
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;
621
+ const { media } = useTheme_v2(), store = react.useMemo(() => _createMediaStore(media), [media]);
622
+ return react.useSyncExternalStore(store.subscribe, store.getSnapshot, getServerSnapshot);
610
623
  }
611
- function subscribe$1(onStoreChange) {
612
- const matchMedia = getMatchMedia();
613
- return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
624
+ function usePrefersDark(getServerSnapshot2 = () => !1) {
625
+ return useMatchMedia("(prefers-color-scheme: dark)", getServerSnapshot2);
614
626
  }
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]);
627
+ function usePrefersReducedMotion(getServerSnapshot2 = () => !1) {
628
+ return useMatchMedia("(prefers-reduced-motion: reduce)", getServerSnapshot2);
633
629
  }
634
630
  function responsiveBorderStyle() {
635
631
  return [border, borderTop, borderRight, borderBottom, borderLeft];
@@ -2808,8 +2804,8 @@ const Root$k = styledComponents.styled.div(
2808
2804
  /* @__PURE__ */ jsxRuntime.jsx(StrokePath, { d: strokePath, mask: "url(#stroke-mask)", strokeWidth: strokeWidth * 2 }),
2809
2805
  /* @__PURE__ */ jsxRuntime.jsx(ShapePath, { d: fillPath })
2810
2806
  ] }) });
2811
- }), key$7 = Symbol.for("@sanity/ui/context/boundaryElement"), BoundaryElementContext = createGlobalScopedContext(
2812
- key$7,
2807
+ }), BoundaryElementContext = createGlobalScopedContext(
2808
+ "@sanity/ui/context/boundaryElement",
2813
2809
  null
2814
2810
  );
2815
2811
  function BoundaryElementProvider(props) {
@@ -2898,7 +2894,10 @@ function getLayerContext(contextValue) {
2898
2894
  return contextValue;
2899
2895
  throw new Error("could not get layer context");
2900
2896
  }
2901
- const key$6 = Symbol.for("@sanity/ui/context/layer"), LayerContext = createGlobalScopedContext(key$6, null);
2897
+ const LayerContext = createGlobalScopedContext(
2898
+ "@sanity/ui/context/layer",
2899
+ null
2900
+ );
2902
2901
  function useLayer() {
2903
2902
  const value = react.useContext(LayerContext);
2904
2903
  if (!value)
@@ -3033,7 +3032,7 @@ const Root$j = styledComponents.styled.div({ position: "relative" }), LayerChild
3033
3032
  }), Layer = react.forwardRef(function(props, ref) {
3034
3033
  const { children, zOffset = 1, ...restProps } = props;
3035
3034
  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");
3035
+ }), key = "@sanity/ui/context/portal", elementKey = Symbol.for(`${key}/element`);
3037
3036
  globalScope[elementKey] = null;
3038
3037
  const defaultContextValue = {
3039
3038
  version: 0,
@@ -3041,7 +3040,7 @@ const defaultContextValue = {
3041
3040
  get element() {
3042
3041
  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
3042
  }
3044
- }, PortalContext = createGlobalScopedContext(key$5, defaultContextValue);
3043
+ }, PortalContext = createGlobalScopedContext(key, defaultContextValue);
3045
3044
  function usePortal() {
3046
3045
  const value = react.useContext(PortalContext);
3047
3046
  if (!value)
@@ -3055,16 +3054,6 @@ function Portal(props) {
3055
3054
  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
3055
  return portalElement ? reactDom.createPortal(children, portalElement) : null;
3057
3056
  }
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
3057
  function PortalProvider(props) {
3069
3058
  const { boundaryElement, children, element, __unstable_elements: elementsProp } = props, elements = useUnique(elementsProp), fallbackElement = react.useSyncExternalStore(
3070
3059
  emptySubscribe,
@@ -3079,7 +3068,18 @@ function PortalProvider(props) {
3079
3068
  return /* @__PURE__ */ jsxRuntime.jsx(PortalContext.Provider, { value, children });
3080
3069
  }
3081
3070
  const emptySubscribe = () => () => {
3082
- }, Root$i = styledComponents.styled.div`
3071
+ };
3072
+ function useUnique(value) {
3073
+ const valueRef = react.useRef(value);
3074
+ return _isEqual(valueRef.current, value) || (valueRef.current = value), valueRef.current;
3075
+ }
3076
+ function _isEqual(objA, objB) {
3077
+ if (!objA || !objB)
3078
+ return objA === objB;
3079
+ const keysA = Object.keys(objA), keysB = Object.keys(objB);
3080
+ return keysA.length !== keysB.length ? !1 : keysA.every((key2) => objA[key2] === objB[key2]);
3081
+ }
3082
+ const Root$i = styledComponents.styled.div`
3083
3083
  display: block;
3084
3084
  width: 0;
3085
3085
  height: 0;
@@ -4313,7 +4313,10 @@ const DEFAULT_TOOLTIP_ARROW_WIDTH = 15, DEFAULT_TOOLTIP_ARROW_HEIGHT = 6, DEFAUL
4313
4313
  })
4314
4314
  );
4315
4315
  TooltipCard.displayName = "TooltipCard";
4316
- const key$4 = Symbol.for("@sanity/ui/context/tooltipDelayGroup"), TooltipDelayGroupContext = createGlobalScopedContext(key$4, null);
4316
+ const TooltipDelayGroupContext = createGlobalScopedContext(
4317
+ "@sanity/ui/context/tooltipDelayGroup",
4318
+ null
4319
+ );
4317
4320
  function useTooltipDelayGroup() {
4318
4321
  return react.useContext(TooltipDelayGroupContext);
4319
4322
  }
@@ -5048,9 +5051,10 @@ function animationDialogStyle(props) {
5048
5051
  }
5049
5052
  ` : styledComponents.css``;
5050
5053
  }
5051
- const key$3 = Symbol.for("@sanity/ui/context/dialog"), DialogContext = createGlobalScopedContext(key$3, {
5052
- version: 0
5053
- });
5054
+ const DialogContext = createGlobalScopedContext(
5055
+ "@sanity/ui/context/dialog",
5056
+ { version: 0 }
5057
+ );
5054
5058
  function useDialog() {
5055
5059
  return react.useContext(DialogContext);
5056
5060
  }
@@ -5276,7 +5280,10 @@ const Root$6 = styledComponents.styled.kbd`
5276
5280
  `, Hotkeys = react.forwardRef(function(props, ref) {
5277
5281
  const { fontSize: fontSize2, keys, padding, radius, space: spaceProp = 0.5, ...restProps } = props, space = useArrayProp(spaceProp);
5278
5282
  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);
5283
+ }), MenuContext = createGlobalScopedContext(
5284
+ "@sanity/ui/context/menu",
5285
+ null
5286
+ );
5280
5287
  function _isFocusable(element) {
5281
5288
  return isHTMLAnchorElement(element) && element.getAttribute("data-disabled") !== "true" || isHTMLButtonElement(element) && !element.disabled;
5282
5289
  }
@@ -6250,7 +6257,10 @@ function useMounted() {
6250
6257
  );
6251
6258
  }
6252
6259
  const subscribe = () => () => {
6253
- }, key$1 = Symbol.for("@sanity/ui/context/toast"), ToastContext = createGlobalScopedContext(key$1, null), Root$1 = styledComponents.styled(Layer)`
6260
+ }, ToastContext = createGlobalScopedContext(
6261
+ "@sanity/ui/context/toast",
6262
+ null
6263
+ ), Root$1 = styledComponents.styled(Layer)`
6254
6264
  position: fixed;
6255
6265
  top: 0;
6256
6266
  left: 0;
@@ -6409,7 +6419,10 @@ function _focusItemElement(el) {
6409
6419
  firstChild && firstChild instanceof HTMLElement && firstChild.focus();
6410
6420
  }
6411
6421
  }
6412
- const key = Symbol.for("@sanity/ui/context/tree"), TreeContext = createGlobalScopedContext(key, null), Tree = react.memo(
6422
+ const TreeContext = createGlobalScopedContext(
6423
+ "@sanity/ui/context/tree",
6424
+ null
6425
+ ), Tree = react.memo(
6413
6426
  react.forwardRef(function(props, forwardedRef) {
6414
6427
  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
6428
  react.useImperativeHandle(
@@ -6861,6 +6874,7 @@ exports.useElementSize = useElementSize;
6861
6874
  exports.useForwardedRef = useForwardedRef;
6862
6875
  exports.useGlobalKeyDown = useGlobalKeyDown;
6863
6876
  exports.useLayer = useLayer;
6877
+ exports.useMatchMedia = useMatchMedia;
6864
6878
  exports.useMediaIndex = useMediaIndex;
6865
6879
  exports.usePortal = usePortal;
6866
6880
  exports.usePrefersDark = usePrefersDark;