Skip to content

Commit

Permalink
click-outside-vue3ライブラリを使わないように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
ZOI-dayo committed Sep 27, 2024
1 parent 5c274b7 commit c8931f8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
10 changes: 0 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"format": "prettier --write src/"
},
"dependencies": {
"click-outside-vue3": "^4.0.1",
"vue": "^3.5.6",
"vue-router": "^4.3.3"
},
Expand Down
39 changes: 31 additions & 8 deletions src/components/Navigations/TopNavbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,42 @@ import PrimaryButton from '@/components/Controls/PrimaryButton.vue'
defineProps({
isLoggedIn: Boolean,
username: String,
username: String
})
const isMenuOpen = ref(false)
const handler = (e: MouseEvent) => {
if (e.target instanceof HTMLElement) {
if (e.target.closest('#top-navbar-menu')) {
return
}
closeMenu()
}
}
const toggleMenu = () => {
if (isMenuOpen.value) {
closeMenu()
} else {
openMenu()
}
}
const openMenu = () => {
isMenuOpen.value = true
document.addEventListener('mousedown', handler)
}
const closeMenu = () => {
isMenuOpen.value = false
document.removeEventListener('mousedown', handler)
}
</script>

<template>
<header
class="fixed flex h-14 w-full items-center bg-background-tertiary px-8 font-primary text-text-primary"
class="fixed z-50 flex h-14 w-full items-center bg-background-tertiary px-8 font-primary text-text-primary"
>
<a
class="flex items-center gap-3"
Expand All @@ -41,8 +68,9 @@ const isMenuOpen = ref(false)
/>
<button
v-if="isLoggedIn"
id="top-navbar-menu"
class="relative flex items-center gap-1"
@click="isMenuOpen = !isMenuOpen"
@click="toggleMenu"
>
<span class="inline-block">{{ username }}</span>
<IconDropdownTriangle
Expand All @@ -51,11 +79,6 @@ const isMenuOpen = ref(false)
/>
<div
v-if="isMenuOpen"
v-click-outside="
() => {
isMenuOpen = false
}
"
class="absolute right-0 top-7 flex w-50 flex-col gap-y-1 rounded-lg border border-border-secondary bg-background-primary p-2"
>
<SideMenuButton
Expand Down
3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import vClickOutside from 'click-outside-vue3'

const app = createApp(App)

app.use(router)
app.use(vClickOutside)
app.directive('click-outside', vClickOutside.directive)

app.mount('#app')

0 comments on commit c8931f8

Please sign in to comment.