Skip to content

Commit 1d09473

Browse files
committed
feat(ContextMenu): a held finger lifts the trigger as iOS 26 does, with the menu beside it
1 parent 3f4fe26 commit 1d09473

15 files changed

Lines changed: 1718 additions & 106 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] On iPhone and iPad, 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. Other touch screens, Android included, keep today's menu under the finger, with nothing lifted.
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 iPhone and iPad a held finger opens it with iOS timing: the trigger starts to grow at 150 ms, and the menu opens at 735 ms, or as soon as the finger lifts after 400 ms. On other touch screens, Android included, a finger held for 500 ms opens it under the finger.',
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 iPhone and iPad a held finger opens it with iOS timing: the trigger starts to grow at 150 ms, 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. On other touch screens, Android included, a finger held for 500 ms opens it under the finger.',
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: on iOS a held finger grows it from its center, by about 6%, until the menu closes.',
193+
'Leave a little room around a block trigger: on iOS 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 杂乱。在 iPhone 和 iPad 上,按住手指即可打开,时序与 iOS 相同:150 毫秒时触发区域开始放大,735 毫秒时菜单打开;若手指在 400 毫秒后抬起,菜单在抬起时立即打开。在其他触摸屏(包括 Android)上,按住 500 毫秒后菜单在手指处打开。',
232+
'右键点击时在光标位置出现的上下文菜单。用于为特定元素或区域提供上下文操作,而不使 UI 杂乱。在 iPhone 和 iPad 上,按住手指即可打开,时序与 iOS 相同:150 毫秒时触发区域开始放大,735 毫秒时菜单打开;若手指在 400 毫秒后抬起,菜单在抬起时立即打开。随后块级触发区域会像 iOS 一样从页面中浮起:它的副本在着色背景上放大约 13%,菜单位于其旁边,空间足够时在下方,否则在上方。在其他触摸屏(包括 Android)上,按住 500 毫秒后菜单在手指处打开。',
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 on iOS: a held finger opens it at 735 ms, or on a lift after 400 ms; the trigger grows from 150 ms. Other touch screens: 500 ms, under the finger.',
273+
'A right-click context menu that appears at the cursor position. Use to provide contextual actions for specific elements or regions. Touch on iOS: a held finger opens it at 735 ms, or on a lift after 400 ms; the trigger grows from 150 ms, then a block trigger lifts (about 13%) over a tint with the menu below or above it. Other touch screens: 500 ms, under the finger.',
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: on iOS a held finger grows it about 6%.',
297+
'Leave room around a block trigger: on iOS 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: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,17 @@ function stubAndroidTouch() {
610610
// so every case moves the faked clock past the ones before it.
611611
let clockEpoch = 0;
612612

613+
/**
614+
* How many times the menu's popover was shown (a touch-opened menu also shows
615+
* the lift layer, its preview and backdrop).
616+
*/
617+
const menuShows = () =>
618+
vi
619+
.mocked(HTMLElement.prototype.showPopover)
620+
.mock.contexts.filter(
621+
el => !(el as HTMLElement).hasAttribute('data-astryx-context-menu-lift'),
622+
).length;
623+
613624
describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', () => {
614625
beforeEach(() => {
615626
stubIosIdiom();
@@ -654,9 +665,68 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
654665
const press = finger(trigger);
655666
press.down();
656667
advance(contextMenu.press.menuDisplayMs - 1);
657-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
668+
expect(menuShows()).toBe(0);
658669
advance(1);
659-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
670+
expect(menuShows()).toBe(1);
671+
press.up();
672+
});
673+
674+
it('lifts a block trigger’s copy, and closes when a finger lifts on the backdrop (UIKit ends at that lift)', () => {
675+
const onOpenChange = vi.fn();
676+
render(
677+
<ContextMenu
678+
items={[{label: 'Item 1'}]}
679+
data-testid="ctx"
680+
onOpenChange={onOpenChange}>
681+
<div>Long-press me</div>
682+
</ContextMenu>,
683+
);
684+
const press = finger(screen.getByTestId('ctx'));
685+
press.down();
686+
advance(contextMenu.press.menuDisplayMs);
687+
press.up();
688+
expect(onOpenChange).toHaveBeenLastCalledWith(true);
689+
const preview = document.querySelector(
690+
'[data-astryx-context-menu-preview]',
691+
);
692+
expect(preview).not.toBeNull();
693+
expect(preview?.getAttribute('aria-hidden')).toBe('true');
694+
expect(preview?.hasAttribute('data-testid')).toBe(false);
695+
const backdrop = document.querySelector(
696+
'[data-astryx-context-menu-lift] > span',
697+
);
698+
if (backdrop == null) {
699+
throw new Error('no backdrop');
700+
}
701+
// A lift there of a finger that went down elsewhere is not a tap there.
702+
fireEvent.pointerUp(backdrop, {
703+
isPrimary: true,
704+
pointerId: 7,
705+
pointerType: 'touch',
706+
});
707+
expect(onOpenChange).toHaveBeenLastCalledWith(true);
708+
fireEvent.pointerDown(backdrop, {
709+
isPrimary: true,
710+
pointerId: 8,
711+
pointerType: 'touch',
712+
});
713+
fireEvent.pointerUp(backdrop, {
714+
isPrimary: true,
715+
pointerId: 8,
716+
pointerType: 'touch',
717+
});
718+
expect(onOpenChange).toHaveBeenLastCalledWith(false);
719+
});
720+
721+
it('keeps an inline trigger’s menu at the finger: no lift', () => {
722+
const trigger = renderMenu({triggerAs: 'span'});
723+
const press = finger(trigger);
724+
press.down();
725+
advance(contextMenu.press.menuDisplayMs);
726+
expect(menuShows()).toBe(1);
727+
expect(
728+
document.querySelector('[data-astryx-context-menu-lift]'),
729+
).toBeNull();
660730
press.up();
661731
});
662732

