React

Cascade Layers (Experimental)

Learn how to control the priority of Crispy styles using CSS Cascade Layers.

When using Crispy components with CSS frameworks that use CSS Cascade Layers (like Tailwind CSS 4), the following configuration is required to override Crispy component styles with the framework's utility classes:

  • Use the Crispy CSS version wrapped with @layer directive
  • Set appropriate priority between layers

CSS Cascade Layers is supported in Chrome 99+, Safari 15.4+, Firefox 97+. (Can I Use)

Overview

By default, Crispy CSS is provided without @layer. Unlayered styles defined without the @layer directive always have higher priority than layered styles. (MDN)

/* Crispy component (unlayered) */
/* Has higher priority than layered styles */
.seed-floating-action-button__root {
  background: var(--seed-color-bg-brand-solid);
}

/* Tailwind utility (layered) */
/* When using class="seed-floating-action-button__root bg-red-100", Crispy style has priority */
@layer utilities {
  .bg-red-100 {
    background-color: var(--color-red-100);
  }
}

To solve this problem, Crispy provides CSS files separately wrapped with @layer.

Configuration

Use base.layered.css instead of base.css

style.css
@import "@seed-design/css/base.layered.css";

Customize layer order

Adjust the declaration order of seed-base and seed-components layers with other layers appropriately.

Tailwind CSS

When using Tailwind CSS 4 to override Crispy component styles with utility classes, the Tailwind utilities layer must have higher priority than the seed-components layer in the @layer declaration.

@layer theme, base, seed-base, components, seed-components, utilities;

Bundler configuration

Add the seed-layered condition to your bundler's module resolution configuration. This ensures that Crispy React components automatically use the layered version when importing CSS internally.

vite.config.ts
import { defineConfig } from "vite";

export default defineConfig({
  resolve: {
    conditions: ["seed-layered"], 
  },
  // ...
});

vite.config.ts

Learn about the Vite conditions option.

Layer order issues with Chunk Splitting

When a bundler splits CSS into multiple chunks, the chunk containing @layer seed-components { ... } may load before other @layer declarations, resulting in unintended layer priority.

You can inline the @layer declaration in HTML to ensure it loads before all <link> tags.

Use the transformIndexHtml plugin hook to inject a <style> tag at the beginning of <head>.

vite.config.ts
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    {
      name: "inject-layer-order",
      transformIndexHtml() {
        return [
          {
            tag: "style", 
            children:
              "@layer theme, base, seed-base, components, seed-components, utilities;", 
            injectTo: "head-prepend", 
          },
        ];
      },
    },
  ],
  // ...
});

Crispy layer structure

Crispy uses two layers.

NameContentFileNotes
seed-baseDesign tokens, global styles, keyframesbase.layered.cssRequires manual import
seed-componentsComponent stylesrecipes/*.layered.cssAutomatically imported with Crispy React components

Tailwind CSS

Learn how to use Crispy tokens as utility classes in Tailwind CSS.

Table of contents