Skip to content
coatx
Esc
↑↓navigate↵open⌘Jpreview
On this page

StyleX compiler mode

Compose coatx tokens and style maps in Vite and Next.js apps with @stylexjs/babel-plugin.

Apps that already compile StyleX can skip the pre-compiled stylesheet and compose coatx tokens and style maps directly. Import tokens from coatx/tokens.stylex — the .stylex extension is required, because the Babel plugin only resolves token files ending in .stylex:

import * as stylex from "@stylexjs/stylex";
import { buttonStyles } from "coatx/button";
import { colors } from "coatx/tokens.stylex";

export const brandTheme = stylex.createTheme(colors, {
  primary: "oklch(0.62 0.21 255)",
  ring: "oklch(0.62 0.21 255)",
});

const custom = stylex.create({
  square: { borderRadius: 0 },
});

export const squarePrimaryButton = () =>
  stylex.attrs(buttonStyles.base, buttonStyles.variantDefault, custom.square);

Vite

Add the StyleX Babel plugin to your Vite config:

import stylex from "@stylexjs/babel-plugin";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    /* your framework plugin, with babel config pointing at @stylexjs/babel-plugin */
  ],
});

The standard setup is vite-plugin-babel (or your framework’s Babel integration) configured with @stylexjs/babel-plugin. See the StyleX Vite docs for the current plugin options.

Next.js

Configure the StyleX Next.js plugin, which wraps the Babel plugin for the App Router:

// next.config.js
const stylex = require("@stylexjs/nextjs-plugin");

module.exports = stylex({})({
  // your Next.js config
});

See the StyleX Next.js guide for version-specific setup.

Layered output (useLayers)

coatx’s pre-compiled styles.css is plain StyleX useLayers: true output: every rule sits in the standard @layer priority1 … priority7 layers. Compile your app with useLayers: true too. Your rules then join the same layers, so StyleX’s shorthand/longhand ordering holds across both stylesheets, and your CSS wins ties because it loads after coatx.

Do not compile your app with useLayers: false next to coatx. StyleX gives identical declarations identical class names, so your unlayered copy of an atom (say border-width: 1px) would outrank coatx’s layered longhands (border-bottom-width: 2px) on the same element.

Alongside Tailwind v4, put the StyleX layers between preflight and utilities:

@layer theme, base, priority1, priority2, priority3, priority4, priority5, priority6, priority7, components, utilities;
@import "tailwindcss";
@import "coatx/styles.css";

Plain unlayered app CSS always beats coatx.

Theming in compiler mode

stylex.createTheme(colors, {...}) produces the same --x... custom properties as the pre-compiled themes. Scope the returned class to any container, exactly like theme():

import { brandTheme } from "./brand-theme.css";

export function BrandPanel({ children }) {
  return <section {...stylex.props(brandTheme)}>{children}</section>;
}

For the full token group reference, see Theming.

Was this page helpful?