@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 +8 -0
- package/dist/index.esm.js +75 -78
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +74 -77
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/toast/toastProvider.tsx +55 -52
- package/src/hooks/useArrayProp.ts +2 -1
- package/src/hooks/useMediaIndex/useMediaIndex.test.tsx +31 -0
- package/src/hooks/useMediaIndex/useMediaIndex.ts +98 -6
- package/src/hooks/useMounted.ts +14 -0
- package/src/hooks/usePrefersDark.hydration.test.tsx +56 -0
- package/src/hooks/usePrefersDark.test.tsx +18 -0
- package/src/hooks/usePrefersDark.ts +51 -18
- package/src/observers/resizeObserver.ts +1 -1
- package/src/utils/portal/portalContext.ts +1 -1
- package/src/utils/portal/portalProvider.tsx +1 -1
- package/src/hooks/useMediaIndex/lib/media.ts +0 -88
package/dist/index.js
CHANGED
|
@@ -235,7 +235,7 @@ function responsiveTextFont(props) {
|
|
|
235
235
|
return responsiveFont("text", props);
|
|
236
236
|
}
|
|
237
237
|
function useArrayProp(val, defaultVal) {
|
|
238
|
-
const __perf_hash__ = JSON.stringify(val != null ? val : defaultVal);
|
|
238
|
+
const __perf_hash__ = react.useMemo(() => JSON.stringify(val != null ? val : defaultVal), [defaultVal, val]);
|
|
239
239
|
return react.useMemo(() => _getArrayProp(val, defaultVal), [__perf_hash__]);
|
|
240
240
|
}
|
|
241
241
|
function _getElements(element, elementsArg) {
|
|
@@ -827,7 +827,7 @@ var ResizeObserver = function () {
|
|
|
827
827
|
};
|
|
828
828
|
return ResizeObserver;
|
|
829
829
|
}();
|
|
830
|
-
const _ResizeObserver = typeof
|
|
830
|
+
const _ResizeObserver = typeof document !== "undefined" && window.ResizeObserver ? window.ResizeObserver : ResizeObserver;
|
|
831
831
|
const _elementSizeObserver = _createElementSizeObserver();
|
|
832
832
|
function _createElementRectValueListener() {
|
|
833
833
|
return {
|
|
@@ -3181,7 +3181,7 @@ function ThemeColorProvider(props) {
|
|
|
3181
3181
|
function useTheme() {
|
|
3182
3182
|
return styled.useTheme();
|
|
3183
3183
|
}
|
|
3184
|
-
const
|
|
3184
|
+
const MEDIA_STORE_CACHE = /* @__PURE__ */new WeakMap();
|
|
3185
3185
|
function _getMediaQuery(media, index) {
|
|
3186
3186
|
if (index === 0) {
|
|
3187
3187
|
return "screen and (max-width: ".concat(media[index] - 1, "px)");
|
|
@@ -3191,48 +3191,41 @@ function _getMediaQuery(media, index) {
|
|
|
3191
3191
|
}
|
|
3192
3192
|
return "screen and (min-width: ".concat(media[index - 1], "px) and (max-width: ").concat(media[index] - 1, "px)");
|
|
3193
3193
|
}
|
|
3194
|
-
function
|
|
3194
|
+
function _createMediaStore(media) {
|
|
3195
3195
|
const mediaLen = media.length;
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
index
|
|
3202
|
-
|
|
3203
|
-
|
|
3196
|
+
let sizes;
|
|
3197
|
+
const getSizes = () => {
|
|
3198
|
+
if (!sizes) {
|
|
3199
|
+
sizes = [];
|
|
3200
|
+
for (let index = mediaLen; index > -1; index -= 1) {
|
|
3201
|
+
const mediaQuery = _getMediaQuery(media, index);
|
|
3202
|
+
sizes.push({
|
|
3203
|
+
index,
|
|
3204
|
+
mq: window.matchMedia(mediaQuery)
|
|
3205
|
+
});
|
|
3206
|
+
}
|
|
3204
3207
|
}
|
|
3205
|
-
|
|
3206
|
-
|
|
3208
|
+
return sizes;
|
|
3209
|
+
};
|
|
3210
|
+
const getSnapshot = () => {
|
|
3207
3211
|
for (const {
|
|
3208
3212
|
index,
|
|
3209
3213
|
mq
|
|
3210
|
-
} of
|
|
3214
|
+
} of getSizes()) {
|
|
3211
3215
|
if (mq.matches) return index;
|
|
3212
3216
|
}
|
|
3213
3217
|
return 0;
|
|
3214
3218
|
};
|
|
3215
|
-
const subscribe =
|
|
3219
|
+
const subscribe = onStoreChange => {
|
|
3216
3220
|
const disposeFns = [];
|
|
3217
3221
|
for (const {
|
|
3218
|
-
index,
|
|
3219
3222
|
mq
|
|
3220
|
-
} of
|
|
3223
|
+
} of getSizes()) {
|
|
3221
3224
|
const handleChange = () => {
|
|
3222
|
-
if (mq.matches)
|
|
3225
|
+
if (mq.matches) onStoreChange();
|
|
3223
3226
|
};
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
} else {
|
|
3227
|
-
mq.addListener(handleChange);
|
|
3228
|
-
}
|
|
3229
|
-
disposeFns.push(() => {
|
|
3230
|
-
if (mq.removeEventListener) {
|
|
3231
|
-
mq.removeEventListener("change", handleChange);
|
|
3232
|
-
} else {
|
|
3233
|
-
mq.removeListener(handleChange);
|
|
3234
|
-
}
|
|
3235
|
-
});
|
|
3227
|
+
mq.addEventListener("change", handleChange);
|
|
3228
|
+
disposeFns.push(() => mq.removeEventListener("change", handleChange));
|
|
3236
3229
|
}
|
|
3237
3230
|
return () => {
|
|
3238
3231
|
for (const disposeFn of disposeFns) {
|
|
@@ -3241,42 +3234,45 @@ function _createMediaManager(media) {
|
|
|
3241
3234
|
};
|
|
3242
3235
|
};
|
|
3243
3236
|
return {
|
|
3244
|
-
|
|
3237
|
+
getSnapshot,
|
|
3245
3238
|
subscribe
|
|
3246
3239
|
};
|
|
3247
3240
|
}
|
|
3248
|
-
function
|
|
3249
|
-
|
|
3250
|
-
if (!manager) {
|
|
3251
|
-
manager = _createMediaManager(media);
|
|
3252
|
-
MEDIA_MANAGER_CACHE.set(media, manager);
|
|
3253
|
-
}
|
|
3254
|
-
return manager;
|
|
3241
|
+
function getServerSnapshot$1() {
|
|
3242
|
+
return 0;
|
|
3255
3243
|
}
|
|
3256
3244
|
function useMediaIndex() {
|
|
3257
3245
|
const theme = useTheme();
|
|
3258
3246
|
const {
|
|
3259
3247
|
media
|
|
3260
3248
|
} = theme.sanity;
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3249
|
+
let store = MEDIA_STORE_CACHE.get(media);
|
|
3250
|
+
if (!store) {
|
|
3251
|
+
store = _createMediaStore(media);
|
|
3252
|
+
MEDIA_STORE_CACHE.set(media, store);
|
|
3253
|
+
}
|
|
3254
|
+
return react.useSyncExternalStore(store.subscribe, store.getSnapshot, getServerSnapshot$1);
|
|
3255
|
+
}
|
|
3256
|
+
let MEDIA_QUERY_CACHE;
|
|
3257
|
+
function getMatchMedia() {
|
|
3258
|
+
if (!MEDIA_QUERY_CACHE) {
|
|
3259
|
+
MEDIA_QUERY_CACHE = window.matchMedia("(prefers-color-scheme: dark)");
|
|
3260
|
+
}
|
|
3261
|
+
return MEDIA_QUERY_CACHE;
|
|
3262
|
+
}
|
|
3263
|
+
function subscribe(onStoreChange) {
|
|
3264
|
+
const matchMedia = getMatchMedia();
|
|
3265
|
+
matchMedia.addEventListener("change", onStoreChange);
|
|
3266
|
+
return () => matchMedia.removeEventListener("change", onStoreChange);
|
|
3267
|
+
}
|
|
3268
|
+
function getSnapshot() {
|
|
3269
|
+
return getMatchMedia().matches;
|
|
3270
|
+
}
|
|
3271
|
+
function getServerSnapshot() {
|
|
3272
|
+
return false;
|
|
3265
3273
|
}
|
|
3266
3274
|
function usePrefersDark() {
|
|
3267
|
-
|
|
3268
|
-
if (typeof window === "undefined") return void 0;
|
|
3269
|
-
return window.matchMedia("(prefers-color-scheme: dark)");
|
|
3270
|
-
}, []);
|
|
3271
|
-
const [dark, setDark] = react.useState((mq == null ? void 0 : mq.matches) || false);
|
|
3272
|
-
react.useEffect(() => {
|
|
3273
|
-
if (!mq) return void 0;
|
|
3274
|
-
setDark(mq.matches);
|
|
3275
|
-
const handleChange = () => setDark(mq.matches);
|
|
3276
|
-
mq.addEventListener("change", handleChange);
|
|
3277
|
-
return () => mq.removeEventListener("change", handleChange);
|
|
3278
|
-
}, [mq]);
|
|
3279
|
-
return dark;
|
|
3275
|
+
return react.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
3280
3276
|
}
|
|
3281
3277
|
const useIsomorphicEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect;
|
|
3282
3278
|
function useForwardedRef(ref) {
|
|
@@ -5363,7 +5359,7 @@ const defaultContextValue = {
|
|
|
5363
5359
|
version: 0,
|
|
5364
5360
|
boundaryElement: null,
|
|
5365
5361
|
get element() {
|
|
5366
|
-
if (typeof
|
|
5362
|
+
if (typeof document === "undefined") {
|
|
5367
5363
|
return null;
|
|
5368
5364
|
}
|
|
5369
5365
|
if (globalScope[elementKey]) {
|
|
@@ -5418,7 +5414,7 @@ function _isEqual(objA, objB) {
|
|
|
5418
5414
|
}
|
|
5419
5415
|
return keysA.every(key => objA[key] === objB[key]);
|
|
5420
5416
|
}
|
|
5421
|
-
const __BROWSER__ = typeof
|
|
5417
|
+
const __BROWSER__ = typeof document !== "undefined";
|
|
5422
5418
|
function PortalProvider(props) {
|
|
5423
5419
|
const {
|
|
5424
5420
|
boundaryElement,
|
|
@@ -8530,6 +8526,11 @@ function Toast(props) {
|
|
|
8530
8526
|
})
|
|
8531
8527
|
}));
|
|
8532
8528
|
}
|
|
8529
|
+
function useMounted() {
|
|
8530
|
+
const [mounted, mount] = react.useReducer(() => true, false);
|
|
8531
|
+
react.useEffect(mount, [mount]);
|
|
8532
|
+
return mounted;
|
|
8533
|
+
}
|
|
8533
8534
|
const key$1 = Symbol.for("@sanity/ui/context/toast");
|
|
8534
8535
|
globalScope[key$1] = globalScope[key$1] || react.createContext(null);
|
|
8535
8536
|
const ToastContext = globalScope[key$1];
|
|
@@ -8544,24 +8545,21 @@ function ToastProvider(props) {
|
|
|
8544
8545
|
paddingY,
|
|
8545
8546
|
zOffset
|
|
8546
8547
|
} = props;
|
|
8547
|
-
const [state,
|
|
8548
|
-
toasts: []
|
|
8549
|
-
});
|
|
8548
|
+
const [state, _setState] = react.useState([]);
|
|
8550
8549
|
const toastsRef = react.useRef({});
|
|
8551
8550
|
const push = react.useCallback(params => {
|
|
8551
|
+
const setState = state2 => react.startTransition(() => _setState(state2));
|
|
8552
8552
|
const id = params.id || String(toastId++);
|
|
8553
8553
|
const duration = params.duration || 5e3;
|
|
8554
8554
|
const dismiss = () => {
|
|
8555
8555
|
var _a;
|
|
8556
8556
|
const timeoutId = (_a = toastsRef.current[id]) == null ? void 0 : _a.timeoutId;
|
|
8557
8557
|
setState(prevState => {
|
|
8558
|
-
const idx = prevState.
|
|
8558
|
+
const idx = prevState.findIndex(t => t.id === id);
|
|
8559
8559
|
if (idx > -1) {
|
|
8560
|
-
const toasts = prevState.
|
|
8560
|
+
const toasts = prevState.slice(0);
|
|
8561
8561
|
toasts.splice(idx, 1);
|
|
8562
|
-
return
|
|
8563
|
-
toasts
|
|
8564
|
-
});
|
|
8562
|
+
return toasts;
|
|
8565
8563
|
}
|
|
8566
8564
|
return prevState;
|
|
8567
8565
|
});
|
|
@@ -8571,15 +8569,13 @@ function ToastProvider(props) {
|
|
|
8571
8569
|
}
|
|
8572
8570
|
};
|
|
8573
8571
|
setState(prevState => {
|
|
8574
|
-
return
|
|
8575
|
-
|
|
8576
|
-
|
|
8577
|
-
|
|
8578
|
-
|
|
8579
|
-
|
|
8580
|
-
|
|
8581
|
-
}])
|
|
8582
|
-
});
|
|
8572
|
+
return prevState.filter(t => t.id !== id).concat([{
|
|
8573
|
+
dismiss,
|
|
8574
|
+
id,
|
|
8575
|
+
params: _objectSpread(_objectSpread({}, params), {}, {
|
|
8576
|
+
duration
|
|
8577
|
+
})
|
|
8578
|
+
}]);
|
|
8583
8579
|
});
|
|
8584
8580
|
if (toastsRef.current[id]) {
|
|
8585
8581
|
clearTimeout(toastsRef.current[id].timeoutId);
|
|
@@ -8602,9 +8598,10 @@ function ToastProvider(props) {
|
|
|
8602
8598
|
version: 0,
|
|
8603
8599
|
push
|
|
8604
8600
|
}), [push]);
|
|
8601
|
+
const mounted = useMounted();
|
|
8605
8602
|
return /* @__PURE__ */jsxRuntime.jsxs(ToastContext.Provider, {
|
|
8606
8603
|
value,
|
|
8607
|
-
children: [children, /* @__PURE__ */jsxRuntime.jsx(Root$1, {
|
|
8604
|
+
children: [children, mounted && /* @__PURE__ */jsxRuntime.jsx(Root$1, {
|
|
8608
8605
|
"data-ui": "ToastProvider",
|
|
8609
8606
|
zOffset,
|
|
8610
8607
|
children: /* @__PURE__ */jsxRuntime.jsx(ToastContainer, {
|
|
@@ -8614,7 +8611,7 @@ function ToastProvider(props) {
|
|
|
8614
8611
|
paddingY,
|
|
8615
8612
|
children: /* @__PURE__ */jsxRuntime.jsx(framerMotion.AnimatePresence, {
|
|
8616
8613
|
initial: false,
|
|
8617
|
-
children: state.
|
|
8614
|
+
children: state.map(_ref27 => {
|
|
8618
8615
|
let {
|
|
8619
8616
|
dismiss,
|
|
8620
8617
|
id,
|