From ef28008482de8c4c54af9a67326e75c9246a1c91 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Tue, 26 Oct 2021 13:17:11 +0100 Subject: [PATCH] Pull state out of select box into parent and fix styling --- src/assets/down-arrow.svg | 1 + src/components/SelectBox.css | 55 ++++++++++++++++-------------------- src/components/SelectBox.js | 16 +++++------ 3 files changed, 34 insertions(+), 38 deletions(-) create mode 100644 src/assets/down-arrow.svg diff --git a/src/assets/down-arrow.svg b/src/assets/down-arrow.svg new file mode 100644 index 00000000..619f62aa --- /dev/null +++ b/src/assets/down-arrow.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/components/SelectBox.css b/src/components/SelectBox.css index c04b13e5..d33f2413 100644 --- a/src/components/SelectBox.css +++ b/src/components/SelectBox.css @@ -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 { diff --git a/src/components/SelectBox.js b/src/components/SelectBox.js index 3d0e6acc..0b0ab01b 100644 --- a/src/components/SelectBox.js +++ b/src/components/SelectBox.js @@ -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 ( -
+
setActive(a => !a)}>
{options.map((opt, i) => ( -
-
setActive(a => !a)}> +
{options ? (