@@ -665,9 +735,9 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
665735
const press = finger(trigger);
666736
press.down();
667737
advance(529);
668-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
738+
expect(menuShows()).toBe(0);
669739
press.up();
670-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
740+
expect(menuShows()).toBe(1);
671741
});
672742

673743
it('opens nothing for a drag past the tolerance after the commit, before the menu', () => {
@@ -679,7 +749,7 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
679749
advance(500);
680750
press.up();
681751
advance(100);
682-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
752+
expect(menuShows()).toBe(0);
683753
});
684754

685755
it('keeps the menu open as the finger lifts: the lift sends no click or compatibility mouse events', () => {
@@ -703,7 +773,7 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
703773
const lift = press.up();
704774
expect(lift.defaultPrevented).toBe(false);
705775
advance(1000);
706-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
776+
expect(menuShows()).toBe(0);
707777
});
708778

709779
it('cancels when the finger drifts past the tolerance on one axis (11.5 px)', () => {
@@ -713,7 +783,7 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
713783
advance(50);
714784
press.moveTo(20, 31.6);
715785
advance(1000);
716-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
786+
expect(menuShows()).toBe(0);
717787
press.up();
718788
});
719789

@@ -724,7 +794,7 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
724794
advance(50);
725795
press.moveTo(31, 31); // 15.6 px away, 11 px on each axis
726796
advance(contextMenu.press.menuDisplayMs);
727-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
797+
expect(menuShows()).toBe(1);
728798
press.up();
729799
});
730800

@@ -736,7 +806,7 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
736806
advance(100);
737807
dispatchTouch('touchstart', trigger, [second], [first, second]);
738808
advance(1000);
739-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
809+
expect(menuShows()).toBe(0);
740810
dispatchTouch('touchend', trigger, [first, second], []);
741811
});
742812

@@ -754,9 +824,9 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
754824
});
755825
trigger.dispatchEvent(native);
756826
expect(native.defaultPrevented).toBe(true);
757-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
827+
expect(menuShows()).toBe(0);
758828
advance(contextMenu.press.menuDisplayMs - 380);
759-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
829+
expect(menuShows()).toBe(1);
760830
press.up();
761831
});
762832

@@ -782,7 +852,7 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
782852
advance(1);
783853
// At the commit, before the menu shows.
784854
expect(vibrate).toHaveBeenCalledTimes(1);
785-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
855+
expect(menuShows()).toBe(0);
786856
expect(vibrate).toHaveBeenCalledWith([
787857
...HAPTIC_VIBRATION_PATTERNS[contextMenu.commitHaptic].value,
788858
]);
@@ -814,7 +884,7 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
814884
it('leaves the page alone for a menu a right-click opened', () => {
815885
const trigger = renderMenu();
816886
fireEvent.contextMenu(trigger, {clientX: 20, clientY: 20});
817-
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
887+
expect(menuShows()).toBe(1);
818888
expect(document.body.style.position).toBe('');
819889
});
820890

@@ -834,7 +904,7 @@ describe('ContextMenu touch in the iOS idiom: UIKit’s long-press timeline', ()
834904
const press = finger(trigger);
835905
press.down();
836906
advance(1000);
837-
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
907+
expect(menuShows()).toBe(0);
838908
press.up();
839909
});
840910
});
@@ -923,6 +993,18 @@ describe('ContextMenu touch outside the iOS idiom: today’s long press', () =>
923993
expect(document.body.style.position).toBe('');
924994
});
925995

996+
it('lifts nothing: no lift layer, and the menu opens under the finger', () => {
997+
const trigger = renderMenu();
998+
const press = finger(trigger);
999+
press.down();
1000+
advance(500);
1001+
expect(menuShows()).toBe(1);
1002+
expect(
1003+
document.querySelector('[data-astryx-context-menu-lift]'),
1004+
).toBeNull();
1005+
press.up();
1006+
});
1007+
9261008
it('cancels when a second finger lands before the menu', () => {
9271009
const trigger = renderMenu();
9281010
const first = {id: 1, x: 20, y: 20};

0 commit comments

Comments
 (0)