@@ -73,12 +73,9 @@ vi.mock('@astryxdesign/core/Layout', async importOriginal => ({
7373 true ;
7474
7575function panelPosition ( menu : HTMLElement | null ) : {
76- left ?: string | null ;
76+ left ?: string ;
7777 top ?: string ;
7878 transform ?: string ;
79- insetInlineStart ?: string ;
80- overflowY ?: string ;
81- maxHeight ?: string ;
8279} {
8380 return JSON . parse ( menu ?. getAttribute ( 'data-style' ) ?? '{}' ) ;
8481}
@@ -307,205 +304,127 @@ describe('useLongPressHookUsage example', () => {
307304 expect ( container . textContent ) . toContain ( 'Pinned via right-click (88, 36).' ) ;
308305 } ) ;
309306
310- it ( 'opens the Actions panel beside the button, never over it ' , ( ) => {
307+ it ( 'reaches every action from the Actions menu without a hold ' , ( ) => {
311308 renderExample ( ) ;
312- const surface = getSurface ( ) ;
313- vi . spyOn ( surface , 'getBoundingClientRect' ) . mockReturnValue (
314- DOMRect . fromRect ( { x : 0 , y : 0 , width : 340 , height : 216 } ) ,
315- ) ;
316- const actionsButton = Array . from (
317- container . querySelectorAll < HTMLButtonElement > ( 'button' ) ,
318- ) . find ( button => button . textContent === 'Actions' ) ;
319- if ( ! actionsButton ) {
320- throw new Error ( 'Expected the Actions button' ) ;
321- }
322- // The surface's style while the panel is open.
323- let openSurfaceStyle : ReturnType < typeof panelPosition > = { } ;
324- const openFrom = ( rect : DOMRect ) => {
325- vi . spyOn ( actionsButton , 'getBoundingClientRect' ) . mockReturnValue ( rect ) ;
326- act ( ( ) => {
327- actionsButton . click ( ) ;
328- } ) ;
329- const menu = container . querySelector < HTMLElement > ( '[role="group"]' ) ;
330- openSurfaceStyle = panelPosition ( surface ) ;
331- act ( ( ) => {
332- surface . dispatchEvent (
333- new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } ) ,
334- ) ;
335- } ) ;
336- return menu ;
337- } ;
338-
339- // The button at the end of its row: the panel opens before it.
340- const before = openFrom (
341- DOMRect . fromRect ( { x : 236 , y : 16 , width : 88 , height : 40 } ) ,
342- ) ;
343- expect ( before ?. textContent ) . toContain ( 'Opened by button at (48, 16)' ) ;
344- expect ( panelPosition ( before ) . left ) . toBe (
345- 'clamp(0px, 48px, calc(100% - 180px))' ,
346- ) ;
309+ const findButton = ( label : string ) =>
310+ Array . from ( document . querySelectorAll < HTMLButtonElement > ( 'button' ) ) . find (
311+ button => button . textContent ?. trim ( ) === label ,
312+ ) ;
313+ const findItem = ( label : string ) =>
314+ Array . from (
315+ document . querySelectorAll < HTMLElement > ( '[role="menuitem"]' ) ,
316+ ) . find ( item => item . textContent ?. includes ( label ) ) ;
347317
348- // The button wrapped to the start of its own row: the panel opens after it.
349- const after = openFrom (
350- DOMRect . fromRect ( { x : 16 , y : 64 , width : 88 , height : 40 } ) ,
351- ) ;
352- expect ( after ?. textContent ) . toContain ( 'Opened by button at (112, 64)' ) ;
353- expect ( openSurfaceStyle ) . not . toHaveProperty ( 'overflowY' ) ;
354-
355- // A narrow card has no room on either side of the button, so the panel
356- // opens below it in both left-to-right and right-to-left layouts. It does
357- // not move up over the button. It keeps to the room below the button and
358- // scrolls its own actions, so the surface neither grows nor scrolls, and a
359- // centered preview around the example does not shift.
360- vi . spyOn ( surface , 'getBoundingClientRect' ) . mockReturnValue (
361- DOMRect . fromRect ( { x : 0 , y : 0 , width : 260 , height : 216 } ) ,
362- ) ;
318+ const actionsButton = findButton ( 'Actions' ) ;
319+ expect ( actionsButton ) . toBeDefined ( ) ;
320+ expect ( actionsButton ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) ;
363321
364- const belowLtr = openFrom (
365- DOMRect . fromRect ( { x : 16 , y : 64 , width : 88 , height : 40 } ) ,
366- ) ;
367- expect ( belowLtr ?. textContent ) . toContain ( 'Opened by button at (16, 112)' ) ;
368- expect ( panelPosition ( belowLtr ) ) . toMatchObject ( {
369- left : null ,
370- insetInlineStart : '16px' ,
371- top : '112px' ,
372- transform : 'none' ,
373- overflowY : 'auto' ,
374- maxHeight : '104px' ,
322+ act ( ( ) => {
323+ actionsButton ?. click ( ) ;
375324 } ) ;
376- expect ( openSurfaceStyle ) . not . toHaveProperty ( 'overflowY' ) ;
325+ expect ( actionsButton ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
326+ // The Actions path opens Core's menu, not the at-point panel.
327+ expect ( container . querySelector ( '[role="group"]' ) ) . toBeNull ( ) ;
377328
378- // A button too near the end edge: the panel's end edge meets the button's.
379- const belowEnd = openFrom (
380- DOMRect . fromRect ( { x : 156 , y : 64 , width : 88 , height : 40 } ) ,
381- ) ;
382- expect ( belowEnd ?. textContent ) . toContain ( 'Opened by button at (64, 112)' ) ;
383- expect ( panelPosition ( belowEnd ) ) . toMatchObject ( {
384- left : null ,
385- insetInlineStart : '64px' ,
386- top : '112px' ,
387- transform : 'none' ,
388- overflowY : 'auto' ,
389- maxHeight : '104px' ,
329+ act ( ( ) => {
330+ findItem ( 'Copy link' ) ?. click ( ) ;
390331 } ) ;
391- expect ( openSurfaceStyle ) . not . toHaveProperty ( 'overflowY' ) ;
392-
393- // Right to left: the button starts the row at the right edge, and the
394- // panel's start edge meets the button's start edge, measured from the
395- // right, so a scrollbar on the left cannot move it.
396- surface . style . direction = 'rtl' ;
397- const belowRtl = openFrom (
398- DOMRect . fromRect ( { x : 156 , y : 64 , width : 88 , height : 40 } ) ,
332+ expect ( container . textContent ) . toContain (
333+ 'Copied link via the Actions menu.' ,
399334 ) ;
400- expect ( belowRtl ?. textContent ) . toContain ( 'Opened by button at (64, 112)' ) ;
401- expect ( panelPosition ( belowRtl ) ) . toMatchObject ( {
402- left : null ,
403- insetInlineStart : '16px' ,
404- top : '112px' ,
405- transform : 'none' ,
406- overflowY : 'auto' ,
407- maxHeight : '104px' ,
335+
336+ act ( ( ) => {
337+ actionsButton ?. click ( ) ;
338+ } ) ;
339+ act ( ( ) => {
340+ findItem ( 'Pin draft' ) ?. click ( ) ;
408341 } ) ;
409- expect ( openSurfaceStyle ) . not . toHaveProperty ( 'overflowY ') ;
342+ expect ( container . textContent ) . toContain ( 'Pinned via the Actions menu. ') ;
410343 } ) ;
411344
412- it ( 'cuts the last visible action of a below-button panel through its middle' , ( ) => {
413- // Rows of the panel, from its top: three 40px actions after a 38px note.
414- const rowTops : Record < string , number > = {
415- 'Copy link' : 38 ,
416- 'Pin draft' : 78 ,
417- Dismiss : 118 ,
418- } ;
419- vi . spyOn ( HTMLElement . prototype , 'scrollHeight' , 'get' ) . mockImplementation (
420- function ( this : HTMLElement ) {
421- return this . getAttribute ( 'role' ) === 'group' ? 166 : 0 ;
422- } ,
423- ) ;
424- vi . spyOn ( HTMLElement . prototype , 'getBoundingClientRect' ) . mockImplementation (
425- function ( this : HTMLElement ) {
426- const label = this . textContent ?? '' ;
427- if ( this . tagName === 'BUTTON' && label in rowTops ) {
428- return DOMRect . fromRect ( {
429- x : 16 ,
430- y : 112 + rowTops [ label ] ,
431- width : 160 ,
432- height : 40 ,
433- } ) ;
434- }
435- // One spy answers for every element: a spy on one element would
436- // replace this prototype spy for all of them.
437- if ( this . dataset . testid === 'long-press-surface' ) {
438- return DOMRect . fromRect ( { x : 0 , y : 0 , width : 260 , height : 216 } ) ;
439- }
440- if ( this . hasAttribute ( 'aria-expanded' ) ) {
441- return DOMRect . fromRect ( { x : 16 , y : 64 , width : 88 , height : 40 } ) ;
442- }
443- return DOMRect . fromRect ( {
444- x : 0 ,
445- y : this . getAttribute ( 'role' ) === 'group' ? 112 : 0 ,
446- width : 0 ,
447- height : 0 ,
448- } ) ;
449- } ,
450- ) ;
345+ it ( 'closes the at-point panel when the Actions menu opens' , ( ) => {
451346 renderExample ( ) ;
452- const actionsButton = container . querySelector < HTMLButtonElement > (
453- 'button[aria-expanded]' ,
454- ) ;
455- if ( ! actionsButton ) {
456- throw new Error ( 'Expected the Actions button' ) ;
457- }
347+ const surface = getSurface ( ) ;
458348 act ( ( ) => {
459- actionsButton . click ( ) ;
349+ surface . dispatchEvent (
350+ new MouseEvent ( 'contextmenu' , {
351+ bubbles : true ,
352+ cancelable : true ,
353+ clientX : 40 ,
354+ clientY : 40 ,
355+ } ) ,
356+ ) ;
460357 } ) ;
461- const menu = container . querySelector < HTMLElement > ( '[role="group"]' ) ;
462- // The room below the button is 104px. "Pin draft" shows 60% at 102px;
463- // "Dismiss" would need 142px. So the panel stops at 102px, through the
464- // middle of "Pin draft", and the cut row shows that the list scrolls.
465- expect ( panelPosition ( menu ) ) . toMatchObject ( {
466- top : '112px' ,
467- overflowY : 'auto' ,
468- maxHeight : '102px' ,
358+ expect ( container . querySelector ( '[role="group"]' ) ) . not . toBeNull ( ) ;
359+
360+ const actionsButton = Array . from (
361+ container . querySelectorAll < HTMLButtonElement > ( 'button' ) ,
362+ ) . find ( button => button . textContent ?. trim ( ) === 'Actions' ) ;
363+ act ( ( ) => {
364+ actionsButton ?. click ( ) ;
469365 } ) ;
366+ expect ( actionsButton ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
367+ expect ( container . querySelector ( '[role="group"]' ) ) . toBeNull ( ) ;
470368 } ) ;
471369
472- it ( 'provides an explicit Actions button fallback and returns focus when closed ' , ( ) => {
370+ it ( 'closes the Actions menu when the at-point panel opens ' , ( ) => {
473371 renderExample ( ) ;
372+ const surface = getSurface ( ) ;
373+ const actionsButton = Array . from (
374+ container . querySelectorAll < HTMLButtonElement > ( 'button' ) ,
375+ ) . find ( button => button . textContent ?. trim ( ) === 'Actions' ) ;
376+ act ( ( ) => {
377+ actionsButton ?. click ( ) ;
378+ } ) ;
379+ expect ( actionsButton ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
474380
381+ act ( ( ) => {
382+ surface . dispatchEvent (
383+ new MouseEvent ( 'contextmenu' , {
384+ bubbles : true ,
385+ cancelable : true ,
386+ clientX : 40 ,
387+ clientY : 40 ,
388+ } ) ,
389+ ) ;
390+ } ) ;
391+ expect ( container . querySelector ( '[role="group"]' ) ) . not . toBeNull ( ) ;
392+ // The popover stays mounted while closed; the trigger reports its state.
393+ expect ( actionsButton ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) ;
394+ } ) ;
395+
396+ it ( 'returns focus to Actions when the at-point panel closes' , ( ) => {
397+ renderExample ( ) ;
398+ const surface = getSurface ( ) ;
475399 const findButton = ( label : string ) =>
476400 Array . from ( container . querySelectorAll < HTMLButtonElement > ( 'button' ) ) . find (
477- button => button . textContent === label ,
401+ button => button . textContent ?. trim ( ) === label ,
478402 ) ;
479-
480403 const actionsButton = findButton ( 'Actions' ) ;
481- expect ( actionsButton ) . toBeDefined ( ) ;
482- expect ( actionsButton ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) ;
483-
484- act ( ( ) => {
485- actionsButton ?. click ( ) ;
486- } ) ;
487-
488- expect ( actionsButton ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
489- const menu = container . querySelector < HTMLElement > ( '[role="group"]' ) ;
490- expect ( menu ) . not . toBeNull ( ) ;
491- expect ( menu ?. textContent ) . toContain ( 'Opened by button' ) ;
404+ const openAtPointer = ( ) => {
405+ act ( ( ) => {
406+ surface . dispatchEvent (
407+ new MouseEvent ( 'contextmenu' , {
408+ bubbles : true ,
409+ cancelable : true ,
410+ clientX : 40 ,
411+ clientY : 40 ,
412+ } ) ,
413+ ) ;
414+ } ) ;
415+ } ;
492416
417+ openAtPointer ( ) ;
493418 const copyButton = findButton ( 'Copy link' ) ;
494419 copyButton ?. focus ( ) ;
495420 act ( ( ) => {
496421 copyButton ?. click ( ) ;
497422 } ) ;
498-
499423 expect ( container . querySelector ( '[role="group"]' ) ) . toBeNull ( ) ;
500- expect ( container . textContent ) . toContain ( 'Copied link via button' ) ;
501424 expect ( document . activeElement ) . toBe ( actionsButton ) ;
502425
503- // Reopen and close with Escape; focus should return to Actions button.
504- act ( ( ) => {
505- actionsButton ?. click ( ) ;
506- } ) ;
426+ openAtPointer ( ) ;
507427 const dismissButton = findButton ( 'Dismiss' ) ;
508- expect ( dismissButton ) . toBeDefined ( ) ;
509428 dismissButton ?. focus ( ) ;
510429 act ( ( ) => {
511430 dismissButton ?. dispatchEvent (
@@ -515,14 +434,9 @@ describe('useLongPressHookUsage example', () => {
515434 expect ( container . querySelector ( '[role="group"]' ) ) . toBeNull ( ) ;
516435 expect ( document . activeElement ) . toBe ( actionsButton ) ;
517436
518- // Reopen and close with Dismiss button; focus should return to Actions button.
519- act ( ( ) => {
520- actionsButton ?. click ( ) ;
521- } ) ;
522- const secondDismissButton = findButton ( 'Dismiss' ) ;
523- secondDismissButton ?. focus ( ) ;
437+ openAtPointer ( ) ;
524438 act ( ( ) => {
525- secondDismissButton ?. click ( ) ;
439+ findButton ( 'Dismiss' ) ?. click ( ) ;
526440 } ) ;
527441 expect ( container . querySelector ( '[role="group"]' ) ) . toBeNull ( ) ;
528442 expect ( document . activeElement ) . toBe ( actionsButton ) ;
0 commit comments