@@ -6,6 +6,9 @@ import PgOverlay from '../overlay/overlay';
66import template from './overlayContextMenu.html' ;
77import style from './overlayContextMenu.css' ;
88
9+ // Only allow a single open context menu
10+ const stack : PgOverlayContextMenu [ ] = [ ] ;
11+
912@Component ( {
1013 selector : 'pg-overlay-context-menu' ,
1114 template,
@@ -16,6 +19,8 @@ export default class PgOverlayContextMenu extends PgOverlay {
1619 @Part ( ) $menu : PgMenu ;
1720
1821 @Prop ( ) source : HTMLElement | null = null ;
22+ @Prop ( ) x : number = 0 ;
23+ @Prop ( ) y : number = 0 ;
1924 @Prop ( ) default : any = null ;
2025 @Prop ( ) items : any [ ] = [ ] ;
2126 @Prop ( ) value : any = null ;
@@ -31,6 +36,8 @@ export default class PgOverlayContextMenu extends PgOverlay {
3136 }
3237
3338 connectedCallback ( ) {
39+ stack . pop ( ) ?. close ( ) ;
40+ stack . push ( this ) ;
3441 this . $menu . addEventListener ( 'select' , this . #handleSelect. bind ( this ) ) ;
3542 this . $overlay . popover = 'auto' ;
3643 if ( this . source !== null ) {
@@ -42,39 +49,13 @@ export default class PgOverlayContextMenu extends PgOverlay {
4249 this . $overlay . addEventListener ( 'toggle' , this . #toggle. bind ( this ) ) ;
4350 // Position
4451 const rect = this . source ?. getBoundingClientRect ( ) ;
45- let x = 0 , y = 0 ;
46- const value = this . value === null || typeof this . value !== 'object'
47- ? this . value
48- : this . value . value ;
49- // value is an item in the items list
50- const index = this . value === null
51- ? - 1
52- : this . items . findIndex ( x => x . value === value ) ;
53- if ( index !== - 1 ) {
54- const height = this . $menu . getItemHeight ( index ) ;
55- // Overlap item
56- y -= this . $menu . getItemOffset ( 0 , index ) ;
57- if ( rect ?. height !== height && rect ?. height ) {
58- y += ( rect . height - height ) / 2 ;
59- }
60- } else if ( this . items . length > 0 ) {
61- // insert default if defined
62- if ( this . default ) {
63- this . default . checked = true ;
64- this . $menu . items . unshift ( this . default ) ;
65- }
66- // focus first item
67- const height = this . $menu . getItemHeight ( 0 ) ;
68- y -= this . $menu . getItemOffset ( 0 , 0 ) ;
69- if ( rect ?. height !== height && rect ?. height ) {
70- y += ( rect . height - height ) / 2 ;
71- }
72- }
52+ const x = this . x - ( rect ?. left || 0 ) ,
53+ y = this . y - ( rect ?. top || 0 ) ;
7354 // ToDo: update to CSS Variables
7455 this . $overlay . style . setProperty ( '--pg-overlay-menu-_x' , `${ x } px` ) ;
7556 this . $overlay . style . setProperty ( '--pg-overlay-menu-_y' , `${ y } px` ) ;
7657 // Focus
77- this . $menu . focus ( index ) ;
58+ this . $menu . focus ( 0 ) ;
7859 }
7960
8061 #toggle( e : ToggleEvent ) {
@@ -85,6 +66,7 @@ export default class PgOverlayContextMenu extends PgOverlay {
8566 }
8667
8768 disconnectedCallback ( ) {
69+
8870 }
8971
9072 #handleSelect( e : any ) {
0 commit comments