@@ -450,9 +450,37 @@ export const aboveLeftOf = (
450450 return menuOptions ;
451451} ;
452452
453+ // Placement method for <ContextMenu /> to position context menu right-aligned and flowing to the right of elementRect,
454+ // and either above or below: wherever there is more space (maybe this should be aboveOrBelowRightOf?)
455+ export const aboveRightOf = (
456+ elementRect : Pick < DOMRect , "left" | "top" | "bottom" > ,
457+ chevronFace = ChevronFace . None ,
458+ vPadding = 0 ,
459+ ) : AboveLeftOf => {
460+ const menuOptions : IPosition & { chevronFace : ChevronFace } = { chevronFace } ;
461+
462+ const buttonLeft = elementRect . left + window . pageXOffset ;
463+ const buttonBottom = elementRect . bottom + window . pageYOffset ;
464+ const buttonTop = elementRect . top + window . pageYOffset ;
465+ // Align the left edge of the menu to the left edge of the button
466+ menuOptions . left = buttonLeft ;
467+ // Align the menu vertically on whichever side of the button has more space available.
468+ if ( buttonBottom < UIStore . instance . windowHeight / 2 ) {
469+ menuOptions . top = buttonBottom + vPadding ;
470+ } else {
471+ menuOptions . bottom = ( UIStore . instance . windowHeight - buttonTop ) + vPadding ;
472+ }
473+
474+ return menuOptions ;
475+ } ;
476+
453477// Placement method for <ContextMenu /> to position context menu right-aligned and flowing to the left of elementRect
454478// and always above elementRect
455- export const alwaysAboveLeftOf = ( elementRect : DOMRect , chevronFace = ChevronFace . None , vPadding = 0 ) => {
479+ export const alwaysAboveLeftOf = (
480+ elementRect : Pick < DOMRect , "right" | "bottom" | "top" > ,
481+ chevronFace = ChevronFace . None ,
482+ vPadding = 0 ,
483+ ) => {
456484 const menuOptions : IPosition & { chevronFace : ChevronFace } = { chevronFace } ;
457485
458486 const buttonRight = elementRect . right + window . pageXOffset ;
@@ -472,7 +500,11 @@ export const alwaysAboveLeftOf = (elementRect: DOMRect, chevronFace = ChevronFac
472500
473501// Placement method for <ContextMenu /> to position context menu right-aligned and flowing to the right of elementRect
474502// and always above elementRect
475- export const alwaysAboveRightOf = ( elementRect : DOMRect , chevronFace = ChevronFace . None , vPadding = 0 ) => {
503+ export const alwaysAboveRightOf = (
504+ elementRect : Pick < DOMRect , "left" | "top" > ,
505+ chevronFace = ChevronFace . None ,
506+ vPadding = 0 ,
507+ ) => {
476508 const menuOptions : IPosition & { chevronFace : ChevronFace } = { chevronFace } ;
477509
478510 const buttonLeft = elementRect . left + window . pageXOffset ;
0 commit comments