[UI] Fixed some glitches of the Dropdown component.

This commit is contained in:
Fabio Manganiello 2023-11-12 17:20:12 +01:00
parent f6c752c80e
commit b63579b81c
Signed by: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 34 additions and 22 deletions

View file

@ -6,7 +6,7 @@
</button> </button>
<div class="body-container hidden" ref="dropdownContainer"> <div class="body-container hidden" ref="dropdownContainer">
<DropdownBody :id="id" :keepOpenOnItemClick="keepOpenOnItemClick" ref="dropdown"> <DropdownBody :id="id" :keepOpenOnItemClick="keepOpenOnItemClick" ref="dropdown" @click="onClick">
<slot /> <slot />
</DropdownBody> </DropdownBody>
</div> </div>
@ -50,22 +50,6 @@ export default {
}, },
computed: { computed: {
dropdownWidth() {
const dropdown = this.$refs.dropdown?.$el
if (!dropdown)
return 0
return parseFloat(getComputedStyle(dropdown).width)
},
dropdownHeight() {
const dropdown = this.$refs.dropdown?.$el
if (!dropdown)
return 0
return parseFloat(getComputedStyle(dropdown).height)
},
buttonStyle() { buttonStyle() {
if (!this.$refs.button) if (!this.$refs.button)
return {} return {}
@ -98,6 +82,27 @@ export default {
this.close() this.close()
}, },
getDropdownWidth() {
const dropdown = this.$refs.dropdown?.$el
if (!dropdown)
return 0
return parseFloat(getComputedStyle(dropdown).width)
},
getDropdownHeight() {
const dropdown = this.$refs.dropdown?.$el
if (!dropdown)
return 0
return parseFloat(getComputedStyle(dropdown).height)
},
onClick() {
if (!this.keepOpenOnItemClick)
this.close()
},
close() { close() {
this.visible = false this.visible = false
document.removeEventListener('click', this.documentClickHndl) document.removeEventListener('click', this.documentClickHndl)
@ -106,6 +111,10 @@ export default {
open() { open() {
document.addEventListener('click', this.documentClickHndl) document.addEventListener('click', this.documentClickHndl)
const element = this.$refs.dropdown?.$el
if (!element.parentElement)
this.$el.appendChild(element)
this.visible = true this.visible = true
this.$refs.dropdownContainer.classList.remove('hidden') this.$refs.dropdownContainer.classList.remove('hidden')
this.$nextTick(() => { this.$nextTick(() => {
@ -120,12 +129,15 @@ export default {
top: buttonPos.top + this.buttonHeight, top: buttonPos.top + this.buttonHeight,
} }
if ((pos.left + this.dropdownWidth) > (window.innerWidth + window.scrollX) / 2) { const dropdownWidth = this.getDropdownWidth()
pos.left -= (this.dropdownWidth - this.buttonWidth) const dropdownHeight = this.getDropdownHeight()
if ((pos.left + dropdownWidth) > (window.innerWidth + window.scrollX) / 2) {
pos.left -= (dropdownWidth - this.buttonWidth)
} }
if ((pos.top + this.dropdownHeight) > (window.innerHeight + window.scrollY) / 2) { if ((pos.top + dropdownHeight) > (window.innerHeight + window.scrollY) / 2) {
pos.top -= (this.dropdownHeight + this.buttonHeight - 10) pos.top -= (dropdownHeight + this.buttonHeight - 10)
} }
const element = this.$refs.dropdown.$el const element = this.$refs.dropdown.$el

View file

@ -1,5 +1,5 @@
<template> <template>
<div class="dropdown" :id="id"> <div class="dropdown" :id="id" @click="$emit('click', $event)">
<slot /> <slot />
</div> </div>
</template> </template>