Skip to content

Commit bf6b720

Browse files
committed
feat(ContextMenu): the menu's glass grows out of the finger as iOS 26's does
1 parent 7f17a8b commit bf6b720

10 files changed

Lines changed: 479 additions & 88 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] When a held finger opens a ContextMenu, the menu now grows out of the finger as iOS 26's does. A small blob of its surface at the press point moves and grows into the menu, overshooting slightly, and the menu's content follows it in from a blur, clipped to it. The menu then wears its own surface again.
6+
7+
@vjeux

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

Lines changed: 2 additions & 2 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. 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.',
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 grows out of the finger to sit beside it, below when it fits and above otherwise.',
164164
bestPractices: [
165165
{
166166
guidance: true,
@@ -229,7 +229,7 @@ export const docs = {
229229
export const docsZh = {
230230
usage: {
231231
description:
232-
'右键点击时在光标位置出现的上下文菜单。用于为特定元素或区域提供上下文操作,而不使 UI 杂乱。在触摸屏上,按住手指即可打开,时序与 iOS 相同:150 毫秒时触发区域开始放大,400 毫秒时在设备支持的情况下伴随触感反馈,735 毫秒时菜单打开;若手指在 400 毫秒后抬起,菜单在抬起时立即打开。随后块级触发区域会像 iOS 一样从页面中浮起:它的副本在着色背景上放大约 13%,菜单位于其旁边,空间足够时在下方,否则在上方。',
232+
'右键点击时在光标位置出现的上下文菜单。用于为特定元素或区域提供上下文操作,而不使 UI 杂乱。在触摸屏上,按住手指即可打开,时序与 iOS 相同:150 毫秒时触发区域开始放大,400 毫秒时在设备支持的情况下伴随触感反馈,735 毫秒时菜单打开;若手指在 400 毫秒后抬起,菜单在抬起时立即打开。随后块级触发区域会像 iOS 一样从页面中浮起:它的副本在着色背景上放大约 13%,菜单从手指处展开并位于其旁边,空间足够时在下方,否则在上方。',
233233
bestPractices: [
234234
{guidance: true, description: '保持菜单项简洁和面向操作。'},
235235
{guidance: true, description: '有很多项时使用分组和分隔线。'},

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ import {contextMenu} from '../motion/uikit';
9191
import {ContextMenuPressRecognizer} from './contextMenuPress';
9292
import {useContextMenuPressFeedback} from './contextMenuPressFeedback';
9393
import {
94-
playMenuEntrance,
9594
useContextMenuLift,
9695
type ContextMenuLiftResult,
9796
} from './ContextMenuLift';
@@ -648,8 +647,7 @@ export function ContextMenu({
648647

649648
// Opens the menu beside a lifted preview: the cursor anchor becomes the
650649
// preview's box (in the trigger's coordinates; the trigger is back at its
651-
// size, under the preview), the layer places the menu against it, and the
652-
// menu's content enters from a blur.
650+
// size, under the preview), and the layer places the menu against it.
653651
const openLifted = useCallback(
654652
({source, lifted}: ContextMenuLiftResult) => {
655653
const x = lifted.x - source.x;
@@ -670,13 +668,9 @@ export function ContextMenu({
670668
? document.activeElement
671669
: triggerRef.current;
672670
layer.show();
673-
const menu = listRef.current?.closest<HTMLElement>('[popover]');
674-
if (menu != null) {
675-
playMenuEntrance(menu);
676-
}
677671
requestAnimationFrame(() => focusFirst());
678672
},
679-
[layer, focusFirst, listRef],
673+
[layer, focusFirst],
680674
);
681675

682676
const handleContextMenu = useCallback(
@@ -777,6 +771,14 @@ export function ContextMenu({
777771
setLiftPlacement(placement);
778772
});
779773
}
774+
// Its glass grows out of the finger, now that its frame is known.
775+
const list = listRef.current;
776+
if (list != null) {
777+
liftRef.current.morphMenu(
778+
list,
779+
recognizer.press ?? recognizer.location(),
780+
);
781+
}
780782
return;
781783
}
782784
// Otherwise the menu opens under the finger (openAtLocalPoint cancels

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

Lines changed: 140 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* the page: a copy of it (UIKit's preview platter) grows on from the size the
1717
* press gave it to its lifted size, a shadow rising under it, above a backdrop
1818
* that tints the page (no blur, no zoom); the view itself stays where it is,
19-
* under the copy. The menu's content fades in from a blur
20-
* (`playMenuEntrance`). When the menu closes, the copy returns to the view's
19+
* under the copy. The menu's glass grows out of the finger, the content
20+
* following it in from a blur (`morphMenu`). When the menu closes, the copy returns to the view's
2121
* place on the dismissal spring while the backdrop and the shadow clear.
2222
*
2323
* Here the copy is a clone of the trigger (inert, hidden from assistive
@@ -28,7 +28,8 @@
2828
* (`--color-background-backdrop`, `--shadow-high`): UIKit's own are
2929
* rgba(0.086, 0.082, 0.165, 0.21) and a black shadow at 0.30, radius 20, no
3030
* offset, shaped by the view's pixels. Position, scale and opacity move on
31-
* the compositor animator; the menu's blur plays as spring-sampled keyframes.
31+
* the compositor animator; the menu's content (its clip and blur) plays as
32+
* keyframes sampled from the same springs.
3233
* Nothing renders during the motion.
3334
*
3435
* SYNC: When modified, update /packages/core/src/ContextMenu/ContextMenu.tsx
@@ -38,16 +39,11 @@
3839
import * as stylex from '@stylexjs/stylex';
3940
import {useCallback, useEffect, useRef, type RefObject} from 'react';
4041
import {createAnimator, type Animator} from '../motion/animator';
41-
import {
42-
SETTLING_EPSILON,
43-
solveSpring,
44-
toSpring,
45-
type SpringDescription,
46-
} from '../motion/spring';
4742
import {contextMenu, springs} from '../motion/uikit';
4843
import {colorVars, shadowVars} from '../theme/tokens.stylex';
4944
import {
5045
liftedBox,
46+
menuMorph,
5147
type LiftBox,
5248
type LiftViewport,
5349
} from './contextMenuLiftGeometry';
@@ -109,6 +105,12 @@ export interface ContextMenuLift {
109105
* mounted (server, or no popover support).
110106
*/
111107
lift(): ContextMenuLiftResult | null;
108+
/**
109+
* The menu's entrance beside the copy: its glass grows out of `press`
110+
* (client px) into `menu`'s frame, and its content, clipped to the glass,
111+
* follows it in from a blur (`menuMorph`).
112+
*/
113+
morphMenu(menu: HTMLElement, press: {x: number; y: number}): void;
112114
/** Return the preview to its place and show the trigger again. */
113115
drop(): void;
114116
}
@@ -130,34 +132,20 @@ function viewportOf(doc: Document): LiftViewport {
130132
}
131133

132134
/**
133-
* WAAPI keyframes along a spring from 0 to 1, sampled at 60 Hz until it
134-
* settles (overshoot included): `frame(p)` builds the keyframe at progress p.
135+
* WAAPI keyframes sampled at 60 Hz over `seconds`: `frame(t)` builds the
136+
* keyframe `t` seconds in.
135137
*/
136-
function springKeyframes(
137-
description: SpringDescription,
138-
frame: (progress: number) => Keyframe,
138+
function sampleKeyframes(
139+
seconds: number,
140+
frame: (t: number) => Keyframe,
139141
): {keyframes: Keyframe[]; duration: number} {
140-
const curve = solveSpring(toSpring(description), {from: 0, to: 1});
141-
const seconds = Math.max(curve.settleTime(SETTLING_EPSILON), 1 / 60);
142-
const steps = Math.max(2, Math.ceil(seconds * 60));
142+
const length = Math.max(seconds, 1 / 60);
143+
const steps = Math.max(2, Math.ceil(length * 60));
143144
const keyframes: Keyframe[] = [];
144145
for (let i = 0; i <= steps; i++) {
145-
const progress = i === steps ? 1 : curve.value((i / steps) * seconds);
146-
keyframes.push({...frame(progress), offset: i / steps});
147-
}
148-
return {keyframes, duration: seconds * 1000};
149-
}
150-
151-
function playKeyframes(
152-
element: HTMLElement,
153-
description: SpringDescription,
154-
frame: (progress: number) => Keyframe,
155-
): Animation | null {
156-
if (typeof element.animate !== 'function') {
157-
return null;
146+
keyframes.push({...frame((i / steps) * length), offset: i / steps});
158147
}
159-
const {keyframes, duration} = springKeyframes(description, frame);
160-
return element.animate(keyframes, {duration, fill: 'forwards'});
148+
return {keyframes, duration: length * 1000};
161149
}
162150

163151
/**
@@ -183,22 +171,6 @@ function shapeRadius(trigger: HTMLElement): string {
183171
return fills ? getComputedStyle(content[0]).borderRadius : own;
184172
}
185173

186-
/**
187-
* The menu's entrance beside a lifted preview: its content fades in from a
188-
* blur on `morphSpring` (Reduced Motion: `springs.menuReducedMotion`).
189-
*/
190-
export function playMenuEntrance(menu: HTMLElement): void {
191-
const blur = LIFT.menuEntranceBlur;
192-
playKeyframes(
193-
menu,
194-
prefersReducedMotion() ? springs.menuReducedMotion : LIFT.morphSpring,
195-
progress => ({
196-
opacity: String(progress),
197-
filter: `blur(${(blur * Math.max(1 - progress, 0)).toFixed(3)}px)`,
198-
}),
199-
);
200-
}
201-
202174
/** A copy of the trigger to lift: inert, unnamed, with no ids to collide. */
203175
function clonePreview(trigger: HTMLElement): HTMLElement {
204176
const clone = trigger.cloneNode(true) as HTMLElement;
@@ -238,10 +210,31 @@ export function useContextMenuLift(
238210
const previewAnimatorRef = useRef<Animator | null>(null);
239211
const backdropAnimatorRef = useRef<Animator | null>(null);
240212
const shadowAnimatorRef = useRef<Animator | null>(null);
213+
const morphRef = useRef<{
214+
menu: HTMLElement;
215+
glass: HTMLElement;
216+
glassAnimator: Animator;
217+
animation: Animation;
218+
} | null>(null);
241219
// Bumps on every lift, so a drop that finishes late leaves a newer lift be.
242220
const generationRef = useRef(0);
243221

222+
const endMorph = useCallback(() => {
223+
const morph = morphRef.current;
224+
if (morph == null) {
225+
return;
226+
}
227+
morphRef.current = null;
228+
morph.animation.cancel();
229+
morph.glassAnimator.dispose();
230+
morph.glass.remove();
231+
for (const property of ['background-color', 'box-shadow', 'overflow']) {
232+
morph.menu.style.removeProperty(property);
233+
}
234+
}, []);
235+
244236
const cleanUp = useCallback(() => {
237+
endMorph();
245238
shadowAnimatorRef.current?.dispose();
246239
shadowAnimatorRef.current = null;
247240
previewAnimatorRef.current?.dispose();
@@ -252,7 +245,7 @@ export function useContextMenuLift(
252245
if (layerEl?.matches(':popover-open')) {
253246
layerEl.hidePopover();
254247
}
255-
}, []);
248+
}, [endMorph]);
256249

257250
useEffect(
258251
() => () => {
@@ -354,6 +347,7 @@ export function useContextMenuLift(
354347
}, [cleanUp, triggerRef]);
355348

356349
const drop = useCallback(() => {
350+
endMorph();
357351
const previewAnimator = previewAnimatorRef.current;
358352
const backdropAnimator = backdropAnimatorRef.current;
359353
if (previewAnimator == null) {
@@ -380,7 +374,102 @@ export function useContextMenuLift(
380374
cleanUp();
381375
}
382376
});
383-
}, [cleanUp]);
377+
}, [cleanUp, endMorph]);
378+
379+
const morphMenu = useCallback(
380+
(menu: HTMLElement, press: {x: number; y: number}) => {
381+
endMorph();
382+
const layerEl = layerRef.current;
383+
if (layerEl == null || typeof menu.animate !== 'function') {
384+
return;
385+
}
386+
const rect = menu.getBoundingClientRect();
387+
const box: LiftBox = {
388+
x: rect.left,
389+
y: rect.top,
390+
width: rect.width,
391+
height: rect.height,
392+
};
393+
if (box.width <= 0 || box.height <= 0) {
394+
return;
395+
}
396+
const style = getComputedStyle(menu);
397+
const radius = Number.parseFloat(style.borderTopLeftRadius) || 0;
398+
const reduced = prefersReducedMotion();
399+
const morph = menuMorph(box, press, {reduced});
400+
const centerX = box.x + box.width / 2;
401+
const centerY = box.y + box.height / 2;
402+
// The glass: a blob in the lift layer wearing the menu's own background,
403+
// radius and shadow, scaled on the compositor from the press point.
404+
const glass = menu.ownerDocument.createElement('span');
405+
glass.setAttribute('data-astryx-context-menu-glass', '');
406+
Object.assign(glass.style, {
407+
position: 'fixed',
408+
left: `${box.x}px`,
409+
top: `${box.y}px`,
410+
width: `${box.width}px`,
411+
height: `${box.height}px`,
412+
borderRadius: style.borderRadius,
413+
backgroundColor: style.backgroundColor,
414+
boxShadow: style.boxShadow,
415+
pointerEvents: 'none',
416+
});
417+
layerEl.append(glass);
418+
const start = morph.at(0);
419+
const at = glass.ownerDocument.timeline?.currentTime;
420+
const startAt = typeof at === 'number' ? at : performance.now();
421+
const glassAnimator = createAnimator(glass);
422+
glassAnimator.set({
423+
x: start.centerX - centerX,
424+
y: start.centerY - centerY,
425+
scaleX: start.width / box.width,
426+
scaleY: start.height / box.height,
427+
});
428+
glassAnimator.animate(
429+
{x: 0, y: 0},
430+
{spring: reduced ? springs.menuReducedMotion : LIFT.morphSpring},
431+
{reducedMotion: 'keep', at: startAt},
432+
);
433+
glassAnimator.animate(
434+
{scaleX: 1, scaleY: 1},
435+
{spring: reduced ? springs.menuReducedMotion : LIFT.morphSizeSpring},
436+
{reducedMotion: 'keep', at: startAt + LIFT.morphSizeDelayMs},
437+
);
438+
// The menu itself goes bare (the blob is its glass) and unclipped by its
439+
// own scroll box; its content follows the blob's centre, scaled, faded in
440+
// from a blur and clipped to the blob.
441+
menu.style.backgroundColor = 'transparent';
442+
menu.style.boxShadow = 'none';
443+
menu.style.overflow = 'visible';
444+
const blur = LIFT.menuEntranceBlur;
445+
const {keyframes, duration} = sampleKeyframes(morph.duration, t => {
446+
const m = morph.at(t);
447+
const scale = m.contentScale;
448+
const insetX = box.width / 2 - m.width / (2 * scale);
449+
const insetY = box.height / 2 - m.height / (2 * scale);
450+
const radiusX = (radius * m.width) / box.width / scale;
451+
const radiusY = (radius * m.height) / box.height / scale;
452+
return {
453+
transform: `translate(${(m.centerX - centerX).toFixed(3)}px, ${(m.centerY - centerY).toFixed(3)}px) scale(${scale.toFixed(5)})`,
454+
clipPath: `inset(${insetY.toFixed(3)}px ${insetX.toFixed(3)}px round ${radiusX.toFixed(3)}px / ${radiusY.toFixed(3)}px)`,
455+
opacity: String(Math.min(Math.max(m.fade, 0), 1)),
456+
filter: `blur(${(blur * Math.max(1 - m.fade, 0)).toFixed(3)}px)`,
457+
};
458+
});
459+
const animation = menu.animate(keyframes, {duration});
460+
const entry = {menu, glass, glassAnimator, animation};
461+
morphRef.current = entry;
462+
animation.finished.then(
463+
() => {
464+
if (morphRef.current === entry) {
465+
endMorph();
466+
}
467+
},
468+
() => {},
469+
);
470+
},
471+
[endMorph],
472+
);
384473

385474
const layer = (
386475
<span
@@ -409,5 +498,5 @@ export function useContextMenuLift(
409498
</span>
410499
);
411500

412-
return {layer, lift, drop};
501+
return {layer, lift, morphMenu, drop};
413502
}

0 commit comments

Comments
 (0)