Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(ContextMenu): the trigger takes only the long-press touch handlers (#7322)

Christopher Chedeaub6e14ce54d4d
diff --git a/.changeset/context-menu-trigger-dom-props.md b/.changeset/context-menu-trigger-dom-props.md
new file mode 100644
index 0000000000..5ee3bebc9f
--- /dev/null
+++ b/.changeset/context-menu-trigger-dom-props.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `ContextMenu` puts only its four touch handlers on the trigger element. It used to spread everything `useLongPress` returns, which since the press model includes the hook's imperative `start`, `moveTo` and `cancel`, so React warned "does not recognize the `moveTo` prop" and "Invalid values for props `start`, `cancel`" on the first ContextMenu of a page. Long-press opening is unchanged.
+
+@vjeux
diff --git a/packages/core/src/ContextMenu/ContextMenu.triggerProps.test.tsx b/packages/core/src/ContextMenu/ContextMenu.triggerProps.test.tsx
new file mode 100644
index 0000000000..e384ddc500
--- /dev/null
+++ b/packages/core/src/ContextMenu/ContextMenu.triggerProps.test.tsx
@@ -0,0 +1,54 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file ContextMenu.triggerProps.test.tsx
+ * @input vitest, @testing-library/react, ContextMenu
+ * @output A guard that the trigger element receives only DOM props: React
+ * warns about nothing, and no hook-internal function lands on it.
+ * @position Testing; validates ContextMenu.tsx. Its own file on purpose: React
+ * warns about an unknown DOM prop once per module, so this case must be the
+ * first ContextMenu render of its file to see the warning at all.
+ */
+import {describe, expect, it, vi} from 'vitest';
+import {render, screen} from '@testing-library/react';
+import {ContextMenu} from './ContextMenu';
+
+describe('ContextMenu trigger element', () => {
+ it('receives only DOM props: no React warning, no long-press API on it', () => {
+ const consoleError = vi
+ .spyOn(console, 'error')
+ .mockImplementation(() => {});
+ try {
+ render(
+ <>
+ <ContextMenu items={[{label: 'Cut'}]} data-testid="block">
+ <div>Right-click me</div>
+ </ContextMenu>
+ <p>
+ Filed under{' '}
+ <ContextMenu
+ items={[{label: 'Copy'}]}
+ triggerAs="span"
+ data-testid="inline">
+ a reference
+ </ContextMenu>
+ </p>
+ </>,
+ );
+ const messages = consoleError.mock.calls.map(args =>
+ args.map(String).join(' '),
+ );
+ expect(messages).toEqual([]);
+ for (const trigger of [
+ screen.getByTestId('block'),
+ screen.getByTestId('inline'),
+ ]) {
+ for (const name of ['start', 'moveto', 'cancel']) {
+ expect(trigger).not.toHaveAttribute(name);
+ }
+ }
+ } finally {
+ consoleError.mockRestore();
+ }
+ });
+});
diff --git a/packages/core/src/ContextMenu/ContextMenu.tsx b/packages/core/src/ContextMenu/ContextMenu.tsx
index 5b46378a6f..6c9d9ebc05 100644
--- a/packages/core/src/ContextMenu/ContextMenu.tsx
+++ b/packages/core/src/ContextMenu/ContextMenu.tsx
@@ -594,7 +594,9 @@ export function ContextMenu({
// `contextmenu` event on long-press, so a context menu is otherwise
// unreachable on touch. Open the menu at the touch point once the press is
// held long enough (see useLongPress for timer/move-cancel/cleanup logic).
- const longPressHandlers = useLongPress({
+ // The hook also returns the imperative start/moveTo/cancel API the menu press
+ // model drives; only its four touch handlers belong on the trigger element.
+ const longPress = useLongPress({
disabled: isDisabled,
onLongPress: useCallback(
(point: {x: number; y: number}) => {
@@ -725,7 +727,10 @@ export function ContextMenu({
ref={useMergedRefs(ref, triggerRef)}
{...triggerProps}
onContextMenu={handleContextMenu}
- {...longPressHandlers}
+ onTouchStart={longPress.onTouchStart}
+ onTouchMove={longPress.onTouchMove}
+ onTouchEnd={longPress.onTouchEnd}
+ onTouchCancel={longPress.onTouchCancel}
data-testid={testId}
{...stylex.props(
styles.trigger,