Aphrodite is a library styling React components.
Learn how to use Aphrodite to style reusable React components.
Here's a simple react button component that we can click on. It's pretty ugly. Let's add some inline styles by creating this style object, and then we'll pass it in as style on our button. Now it's prettier, and it still works the same. Also because we're defining inline styles, they come along for free when anyone uses this component.
Now let's add in Aphrodite by importing StyleSheet and css from the aphrodite package. We take our styles object and wrap it in a call, so StyleSheet.create. We also add one level nesting to our styles object. Call everything button. Instead of assigning things to style, we'll use className.
We call our css function (styles.button). When we save, everything looks the exact same. If we inspect our element though, we notice that now we have a class instead of inline styles. Also if we look up in the head we see this data-aphrodite style tag, which contains styles that look suspiciously like what we defined here.
That's because when you call css and pass in a StyleSheet object, Aphrodite is parsing this code, turning it into css, and injecting it into the DOM. It also uses the object key to generate a unique class name, that it then shares and passes in as the className attribute.
Let's use css we already know, to add an active and a hover state to this button. We first define an active pseudo class, to add a boxShadow when someone's clicking on the button. It's beautiful. Next we'll add a hover pseudo class, so that when someone hovers on the button it turns colors.
Our button is a little easier to interact with now, and we get all the benefits of css like pseudo classes, along with the benefits of inline styles like encapsulation and avoiding selector wars and specificity problems.