Here, we have a simple React application that renders a div with the particular content to the document. We are going to bring in the style function from TypeStyle, and then we are going to use the style function to generate a CSS class that sets the color to red.
We'll go ahead and assign the CSS class to the root div that is rendered by ReactDOM. The style function allows you to pass in as many style objects as you want and internally merges them all together before generating the CSS class. For example, we can pass in another style object that changes the font size to be 30 pixels.
Supporting multiple objects within the style function allows you to easily move reusable styles out of the style function into their own variables. For example, we can move the font-size object into its own variable, and then reuse it whenever we want the same font size. At this point, font-size is essentially a mixin.
You can even make utility functions out of such things in your code. As an example, we can convert the font-size variable into a function that takes a value of type number, and returns an object with the font-size property as desired. We finally call this function with the desired value for a particular class.
If the value passed in is a string, we assume it already has the correct unit and use it as it is. Otherwise, for numbers, we assume that the intended unit is pixels and add that when converting it to a string. Finally, we return our mixin, which contains the font-size CSS property.