diff --git a/src/app/features/room/message/Reactions.test.tsx b/src/app/features/room/message/Reactions.test.tsx
index 3cef02e69..7d6b7bd7f 100644
--- a/src/app/features/room/message/Reactions.test.tsx
+++ b/src/app/features/room/message/Reactions.test.tsx
@@ -1,5 +1,5 @@
-import type { TouchEventHandler } from 'react';
-import { cleanup, fireEvent, render } from '@testing-library/react';
+import type { ReactNode, TouchEventHandler } from 'react';
+import { act, cleanup, fireEvent, render } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { MatrixEvent, Relations, Room } from '$types/matrix-sdk';
@@ -22,7 +22,18 @@ vi.mock('$components/emoji-board', () => ({
}));
vi.mock('$features/room/reaction-viewer', () => ({
- ReactionViewer: () => null,
+ ReactionViewer: ({ initialKey }: { initialKey?: string }) => (
+
+ ),
+}));
+
+vi.mock('focus-trap-react', () => ({
+ default: ({ children }: { children: ReactNode }) => children,
}));
import { Reactions } from './Reactions';
@@ -64,6 +75,10 @@ function renderReactions(onTouchStart: TouchEventHandler) {
}
describe('Reactions touch handling', () => {
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
it('does not let touchstart on a reaction chip reach the parent Message handler', () => {
const onParentTouchStart = vi.fn<() => void>();
const { container } = renderReactions(onParentTouchStart);
@@ -85,4 +100,18 @@ describe('Reactions touch handling', () => {
expect(onParentTouchStart).toHaveBeenCalledTimes(1);
});
+
+ it('opens the selected reaction viewer after a long press', () => {
+ vi.useFakeTimers();
+ const onParentTouchStart = vi.fn<() => void>();
+ const { container, getByTestId } = renderReactions(onParentTouchStart);
+
+ const chip = container.querySelector('[data-reaction-key]');
+ if (!chip) throw new Error('reaction chip not rendered');
+ fireEvent.touchStart(chip, { touches: [{ clientX: 20, clientY: 30 }] });
+ act(() => vi.advanceTimersByTime(500));
+
+ expect(getByTestId('reaction-viewer')).toHaveAttribute('data-initial-key', '👍');
+ expect(onParentTouchStart).not.toHaveBeenCalled();
+ });
});
diff --git a/src/app/features/room/message/Reactions.tsx b/src/app/features/room/message/Reactions.tsx
index 5e40e97e6..43bfcce49 100644
--- a/src/app/features/room/message/Reactions.tsx
+++ b/src/app/features/room/message/Reactions.tsx
@@ -1,5 +1,5 @@
import type { MouseEventHandler } from 'react';
-import { useCallback, useState } from 'react';
+import { useCallback, useRef, useState } from 'react';
import {
Box,
Modal,
@@ -27,6 +27,7 @@ import { useRelations } from '$hooks/useRelations';
import { stopPropagation } from '$utils/keyboard';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { useDismissOnBack } from '$utils/androidBack';
+import { useMobileLongPress } from '$hooks/useMobileLongPress';
import { ReactionViewer } from '$features/room/reaction-viewer';
import * as css from './styles.css';
@@ -58,6 +59,10 @@ export const Reactions = as<'div', ReactionsProps>(
const useAuthentication = useMediaAuthentication();
const [viewer, setViewer] = useState(false);
const [emojiBoardAnchor, setEmojiBoardAnchor] = useState();
+ const pressedReactionKey = useRef();
+ const reactionLongPress = useMobileLongPress(() => {
+ if (pressedReactionKey.current) setViewer(pressedReactionKey.current);
+ });
// Android back closes the mobile emoji board instead of navigating away.
useDismissOnBack(() => setEmojiBoardAnchor(undefined), emojiBoardAnchor !== undefined);
const myUserId = mx.getUserId();
@@ -116,9 +121,26 @@ export const Reactions = as<'div', ReactionsProps>(
mx={mx}
reaction={key}
count={events.size}
- onClick={canToggle ? () => onReactionToggle(mEventId, key) : undefined}
+ onClick={
+ canToggle
+ ? () => {
+ if (reactionLongPress.firedRef.current) {
+ reactionLongPress.firedRef.current = false;
+ return;
+ }
+ onReactionToggle(mEventId, key);
+ }
+ : undefined
+ }
onContextMenu={handleViewReaction}
- onTouchStart={(evt) => evt.stopPropagation()}
+ onTouchStart={(evt) => {
+ evt.stopPropagation();
+ pressedReactionKey.current = key;
+ reactionLongPress.onTouchStart(evt);
+ }}
+ onTouchEnd={reactionLongPress.onTouchEnd}
+ onTouchMove={reactionLongPress.onTouchMove}
+ onTouchCancel={reactionLongPress.onTouchCancel}
aria-disabled={!canToggle}
useAuthentication={useAuthentication}
/>