@sanity/ui 2.8.0 → 2.8.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.mjs 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, useEffect, useRef, useImperativeHandle, useDebugValue, useSyncExternalStore, createContext, useContext, forwardRef, useId, useCallback, Children, isValidElement, cloneElement, Component, memo, useLayoutEffect, useReducer, Fragment as Fragment$1, startTransition } from "react";
3
+ import { useMemo, useState, useRef, useEffect, 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,8 +131,48 @@ 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
+ }
134
140
  function useClickOutside(listener, elementsArg = EMPTY_ARRAY, boundaryElement) {
135
- const [legacyElement, setLegacyElement] = useState(null), onEvent = useEffectEvent((evt) => {
141
+ const [element, setElement] = useState(null), [elements, setElements] = useState(() => _getElements(element, elementsArg)), elementsRef = useRef(elements);
142
+ return useEffect(() => {
143
+ const prevElements = elementsRef.current, nextElements = _getElements(element, elementsArg);
144
+ if (prevElements.length !== nextElements.length) {
145
+ setElements(nextElements), elementsRef.current = nextElements;
146
+ return;
147
+ }
148
+ for (const el of prevElements)
149
+ if (!nextElements.includes(el)) {
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;
156
+ return;
157
+ }
158
+ }, [element, elementsArg]), useEffect(() => {
159
+ if (!listener) return;
160
+ const handleWindowMouseDown = (evt) => {
161
+ const target = evt.target;
162
+ if (target instanceof Node && !(boundaryElement && !boundaryElement.contains(target))) {
163
+ for (const el of elements)
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);
171
+ };
172
+ }, [boundaryElement, listener, elements]), setElement;
173
+ }
174
+ function useClickOutsideEvent(listener, elementsArg = () => EMPTY_ARRAY, boundaryElement) {
175
+ const onEvent = useEffectEvent((evt) => {
136
176
  if (!listener)
137
177
  return;
138
178
  const target = evt.target;
@@ -141,20 +181,19 @@ function useClickOutside(listener, elementsArg = EMPTY_ARRAY, boundaryElement) {
141
181
  const resolvedBoundaryElement = typeof boundaryElement == "function" ? boundaryElement() : boundaryElement;
142
182
  if (resolvedBoundaryElement && !resolvedBoundaryElement.contains(target))
143
183
  return;
144
- const elements = (Array.isArray(elementsArg) ? [legacyElement, ...elementsArg] : elementsArg()).flat();
184
+ const elements = (Array.isArray(elementsArg) ? elementsArg : elementsArg()).flat();
145
185
  for (const el of elements)
146
186
  if (el && (target === el || el.contains(target)))
147
187
  return;
148
188
  listener(evt);
149
189
  }), hasListener = !!listener;
150
- if (useEffect(() => {
190
+ useEffect(() => {
151
191
  if (!hasListener) return;
152
192
  const handleEvent = (evt) => onEvent(evt);
153
193
  return document.addEventListener("mousedown", handleEvent), () => {
154
194
  document.removeEventListener("mousedown", handleEvent);
155
195
  };
156
- }, [hasListener, onEvent]), Array.isArray(elementsArg))
157
- return setLegacyElement;
196
+ }, [hasListener, onEvent]);
158
197
  }
159
198
  function useCustomValidity(ref, customValidity) {
160
199
  useEffect(() => {
@@ -6841,6 +6880,7 @@ export {
6841
6880
  useArrayProp,
6842
6881
  useBoundaryElement,
6843
6882
  useClickOutside,
6883
+ useClickOutsideEvent,
6844
6884
  useCustomValidity,
6845
6885
  useDialog,
6846
6886
  useElementRect,