1

I have a bit of a condrum I can't seem to solve I am querying colors from a graphQL database from DatoCMS and want to change the color of a Pseudo element in my Gatsby js app I can do so just fine like this with a regular selector

<p style={{color: pricing.packageBorderColor.hex}} className="price-session">
   <span>${pricing.priceAmount}</span> | <span>{pricing.lengthOfSession}</span>
</p>

However I am not sure how to introduce sudo selector like :after into the mix.

const ListItem = styled.li`
  list-style-type: none;
  font-size: 20px;
  display: inline-block;
  width: 330px;
  &:before {
    content: url(data:image/svg+xml,${encodeURIComponent(renderToString(<FontAwesomeIcon icon={faCheck} />))});
    width: 20px;
    display: block;
    float: left;
    position: absolute;
    margin-left: -31px;
    color: {pricing.packageBorderColor.hex} // This is what I'd ideally like to do, but doesnt seem doable
  }
  span{
    display:block;
    float:left; 
    margin-top:3px;
  }
`

I have thought maybe styled components and This works, however I then Can't add my variables because styled components seems to live outside there scope before my loop and react component.

Updated Attempt

const CircleSave = styled.div`
  &:after{
    background: ({color}) => color
  }

`

<CircleSave color={pricing.packageBorderColor.hex} className="circle-save">
   <p>${pricing.packageSavings}</p>
   <p>savings</p>
 </CircleSave>

I get the following error in my rendered css

.chrVyZ:after {
    background: ({color}) => color;
}
Anders Kitson
  • 1,413
  • 6
  • 38
  • 98

1 Answers1

1

You can use styled components passed props to pass props like this:

const ListItem = styled.li`
  list-style-type: none;
  font-size: 20px;
  display: inline-block;
  width: 330px;
  &:before {
    content: url(data:image/svg+xml,${encodeURIComponent(renderToString(<FontAwesomeIcon icon={faCheck} />))});
    width: 20px;
    display: block;
    float: left;
    position: absolute;
    margin-left: -31px;
    color: ${({ color }) => color}; // This is what I'd ideally like to do, but doesnt seem doable
  }
  span{
    display:block;
    float:left; 
    margin-top:3px;
  }
`

and then use it like normal component with a color prop:

<ListItem color={pricing.packageBorderColor.hex}/>
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • I tried your code, I must be doing something wrong, I updated my code above with my attempt, and the css error I got in inspect mode. Thanks – Anders Kitson Dec 13 '19 at 00:15
  • 1
    Forgot to add inline string template. It should be like this `color: ${({ color }) => color};`. I've updated my answer. – Christos Lytras Dec 13 '19 at 00:19