Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

convert PopoutMenu.vue to typescript #160

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions lib/components/base/PopoutMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,50 @@
class="popup-menu"
:class="`position-${position}-${direction} ${dropdownVisible ? 'visible' : ''}`"
>
<slot name="menu"> </slot>
<slot name="menu"></slot>
</div>
</div>
</template>

<script setup>
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'

const props = defineProps({
disabled: {
type: Boolean,
default: false,
},
position: {
type: String,
default: 'bottom',
},
direction: {
type: String,
default: 'left',
},
})
type Positions = 'top' | 'bottom' | 'left' | 'right'
type Direction = 'up' | 'down' | 'left' | 'right'

const props = withDefaults(
defineProps<{
disabled?: boolean
position?: Positions
direction?: Direction
}>(),
{
disabled: false,
position: 'bottom',
direction: 'left',
}
)

defineOptions({
inheritAttrs: false,
})

const dropdownVisible = ref(false)
const dropdown = ref(null)
const dropdownButton = ref(null)
const dropdown = ref<HTMLElement | null>(null)
const dropdownButton = ref<HTMLElement | null>(null)

const toggleDropdown = () => {
if (!props.disabled) {
dropdownVisible.value = !dropdownVisible.value
if (!dropdownVisible.value) {
dropdownButton.value.focus()
dropdownButton.value!.focus()
}
}
}

const hide = () => {
dropdownVisible.value = false
dropdownButton.value.focus()
dropdownButton.value!.focus()
}

const show = () => {
Expand All @@ -66,12 +68,13 @@ defineExpose({
hide,
})

const handleClickOutside = (event) => {
const handleClickOutside = (event: MouseEvent) => {
const elements = document.elementsFromPoint(event.clientX, event.clientY)

if (
dropdown.value.$el !== event.target &&
!elements.includes(dropdown.value.$el) &&
!dropdown.value.contains(event.target)
dropdown.value !== event.target &&
!elements.includes(dropdown.value!) &&
!dropdown.value!.contains(event.target as HTMLElement)
) {
dropdownVisible.value = false
}
Expand Down