A Pen by FridayCandour on CodePen.
Created
August 28, 2023 09:50
-
-
Save FridayCandour/c92fe4f3f2d557b02d6ac0cffc86100d to your computer and use it in GitHub Desktop.
Custom-select-dropdown-CSS+JS
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
<div class="custom-select"> | |
<button | |
class="select-button" | |
role="combobox" | |
aria-labelledby="select button" | |
aria-haspopup="listbox" | |
aria-expanded="false" | |
aria-controls="select-dropdown" | |
> | |
<span class="selected-value">Open this select menu</span> | |
<span class="arrow"></span> | |
</button> | |
<ul class="select-dropdown" role="listbox" id="select-dropdown"> | |
<li role="option"> | |
<input type="radio" id="github" name="social-account" /> | |
<label for="github"><i class="bx bxl-github"></i>GitHub</label> | |
</li> | |
<li role="option"> | |
<input type="radio" id="instagram" name="social-account" /> | |
<label for="instagram" | |
><i class="bx bxl-instagram"></i>Instagram</label | |
> | |
</li> | |
<li role="option"> | |
<input type="radio" id="facebook" name="social-account" /> | |
<label for="facebook" | |
><i class="bx bxl-facebook-circle"></i>Facebook</label | |
> | |
</li> | |
<li role="option"> | |
<input type="radio" id="linkedIn" name="social-account" /> | |
<label for="linkedIn" | |
><i class="bx bxl-linkedin-square"></i>LinkedIn</label | |
> | |
</li> | |
<li role="option"> | |
<input type="radio" id="twitter" name="social-account" /> | |
<label for="twitter"><i class="bx bxl-twitter"></i>Twitter</label> | |
</li> | |
<li role="option"> | |
<input type="radio" id="reddit" name="social-account" /> | |
<label for="reddit"><i class="bx bxl-reddit"></i>Reddit</label> | |
</li> | |
</ul> | |
</div> | |
<link href="https://unpkg.com/[email protected]/css/boxicons.min.css" rel="stylesheet" | |
/> |
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
const customSelect = document.querySelector(".custom-select"); | |
const selectBtn = document.querySelector(".select-button"); | |
const selectedValue = document.querySelector(".selected-value"); | |
const optionsList = document.querySelectorAll(".select-dropdown li"); | |
// add click event to select button | |
selectBtn.addEventListener("click", () => { | |
// add/remove active class on the container element | |
customSelect.classList.toggle("active"); | |
// update the aria-expanded attribute based on the current state | |
selectBtn.setAttribute( | |
"aria-expanded", | |
selectBtn.getAttribute("aria-expanded") === "true" ? "false" : "true" | |
); | |
}); | |
optionsList.forEach((option) => { | |
function handler(e) { | |
// Click Events | |
if (e.type === "click" && e.clientX !== 0 && e.clientY !== 0) { | |
selectedValue.textContent = this.children[1].textContent; | |
customSelect.classList.remove("active"); | |
} | |
// Key Events | |
if (e.key === "Enter") { | |
selectedValue.textContent = this.textContent; | |
customSelect.classList.remove("active"); | |
} | |
} | |
option.addEventListener("keyup", handler); | |
option.addEventListener("click", handler); | |
}); |
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
.custom-select { | |
position: relative; | |
width: 400px; | |
max-width: 100%; | |
font-size: 1.15rem; | |
color: #000; | |
margin-top: 3rem; | |
} | |
.select-button { | |
width: 100%; | |
font-size: 1.15rem; | |
background-color: #fff; | |
padding: 0.675em 1em; | |
border: 1px solid #caced1; | |
border-radius: 0.25rem; | |
cursor: pointer; | |
display: flex; | |
justify-content: space-between; | |
align-items: center; | |
} | |
.selected-value { | |
text-align: left; | |
} | |
.arrow { | |
border-left: 5px solid transparent; | |
border-right: 5px solid transparent; | |
border-top: 6px solid #000; | |
transition: transform ease-in-out 0.3s; | |
} | |
.select-dropdown { | |
position: absolute; | |
list-style: none; | |
width: 100%; | |
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2); | |
background-color: #fff; | |
border: 1px solid #caced1; | |
border-radius: 4px; | |
padding: 10px; | |
margin-top: 10px; | |
max-height: 200px; | |
overflow-y: auto; | |
transition: 0.5s ease; | |
transform: scaleY(0); | |
opacity: 0; | |
visibility: hidden; | |
} | |
.select-dropdown:focus-within { | |
box-shadow: 0 10px 25px rgba(94, 108, 233, 0.6); | |
} | |
.select-dropdown li { | |
position: relative; | |
cursor: pointer; | |
display: flex; | |
gap: 1rem; | |
align-items: center; | |
} | |
.select-dropdown li label { | |
width: 100%; | |
padding: 8px 10px; | |
cursor: pointer; | |
display: flex; | |
gap: 1rem; | |
align-items: center; | |
} | |
.select-dropdown::-webkit-scrollbar { | |
width: 7px; | |
} | |
.select-dropdown::-webkit-scrollbar-track { | |
background: #f1f1f1; | |
border-radius: 25px; | |
} | |
.select-dropdown::-webkit-scrollbar-thumb { | |
background: #ccc; | |
border-radius: 25px; | |
} | |
.select-dropdown li:hover, | |
.select-dropdown input:checked ~ label { | |
background-color: #f2f2f2; | |
} | |
.select-dropdown input:focus ~ label { | |
background-color: #dfdfdf; | |
} | |
.select-dropdown input[type="radio"] { | |
position: absolute; | |
left: 0; | |
opacity: 0; | |
} | |
/* interactivity */ | |
.custom-select.active .arrow { | |
transform: rotate(180deg); | |
} | |
.custom-select.active .select-dropdown { | |
opacity: 1; | |
visibility: visible; | |
transform: scaleY(1); | |
} | |
/*============= | |
Aesthetics | |
=========================*/ | |
* { | |
box-sizing: border-box; | |
} | |
body { | |
min-height: 100vh; | |
display: flex; | |
justify-content: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment