@sanity/ui 1.0.7 → 1.0.8

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.d.ts CHANGED
@@ -7721,6 +7721,14 @@ export declare interface MediaQueryProps {
7721
7721
  media?: number[]
7722
7722
  }
7723
7723
 
7724
+ /**
7725
+ * @internal
7726
+ */
7727
+ export declare interface _MediaStore {
7728
+ subscribe: (onStoreChange: () => void) => () => void
7729
+ getSnapshot: () => number
7730
+ }
7731
+
7724
7732
  /**
7725
7733
  * @public
7726
7734
  */
package/dist/index.esm.js CHANGED
@@ -59,7 +59,7 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
59
59
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
60
60
  function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
61
61
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
62
- import { useMemo, useState, useRef, useEffect, createContext, useContext, useLayoutEffect, forwardRef, useId, useCallback, cloneElement, isValidElement, createElement, Component, memo, useReducer, Children, Fragment as Fragment$1 } from 'react';
62
+ import { useMemo, useState, useRef, useEffect, createContext, useContext, useSyncExternalStore, useLayoutEffect, forwardRef, useId, useCallback, cloneElement, isValidElement, createElement, Component, memo, useReducer, Children, Fragment as Fragment$1, startTransition } from 'react';
63
63
  import ReactIs, { isElement as isElement$1, isFragment, isValidElementType } from 'react-is';
64
64
  import styled, { ThemeProvider as ThemeProvider$1, useTheme as useTheme$1, css, keyframes } from 'styled-components';
65
65
  import { white as white$1, black as black$1, hues } from '@sanity/color';
@@ -222,7 +222,7 @@ function responsiveTextFont(props) {
222
222
  return responsiveFont("text", props);
223
223
  }
224
224
  function useArrayProp(val, defaultVal) {
225
- const __perf_hash__ = JSON.stringify(val != null ? val : defaultVal);
225
+ const __perf_hash__ = useMemo(() => JSON.stringify(val != null ? val : defaultVal), [defaultVal, val]);
226
226
  return useMemo(() => _getArrayProp(val, defaultVal), [__perf_hash__]);
227
227
  }
228
228
  function _getElements(element, elementsArg) {
@@ -814,7 +814,7 @@ var ResizeObserver = function () {
814
814
  };
815
815
  return ResizeObserver;
816
816
  }();
817
- const _ResizeObserver = typeof window !== "undefined" && window.ResizeObserver ? window.ResizeObserver : ResizeObserver;
817
+ const _ResizeObserver = typeof document !== "undefined" && window.ResizeObserver ? window.ResizeObserver : ResizeObserver;
818
818
  const _elementSizeObserver = _createElementSizeObserver();
