Created
November 29, 2019 19:44
-
-
Save SMontiel/09a22375dfd68955efed589857baaa26 to your computer and use it in GitHub Desktop.
responsive-navbar-with-dropdown-vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="relative"> | |
<button @click="isOpen = !isOpen" class="relative z-10 block w-10 h-10 rounded-full overflow-hidden border-2 border-gray-600 focus:outline-none focus:border-white"> | |
<img class="w-full h-full object-cover" src="https://images.unsplash.com/photo-1487412720507-e7ab37603c6f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=256&q=80" alt="Your avatar"> | |
</button> | |
<button v-if="isOpen" @click="isOpen = false" tabindex="-1" class="fixed inset-0 w-full h-full cursor-default"></button> | |
<div v-if="isOpen" class="absolute right-0 w-48 mt-1 py-2 bg-white rounded-lg shadow-md"> | |
<p class="block px-4 pb-2 text-gray-800 text-lg font-semibold border-b">{{ this.username }}</p> | |
<a href="#" class="block px-4 mt-0 py-2 text-gray-800 hover:bg-secondary-600 hover:text-white">Account settings</a> | |
<a :href="logoutRoute" class="block px-4 py-2 text-gray-800 hover:bg-secondary-600 hover:text-white" | |
onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> | |
Sign out | |
</a> | |
<form id="logout-form" :action="logoutRoute" method="POST" class="hidden"> | |
<slot></slot> | |
</form> | |
</div> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: "AccountDropdown", | |
props: { | |
logoutRoute: String, | |
username: String | |
}, | |
data() { | |
return { | |
isOpen: false | |
}; | |
}, | |
created() { | |
const handleEscape = (e) => { | |
if (e.key === 'Esc' || e.key === 'Escape') { | |
this.isOpen = false | |
} | |
} | |
document.addEventListener('keydown', handleEscape) | |
this.$once('hook:beforeDestroy', () => { | |
document.removeEventListener('keydown', handleEscape) | |
}) | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment