@sanity/ui 2.7.1-canary.1 → 2.8.0

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.mjs CHANGED
@@ -133,8 +133,13 @@ function useArrayProp(val, defaultVal) {
133
133
  }
134
134
  function useClickOutside(listener, elementsArg = EMPTY_ARRAY, boundaryElement) {
135
135
  const [legacyElement, setLegacyElement] = useState(null), onEvent = useEffectEvent((evt) => {
136
+ if (!listener)
137
+ return;
136
138
  const target = evt.target;
137
- if (!(target instanceof Node) || boundaryElement && !boundaryElement.contains(target))
139
+ if (!(target instanceof Node))
140
+ return;
141
+ const resolvedBoundaryElement = typeof boundaryElement == "function" ? boundaryElement() : boundaryElement;
142
+ if (resolvedBoundaryElement && !resolvedBoundaryElement.contains(target))
138
143
  return;
139
144
  const elements = (Array.isArray(elementsArg) ? [legacyElement, ...elementsArg] : elementsArg()).flat();
140
145
  for (const el of elements)
@@ -145,8 +150,8 @@ function useClickOutside(listener, elementsArg = EMPTY_ARRAY, boundaryElement) {
145
150
  if (useEffect(() => {
146
151
  if (!hasListener) return;
147
152
  const handleEvent = (evt) => onEvent(evt);
148
- return document.addEventListener("mousedown", handleEvent), document.addEventListener("touchstart", handleEvent), () => {
149
- document.removeEventListener("mousedown", handleEvent), document.removeEventListener("touchstart", handleEvent);
153
+ return document.addEventListener("mousedown", handleEvent), () => {
154
+ document.removeEventListener("mousedown", handleEvent);
150
155
  };
151
156
  }, [hasListener, onEvent]), Array.isArray(elementsArg))
152
157
  return setLegacyElement;
@@ -5104,14 +5109,11 @@ const Root$7 = styled(Layer)(responsivePaddingStyle, dialogStyle, responsiveDial
5104
5109
  [boundaryElement, isTopLayer, onClose, portalElement]
5105
5110
  )
5106
5111
  ), useClickOutside(
5107
- useCallback(
5108
- (event) => {
5109
- if (!isTopLayer || !onClickOutside) return;
5110
- const target = event.target;
5111
- target && !isTargetWithinScope(boundaryElement, portalElement, target) || onClickOutside();
5112
- },
5113
- [boundaryElement, isTopLayer, onClickOutside, portalElement]
5114
- ),
5112
+ (event) => {
5113
+ if (!isTopLayer || !onClickOutside) return;
5114
+ const target = event.target;
5115
+ target && !isTargetWithinScope(boundaryElement, portalElement, target) || onClickOutside();
5116
+ },
5115
5117
  [rootElement]
5116
5118
  );
5117
5119
  const setRef = useCallback((el) => {
@@ -5448,13 +5450,7 @@ const Root$5 = styled(Box)`
5448
5450
  );
5449
5451
  useEffect(() => {
5450
5452
  onItemSelect && onItemSelect(activeIndex);
5451
- }, [activeIndex, onItemSelect]), useClickOutside(
5452
- useCallback(
5453
- (event) => isTopLayer && onClickOutside && onClickOutside(event),
5454
- [isTopLayer, onClickOutside]
5455
- ),
5456
- [rootElement]
5457
- ), useGlobalKeyDown(
5453
+ }, [activeIndex, onItemSelect]), useClickOutside((event) => isTopLayer && onClickOutside && onClickOutside(event), [rootElement]), useGlobalKeyDown(
5458
5454
  useCallback(
5459
5455
  (event) => {
5460
5456
  isTopLayer && event.key === "Escape" && (event.stopPropagation(), onEscape && onEscape());