819
819
  function _createElementRectValueListener() {
820
820
  return {
@@ -3168,7 +3168,7 @@ function ThemeColorProvider(props) {
3168
3168
  function useTheme() {
3169
3169
  return useTheme$1();
3170
3170
  }
3171
- const MEDIA_MANAGER_CACHE = /* @__PURE__ */new WeakMap();
3171
+ const MEDIA_STORE_CACHE = /* @__PURE__ */new WeakMap();
3172
3172
  function _getMediaQuery(media, index) {
3173
3173
  if (index === 0) {
3174
3174
  return "screen and (max-width: ".concat(media[index] - 1, "px)");
@@ -3178,48 +3178,41 @@ function _getMediaQuery(media, index) {
3178
3178
  }
3179
3179
  return "screen and (min-width: ".concat(media[index - 1], "px) and (max-width: ").concat(media[index] - 1, "px)");
3180
3180
  }
3181
- function _createMediaManager(media) {
3181
+ function _createMediaStore(media) {
3182
3182
  const mediaLen = media.length;
3183
- const sizes = [];
3184
- if (typeof window !== "undefined") {
3185
- for (let index = mediaLen; index > -1; index -= 1) {
3186
- const mediaQuery = _getMediaQuery(media, index);
3187
- sizes.push({
3188
- index,
3189
- mq: window.matchMedia(mediaQuery)
3190
- });
3183
+ let sizes;
3184
+ const getSizes = () => {
3185
+ if (!sizes) {
3186
+ sizes = [];
3187
+ for (let index = mediaLen; index > -1; index -= 1) {
3188
+ const mediaQuery = _getMediaQuery(media, index);
3189
+ sizes.push({
3190
+ index,
3191
+ mq: window.matchMedia(mediaQuery)
3192
+ });
3193
+ }
3191
3194
  }
3192
- }
3193
- const getCurrentIndex = () => {
3195
+ return sizes;
3196
+ };
3197
+ const getSnapshot = () => {
3194
3198
  for (const {
3195
3199
  index,
3196
3200
  mq
3197
- } of sizes) {
3201
+ } of getSizes()) {
3198
3202
  if (mq.matches) return index;
3199
3203
  }
3200
3204
  return 0;
3201
3205
  };
3202
- const subscribe = subscriber => {
3206
+ const subscribe = onStoreChange => {
3203
3207
  const disposeFns = [];
3204
3208
  for (const {
3205
- index,
3206
3209
  mq
3207
- } of sizes) {
3210
+ } of getSizes()) {
3208
3211
  const handleChange = () => {
3209
- if (mq.matches) subscriber(index);
3212
+ if (mq.matches) onStoreChange();
3210
3213
  };
3211
- if (mq.addEventListener) {
3212
- mq.addEventListener("change", handleChange);
3213
- } else {
3214
- mq.addListener(handleChange);
3215
- }
3216
- disposeFns.push(() => {
3217
- if (mq.removeEventListener) {
3218
- mq.removeEventListener("change", handleChange);
3219
- } else {
3220
- mq.removeListener(handleChange);
3221
- }
3222
- });
3214
+ mq.addEventListener("change", handleChange);
3215
+ disposeFns.push(() => mq.removeEventListener("change", handleChange));
3223
3216
  }
3224
3217
  return () => {
3225
3218
  for (const disposeFn of disposeFns) {
@@ -3228,42 +3221,45 @@ function _createMediaManager(media) {
3228
3221
  };
3229
3222
  };
3230
3223
  return {
3231
- getCurrentIndex,
3224
+ getSnapshot,
3232
3225
  subscribe
3233
3226
  };
3234
3227
  }
3235
- function _getMediaManager(media) {
3236
- let manager = MEDIA_MANAGER_CACHE.get(media);
3237
- if (!manager) {
3238
- manager = _createMediaManager(media);
3239
- MEDIA_MANAGER_CACHE.set(media, manager);
3240
- }
3241
- return manager;
3228
+ function getServerSnapshot$1() {
3229
+ return 0;
3242
3230
  }
3243
3231
  function useMediaIndex() {
3244
3232
  const theme = useTheme();
3245
3233
  const {
3246
3234
  media
3247
3235
  } = theme.sanity;
3248
- const manager = useMemo(() => _getMediaManager(media), [media]);
3249
- const [index, setIndex] = useState(manager.getCurrentIndex);
3250
- useEffect(() => manager.subscribe(setIndex), [manager]);
3251
- return index;
3236
+ let store = MEDIA_STORE_CACHE.get(media);
3237
+ if (!store) {
3238
+ store = _createMediaStore(media);
3239
+ MEDIA_STORE_CACHE.set(media, store);
3240
+ }
3241
+ return useSyncExternalStore(store.subscribe, store.getSnapshot, getServerSnapshot$1);
3242
+ }
3243
+ let MEDIA_QUERY_CACHE;
3244
+ function getMatchMedia() {
3245
+ if (!MEDIA_QUERY_CACHE) {
3246
+ MEDIA_QUERY_CACHE = window.matchMedia("(prefers-color-scheme: dark)");
3247
+ }
3248
+ return MEDIA_QUERY_CACHE;
3249
+ }
3250
+ function subscribe(onStoreChange) {
3251
+ const matchMedia = getMatchMedia();
3252
+ matchMedia.addEventListener("change", onStoreChange);
3253
+ return () => matchMedia.removeEventListener("change", onStoreChange);
3254
+ }
3255
+ function getSnapshot() {
3256
+ return getMatchMedia().matches;
3257
+ }
3258
+ function getServerSnapshot() {
3259
+ return false;
3252
3260
  }
3253
3261
  function usePrefersDark() {
3254
- const mq = useMemo(() => {
3255
- if (typeof window === "undefined") return void 0;
3256
- return window.matchMedia("(prefers-color-scheme: dark)");
3257
- }, []);
3258
- const [dark, setDark] = useState((mq == null ? void 0 : mq.matches) || false);
3259
- useEffect(() => {
3260
- if (!mq) return void 0;
3261
- setDark(mq.matches);
3262
- const handleChange = () => setDark(mq.matches);
3263
- mq.addEventListener("change", handleChange);
3264
- return () => mq.removeEventListener("change", handleChange);
3265
- }, [mq]);
3266
- return dark;
3262
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
3267
3263
  }
3268
3264
  const useIsomorphicEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
3269
3265
  function useForwardedRef(ref) {
@@ -5350,7 +5346,7 @@ const defaultContextValue = {
5350
5346
  version: 0,
5351
5347
  boundaryElement: null,
5352
5348
  get element() {
5353
- if (typeof window === "undefined") {
5349
+ if (typeof document === "undefined") {
5354
5350
  return null;
5355
5351
  }
5356
5352
  if (globalScope[elementKey]) {
@@ -5405,7 +5401,7 @@ function _isEqual(objA, objB) {
5405
5401
  }
5406
5402
  return keysA.every(key => objA[key] === objB[key]);
5407
5403
  }
5408
- const __BROWSER__ = typeof window !== "undefined";
5404
+ const __BROWSER__ = typeof document !== "undefined";
5409
5405
  function PortalProvider(props) {
5410
5406
  const {
5411
5407
  boundaryElement,
@@ -8517,6 +8513,11 @@ function Toast(props) {
8517
8513
  })
8518
8514
  }));
8519
8515
  }
8516
+ function useMounted() {
8517
+ const [mounted, mount] = useReducer(() => true, false);
8518
+ useEffect(mount, [mount]);
8519
+ return mounted;
8520
+ }
8520
8521
  const key$1 = Symbol.for("@sanity/ui/context/toast");
8521
8522
  globalScope[key$1] = globalScope[key$1] || createContext(null);
8522
8523
  const ToastContext = globalScope[key$1];
@@ -8531,24 +8532,21 @@ function ToastProvider(props) {
8531
8532
  paddingY,
8532
8533
  zOffset
8533
8534
  } = props;
8534
- const [state, setState] = useState({
8535
- toasts: []
8536
- });
8535
+ const [state, _setState] = useState([]);
8537
8536
  const toastsRef = useRef({});
8538
8537
  const push = useCallback(params => {
8538
+ const setState = state2 => startTransition(() => _setState(state2));
8539
8539
  const id = params.id || String(toastId++);
8540
8540
  const duration = params.duration || 5e3;
8541
8541
  const dismiss = () => {
8542
8542
  var _a;
8543
8543
  const timeoutId = (_a = toastsRef.current[id]) == null ? void 0 : _a.timeoutId;
8544
8544
  setState(prevState => {
8545
- const idx = prevState.toasts.findIndex(t => t.id === id);
8545
+ const idx = prevState.findIndex(t => t.id === id);
8546
8546
  if (idx > -1) {
8547
- const toasts = prevState.toasts.slice(0);
8547
+ const toasts = prevState.slice(0);
8548
8548
  toasts.splice(idx, 1);
8549
- return _objectSpread(_objectSpread({}, prevState), {}, {
8550
- toasts
8551
- });
8549
+ return toasts;
8552
8550
  }
8553
8551
  return prevState;
8554
8552
  });
@@ -8558,15 +8556,13 @@ function ToastProvider(props) {
8558
8556
  }
8559
8557
  };
8560
8558
  setState(prevState => {
8561
- return _objectSpread(_objectSpread({}, prevState), {}, {
8562
- toasts: prevState.toasts.filter(t => t.id !== id).concat([{
8563
- dismiss,
8564
- id,
8565
- params: _objectSpread(_objectSpread({}, params), {}, {
8566
- duration
8567
- })
8568
- }])
8569
- });
8559
+ return prevState.filter(t => t.id !== id).concat([{
8560
+ dismiss,
8561
+ id,
8562
+ params: _objectSpread(_objectSpread({}, params), {}, {
8563
+ duration
8564
+ })
8565
+ }]);
8570
8566
  });
8571
8567
  if (toastsRef.current[id]) {
8572
8568
  clearTimeout(toastsRef.current[id].timeoutId);
@@ -8589,9 +8585,10 @@ function ToastProvider(props) {
8589
8585
  version: 0,
8590
8586
  push
8591
8587
  }), [push]);
8588
+ const mounted = useMounted();
8592
8589
  return /* @__PURE__ */jsxs(ToastContext.Provider, {
8593
8590
  value,
8594
- children: [children, /* @__PURE__ */jsx(Root$1, {
8591
+ children: [children, mounted && /* @__PURE__ */jsx(Root$1, {
8595
8592
  "data-ui": "ToastProvider",
8596
8593
  zOffset,
8597
8594
  children: /* @__PURE__ */jsx(ToastContainer, {
@@ -8601,7 +8598,7 @@ function ToastProvider(props) {
8601
8598
  paddingY,
8602
8599
  children: /* @__PURE__ */jsx(AnimatePresence, {
8603
8600
  initial: false,
8604
- children: state.toasts.map(_ref27 => {
8601
+ children: state.map(_ref27 => {
8605
8602
  let {
8606
8603
  dismiss,
8607
8604
  id,