1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-20 14:37:43 +01:00

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'); @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 styling */
.select-box { .select-box {
@ -23,9 +19,10 @@ body {
transition: all 0.2s ease-in-out; transition: all 0.2s ease-in-out;
overflow: hidden; overflow: hidden;
border-radius: 5px; border-radius: 5px;
background-color: #272C4F; background-color: var(--choice);
order: 1; order: 1;
position: absolute; position: absolute;
z-index: 1;
top: 50px; top: 50px;
} }
@ -37,34 +34,29 @@ body {
border-radius: 5px; border-radius: 5px;
display: flex; display: flex;
align-items: center; align-items: center;
background-color: #272C4F; background-color: var(--choice);
color: white; color: white;
order: 0; order: 0;
} }
.selected::after { .select-box .selected::after {
/* Font Awesome */ content: "";
content: "\f078"; width: 1.2rem;
font-family: "Font Awesome 5 Free"; height: 1.2rem;
font-weight: 900; background: url(../assets/down-arrow.svg);
-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; position: absolute;
right: 15px; right: 15px;
top: 50%; top: 50%;
transition: transform 150ms; transition: transform 150ms;
transform: translateY(-50%); transform: translateY(-50%);
background-size: contain;
background-position: center;
} }
.select-box .option .item { .select-box .option .item {
color: #AFB5DD; color: var(--text);
font-weight: bold;
} }
.select-box .options-container.active { .select-box .options-container.active {
@ -90,17 +82,20 @@ body {
background: #81878f; background: #81878f;
border-radius: 0 5px 5px 0; border-radius: 0 5px 5px 0;
} }
.select-box .option {
padding: 12px 15px;
}
.select-box .option, .select-box .option,
.selected { .selected {
padding: 12px 15px;
cursor: pointer; cursor: pointer;
} }
.select-box .option:hover { .select-box .options-container .option:hover {
background: #2F345B; background: var(--choice-hover);
} }
.select-box .option:hover .item { .select-box .options-container .option:hover .item {
color: white; color: var(--choice-active-text, var(--text));
} }
.select-box label { .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)) { 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 [active, setActive] = useState(false)
const containerRef = useRef(); const containerRef = useRef();
@ -41,7 +40,7 @@ export function SelectBox({ options }) {
useEffect(() => { useEffect(() => {
// add when mounted // add when mounted
document.addEventListener("mousedown", handleClick); document.addEventListener("mousedown", handleClick);
document.addEventListener("scroll", closeDropdown); //document.addEventListener("scroll", closeDropdown);
// return function to be called when unmounted // return function to be called when unmounted
return () => { return () => {
document.removeEventListener("mousedown", handleClick); 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) setSelectedItem(i)
closeDropdown() closeDropdown()
} }
return ( return (
<div className="select-box" ref={containerRef}> <div className="select-box" ref={containerRef} onClick={() => setActive(a => !a)}>
<div className={"options-container" + (active ? " active" : "")}> <div className={"options-container" + (active ? " active" : "")}>
{options.map((opt, i) => ( {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>
<div className="selected" onClick={() => setActive(a => !a)}> <div className="selected">
{options ? ( {options ? (
<Option option={options[selectedItem]} /> <Option option={options[selectedItem]} />
) : null} ) : null}