fix: perfect search modal

This commit is contained in:
chansee97 2024-05-11 22:45:31 +08:00
parent df6c8e5aef
commit 35df1832f4
2 changed files with 21 additions and 5 deletions

View File

@ -7,7 +7,7 @@ const appStore = useAppStore()
<template> <template>
<n-tooltip placement="bottom" trigger="hover"> <n-tooltip placement="bottom" trigger="hover">
<template #trigger> <template #trigger>
<CommonWrapper @click="appStore.toggleFullScreen()"> <CommonWrapper @click="appStore.toggleFullScreen">
<icon-park-outline-off-screen-two v-if="appStore.fullScreen" /> <icon-park-outline-off-screen-two v-if="appStore.fullScreen" />
<icon-park-outline-full-screen-two v-else /> <icon-park-outline-full-screen-two v-else />
</CommonWrapper> </CommonWrapper>

View File

@ -12,6 +12,9 @@ const selectedIndex = ref<number>(0)
const { bool: showModal, setTrue: openModal, setFalse: closeModal, toggle: toggleModal } = useBoolean(false) const { bool: showModal, setTrue: openModal, setFalse: closeModal, toggle: toggleModal } = useBoolean(false)
//
const { bool: keyboardFlag, setTrue: setKeyboardTrue, setFalse: setKeyboardFalse } = useBoolean(false)
const { ctrl_k, arrowup, arrowdown, enter/* keys you want to monitor */ } = useMagicKeys({ const { ctrl_k, arrowup, arrowdown, enter/* keys you want to monitor */ } = useMagicKeys({
passive: false, passive: false,
onEventFired(e) { onEventFired(e) {
@ -76,6 +79,8 @@ watchEffect(() => {
if (!showModal.value || !options.value.length) if (!showModal.value || !options.value.length)
return return
// mouseover
setKeyboardTrue()
if (arrowup.value) if (arrowup.value)
handleArrowup() handleArrowup()
@ -97,8 +102,9 @@ function handleArrowup() {
selectedIndex.value-- selectedIndex.value--
nextTick(() => { nextTick(() => {
const distance = selectedIndex.value * 70 > 350 ? selectedIndex.value * 70 - 350 : 0
scrollbarRef.value?.scrollTo({ scrollbarRef.value?.scrollTo({
top: selectedIndex.value * 70, top: distance,
}) })
}) })
} }
@ -112,8 +118,9 @@ function handleArrowdown() {
selectedIndex.value++ selectedIndex.value++
nextTick(() => { nextTick(() => {
const distance = selectedIndex.value * 70 > 350 ? selectedIndex.value * 70 - 350 : 0
scrollbarRef.value?.scrollTo({ scrollbarRef.value?.scrollTo({
top: selectedIndex.value * 70, top: distance,
}) })
}) })
} }
@ -124,6 +131,14 @@ function handleEnter() {
if (target) if (target)
handleSelect(target.value) handleSelect(target.value)
} }
//
function handleMouseEnter(index: number) {
if (keyboardFlag.value)
return
selectedIndex.value = index
}
</script> </script>
<template> <template>
@ -156,7 +171,7 @@ function handleEnter() {
<n-scrollbar ref="scrollbarRef" class="h-450px"> <n-scrollbar ref="scrollbarRef" class="h-450px">
<ul <ul
v-if="options.length" v-if="options.length"
class="flex flex-col gap-8px p-1 p-r-3" class="flex flex-col gap-8px p-8px p-r-3"
> >
<n-el <n-el
v-for="(option, index) in options" v-for="(option, index) in options"
@ -164,7 +179,8 @@ function handleEnter() {
class="cursor-pointer shadow h-62px" class="cursor-pointer shadow h-62px"
:class="{ 'text-[var(--base-color)] bg-[var(--primary-color-hover)]': index === selectedIndex }" :class="{ 'text-[var(--base-color)] bg-[var(--primary-color-hover)]': index === selectedIndex }"
@click="handleSelect(option.value)" @click="handleSelect(option.value)"
@mouseover.stop="selectedIndex = index" @mouseenter="handleMouseEnter(index)"
@mousemove="setKeyboardFalse"
> >
<div class="grid grid-rows-2 grid-cols-[40px_1fr_30px] h-full p-2"> <div class="grid grid-rows-2 grid-cols-[40px_1fr_30px] h-full p-2">
<nova-icon :icon="option.icon" class="row-span-2 place-self-center" /> <nova-icon :icon="option.icon" class="row-span-2 place-self-center" />