fix(reactions): open viewer on mobile long press (#1729)

<!-- Please read
https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before
submitting your pull request -->

### Description

<!-- Please include a summary of the change. Please also include
relevant motivation and context. List any dependencies that are required
for this change. -->

Fixes #

#### Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings

### AI disclosure:

- [ ] Partially AI assisted (clarify which code was AI assisted and
briefly explain what it does).
- [ ] Fully AI generated (explain what all the generated code does in
moderate detail).
<!-- Write any explanation required here, but do not generate the
explanation using AI!! You must prove you understand what the code in
this PR does. -->
This commit is contained in:
Erwan Leboucher authored and GitHub committed 2026-08-12 09:47:26 +02:00
commit a660bc24b6
2 files changed
+57 -6

No files matched your search

@@ -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 }) => (
<button
type="button"
aria-label="Reaction viewer"
data-testid="reaction-viewer"
data-initial-key={initialKey}
/>
),
}));
vi.mock('focus-trap-react', () => ({
default: ({ children }: { children: ReactNode }) => children,
}));
import { Reactions } from './Reactions';
@@ -64,6 +75,10 @@ function renderReactions(onTouchStart: TouchEventHandler<HTMLDivElement>) {
}
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();
});
});
+25 -3
View File
@@ -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<boolean | string>(false);
const [emojiBoardAnchor, setEmojiBoardAnchor] = useState<RectCords>();
const pressedReactionKey = useRef<string>();
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}
/>