@sanity/ui 2.7.1-canary.0 → 2.7.1-canary.2
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 +67 -3
- package/dist/index.d.ts +67 -3
- package/dist/index.esm.js +29 -61
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +28 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +29 -61
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/src/core/components/dialog/dialog.tsx +9 -12
- package/src/core/components/menu/menu.tsx +1 -7
- package/src/core/hooks/useClickOutside.test.tsx +592 -0
- package/src/core/hooks/useClickOutside.ts +112 -63
- package/src/core/hooks/useMatchMedia.ts +3 -9
- package/src/core/primitives/popover/__workshop__/AlignedStory.tsx +9 -7
package/dist/index.js
CHANGED
|
@@ -126,45 +126,30 @@ 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
|
-
const prevElements = elementsRef.current, nextElements = _getElements(element, elementsArg);
|
|
139
|
-
if (prevElements.length !== nextElements.length) {
|
|
140
|
-
setElements(nextElements), elementsRef.current = nextElements;
|
|
130
|
+
const [legacyElement, setLegacyElement] = react.useState(null), onEvent = useEffectEvent.useEffectEvent((evt) => {
|
|
131
|
+
if (!listener)
|
|
141
132
|
return;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
133
|
+
const target = evt.target;
|
|
134
|
+
if (!(target instanceof Node))
|
|
135
|
+
return;
|
|
136
|
+
const resolvedBoundaryElement = typeof boundaryElement == "function" ? boundaryElement() : boundaryElement;
|
|
137
|
+
if (resolvedBoundaryElement && !resolvedBoundaryElement.contains(target))
|
|
138
|
+
return;
|
|
139
|
+
const elements = (Array.isArray(elementsArg) ? [legacyElement, ...elementsArg] : elementsArg()).flat();
|
|
140
|
+
for (const el of elements)
|
|
141
|
+
if (el && (target === el || el.contains(target)))
|
|
151
142
|
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);
|
|
143
|
+
listener(evt);
|
|
144
|
+
}), hasListener = !!listener;
|
|
145
|
+
if (react.useEffect(() => {
|
|
146
|
+
if (!hasListener) return;
|
|
147
|
+
const handleEvent = (evt) => onEvent(evt);
|
|
148
|
+
return document.addEventListener("mousedown", handleEvent), () => {
|
|
149
|
+
document.removeEventListener("mousedown", handleEvent);
|
|
166
150
|
};
|
|
167
|
-
}, [
|
|
151
|
+
}, [hasListener, onEvent]), Array.isArray(elementsArg))
|
|
152
|
+
return setLegacyElement;
|
|
168
153
|
}
|
|
169
154
|
function useCustomValidity(ref, customValidity) {
|
|
170
155
|
react.useEffect(() => {
|
|
@@ -515,21 +500,13 @@ function useGlobalKeyDown(onKeyDown) {
|
|
|
515
500
|
function useMatchMedia(mediaQueryString, getServerSnapshot2) {
|
|
516
501
|
const { subscribe: subscribe2, getSnapshot } = react.useMemo(() => {
|
|
517
502
|
let MEDIA_QUERY_CACHE;
|
|
518
|
-
|
|
519
|
-
return MEDIA_QUERY_CACHE === void 0 && (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE;
|
|
520
|
-
}
|
|
503
|
+
const getMatchMedia = () => (MEDIA_QUERY_CACHE || (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE);
|
|
521
504
|
return {
|
|
522
505
|
subscribe: (onStoreChange) => {
|
|
523
506
|
const matchMedia = getMatchMedia();
|
|
524
507
|
return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
|
|
525
508
|
},
|
|
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
|
-
}
|
|
509
|
+
getSnapshot: () => getMatchMedia().matches
|
|
533
510
|
};
|
|
534
511
|
}, [mediaQueryString]);
|
|
535
512
|
return react.useDebugValue(mediaQueryString), react.useSyncExternalStore(subscribe2, getSnapshot, getServerSnapshot2);
|
|
@@ -5127,14 +5104,11 @@ const Root$7 = styledComponents.styled(Layer)(responsivePaddingStyle, dialogStyl
|
|
|
5127
5104
|
[boundaryElement, isTopLayer, onClose, portalElement]
|
|
5128
5105
|
)
|
|
5129
5106
|
), useClickOutside(
|
|
5130
|
-
|
|
5131
|
-
(
|
|
5132
|
-
|
|
5133
|
-
|
|
5134
|
-
|
|
5135
|
-
},
|
|
5136
|
-
[boundaryElement, isTopLayer, onClickOutside, portalElement]
|
|
5137
|
-
),
|
|
5107
|
+
(event) => {
|
|
5108
|
+
if (!isTopLayer || !onClickOutside) return;
|
|
5109
|
+
const target = event.target;
|
|
5110
|
+
target && !isTargetWithinScope(boundaryElement, portalElement, target) || onClickOutside();
|
|
5111
|
+
},
|
|
5138
5112
|
[rootElement]
|
|
5139
5113
|
);
|
|
5140
5114
|
const setRef = react.useCallback((el) => {
|
|
@@ -5471,13 +5445,7 @@ const Root$5 = styledComponents.styled(Box)`
|
|
|
5471
5445
|
);
|
|
5472
5446
|
react.useEffect(() => {
|
|
5473
5447
|
onItemSelect && onItemSelect(activeIndex);
|
|
5474
|
-
}, [activeIndex, onItemSelect]), useClickOutside(
|
|
5475
|
-
react.useCallback(
|
|
5476
|
-
(event) => isTopLayer && onClickOutside && onClickOutside(event),
|
|
5477
|
-
[isTopLayer, onClickOutside]
|
|
5478
|
-
),
|
|
5479
|
-
[rootElement]
|
|
5480
|
-
), useGlobalKeyDown(
|
|
5448
|
+
}, [activeIndex, onItemSelect]), useClickOutside((event) => isTopLayer && onClickOutside && onClickOutside(event), [rootElement]), useGlobalKeyDown(
|
|
5481
5449
|
react.useCallback(
|
|
5482
5450
|
(event) => {
|
|
5483
5451
|
isTopLayer && event.key === "Escape" && (event.stopPropagation(), onEscape && onEscape());
|