Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(ui): close dropdowns on swipe, not just tap (#4481)

Jacob Beckermana0c6178f7fea
diff --git a/js/app/packages/ui/components/Dropdown.tsx b/js/app/packages/ui/components/Dropdown.tsx
index 0b41aba40a..f0250cd710 100644
--- a/js/app/packages/ui/components/Dropdown.tsx
+++ b/js/app/packages/ui/components/Dropdown.tsx
@@ -1,3 +1,4 @@
+import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { DropdownMenu as KobalteDropdownMenu } from '@kobalte/core/dropdown-menu';
import CheckIcon from '@phosphor/check.svg';
import { type ComponentProps, onCleanup, splitProps } from 'solid-js';
@@ -88,6 +89,77 @@ function installKeyboardNavigation(el: HTMLElement) {
onCleanup(cleanup);
}
+// Kobalte's dismissable layer deliberately defers "outside interaction"
+// dismissal on touch devices until a `click` event fires, so that a
+// scroll/swipe passing over the trigger isn't mistaken for a tap (see
+// createInteractOutside in @kobalte/core). A tap outside naturally produces
+// that click, so it still closes the menu. But a swipe (e.g. scrolling a
+// list behind the dropdown, or a swipe gesture that passes near it) never
+// produces a click, so it's left open — anywhere a tap dismisses the menu,
+// a swipe should too.
+//
+// We detect a swipe as touch movement past a small threshold that starts
+// outside the menu content, then dispatch a synthetic Escape keydown — the
+// same signal Kobalte's DismissableLayer already uses to close the
+// top-most open layer — rather than synthesizing a `click`, which could
+// wrongly activate whatever element the swipe happened to pass over.
+const SWIPE_DISMISS_THRESHOLD_PX = 10;
+
+function installSwipeToDismiss(contentEl: HTMLElement) {
+ if (!isTouchDevice()) return;
+
+ let startX = 0;
+ let startY = 0;
+ let tracking = false;
+
+ function onTouchStart(e: TouchEvent) {
+ const touch = e.touches[0];
+ const target = e.target as Node | null;
+ if (!touch || !target || contentEl.contains(target)) {
+ tracking = false;
+ return;
+ }
+ startX = touch.clientX;
+ startY = touch.clientY;
+ tracking = true;
+ }
+
+ function onTouchMove(e: TouchEvent) {
+ if (!tracking) return;
+ const touch = e.touches[0];
+ if (!touch) return;
+
+ const dx = touch.clientX - startX;
+ const dy = touch.clientY - startY;
+ if (Math.hypot(dx, dy) < SWIPE_DISMISS_THRESHOLD_PX) return;
+
+ tracking = false;
+ document.dispatchEvent(
+ new KeyboardEvent('keydown', {
+ key: 'Escape',
+ bubbles: true,
+ cancelable: true,
+ })
+ );
+ }
+
+ function stopTracking() {
+ tracking = false;
+ }
+
+ document.addEventListener('touchstart', onTouchStart, { passive: true });
+ document.addEventListener('touchmove', onTouchMove, { passive: true });
+ document.addEventListener('touchend', stopTracking, { passive: true });
+ document.addEventListener('touchcancel', stopTracking, { passive: true });
+
+ onCleanup(() => {
+ document.removeEventListener('touchstart', onTouchStart);
+ document.removeEventListener('touchmove', onTouchMove);
+ document.removeEventListener('touchend', stopTracking);
+ document.removeEventListener('touchcancel', stopTracking);
+ });
+}
+
function callRef<T>(ref: ((el: T) => void) | undefined, el: T) {
ref?.(el);
}
@@ -104,6 +176,7 @@ function DropdownContent(props: DropdownContentProps) {
]);
const setContentRef = (el: HTMLElement) => {
installKeyboardNavigation(el);
+ installSwipeToDismiss(el);
callRef(local.ref, el);
};
@@ -145,6 +218,7 @@ function DropdownSubContent(props: DropdownSubContentProps) {
]);
const setContentRef = (el: HTMLElement) => {
installKeyboardNavigation(el);
+ installSwipeToDismiss(el);
callRef(local.ref, el);
};