I have following component in React:
const Button = styled.div`
width: 30px;
height: 30px;
position: absolute;
right: 2em;
top: 50%;
transform: translateY(-50%);
padding: 0;
margin: 0;
&::before,
&::after {
content: "";
position: absolute;
background-color: #3d3935;
transition: transform 0.25s ease-out;
}
&::before {
top: 0;
left: 50%;
width: 4px;
height: 100%;
margin-left: -2px;
}
&::after {
top: 50%;
left: 0;
width: 100%;
height: 4px;
margin-top: -2px;
}
`;
It just renders a component with library Styled-components. It basically shows a +
sign.
But then, I would like to rotate each of the lines separately, using:
&::before {
transform: rotate(${this.state.expanded ? '0' : '45'}deg);
}
&::after {
transform: rotate(${this.state.expanded ? '0' : '135'}deg);
}
And it works great, but unfortunately there are no transitions (it happens immediately). Tried to include transitions in each of these lines, but it still doesn't affect the changes.
Another solution which I've tried was to add a class, e.g. rotated
:
&.rotated::before {
transform: rotate(45deg);
}
But styled components doesn't provide actually possibility to change classes dynamically using just it's logic.
Looking forward for any kind of help, thank you.
One way to dynamically change css properties with styled components is to insert a custom prop into your React component and access said property using the dollar sign and curly braces commonly used for template literals. Our current example is testing to see if our use pointer prop is true.
To pass props to React components created with styled-components, we can interpolate functions into the string that creates the component. We create the ArrowStyled component with the styled. div tag. Then string that we pass into the tag has the rotate value set from a prop.
To add active class using React styled-components, we can add props to the component. We create the keyframes with the keyframes tag and a keyframe string. Then we add the Button component with the styled. button tag.
Advantages of using Styled-components Below are some of benefits of using styled-components: Eliminates class name bugs: styled-components provide unique class names for your styles, thus eliminating the problems with class names duplications, misspellings, and overlaps.
You can try passing a conditional props to the component.
import styled, { css } from 'styled-components';
<Button expanded={ this.state.expanded } />
And then in you SC:
const Button = styled.div`
transform: rotate(0deg);
transition: transform .2s ease-out;
${ props => props.expanded && css`
transform: rotate(45deg);
`};
`;
You can also use a ternary operator:
const Button = styled.div`
transform: ${props => props.expanded ? 'rotate(180deg)' : 'rotate(0deg)'};
transition: transform .2s ease-out;
`;
Also make sure to define the Button outside the function you are using it in. Made that mistake right now, and didn't understand why the transition didn't work (tips for future me).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With