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.

feat(mobile): improve soup navigation touch highlight (#3782)

Peter Chinmana13b547faf91
diff --git a/js/app/packages/app/component/next-soup/soup-view/soup-entity-context-menu.tsx b/js/app/packages/app/component/next-soup/soup-view/soup-entity-context-menu.tsx
index 7d4f008e39..6d6c69d3ba 100644
--- a/js/app/packages/app/component/next-soup/soup-view/soup-entity-context-menu.tsx
+++ b/js/app/packages/app/component/next-soup/soup-view/soup-entity-context-menu.tsx
@@ -1,5 +1,5 @@
import { ContextMenuContent } from '@core/component/ContextMenu';
-import { longPressHighlight } from '@core/directive/longPressHighlight';
+import { touchHandler } from '@core/directive/touchHandler';
import { isMobile } from '@core/mobile/isMobile';
import type { EntityData } from '@entity';
import { ContextMenu } from '@kobalte/core/context-menu';
@@ -25,12 +25,11 @@ export const SoupEntityContextMenu: FlowComponent<
<div
class="size-full"
data-soup-entity
- ref={(el) =>
- longPressHighlight(el, () => ({
- className: 'touch-highlight',
+ ref={(el) => {
+ touchHandler(el, () => ({
onLongPress: () => drawerManager?.open(props.entity, soup),
- }))
- }
+ }));
+ }}
>
{props.children}
</div>
diff --git a/js/app/packages/app/component/next-soup/soup-view/soup-navigation-touch-highlight.ts b/js/app/packages/app/component/next-soup/soup-view/soup-navigation-touch-highlight.ts
index cc24de898b..114a340942 100644
--- a/js/app/packages/app/component/next-soup/soup-view/soup-navigation-touch-highlight.ts
+++ b/js/app/packages/app/component/next-soup/soup-view/soup-navigation-touch-highlight.ts
@@ -1,32 +1,199 @@
import { isTouchDevice } from '@core/mobile/isTouchDevice';
+import { onCleanup } from 'solid-js';
-const NAVIGATION_TOUCH_HIGHLIGHT_CLASS = 'navigation-touch-highlight';
-const NAVIGATION_TOUCH_HIGHLIGHT_TIMEOUT_MS = 500;
-const navigationTouchHighlightTimers = new WeakMap<HTMLElement, number>();
+const TOUCH_HIGHLIGHT_CLASS = 'touch-highlight';
+const TOUCH_HIGHLIGHT_DELAY_MS = 20;
+const SOUP_TOUCH_HIGHLIGHT_CANCEL_DISTANCE_PX = 5;
+const CLICK_GRACE_MS = 350;
+// Covers delayed split rendering after the open call completes synchronously.
+const NAVIGATION_HIGHLIGHT_MIN_MS = 500;
+
+type TouchHighlightState = {
+ startX: number;
+ startY: number;
+ activeElement: HTMLElement | undefined;
+ touchActive: boolean;
+ enterTimer: number | undefined;
+};
+
+const createTouchHighlightState = (): TouchHighlightState => ({
+ startX: 0,
+ startY: 0,
+ activeElement: undefined,
+ touchActive: false,
+ enterTimer: undefined,
+});
+
+const pendingTouchClearTimers = new WeakMap<HTMLElement, number>();
+const touchListenerOptions = { passive: true, capture: true };
+
+const clearScheduledTimer = (timer: number | undefined) => {
+ if (timer !== undefined) {
+ window.clearTimeout(timer);
+ }
+};
+
+const clearPendingTouchClear = (element: HTMLElement) => {
+ const timer = pendingTouchClearTimers.get(element);
+ if (timer !== undefined) {
+ window.clearTimeout(timer);
+ pendingTouchClearTimers.delete(element);
+ }
+};
+
+const scheduleTouchClear = (element: HTMLElement) => {
+ clearPendingTouchClear(element);
+
+ const timer = window.setTimeout(() => {
+ pendingTouchClearTimers.delete(element);
+ element.classList.remove(TOUCH_HIGHLIGHT_CLASS);
+ }, CLICK_GRACE_MS);
+
+ pendingTouchClearTimers.set(element, timer);
+};
+
+const isTouchMovementCanceled = (
+ touch: Touch,
+ state: TouchHighlightState
+): boolean => {
+ const dx = touch.clientX - state.startX;
+ const dy = touch.clientY - state.startY;
+ return Math.hypot(dx, dy) > SOUP_TOUCH_HIGHLIGHT_CANCEL_DISTANCE_PX;
+};
+
+const soupEntityFromEvent = (event: TouchEvent) => {
+ const target = event.target;
+ if (!(target instanceof Element)) return undefined;
+
+ const trigger = target.closest('[data-soup-entity]');
+ if (!(trigger instanceof HTMLElement)) return undefined;
+
+ return trigger;
+};
+
+const cancelTouchHighlight = (state: TouchHighlightState) => {
+ const element = state.activeElement;
+ state.touchActive = false;
+ state.activeElement = undefined;
+ clearScheduledTimer(state.enterTimer);
+ state.enterTimer = undefined;
+
+ if (!element) return;
+
+ clearPendingTouchClear(element);
+ element.classList.remove(TOUCH_HIGHLIGHT_CLASS);
+};
+
+export function soupNavigationTouchHighlight(container: HTMLElement) {
+ if (!isTouchDevice()) return;
+
+ const state = createTouchHighlightState();
+
+ const handleTouchStart = (event: TouchEvent) => {
+ if (event.touches.length !== 1) {
+ cancelTouchHighlight(state);
+ return;
+ }
+
+ const element = soupEntityFromEvent(event);
+ if (!element) {
+ cancelTouchHighlight(state);
+ return;
+ }
+
+ const touch = event.touches[0];
+ state.startX = touch.clientX;
+ state.startY = touch.clientY;
+ state.activeElement = element;
+ state.touchActive = true;
+
+ clearPendingTouchClear(element);
+ clearScheduledTimer(state.enterTimer);
+ state.enterTimer = window.setTimeout(() => {
+ state.enterTimer = undefined;
+ element.classList.add(TOUCH_HIGHLIGHT_CLASS);
+ }, TOUCH_HIGHLIGHT_DELAY_MS);
+ };
+
+ const handleTouchMove = (event: TouchEvent) => {
+ if (!state.touchActive) return;
+
+ const touch = event.touches[0];
+ if (!touch) return;
+
+ if (isTouchMovementCanceled(touch, state)) {
+ cancelTouchHighlight(state);
+ }
+ };
+
+ const handleTouchEnd = (event: TouchEvent) => {
+ const touch = event.changedTouches[0];
+ if (!touch) return;
+
+ state.touchActive = false;
+ const element = state.activeElement;
+ state.activeElement = undefined;
+ clearScheduledTimer(state.enterTimer);
+ state.enterTimer = undefined;
+
+ if (element) scheduleTouchClear(element);
+ };
+
+ const handleTouchCancel = () => {
+ cancelTouchHighlight(state);
+ };
+
+ container.addEventListener(
+ 'touchstart',
+ handleTouchStart,
+ touchListenerOptions
+ );
+ container.addEventListener(
+ 'touchmove',
+ handleTouchMove,
+ touchListenerOptions
+ );
+ container.addEventListener('touchend', handleTouchEnd, touchListenerOptions);
+ container.addEventListener(
+ 'touchcancel',
+ handleTouchCancel,
+ touchListenerOptions
+ );
+
+ onCleanup(() => {
+ cancelTouchHighlight(state);
+ container.removeEventListener('touchstart', handleTouchStart, true);
+ container.removeEventListener('touchmove', handleTouchMove, true);
+ container.removeEventListener('touchend', handleTouchEnd, true);
+ container.removeEventListener('touchcancel', handleTouchCancel, true);
+ });
+}
export function persistSoupNavigationTouchHighlight(
event: MouseEvent | PointerEvent
-) {
- if (!isTouchDevice()) return false;
+): (() => void) | undefined {
+ if (!isTouchDevice()) return undefined;
const target = event.currentTarget ?? event.target;
- if (!(target instanceof Element)) return false;
+ if (!(target instanceof Element)) return undefined;
const trigger = target.closest('[data-soup-entity]');
- if (!(trigger instanceof HTMLElement)) return false;
+ if (!(trigger instanceof HTMLElement)) return undefined;
- trigger.classList.add(NAVIGATION_TOUCH_HIGHLIGHT_CLASS);
+ clearPendingTouchClear(trigger);
+ trigger.classList.add(TOUCH_HIGHLIGHT_CLASS);
- const existingTimer = navigationTouchHighlightTimers.get(trigger);
- if (existingTimer !== undefined) {
- window.clearTimeout(existingTimer);
- }
+ const navigationStartedAt = performance.now();
+ let navigationClearTimer: number | undefined;
- const timer = window.setTimeout(() => {
- trigger.classList.remove(NAVIGATION_TOUCH_HIGHLIGHT_CLASS);
- navigationTouchHighlightTimers.delete(trigger);
- }, NAVIGATION_TOUCH_HIGHLIGHT_TIMEOUT_MS);
+ return () => {
+ const elapsed = performance.now() - navigationStartedAt;
+ const remaining = Math.max(0, NAVIGATION_HIGHLIGHT_MIN_MS - elapsed);
- navigationTouchHighlightTimers.set(trigger, timer);
- return true;
+ clearScheduledTimer(navigationClearTimer);
+ navigationClearTimer = window.setTimeout(() => {
+ navigationClearTimer = undefined;
+ trigger.classList.remove(TOUCH_HIGHLIGHT_CLASS);
+ }, remaining);
+ };
}
diff --git a/js/app/packages/app/component/next-soup/soup-view/soup-view.tsx b/js/app/packages/app/component/next-soup/soup-view/soup-view.tsx
index e498223e74..fb56a6f2ad 100644
--- a/js/app/packages/app/component/next-soup/soup-view/soup-view.tsx
+++ b/js/app/packages/app/component/next-soup/soup-view/soup-view.tsx
@@ -24,7 +24,10 @@ import { useFilterRefinements } from '@app/component/next-soup/soup-view/filters
import { MaybeSoupEntityActionDrawerManager } from '@app/component/next-soup/soup-view/SoupEntityActionDrawerManager';
import type { SystemSortOption } from '@app/component/next-soup/soup-view/sort-options';
import { SoupEntityContextMenu } from '@app/component/next-soup/soup-view/soup-entity-context-menu';
-import { persistSoupNavigationTouchHighlight } from '@app/component/next-soup/soup-view/soup-navigation-touch-highlight';
+import {
+ persistSoupNavigationTouchHighlight,
+ soupNavigationTouchHighlight,
+} from '@app/component/next-soup/soup-view/soup-navigation-touch-highlight';
import { activeSoupViewCounts } from '@app/component/next-soup/soup-view/soup-view-cache-key';
import {
SoupViewContextProvider,
@@ -752,13 +755,17 @@ export const SoupViewList = (props: SoupViewListProps) => {
return;
}
- persistSoupNavigationTouchHighlight(event);
+ const finishTouchHighlight = persistSoupNavigationTouchHighlight(event);
- await openEntityInSplitFromUnifiedList(entity, {
- openInNewSplit: event.shiftKey,
- location,
- splitHandle: panel.handle,
- });
+ try {
+ await openEntityInSplitFromUnifiedList(entity, {
+ openInNewSplit: event.shiftKey,
+ location,
+ splitHandle: panel.handle,
+ });
+ } finally {
+ finishTouchHighlight?.();
+ }
};
let lastClickedEntityId = -1;
@@ -1096,7 +1103,10 @@ export const SoupViewList = (props: SoupViewListProps) => {
>
<SoupList
cache={listStateCache.get(cacheKey)?.virtualCache}
- ref={setLocalEntityListRef}
+ ref={(el) => {
+ setLocalEntityListRef(el);
+ soupNavigationTouchHighlight(el);
+ }}
virtualizerClass={cn(
previewVisible() && 'pt-1' /* scuffed */,
'scrollbar-hidden'
@@ -1375,7 +1385,7 @@ const DEFAULT_OVERSCAN = 5;
const FLOATING_BUTTON_SCROLL_UP_THRESHOLD = 5;
interface SoupListProps {
- ref?: (el: HTMLElement) => void;
+ ref?: (el: HTMLDivElement) => void;
virtualizerRef?: (handle: VirtualizerHandle) => void;
class?: string;
virtualizerClass?: string;
diff --git a/js/app/packages/app/index.css b/js/app/packages/app/index.css
index e89043affd..3f98dfc652 100644
--- a/js/app/packages/app/index.css
+++ b/js/app/packages/app/index.css
@@ -465,11 +465,7 @@ html:not([data-touch-device="true"]) [data-message]:hover{
/* Immediate touch feedback for mobile soup entity rows */
[data-soup-entity].touch-highlight [data-swipe-content],
-[data-soup-entity].touch-highlight .soup-list-entity,
-[data-soup-entity].navigation-touch-highlight [data-swipe-content],
-[data-soup-entity].navigation-touch-highlight .soup-list-entity,
-.soup-list-entity.navigation-touch-highlight,
-.soup-list-entity.navigation-touch-highlight [data-swipe-content] {
+[data-soup-entity].touch-highlight .soup-list-entity {
background-color: var(--color-hover);
transition: none;
}
diff --git a/js/app/packages/channel/Message/ChannelMessage.tsx b/js/app/packages/channel/Message/ChannelMessage.tsx
index b5ff4f472b..5ca19c1530 100644
--- a/js/app/packages/channel/Message/ChannelMessage.tsx
+++ b/js/app/packages/channel/Message/ChannelMessage.tsx
@@ -1,5 +1,5 @@
import { useMessageActionDrawer } from '@channel/Mobile/message-action-drawer-context';
-import { longPressHighlight } from '@core/directive/longPressHighlight';
+import { touchHandler } from '@core/directive/touchHandler';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import TrashIcon from '@icon/square-trash.svg';
import { cn } from '@ui';
@@ -189,8 +189,8 @@ export function ChannelMessage(props: ChannelMessageProps) {
selected={props.selectionState?.isSelected}
onClick={props.onClick}
ref={(el) =>
- longPressHighlight(el, () => ({
- className: 'channel-message-long-press-highlight',
+ touchHandler(el, () => ({
+ touchClassName: 'channel-message-long-press-highlight',
onLongPress: () => drawerManager?.open(props.message, props.actions),
}))
}
diff --git a/js/app/packages/core/directive/longPressHighlight.ts b/js/app/packages/core/directive/longPressHighlight.ts
deleted file mode 100644
index 0005839470..0000000000
--- a/js/app/packages/core/directive/longPressHighlight.ts
+++ /dev/null
@@ -1,102 +0,0 @@
-import { type Accessor, onCleanup } from 'solid-js';
-import { touchHandler } from './touchHandler';
-
-const LONG_PRESS_ENTER_DELAY_MS = 100;
-const LONG_PRESS_EXIT_DELAY_MS = 50;
-
-interface LongPressHighlightOptions {
- onLongPress?: () => void;
- /** CSS class added while the touch highlight is active. */
- className: string;
-}
-
-declare module 'solid-js' {
- namespace JSX {
- interface Directives {
- longPressHighlight: LongPressHighlightOptions;
- }
- }
-}
-
-/**
- * Wraps touchHandler to manage a touch highlight CSS class lifecycle on an element.
- * Pair with a corresponding CSS animation for the configured className.
- */
-export function longPressHighlight(
- element: HTMLElement,
- options: Accessor<LongPressHighlightOptions>
-) {
- let enterTimer: number | undefined;
- let exitTimer: number | undefined;
- let activeClassName: string | undefined;
-
- const highlightClassName = () => options().className;
-
- const cancelExit = () => {
- if (exitTimer !== undefined) {
- clearTimeout(exitTimer);
- exitTimer = undefined;
- }
- };
-
- const startAnimation = () => {
- enterTimer = undefined;
- cancelExit();
-
- const className = highlightClassName();
- if (activeClassName && activeClassName !== className) {
- element.classList.remove(activeClassName);
- }
-
- activeClassName = className;
- element.classList.add(className);
- };
-
- const cancelEnter = () => {
- if (enterTimer !== undefined) {
- clearTimeout(enterTimer);
- enterTimer = undefined;
- }
- };
-
- const endAnimation = () => {
- cancelExit();
- if (activeClassName) {
- element.classList.remove(activeClassName);
- activeClassName = undefined;
- return;
- }
- element.classList.remove(highlightClassName());
- };
-
- const scheduleEndAnimation = (delay: number) => {
- cancelExit();
- exitTimer = window.setTimeout(endAnimation, delay);
- };
-
- touchHandler(element, () => ({
- onTouchStart: () => {
- enterTimer = window.setTimeout(startAnimation, LONG_PRESS_ENTER_DELAY_MS);
- },
- onLongPress: () => {
- options().onLongPress?.();
- },
- onCancel: () => {
- cancelEnter();
- endAnimation();
- },
- onTouchEnd: (_e, longpress) => {
- cancelEnter();
- if (!longpress) {
- endAnimation();
- } else {
- scheduleEndAnimation(LONG_PRESS_EXIT_DELAY_MS);
- }
- },
- }));
-
- onCleanup(() => {
- cancelEnter();
- endAnimation();
- });
-}
diff --git a/js/app/packages/core/directive/touchHandler.ts b/js/app/packages/core/directive/touchHandler.ts
index 81afcdb669..e61112dd3a 100644
--- a/js/app/packages/core/directive/touchHandler.ts
+++ b/js/app/packages/core/directive/touchHandler.ts
@@ -1,6 +1,10 @@
import { hapticImpact } from '@core/mobile/haptics';
+import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { type Accessor, createSignal, onCleanup } from 'solid-js';
+const TOUCH_CLASS_ENTER_DELAY_MS = 100;
+const TOUCH_CLASS_EXIT_DELAY_MS = 50;
+
interface TouchHandlerOptions {
onLongPress?: (e: TouchEvent) => void;
onTouchStart?: (e: TouchEvent) => void;
@@ -10,6 +14,10 @@ interface TouchHandlerOptions {
delay?: number;
moveThreshold?: number;
stopTouchStartPropagation?: boolean;
+ /** CSS class added while the touch highlight is active. */
+ touchClassName?: string;
+ touchClassEnterDelay?: number;
+ touchClassExitDelay?: number;
}
declare module 'solid-js' {
@@ -35,15 +43,68 @@ export function touchHandler(
element: HTMLElement,
options: Accessor<TouchHandlerOptions>
) {
- if (!options().onLongPress && !options().onShortTouch) {
+ if (!isTouchDevice()) {
return;
}
let timer: number;
+ let touchClassEnterTimer: number | undefined;
+ let touchClassExitTimer: number | undefined;
+ let activeTouchClassName: string | undefined;
let startPosition: { x: number; y: number } | undefined;
let longPressTriggered = false;
const [validShortTouch, setValidShortTouch] = createSignal(true);
+ const touchClassName = () => options().touchClassName;
+
+ function cancelTouchClassExit() {
+ if (touchClassExitTimer !== undefined) {
+ clearTimeout(touchClassExitTimer);
+ touchClassExitTimer = undefined;
+ }
+ }
+
+ function startTouchClass() {
+ touchClassEnterTimer = undefined;
+ cancelTouchClassExit();
+
+ const className = touchClassName();
+ if (!className) return;
+
+ if (activeTouchClassName && activeTouchClassName !== className) {
+ element.classList.remove(activeTouchClassName);
+ }
+
+ activeTouchClassName = className;
+ element.classList.add(className);
+ }
+
+ function cancelTouchClassEnter() {
+ if (touchClassEnterTimer !== undefined) {
+ clearTimeout(touchClassEnterTimer);
+ touchClassEnterTimer = undefined;
+ }
+ }
+
+ function endTouchClass() {
+ cancelTouchClassExit();
+ if (activeTouchClassName) {
+ element.classList.remove(activeTouchClassName);
+ activeTouchClassName = undefined;
+ return;
+ }
+
+ const className = touchClassName();
+ if (className) {
+ element.classList.remove(className);
+ }
+ }
+
+ function scheduleTouchClassEnd(delay: number) {
+ cancelTouchClassExit();
+ touchClassExitTimer = window.setTimeout(endTouchClass, delay);
+ }
+
function getDistance(x: number, y: number) {
if (!startPosition) return 0;
const deltaX = Math.abs(x - startPosition.x);
@@ -68,12 +129,16 @@ export function touchHandler(
function handleTouchCancel() {
clearState();
+ cancelTouchClassEnter();
+ endTouchClass();
options().onCancel?.();
}
function handleTouchStart(e: TouchEvent) {
if (e.touches.length > 1) {
clearState();
+ cancelTouchClassEnter();
+ endTouchClass();
setValidShortTouch(false);
return;
}
@@ -87,6 +152,12 @@ export function touchHandler(
}
options().onTouchStart?.(e);
+ if (touchClassName()) {
+ touchClassEnterTimer = window.setTimeout(
+ startTouchClass,
+ options().touchClassEnterDelay ?? TOUCH_CLASS_ENTER_DELAY_MS
+ );
+ }
timer = window.setTimeout(() => {
longPressTriggered = true;
setLongPressActivated(true);
@@ -108,7 +179,17 @@ export function touchHandler(
}
function handleTouchEnd(e: TouchEvent) {
- options().onTouchEnd?.(e, longPressTriggered);
+ const wasLongPressTriggered = longPressTriggered;
+
+ options().onTouchEnd?.(e, wasLongPressTriggered);
+ cancelTouchClassEnter();
+ if (!wasLongPressTriggered) {
+ endTouchClass();
+ } else {
+ scheduleTouchClassEnd(
+ options().touchClassExitDelay ?? TOUCH_CLASS_EXIT_DELAY_MS
+ );
+ }
const isAnchorElement = (e.target as Element)?.closest('a');
const isButtonElement = (e.target as Element)?.closest('button');
@@ -120,7 +201,7 @@ export function touchHandler(
const touchedSomethingSharp =
isAnchorElement || isButtonElement || isDocumentMention || isInternalLink;
- if (longPressTriggered) {
+ if (wasLongPressTriggered) {
e.stopPropagation();
e.preventDefault();
} else if (validShortTouch() && !touchedSomethingSharp) {
@@ -135,6 +216,8 @@ export function touchHandler(
element.addEventListener('touchcancel', handleTouchCancel, { passive: true });
onCleanup(() => {
+ cancelTouchClassEnter();
+ endTouchClass();
element.removeEventListener('touchstart', handleTouchStart);
element.removeEventListener('touchmove', handleTouchMove);
element.removeEventListener('touchend', handleTouchEnd);