Cssta is a styling system for React Native 📱 that lets you define components using CSS.
It takes heavy inspiration from styled-components, but makes changes for readability 👀, to enable more features 🤙, and performance ⚡️.
Most notably, Cssta supports media queries, CSS animations and transitions, and CSS custom properties.
Performance-wise, Cssta compiles your CSS at compile time via a babel-plugin. Cssta is also able to perform Svelte-like stripping of its framework, and is able to build many components with no Cssta runtime.
import cssta from "cssta/native";const BlueView = cssta(View)` background: blue;`;<BlueView>I am a View with a blue background</BlueView>;
This returns a regular React component, which when used, will have the styling applied.
We have a repo with a whole bunch of examples if you want to see how it’s setup and examples of usage. It’s over on CsstaExample.
🔧 Setup
Cssta can be used with its own babel plugin, or can use babel-plugin-macros.
If you want to use babel-plugin-macros, change babel-plugin-cssta to babel-plugin-macros. You’ll then need to import the macro version of Cssta whenever you need to use it.
-import cssta from "cssta/native"+import cssta from "cssta/native.macro"
📝 CSS
Cssta supports all the CSS React Native supports, and has the same syntax as your browser.
We extend the attribute selector syntax in CSS. Now when your attribute name starts with an at symbol, we’ll query the React props. We call these prop selectors. You can use,
[@stringAttribute="stringValue"] for string props
[@booleanAttribute] for boolean props
You’ll always need the & selector when using prop selectors.
const Message = cssta(Text)` padding: 6px 12px; &[@large] { padding: 12px 18px; } &:not([@noOutline]) { border: 1px solid grey; } &[@priority="critical"] { background-color: red; } &[@priority="important"] { background-color: orange; }`;<Message large>Large Button with an Outline</Message>;<Message noOutline>Button with no Outline</Message>;<Message priority="critical">Red Button with an Outline</Message>
All properties defined in prop selectors are not passed down to the component—they’re really only for styling. All other props get passed down.
These work just as they do in CSS. We support min- and max-width and height, as well as orientation: portrait and orientation: landscape. We also support a non-standard platform, which queries whatever Platform.OS returns,
The best way to do theming in Cssta is by using CSS custom properties. Use them as in Cssta as you’d use them on the web, and they’ll just work.
const Inverted = cssta(View)` background-color: black; --primary: white;`;const ThemedText = cssta(Text)` color: var(--primary, black); border: 1px solid var(--primary, black); padding: 6px 12px;`;<ThemedText>I black text</ThemedText>;<Inverted> <ThemedText>I am white text on a black background!</ThemedText></Inverted>;
In addition to CSS custom properties, you can use JavaScript’s ${value} syntax to interpolate values. Note that you can only interpolate values or parts of values, and not entire rules or mixins. This is mostly useful for using platform constants.