Skip to content
Snippets Groups Projects
Commit a455c6d4 authored by Aanand Prasad's avatar Aanand Prasad Committed by Jure
Browse files

refactor(ui): refactor Menu CSS

Pass the 'open' prop into child elements that styled based on it. This is both more idiomatic for styled components and works around a bug that was making the snapshot tests volatile.
parent 087013f8
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,10 @@ import styled from 'styled-components' ...@@ -4,6 +4,10 @@ import styled from 'styled-components'
// TODO: match the width of the container to the width of the widest option? // TODO: match the width of the container to the width of the widest option?
// TODO: use a <select> element instead of divs? // TODO: use a <select> element instead of divs?
const Root = styled.div`
font-size: 1em;
`
const Label = styled.span` const Label = styled.span`
display: block; display: block;
font-size: 1em; font-size: 1em;
...@@ -20,6 +24,15 @@ const Opener = styled.button.attrs({ ...@@ -20,6 +24,15 @@ const Opener = styled.button.attrs({
cursor: pointer; cursor: pointer;
font-family: inherit; font-family: inherit;
font-size: 1.2em; font-size: 1.2em;
border-left: 2px solid
${props => (props.open ? 'var(--color-primary)' : 'lightgrey')};
color: ${props => (props.open ? 'var(--color-primary)' : 'inherit')};
&:hover {
border-left: 2px solid var(--color-primary);
color: var(--color-primary);
}
` `
const Placeholder = styled.span` const Placeholder = styled.span`
...@@ -27,6 +40,11 @@ const Placeholder = styled.span` ...@@ -27,6 +40,11 @@ const Placeholder = styled.span`
font-style: italic; font-style: italic;
font-weight: 400; font-weight: 400;
text-transform: normal; text-transform: normal;
color: #aaa;
&:hover {
color: var(--color-primary);
}
` `
const Arrow = styled.span` const Arrow = styled.span`
...@@ -34,6 +52,7 @@ const Arrow = styled.span` ...@@ -34,6 +52,7 @@ const Arrow = styled.span`
font-size: 50%; font-size: 50%;
margin-left: 10px; margin-left: 10px;
transition: transform 0.2s; transition: transform 0.2s;
transform: scaleX(2.2) scaleY(${props => (props.open ? -1.2 : 1.2)});
` `
const Main = styled.div.attrs({ const Main = styled.div.attrs({
...@@ -58,6 +77,9 @@ const Options = styled.div` ...@@ -58,6 +77,9 @@ const Options = styled.div`
transition: opacity 2s; transition: opacity 2s;
width: 0; width: 0;
z-index: 10; z-index: 10;
min-width: ${props => (props.open ? '10em' : '0')};
opacity: ${props => (props.open ? '1' : '0')};
` `
const Option = styled.div.attrs({ const Option = styled.div.attrs({
...@@ -77,38 +99,6 @@ const Option = styled.div.attrs({ ...@@ -77,38 +99,6 @@ const Option = styled.div.attrs({
} }
` `
const Root = styled.div`
font-size: 1em;
${Opener} {
border-left: 2px solid
${props => (props.open ? 'var(--color-primary)' : 'lightgrey')};
color: ${props => (props.open ? 'var(--color-primary)' : 'inherit')};
${Placeholder} {
color: #aaa;
}
}
${Opener}:hover {
border-left: 2px solid var(--color-primary);
color: var(--color-primary);
${Placeholder} {
color: var(--color-primary);
}
}
${Arrow} {
transform: scaleX(2.2) scaleY(${props => (props.open ? -1.2 : 1.2)});
}
${Options} {
min-width: ${props => (props.open ? '10em' : '0')};
opacity: ${props => (props.open ? '1' : '0')};
}
`
/* Not used for now /* Not used for now
.inline { .inline {
align-items: flex-end; align-items: flex-end;
...@@ -182,19 +172,19 @@ class Menu extends React.Component { ...@@ -182,19 +172,19 @@ class Menu extends React.Component {
<Main> <Main>
<OpenerContainer> <OpenerContainer>
<Opener onClick={this.toggleMenu}> <Opener onClick={this.toggleMenu} open={open}>
{selected ? ( {selected ? (
<span>{this.optionLabel(selected)}</span> <span>{this.optionLabel(selected)}</span>
) : ( ) : (
<Placeholder>{placeholder}</Placeholder> <Placeholder>{placeholder}</Placeholder>
)} )}
<Arrow></Arrow> <Arrow open={open}></Arrow>
</Opener> </Opener>
</OpenerContainer> </OpenerContainer>
<OptionsContainer> <OptionsContainer>
{open && ( {open && (
<Options> <Options open={open}>
{options.map(option => ( {options.map(option => (
<Option <Option
active={option.value === selected} active={option.value === selected}
......
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Menu is rendered correctly 1`] = ` exports[`Menu is rendered correctly 1`] = `
.c3 { .c0 {
font-size: 1em;
}
.c2 {
background: transparent; background: transparent;
border: none; border: none;
cursor: pointer; cursor: pointer;
font-family: inherit; font-family: inherit;
font-size: 1.2em; font-size: 1.2em;
border-left: 2px solid lightgrey;
color: inherit;
} }
.c5 { .c2:hover {
border-left: 2px solid var(--color-primary);
color: var(--color-primary);
}
.c3 {
display: inline-block; display: inline-block;
font-size: 50%; font-size: 50%;
margin-left: 10px; margin-left: 10px;
-webkit-transition: -webkit-transform 0.2s; -webkit-transition: -webkit-transform 0.2s;
-webkit-transition: transform 0.2s; -webkit-transition: transform 0.2s;
transition: transform 0.2s; transition: transform 0.2s;
-webkit-transform: scaleX(2.2) scaleY(1.2);
-ms-transform: scaleX(2.2) scaleY(1.2);
transform: scaleX(2.2) scaleY(1.2);
} }
.c1 { .c1 {
position: relative; position: relative;
} }
.c6 { .c4 {
position: absolute; position: absolute;
} }
.c0 {
font-size: 1em;
}
.c0 .c2 {
border-left: 2px solid lightgrey;
color: inherit;
}
.c0 .c2 .sc-bxivhb {
color: #aaa;
}
.c0 .c2:hover {
border-left: 2px solid var(--color-primary);
color: var(--color-primary);
}
.c0 .c2:hover .sc-bxivhb {
color: var(--color-primary);
}
.c0 .c4 {
-webkit-transform: scaleX(2.2) scaleY(1.2);
-ms-transform: scaleX(2.2) scaleY(1.2);
transform: scaleX(2.2) scaleY(1.2);
}
.c0 .sc-gzVnrw {
min-width: 0;
opacity: 0;
}
<div <div
className="c0" className="c0"
open={false} open={false}
...@@ -71,54 +52,70 @@ exports[`Menu is rendered correctly 1`] = ` ...@@ -71,54 +52,70 @@ exports[`Menu is rendered correctly 1`] = `
className="" className=""
> >
<button <button
className="c2 c3" className="c2"
onClick={[Function]} onClick={[Function]}
open={false}
type="button" type="button"
> >
<span> <span>
Foo Foo
</span> </span>
<span <span
className="c4 c5" className="c3"
open={false}
> >
</span> </span>
</button> </button>
</div> </div>
<div <div
className="c6" className="c4"
/> />
</div> </div>
</div> </div>
`; `;
exports[`Menu is rendered correctly when open 1`] = ` exports[`Menu is rendered correctly when open 1`] = `
.c3 { .c0 {
font-size: 1em;
}
.c2 {
background: transparent; background: transparent;
border: none; border: none;
cursor: pointer; cursor: pointer;
font-family: inherit; font-family: inherit;
font-size: 1.2em; font-size: 1.2em;
border-left: 2px solid var(--color-primary);
color: var(--color-primary);
} }
.c5 { .c2:hover {
border-left: 2px solid var(--color-primary);
color: var(--color-primary);
}
.c3 {
display: inline-block; display: inline-block;
font-size: 50%; font-size: 50%;
margin-left: 10px; margin-left: 10px;
-webkit-transition: -webkit-transform 0.2s; -webkit-transition: -webkit-transform 0.2s;
-webkit-transition: transform 0.2s; -webkit-transition: transform 0.2s;
transition: transform 0.2s; transition: transform 0.2s;
-webkit-transform: scaleX(2.2) scaleY(-1.2);
-ms-transform: scaleX(2.2) scaleY(-1.2);
transform: scaleX(2.2) scaleY(-1.2);
} }
.c1 { .c1 {
position: relative; position: relative;
} }
.c6 { .c4 {
position: absolute; position: absolute;
} }
.c8 { .c5 {
background-color: white; background-color: white;
border-bottom: 2px solid var(--color-primary); border-bottom: 2px solid var(--color-primary);
border-left: 2px solid var(--color-primary); border-left: 2px solid var(--color-primary);
...@@ -131,9 +128,11 @@ exports[`Menu is rendered correctly when open 1`] = ` ...@@ -131,9 +128,11 @@ exports[`Menu is rendered correctly when open 1`] = `
transition: opacity 2s; transition: opacity 2s;
width: 0; width: 0;
z-index: 10; z-index: 10;
min-width: 10em;
opacity: 1;
} }
.c9 { .c6 {
color: black; color: black;
font-weight: 600; font-weight: 600;
cursor: pointer; cursor: pointer;
...@@ -142,11 +141,11 @@ exports[`Menu is rendered correctly when open 1`] = ` ...@@ -142,11 +141,11 @@ exports[`Menu is rendered correctly when open 1`] = `
white-space: nowrap; white-space: nowrap;
} }
.c9:hover { .c6:hover {
color: var(--color-primary); color: var(--color-primary);
} }
.c10 { .c7 {
color: #444; color: #444;
font-weight: inherit; font-weight: inherit;
cursor: pointer; cursor: pointer;
...@@ -155,72 +154,10 @@ exports[`Menu is rendered correctly when open 1`] = ` ...@@ -155,72 +154,10 @@ exports[`Menu is rendered correctly when open 1`] = `
white-space: nowrap; white-space: nowrap;
} }
.c10:hover { .c7:hover {
color: var(--color-primary);
}
.gQZhbO .c2 {
border-left: 2px solid lightgrey;
color: inherit;
}
.gQZhbO .c2 .sc-bxivhb {
color: #aaa;
}
.gQZhbO .c2:hover {
border-left: 2px solid var(--color-primary);
color: var(--color-primary);
}
.gQZhbO .c2:hover .sc-bxivhb {
color: var(--color-primary);
}
.gQZhbO .c4 {
-webkit-transform: scaleX(2.2) scaleY(1.2);
-ms-transform: scaleX(2.2) scaleY(1.2);
transform: scaleX(2.2) scaleY(1.2);
}
.gQZhbO .c7 {
min-width: 0;
opacity: 0;
}
.c0 {
font-size: 1em;
}
.c0 .c2 {
border-left: 2px solid var(--color-primary);
color: var(--color-primary); color: var(--color-primary);
} }
.c0 .c2 .sc-bxivhb {
color: #aaa;
}
.c0 .c2:hover {
border-left: 2px solid var(--color-primary);
color: var(--color-primary);
}
.c0 .c2:hover .sc-bxivhb {
color: var(--color-primary);
}
.c0 .c4 {
-webkit-transform: scaleX(2.2) scaleY(-1.2);
-ms-transform: scaleX(2.2) scaleY(-1.2);
transform: scaleX(2.2) scaleY(-1.2);
}
.c0 .c7 {
min-width: 10em;
opacity: 1;
}
<div <div
className="c0" className="c0"
open={true} open={true}
...@@ -233,29 +170,32 @@ exports[`Menu is rendered correctly when open 1`] = ` ...@@ -233,29 +170,32 @@ exports[`Menu is rendered correctly when open 1`] = `
className="" className=""
> >
<button <button
className="c2 c3" className="c2"
onClick={[Function]} onClick={[Function]}
open={true}
type="button" type="button"
> >
<span> <span>
Foo Foo
</span> </span>
<span <span
className="c4 c5" className="c3"
open={true}
> >
</span> </span>
</button> </button>
</div> </div>
<div <div
className="c6" className="c4"
> >
<div <div
className="c7 c8" className="c5"
open={true}
> >
<div <div
aria-selected={true} aria-selected={true}
className="c9" className="c6"
onClick={[Function]} onClick={[Function]}
onKeyPress={[Function]} onKeyPress={[Function]}
role="option" role="option"
...@@ -265,7 +205,7 @@ exports[`Menu is rendered correctly when open 1`] = ` ...@@ -265,7 +205,7 @@ exports[`Menu is rendered correctly when open 1`] = `
</div> </div>
<div <div
aria-selected={false} aria-selected={false}
className="c10" className="c7"
onClick={[Function]} onClick={[Function]}
onKeyPress={[Function]} onKeyPress={[Function]}
role="option" role="option"
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment