@sanity/ui 2.7.1-canary.0 → 2.7.1-canary.1
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.mts +43 -4
- package/dist/index.d.ts +43 -4
- package/dist/index.esm.js +18 -46
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +17 -45
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +18 -46
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/core/components/dialog/dialog.tsx +1 -1
- package/src/core/components/menu/menu.tsx +1 -1
- package/src/core/components/menu/menuButton.tsx +1 -1
- package/src/core/components/menu/menuContext.ts +1 -1
- package/src/core/hooks/useClickOutside.test.tsx +70 -0
- package/src/core/hooks/useClickOutside.ts +90 -65
- package/src/core/hooks/useMatchMedia.ts +3 -9
package/dist/index.js
CHANGED
|
@@ -126,45 +126,25 @@ function useArrayProp(val, defaultVal) {
|
|
|
126
126
|
[__perf_hash__]
|
|
127
127
|
);
|
|
128
128
|
}
|
|
129
|
-
function _getElements(element, elementsArg) {
|
|
130
|
-
const ret = [element];
|
|
131
|
-
for (const el of elementsArg)
|
|
132
|
-
Array.isArray(el) ? ret.push(...el) : ret.push(el);
|
|
133
|
-
return ret.filter(Boolean);
|
|
134
|
-
}
|
|
135
129
|
function useClickOutside(listener, elementsArg = EMPTY_ARRAY, boundaryElement) {
|
|
136
|
-
const [
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (prevElements.length !== nextElements.length) {
|
|
140
|
-
setElements(nextElements), elementsRef.current = nextElements;
|
|
130
|
+
const [legacyElement, setLegacyElement] = react.useState(null), onEvent = useEffectEvent.useEffectEvent((evt) => {
|
|
131
|
+
const target = evt.target;
|
|
132
|
+
if (!(target instanceof Node) || boundaryElement && !boundaryElement.contains(target))
|
|
141
133
|
return;
|
|
142
|
-
|
|
143
|
-
for (const el of
|
|
144
|
-
if (
|
|
145
|
-
setElements(nextElements), elementsRef.current = nextElements;
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
for (const el of nextElements)
|
|
149
|
-
if (!prevElements.includes(el)) {
|
|
150
|
-
setElements(nextElements), elementsRef.current = nextElements;
|
|
134
|
+
const elements = (Array.isArray(elementsArg) ? [legacyElement, ...elementsArg] : elementsArg()).flat();
|
|
135
|
+
for (const el of elements)
|
|
136
|
+
if (el && (target === el || el.contains(target)))
|
|
151
137
|
return;
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (target === el || el.contains(target))
|
|
160
|
-
return;
|
|
161
|
-
listener(evt);
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
return window.addEventListener("mousedown", handleWindowMouseDown), () => {
|
|
165
|
-
window.removeEventListener("mousedown", handleWindowMouseDown);
|
|
138
|
+
listener(evt);
|
|
139
|
+
}), hasListener = !!listener;
|
|
140
|
+
if (react.useEffect(() => {
|
|
141
|
+
if (!hasListener) return;
|
|
142
|
+
const handleEvent = (evt) => onEvent(evt);
|
|
143
|
+
return document.addEventListener("mousedown", handleEvent), document.addEventListener("touchstart", handleEvent), () => {
|
|
144
|
+
document.removeEventListener("mousedown", handleEvent), document.removeEventListener("touchstart", handleEvent);
|
|
166
145
|
};
|
|
167
|
-
}, [
|
|
146
|
+
}, [hasListener, onEvent]), Array.isArray(elementsArg))
|
|
147
|
+
return setLegacyElement;
|
|
168
148
|
}
|
|
169
149
|
function useCustomValidity(ref, customValidity) {
|
|
170
150
|
react.useEffect(() => {
|
|
@@ -515,21 +495,13 @@ function useGlobalKeyDown(onKeyDown) {
|
|
|
515
495
|
function useMatchMedia(mediaQueryString, getServerSnapshot2) {
|
|
516
496
|
const { subscribe: subscribe2, getSnapshot } = react.useMemo(() => {
|
|
517
497
|
let MEDIA_QUERY_CACHE;
|
|
518
|
-
|
|
519
|
-
return MEDIA_QUERY_CACHE === void 0 && (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE;
|
|
520
|
-
}
|
|
498
|
+
const getMatchMedia = () => (MEDIA_QUERY_CACHE || (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE);
|
|
521
499
|
return {
|
|
522
500
|
subscribe: (onStoreChange) => {
|
|
523
501
|
const matchMedia = getMatchMedia();
|
|
524
502
|
return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
|
|
525
503
|
},
|
|
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
|
-
}
|
|
504
|
+
getSnapshot: () => getMatchMedia().matches
|
|
533
505
|
};
|
|
534
506
|
}, [mediaQueryString]);
|
|
535
507
|
return react.useDebugValue(mediaQueryString), react.useSyncExternalStore(subscribe2, getSnapshot, getServerSnapshot2);
|