If you pass an array of objects as the first argument to the css function, the objects will be expanded (as in the case of bg) then deep merged. Only the “surviving” property/value pairs are used.
Atomic Classes
Each property/value pair creates an atomic class name by default. You’ll notice that regardless of the order that the rules are listed, they always result in the same order in the stylesheet. That means that all of the textAlign properties will always be together, which makes it possible to audit the styles by just reading. This is a live editor, try changing some of the values for maxWidth or fontSize here and see what the effect is immediately. Switch to the HTML tab and notice that the class name for that property is recalculated based on the value, but the two letter prefix that is based on the property does not change.
Grouped Classes
But if you prefer to have fewer class names attached to elements, that is an option, too. Just wrap the styles in an object with as the key. Depending on your needs, you could choose to group styles together under a single class name or individual classes depending on what your needs are. If you’re planning on re-using a group of styles throughout your project, you’ll want to group them with &. It results in a slightly larger stylesheet, but fewer class names in the HTML.
Media queries can be nested or inverted. This is seriously powerful feature. If you’re creating a design to switch between light mode and dark mode, you can keep the values for each at the “bottom” of the style object under the CSS property it affects. This provides the flexibility to create a media query at any point without enforcing a specific structure.
All selectors are supported. Just be cautioned. This could be considered a bad practice. Just because you can doesn’t mean you should. You’ll notice that when global selectors are used, class names for those styles are not emitted to the element just added to the stylesheet.
Nesting Selector
Placeholder Classes (%)
Smart Integers
Converts integers to px values, except for those where it wouldn’t be correct. As examples, lineHeight, order, and fontWeight.
Fallback Array Values
Fallbacks use arrays.
Production Ready
@ptb/style is the most flexible tool kit available to style your web app. Add styles dynamically at runtime in the browser, server-side render and insert styles into the page <head>, or extract styles to a CSS file during server-side rendering or at build-time.
Single-Page or Client-Side
Without any setup, it can be used immediately to add styles and class names in any single-page application or client-side rendered app.
npm install @ptb/style
or
yarn add @ptb/style
Simply add to your project, then import it in each file where it is used. No container, context, Babel configuration, or webpack plugins required.
Run the gatsby build command and styles will be extracted to src/styles.css and the css function call will be replaced with the associated class names.
Insert Styles Into <head>
To prevent a flash of unstyled content (FOUC), you can use the included Gatsby plugin to inline the styles in the HTML <head>. Just add the plugin to the gatsby-config.js file.
Using Tailwind with @ptb/style is possibly simpler than Tailwind on its own! The Babel macro twin.macro generates a JavaScript object that is perfect as input for the css function.
Then, in each component file, import from twin.macro and call Tailwind CSS classes as a tagged template. At build time, the tw`` expression will be replaced by a JavaScript object representing those styles.
Note: The twin.macro import must be listed before the @ptb/style/macro import.