Using React & Styled-Components.
How would you go about solving the following issue:
- A progress bar, red or green background based on %, empty area is grey
- Percentage text on the right of the progress bar (always one place)
- Text color changes based if the red or green background overlaps it
What I came up with:
export const BarContainer = styled.div`
width: 100%;
height: 30px;
background: grey;
border-radius: 16px;
`;
export const Bar = styled.div`
background-color: ${props => props.background ? props.background : 'red'};
width: ${props => (props.percent ? `${props.percent}%` : '0%')};
height: 100%;
transition: width 0.2s ease;
&:before {
color: #fff;
font-size: 14px;
content: ${props => (props.percent ? `'${props.percent}%'` : '0%')};
display: block;
text-align: right;
mix-blend-mode: difference;
}
`;
... some where in the code
<BarContainer>
<Bar percent={50} background="green" />
</BarContainer>
Issue is that mix-blend-mode: difference makes the text pink, not white on overlay. it should be white on overlay, black otherwise.