@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.d.mts
CHANGED
|
@@ -628,7 +628,7 @@ export declare interface CheckboxProps {
|
|
|
628
628
|
/**
|
|
629
629
|
* @public
|
|
630
630
|
*/
|
|
631
|
-
export declare type ClickOutsideListener = (event: MouseEvent) => void
|
|
631
|
+
export declare type ClickOutsideListener = (event: MouseEvent | TouchEvent) => void
|
|
632
632
|
|
|
633
633
|
/**
|
|
634
634
|
* @public
|
|
@@ -1432,7 +1432,7 @@ export declare interface MenuProps extends ResponsivePaddingProps {
|
|
|
1432
1432
|
* @deprecated Use `shouldFocus="last"` instead.
|
|
1433
1433
|
*/
|
|
1434
1434
|
focusLast?: boolean
|
|
1435
|
-
onClickOutside?: (event: MouseEvent) => void
|
|
1435
|
+
onClickOutside?: (event: MouseEvent | TouchEvent) => void
|
|
1436
1436
|
onEscape?: () => void
|
|
1437
1437
|
onItemClick?: () => void
|
|
1438
1438
|
onItemSelect?: (index: number) => void
|
|
@@ -2653,9 +2653,48 @@ export declare function useBoundaryElement(): BoundaryElementContextValue
|
|
|
2653
2653
|
*/
|
|
2654
2654
|
export declare function useClickOutside(
|
|
2655
2655
|
listener: ClickOutsideListener,
|
|
2656
|
-
elementsArg
|
|
2656
|
+
elementsArg: () => (HTMLElement | HTMLElement[] | null)[],
|
|
2657
2657
|
boundaryElement?: HTMLElement | null,
|
|
2658
|
-
):
|
|
2658
|
+
): void
|
|
2659
|
+
|
|
2660
|
+
/**
|
|
2661
|
+
* @public
|
|
2662
|
+
* @deprecated
|
|
2663
|
+
* Instead of:
|
|
2664
|
+
* ```tsx
|
|
2665
|
+
* const buttonRef = useRef(null)
|
|
2666
|
+
* const setElement = useClickOutside(() => {}, [buttonRef.current])
|
|
2667
|
+
* return (
|
|
2668
|
+
* <>
|
|
2669
|
+
* <button ref={buttonRef} />
|
|
2670
|
+
* {open && <div ref={setElement} />}
|
|
2671
|
+
* </>
|
|
2672
|
+
* )
|
|
2673
|
+
* ```
|
|
2674
|
+
* Use:
|
|
2675
|
+
* ```tsx
|
|
2676
|
+
* const buttonRef = useRef()
|
|
2677
|
+
* const [element, setElement] = useState(null)
|
|
2678
|
+
* useClickOutside(() => {}, () => [buttonRef.current, element])
|
|
2679
|
+
* return (
|
|
2680
|
+
* <>
|
|
2681
|
+
* <button ref={buttonRef} />
|
|
2682
|
+
* {open && <div ref={setElement} />}
|
|
2683
|
+
* </>
|
|
2684
|
+
* )
|
|
2685
|
+
* ```
|
|
2686
|
+
*/
|
|
2687
|
+
export declare function useClickOutside(
|
|
2688
|
+
listener: ClickOutsideListener,
|
|
2689
|
+
elementsArg: (HTMLElement | HTMLElement[] | null)[],
|
|
2690
|
+
boundaryElement?: HTMLElement | null,
|
|
2691
|
+
): UseClickOutsideSetElement
|
|
2692
|
+
|
|
2693
|
+
/**
|
|
2694
|
+
* @public
|
|
2695
|
+
* @deprecated -- do not use
|
|
2696
|
+
*/
|
|
2697
|
+
export declare type UseClickOutsideSetElement = (el: HTMLElement | null) => void
|
|
2659
2698
|
|
|
2660
2699
|
/**
|
|
2661
2700
|
* @beta
|
package/dist/index.d.ts
CHANGED
|
@@ -628,7 +628,7 @@ export declare interface CheckboxProps {
|
|
|
628
628
|
/**
|
|
629
629
|
* @public
|
|
630
630
|
*/
|
|
631
|
-
export declare type ClickOutsideListener = (event: MouseEvent) => void
|
|
631
|
+
export declare type ClickOutsideListener = (event: MouseEvent | TouchEvent) => void
|
|
632
632
|
|
|
633
633
|
/**
|
|
634
634
|
* @public
|
|
@@ -1432,7 +1432,7 @@ export declare interface MenuProps extends ResponsivePaddingProps {
|
|
|
1432
1432
|
* @deprecated Use `shouldFocus="last"` instead.
|
|
1433
1433
|
*/
|
|
1434
1434
|
focusLast?: boolean
|
|
1435
|
-
onClickOutside?: (event: MouseEvent) => void
|
|
1435
|
+
onClickOutside?: (event: MouseEvent | TouchEvent) => void
|
|
1436
1436
|
onEscape?: () => void
|
|
1437
1437
|
onItemClick?: () => void
|
|
1438
1438
|
onItemSelect?: (index: number) => void
|
|
@@ -2653,9 +2653,48 @@ export declare function useBoundaryElement(): BoundaryElementContextValue
|
|
|
2653
2653
|
*/
|
|
2654
2654
|
export declare function useClickOutside(
|
|
2655
2655
|
listener: ClickOutsideListener,
|
|
2656
|
-
elementsArg
|
|
2656
|
+
elementsArg: () => (HTMLElement | HTMLElement[] | null)[],
|
|
2657
2657
|
boundaryElement?: HTMLElement | null,
|
|
2658
|
-
):
|
|
2658
|
+
): void
|
|
2659
|
+
|
|
2660
|
+
/**
|
|
2661
|
+
* @public
|
|
2662
|
+
* @deprecated
|
|
2663
|
+
* Instead of:
|
|
2664
|
+
* ```tsx
|
|
2665
|
+
* const buttonRef = useRef(null)
|
|
2666
|
+
* const setElement = useClickOutside(() => {}, [buttonRef.current])
|
|
2667
|
+
* return (
|
|
2668
|
+
* <>
|
|
2669
|
+
* <button ref={buttonRef} />
|
|
2670
|
+
* {open && <div ref={setElement} />}
|
|
2671
|
+
* </>
|
|
2672
|
+
* )
|
|
2673
|
+
* ```
|
|
2674
|
+
* Use:
|
|
2675
|
+
* ```tsx
|
|
2676
|
+
* const buttonRef = useRef()
|
|
2677
|
+
* const [element, setElement] = useState(null)
|
|
2678
|
+
* useClickOutside(() => {}, () => [buttonRef.current, element])
|
|
2679
|
+
* return (
|
|
2680
|
+
* <>
|
|
2681
|
+
* <button ref={buttonRef} />
|
|
2682
|
+
* {open && <div ref={setElement} />}
|
|
2683
|
+
* </>
|
|
2684
|
+
* )
|
|
2685
|
+
* ```
|
|
2686
|
+
*/
|
|
2687
|
+
export declare function useClickOutside(
|
|
2688
|
+
listener: ClickOutsideListener,
|
|
2689
|
+
elementsArg: (HTMLElement | HTMLElement[] | null)[],
|
|
2690
|
+
boundaryElement?: HTMLElement | null,
|
|
2691
|
+
): UseClickOutsideSetElement
|
|
2692
|
+
|
|
2693
|
+
/**
|
|
2694
|
+
* @public
|
|
2695
|
+
* @deprecated -- do not use
|
|
2696
|
+
*/
|
|
2697
|
+
export declare type UseClickOutsideSetElement = (el: HTMLElement | null) => void
|
|
2659
2698
|
|
|
2660
2699
|
/**
|
|
2661
2700
|
* @beta
|
package/dist/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { buildTheme, createColorTheme as createColorTheme$1, hexToRgb as hexToRgb$1, hslToRgb as hslToRgb$1, multiply as multiply$1, parseColor as parseColor$1, rgbToHex as rgbToHex$1, rgbToHsl as rgbToHsl$1, rgba as rgba$1, screen as screen$1, getTheme_v2, getScopedTheme } from "@sanity/ui/theme";
|
|
2
2
|
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
3
|
-
import { useMemo, useState,
|
|
3
|
+
import { useMemo, useState, useEffect, useRef, useImperativeHandle, useDebugValue, useSyncExternalStore, createContext, useContext, forwardRef, useId, useCallback, Children, isValidElement, cloneElement, Component, memo, useLayoutEffect, useReducer, Fragment as Fragment$1, startTransition } from "react";
|
|
4
4
|
import ReactIs, { isValidElementType } from "react-is";
|
|
5
5
|
import { ThemeProvider as ThemeProvider$1, useTheme as useTheme$1, css, styled, keyframes } from "styled-components";
|
|
6
6
|
import { SpinnerIcon, CheckmarkIcon, RemoveIcon, ChevronDownIcon, CloseIcon, ChevronRightIcon, ToggleArrowRightIcon } from "@sanity/icons";
|
|
@@ -131,45 +131,25 @@ function useArrayProp(val, defaultVal) {
|
|
|
131
131
|
[__perf_hash__]
|
|
132
132
|
);
|
|
133
133
|
}
|
|
134
|
-
function _getElements(element, elementsArg) {
|
|
135
|
-
const ret = [element];
|
|
136
|
-
for (const el of elementsArg)
|
|
137
|
-
Array.isArray(el) ? ret.push(...el) : ret.push(el);
|
|
138
|
-
return ret.filter(Boolean);
|
|
139
|
-
}
|
|
140
134
|
function useClickOutside(listener, elementsArg = EMPTY_ARRAY, boundaryElement) {
|
|
141
|
-
const [
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (prevElements.length !== nextElements.length) {
|
|
145
|
-
setElements(nextElements), elementsRef.current = nextElements;
|
|
135
|
+
const [legacyElement, setLegacyElement] = useState(null), onEvent = useEffectEvent((evt) => {
|
|
136
|
+
const target = evt.target;
|
|
137
|
+
if (!(target instanceof Node) || boundaryElement && !boundaryElement.contains(target))
|
|
146
138
|
return;
|
|
147
|
-
|
|
148
|
-
for (const el of
|
|
149
|
-
if (
|
|
150
|
-
setElements(nextElements), elementsRef.current = nextElements;
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
for (const el of nextElements)
|
|
154
|
-
if (!prevElements.includes(el)) {
|
|
155
|
-
setElements(nextElements), elementsRef.current = nextElements;
|
|
139
|
+
const elements = (Array.isArray(elementsArg) ? [legacyElement, ...elementsArg] : elementsArg()).flat();
|
|
140
|
+
for (const el of elements)
|
|
141
|
+
if (el && (target === el || el.contains(target)))
|
|
156
142
|
return;
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (target === el || el.contains(target))
|
|
165
|
-
return;
|
|
166
|
-
listener(evt);
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
return window.addEventListener("mousedown", handleWindowMouseDown), () => {
|
|
170
|
-
window.removeEventListener("mousedown", handleWindowMouseDown);
|
|
143
|
+
listener(evt);
|
|
144
|
+
}), hasListener = !!listener;
|
|
145
|
+
if (useEffect(() => {
|
|
146
|
+
if (!hasListener) return;
|
|
147
|
+
const handleEvent = (evt) => onEvent(evt);
|
|
148
|
+
return document.addEventListener("mousedown", handleEvent), document.addEventListener("touchstart", handleEvent), () => {
|
|
149
|
+
document.removeEventListener("mousedown", handleEvent), document.removeEventListener("touchstart", handleEvent);
|
|
171
150
|
};
|
|
172
|
-
}, [
|
|
151
|
+
}, [hasListener, onEvent]), Array.isArray(elementsArg))
|
|
152
|
+
return setLegacyElement;
|
|
173
153
|
}
|
|
174
154
|
function useCustomValidity(ref, customValidity) {
|
|
175
155
|
useEffect(() => {
|
|
@@ -520,21 +500,13 @@ function useGlobalKeyDown(onKeyDown) {
|
|
|
520
500
|
function useMatchMedia(mediaQueryString, getServerSnapshot2) {
|
|
521
501
|
const { subscribe: subscribe2, getSnapshot } = useMemo(() => {
|
|
522
502
|
let MEDIA_QUERY_CACHE;
|
|
523
|
-
|
|
524
|
-
return MEDIA_QUERY_CACHE === void 0 && (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE;
|
|
525
|
-
}
|
|
503
|
+
const getMatchMedia = () => (MEDIA_QUERY_CACHE || (MEDIA_QUERY_CACHE = window.matchMedia(mediaQueryString)), MEDIA_QUERY_CACHE);
|
|
526
504
|
return {
|
|
527
505
|
subscribe: (onStoreChange) => {
|
|
528
506
|
const matchMedia = getMatchMedia();
|
|
529
507
|
return matchMedia.addEventListener("change", onStoreChange), () => matchMedia.removeEventListener("change", onStoreChange);
|
|
530
508
|
},
|
|
531
|
-
getSnapshot: () =>
|
|
532
|
-
try {
|
|
533
|
-
return getMatchMedia().matches;
|
|
534
|
-
} catch (err) {
|
|
535
|
-
throw new Error(`Failed to match media query: ${mediaQueryString}`, { cause: err });
|
|
536
|
-
}
|
|
537
|
-
}
|
|
509
|
+
getSnapshot: () => getMatchMedia().matches
|
|
538
510
|
};
|
|
539
511
|
}, [mediaQueryString]);
|
|
540
512
|
return useDebugValue(mediaQueryString), useSyncExternalStore(subscribe2, getSnapshot, getServerSnapshot2);
|