Created
November 18, 2020 00:57
-
-
Save jakerieger/a857f7afe32df9205ab2865f2fafdad1 to your computer and use it in GitHub Desktop.
This is a customizable dropdown component for Vuejs
This file contains 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="custom-select-wrapper"> | |
<div class="custom-select"> | |
<div class="custom-select__trigger"><span>{{ options[0].name }}</span> | |
<div class="arrow"></div> | |
</div> | |
<div class="custom-options"> | |
<span v-for="option in options" v-bind:key="option.id" :class="{ 'custom-option selected': option.id == 0, 'custom-option': option.id !== 0 }">{{ option.name }}</span> | |
</div> | |
</div> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'Dropdown', | |
mounted() { | |
document.querySelector('.custom-select-wrapper').addEventListener('click', function() { | |
this.querySelector('.custom-select').classList.toggle('open') | |
console.log('clicked') | |
}) | |
for (const option of document.querySelectorAll(".custom-option")) { | |
option.addEventListener('click', function() { | |
if (!this.classList.contains('selected')) { | |
this.parentNode.querySelector('.custom-option.selected').classList.remove('selected') | |
this.classList.add('selected') | |
this.closest('.custom-select').querySelector('.custom-select__trigger span').textContent = this.textContent | |
} | |
}) | |
} | |
window.addEventListener('click', function(e) { | |
const select = document.querySelector('.custom-select') | |
if (!select.contains(e.target)) { | |
select.classList.remove('open') | |
} | |
}) | |
}, | |
props: { | |
options: { | |
id: Object, | |
name: String | |
} | |
}, | |
methods: { | |
} | |
} | |
</script> | |
<style scoped> | |
.custom-select-wrapper { | |
-webkit-app-region: none; | |
position: relative; | |
user-select: none; | |
width: 100%; | |
width: 200px; | |
} | |
.custom-select { | |
position: relative; | |
display: flex; | |
flex-direction: column; | |
margin-right: 10px; | |
background: #0f0e1f; | |
border-radius: 5px; | |
} | |
.custom-select__trigger { | |
position: relative; | |
display: flex; | |
align-items: center; | |
justify-content: space-between; | |
padding: 0 22px; | |
font-size: 14pt; | |
font-weight: 300; | |
height: 40px; | |
background: transparent; | |
cursor: pointer; | |
color: #d1cfff; | |
} | |
.custom-options { | |
position: absolute; | |
display: block; | |
top: 100%; | |
left: 0; | |
right: 0; | |
border-top: 0; | |
background: #fff; | |
transition: all 0.5s; | |
opacity: 0; | |
visibility: hidden; | |
pointer-events: none; | |
z-index: 99; | |
font-size: 14pt; | |
} | |
.custom-select.open .custom-options { | |
opacity: 1; | |
visibility: visible; | |
pointer-events: all; | |
} | |
.custom-option { | |
position: relative; | |
display: block; | |
padding: 0 22px 0 18px; | |
font-size: 14pt; | |
font-weight: 300; | |
color: #d1cfff;; | |
line-height: 50px; | |
cursor: pointer; | |
transition: all 0.5s; | |
background: #0f0e1f; | |
} | |
.custom-option:hover { | |
cursor: pointer; | |
background-color: #665fcf; | |
} | |
.custom-option.selected { | |
color: #d1cfff;; | |
background-color: #665fcf; | |
} | |
.arrow { | |
position: relative; | |
height: 10px; | |
width: 10px; | |
} | |
.arrow::before, | |
.arrow::after { | |
content: ""; | |
position: absolute; | |
bottom: 0px; | |
width: 0.15rem; | |
height: 100%; | |
transition: all 0.5s; | |
} | |
.arrow::before { | |
left: -3px; | |
transform: rotate(45deg); | |
background-color: #d1cfff;; | |
} | |
.arrow::after { | |
left: 3px; | |
transform: rotate(-45deg); | |
background-color: #d1cfff;; | |
} | |
.open .arrow::before { | |
left: -3px; | |
transform: rotate(-45deg); | |
} | |
.open .arrow::after { | |
left: 3px; | |
transform: rotate(45deg); | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment