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
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
3839import * as stylex from '@stylexjs/stylex' ;
3940import { useCallback , useEffect , useRef , type RefObject } from 'react' ;
4041import { createAnimator , type Animator } from '../motion/animator' ;
41- import {
42- SETTLING_EPSILON ,
43- solveSpring ,
44- toSpring ,
45- type SpringDescription ,
46- } from '../motion/spring' ;
4742import { contextMenu , springs } from '../motion/uikit' ;
4843import { colorVars , shadowVars } from '../theme/tokens.stylex' ;
4944import {
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. */
203175function 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