Pull state out of select box into parent and fix styling

This commit is contained in:
William Oldham 2021-10-26 13:17:11 +01:00
parent 0f83a74a94
commit ef28008482
3 changed files with 34 additions and 38 deletions

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-down"><polyline points="6 9 12 15 18 9"></polyline></svg>

After

Width:  |  Height:  |  Size: 261 B

View File

@ -1,8 +1,4 @@
@import url('https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i&display=swap');
body {
background-color: #1B1F3B;
font-family: 'Open Sans', Arial, sans-serif;
}
/* select box styling */
.select-box {
@ -23,9 +19,10 @@ body {
transition: all 0.2s ease-in-out;
overflow: hidden;
border-radius: 5px;
background-color: #272C4F;
background-color: var(--choice);
order: 1;
position: absolute;
z-index: 1;
top: 50px;
}
@ -37,34 +34,29 @@ body {
border-radius: 5px;
display: flex;
align-items: center;
background-color: #272C4F;
background-color: var(--choice);
color: white;
order: 0;
}
.selected::after {
/* Font Awesome */
content: "\f078";
font-family: "Font Awesome 5 Free";
font-weight: 900;
-webkit-font-smoothing: antialiased;
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
line-height: 1;
/* BG Img */
/* content: url("img/arrow-down.svg"); */
position: absolute;
right: 15px;
top: 50%;
transition: transform 150ms;
transform: translateY(-50%);
.select-box .selected::after {
content: "";
width: 1.2rem;
height: 1.2rem;
background: url(../assets/down-arrow.svg);
position: absolute;
right: 15px;
top: 50%;
transition: transform 150ms;
transform: translateY(-50%);
background-size: contain;
background-position: center;
}
.select-box .option .item {
color: #AFB5DD;
color: var(--text);
font-weight: bold;
}
.select-box .options-container.active {
@ -90,17 +82,20 @@ body {
background: #81878f;
border-radius: 0 5px 5px 0;
}
.select-box .option {
padding: 12px 15px;
}
.select-box .option,
.selected {
padding: 12px 15px;
cursor: pointer;
}
.select-box .option:hover {
background: #2F345B;
.select-box .options-container .option:hover {
background: var(--choice-hover);
}
.select-box .option:hover .item {
color: white;
.select-box .options-container .option:hover .item {
color: var(--choice-active-text, var(--text));
}
.select-box label {

View File

@ -15,12 +15,11 @@ function Option({ option, onClick }) {
)
}
export function SelectBox({ options }) {
export function SelectBox({ options, selectedItem, setSelectedItem }) {
if (!Array.isArray(options)) {
throw "Items must be an array!"
throw new Error("Items must be an array!")
}
const [selectedItem, setSelectedItem] = useState(0)
const [active, setActive] = useState(false)
const containerRef = useRef();
@ -41,7 +40,7 @@ export function SelectBox({ options }) {
useEffect(() => {
// add when mounted
document.addEventListener("mousedown", handleClick);
document.addEventListener("scroll", closeDropdown);
//document.addEventListener("scroll", closeDropdown);
// return function to be called when unmounted
return () => {
document.removeEventListener("mousedown", handleClick);
@ -49,19 +48,20 @@ export function SelectBox({ options }) {
};
}, []);
const onOptionClick = (option, i) => {
const onOptionClick = (e, option, i) => {
e.stopPropagation()
setSelectedItem(i)
closeDropdown()
}
return (
<div className="select-box" ref={containerRef}>
<div className="select-box" ref={containerRef} onClick={() => setActive(a => !a)}>
<div className={"options-container" + (active ? " active" : "")}>
{options.map((opt, i) => (
<Option option={opt} key={i} onClick={() => onOptionClick(opt, i)} />
<Option option={opt} key={i} onClick={(e) => onOptionClick(e, opt, i)} />
))}
</div>
<div className="selected" onClick={() => setActive(a => !a)}>
<div className="selected">
{options ? (
<Option option={options[selectedItem]} />
) : null}