wip: set contextmenu x/y according to position in window

This commit is contained in:
Yohan Boniface 2024-07-12 12:21:30 +02:00
parent 4a6b271bbe
commit 96c9bf9413

View file

@ -26,8 +26,7 @@ export default class ContextMenu {
this.container.appendChild(li) this.container.appendChild(li)
} }
document.body.appendChild(this.container) document.body.appendChild(this.container)
this.container.style.top = `${y}px` this.setPosition([x, y], this.container)
this.container.style.left = `${x}px`
this.container.querySelector('button').focus() this.container.querySelector('button').focus()
this.container.addEventListener('keydown', (event) => { this.container.addEventListener('keydown', (event) => {
if (event.key === 'Escape') { if (event.key === 'Escape') {
@ -37,6 +36,19 @@ export default class ContextMenu {
}) })
} }
setPosition([x, y], element) {
if (x < window.innerWidth / 2) {
this.container.style.left = `${x}px`
} else {
this.container.style.left = `${x - element.offsetWidth}px`
}
if (y < window.innerHeight / 2) {
this.container.style.top = `${y}px`
} else {
this.container.style.top = `${y - element.offsetHeight}px`
}
}
close() { close() {
try { try {
this.container.remove() this.container.remove()