Skip to content

🎥 Make videos programmatically with React

Better click outside of modal behavior

Jonny Burger1c03739722a1
diff --git a/packages/cli/src/editor/state/z-index.tsx b/packages/cli/src/editor/state/z-index.tsx
index 601f587235..afb0620a23 100644
--- a/packages/cli/src/editor/state/z-index.tsx
+++ b/packages/cli/src/editor/state/z-index.tsx
@@ -56,22 +56,41 @@ export const HigherZIndex: React.FC<{
}, [currentIndex, highestContext]);
useEffect(() => {
- const listener = (e: MouseEvent) => {
- const outsideClick = !containerRef.current?.contains(e.target as Node);
- if (
- outsideClick &&
- highestContext.highestIndex === currentIndex &&
- !getClickLock() &&
- // Don't trigger if that click removed that node
- document.contains(e.target as Node)
- ) {
- e.stopPropagation();
- onOutsideClick();
+ let onUp: ((upEvent: MouseEvent) => void) | null = null;
+
+ const listener = (downEvent: MouseEvent) => {
+ const outsideClick = !containerRef.current?.contains(
+ downEvent.target as Node
+ );
+ if (!outsideClick) {
+ return;
}
+
+ onUp = (upEvent: MouseEvent) => {
+ if (
+ outsideClick &&
+ highestContext.highestIndex === currentIndex &&
+ !getClickLock() &&
+ // Don't trigger if that click removed that node
+ document.contains(upEvent.target as Node)
+ ) {
+ upEvent.stopPropagation();
+ onOutsideClick();
+ }
+ };
+
+ window.addEventListener('pointerup', onUp, {once: true});
};
- window.addEventListener('click', listener);
- return () => window.removeEventListener('click', listener);
+ window.addEventListener('pointerdown', listener);
+ return () => {
+ if (onUp) {
+ // @ts-expect-error
+ window.removeEventListener('pointerup', onUp, {once: true});
+ }
+
+ return window.removeEventListener('pointerdown', listener);
+ };
}, [currentIndex, highestContext.highestIndex, onOutsideClick]);
const value = useMemo((): ZIndex => {