Skip to content

Commit 8fc6d73

Browse files
committed
feat(ContextMenu): a held finger lifts the trigger as iOS 26 does, with the menu beside it
1 parent 57fd76a commit 8fc6d73

15 files changed

Lines changed: 1496 additions & 102 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[feat] A ContextMenu opened by a held finger now presents as iOS 26's does. A copy of a block trigger lifts out of the page: it grows on from the press to 26 px larger on its longer side (15% at most), with the theme's high shadow fading in under it, over the theme's backdrop colour, and it stays 16 px from the screen's sides and 20 px from its top and bottom. A tap on the backdrop closes the menu. The menu sits 16 px from the copy, below it when it fits and above it otherwise, centered on it or flush with the edge it is held against, and its content enters from a blur. Closing returns the copy to the trigger. Inline triggers, pointer opens and the bottom sheet are unchanged.
6+
7+
@vjeux

‎packages/cli/test/__golden__/no-icon-set/search-close.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "search",
44
"data": {
55
"query": "close",
6-
"matchCount": 45,
6+
"matchCount": 44,
77
"results": [
88
{
99
"domain": "component",

‎packages/cli/test/__golden__/no-icon-set/search-close.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Results for "close" (20 of 45)
1+
Results for "close" (20 of 44)
22
name: AlertDialog
33
domain: component
44
package: @astryxdesign/core

‎packages/core/src/ContextMenu/ContextMenu.doc.mjs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export const docs = {
160160
usage: {
161161
anatomy,
162162
description:
163-
'A right-click context menu that appears at the cursor position. Use to provide contextual actions for specific elements or regions without cluttering the UI with visible buttons. On a touch screen a held finger opens it with iOS timing: the trigger starts to grow at 150 ms, a haptic plays at 400 ms where the device can play one, and the menu opens at 735 ms, or as soon as the finger lifts after 400 ms.',
163+
'A right-click context menu that appears at the cursor position. Use to provide contextual actions for specific elements or regions without cluttering the UI with visible buttons. On a touch screen a held finger opens it with iOS timing: the trigger starts to grow at 150 ms, a haptic plays at 400 ms where the device can play one, and the menu opens at 735 ms, or as soon as the finger lifts after 400 ms. A block trigger then lifts out of the page, as on iOS: a copy of it grows about 13% over a tinted backdrop, and the menu sits beside it, below when it fits and above otherwise.',
164164
bestPractices: [
165165
{
166166
guidance: true,
@@ -190,7 +190,7 @@ export const docs = {
190190
{
191191
guidance: true,
192192
description:
193-
'Leave a little room around a block trigger: a held finger grows it from its center, by about 6%, until the menu closes.',
193+
'Leave a little room around a block trigger: a held finger grows it from its center, by about 6%, and its lifted copy is about 13% larger while the menu is open.',
194194
},
195195
{
196196
guidance: false,
@@ -229,7 +229,7 @@ export const docs = {
229229
export const docsZh = {
230230
usage: {
231231
description:
232-
'右键点击时在光标位置出现的上下文菜单。用于为特定元素或区域提供上下文操作,而不使 UI 杂乱。在触摸屏上,按住手指即可打开,时序与 iOS 相同:150 毫秒时触发区域开始放大,400 毫秒时在设备支持的情况下伴随触感反馈,735 毫秒时菜单打开;若手指在 400 毫秒后抬起,菜单在抬起时立即打开。',
232+
'右键点击时在光标位置出现的上下文菜单。用于为特定元素或区域提供上下文操作,而不使 UI 杂乱。在触摸屏上,按住手指即可打开,时序与 iOS 相同:150 毫秒时触发区域开始放大,400 毫秒时在设备支持的情况下伴随触感反馈,735 毫秒时菜单打开;若手指在 400 毫秒后抬起,菜单在抬起时立即打开。随后块级触发区域会像 iOS 一样从页面中浮起:它的副本在着色背景上放大约 13%,菜单位于其旁边,空间足够时在下方,否则在上方。',
233233
bestPractices: [
234234
{guidance: true, description: '保持菜单项简洁和面向操作。'},
235235
{guidance: true, description: '有很多项时使用分组和分隔线。'},
@@ -270,7 +270,7 @@ export const docsDense = {
270270
usage: {
271271
anatomy,
272272
description:
273-
'A right-click context menu that appears at the cursor position. Use to provide contextual actions for specific elements or regions. Touch: a held finger opens it at 735 ms, or on a lift after 400 ms (iOS timing); the trigger grows from 150 ms.',
273+
'A right-click context menu that appears at the cursor position. Use to provide contextual actions for specific elements or regions. Touch: a held finger opens it at 735 ms, or on a lift after 400 ms (iOS timing); the trigger grows from 150 ms, then a block trigger lifts (about 13%) over a tint with the menu below or above it.',
274274
bestPractices: [
275275
{guidance: true, description: 'Keep items concise and action-oriented.'},
276276
{
@@ -294,7 +294,7 @@ export const docsDense = {
294294
{
295295
guidance: true,
296296
description:
297-
'Leave room around a block trigger: a held finger grows it about 6%.',
297+
'Leave room around a block trigger: a held finger grows it about 6%, its lifted copy about 13%.',
298298
},
299299
{
300300
guidance: false,

‎packages/core/src/ContextMenu/ContextMenu.test.tsx‎

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,17 @@ const advance = (ms: number) =>
567567
// so every case moves the faked clock past the ones before it.
568568
let clockEpoch = 0;
569569

570+
/**
571+
* How many times the menu's popover was shown (a touch-opened menu also shows
572+
* the lift layer, its preview and backdrop).
573+
*/
574+
const menuShows = () =>
575+
vi
576+
.mocked(HTMLElement.prototype.showPopover)
577+
.mock.contexts.filter(
578+
el => !(el as HTMLElement).hasAttribute('data-astryx-context-menu-lift'),
579+
).length;
580+
570581
describe('ContextMenu touch: UIKit’s long-press timeline', () => {
571582
beforeEach(() => {
572583
vi.useFakeTimers({
@@ -609,9 +620,68 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
609620
const press = finger(trigger);
610621
press.down();
611622
advance(contextMenu.press.menuDisplayMs - 1);
612-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
623+
expect(menuShows()).toBe(0);
613624
advance(1);
614-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
625+
expect(menuShows()).toBe(1);
626+
press.up();
627+
});
628+
629+
it('lifts a block trigger’s copy, and closes when a finger lifts on the backdrop (UIKit ends at that lift)', () => {
630+
const onOpenChange = vi.fn();
631+
render(
632+
<ContextMenu
633+
items={[{label: 'Item 1'}]}
634+
data-testid="ctx"
635+
onOpenChange={onOpenChange}>
636+
<div>Long-press me</div>
637+
</ContextMenu>,
638+
);
639+
const press = finger(screen.getByTestId('ctx'));
640+
press.down();
641+
advance(contextMenu.press.menuDisplayMs);
642+
press.up();
643+
expect(onOpenChange).toHaveBeenLastCalledWith(true);
644+
const preview = document.querySelector(
645+
'[data-astryx-context-menu-preview]',
646+
);
647+
expect(preview).not.toBeNull();
648+
expect(preview?.getAttribute('aria-hidden')).toBe('true');
649+
expect(preview?.hasAttribute('data-testid')).toBe(false);
650+
const backdrop = document.querySelector(
651+
'[data-astryx-context-menu-lift] > span',
652+
);
653+
if (backdrop == null) {
654+
throw new Error('no backdrop');
655+
}
656+
// A lift there of a finger that went down elsewhere is not a tap there.
657+
fireEvent.pointerUp(backdrop, {
658+
isPrimary: true,
659+
pointerId: 7,
660+
pointerType: 'touch',
661+
});
662+
expect(onOpenChange).toHaveBeenLastCalledWith(true);
663+
fireEvent.pointerDown(backdrop, {
664+
isPrimary: true,
665+
pointerId: 8,
666+
pointerType: 'touch',
667+
});
668+
fireEvent.pointerUp(backdrop, {
669+
isPrimary: true,
670+
pointerId: 8,
671+
pointerType: 'touch',
672+
});
673+
expect(onOpenChange).toHaveBeenLastCalledWith(false);
674+
});
675+
676+
it('keeps an inline trigger’s menu at the finger: no lift', () => {
677+
const trigger = renderMenu({triggerAs: 'span'});
678+
const press = finger(trigger);
679+
press.down();
680+
advance(contextMenu.press.menuDisplayMs);
681+
expect(menuShows()).toBe(1);
682+
expect(
683+
document.querySelector('[data-astryx-context-menu-lift]'),
684+
).toBeNull();
615685
press.up();
616686
});
617687

@@ -620,9 +690,9 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
620690
const press = finger(trigger);
621691
press.down();
622692
advance(529);
623-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
693+
expect(menuShows()).toBe(0);
624694
press.up();
625-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
695+
expect(menuShows()).toBe(1);
626696
});
627697

628698
it('opens nothing for a drag past the tolerance after the commit, before the menu', () => {
@@ -634,7 +704,7 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
634704
advance(500);
635705
press.up();
636706
advance(100);
637-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
707+
expect(menuShows()).toBe(0);
638708
});
639709

640710
it('keeps the menu open as the finger lifts: the lift sends no click or compatibility mouse events', () => {
@@ -658,7 +728,7 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
658728
const lift = press.up();
659729
expect(lift.defaultPrevented).toBe(false);
660730
advance(1000);
661-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
731+
expect(menuShows()).toBe(0);
662732
});
663733

664734
it('cancels when the finger drifts past the tolerance on one axis (11.5 px)', () => {
@@ -668,7 +738,7 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
668738
advance(50);
669739
press.moveTo(20, 31.6);
670740
advance(1000);
671-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
741+
expect(menuShows()).toBe(0);
672742
press.up();
673743
});
674744

@@ -679,7 +749,7 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
679749
advance(50);
680750
press.moveTo(31, 31); // 15.6 px away, 11 px on each axis
681751
advance(contextMenu.press.menuDisplayMs);
682-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
752+
expect(menuShows()).toBe(1);
683753
press.up();
684754
});
685755

@@ -691,7 +761,7 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
691761
advance(100);
692762
dispatchTouch('touchstart', trigger, [second], [first, second]);
693763
advance(1000);
694-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
764+
expect(menuShows()).toBe(0);
695765
dispatchTouch('touchend', trigger, [first, second], []);
696766
});
697767

@@ -709,9 +779,9 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
709779
});
710780
trigger.dispatchEvent(native);
711781
expect(native.defaultPrevented).toBe(true);
712-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
782+
expect(menuShows()).toBe(0);
713783
advance(contextMenu.press.menuDisplayMs - 380);
714-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
784+
expect(menuShows()).toBe(1);
715785
press.up();
716786
});
717787

@@ -737,7 +807,7 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
737807
advance(1);
738808
// At the commit, before the menu shows.
739809
expect(vibrate).toHaveBeenCalledTimes(1);
740-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
810+
expect(menuShows()).toBe(0);
741811
expect(vibrate).toHaveBeenCalledWith([
742812
...HAPTIC_VIBRATION_PATTERNS[contextMenu.commitHaptic].value,
743813
]);
@@ -769,7 +839,7 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
769839
it('leaves the page alone for a menu a right-click opened', () => {
770840
const trigger = renderMenu();
771841
fireEvent.contextMenu(trigger, {clientX: 20, clientY: 20});
772-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
842+
expect(menuShows()).toBe(1);
773843
expect(document.body.style.position).toBe('');
774844
});
775845

@@ -789,7 +859,7 @@ describe('ContextMenu touch: UIKit’s long-press timeline', () => {
789859
const press = finger(trigger);
790860
press.down();
791861
advance(1000);
792-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
862+
expect(menuShows()).toBe(0);
793863
press.up();
794864
});
795865
});

0 commit comments

Comments
 (0)