Documentation
On this page
Install Cirth for React
A React app built with Vite, as React's own documentation suggests for a new app, imports CSS from JavaScript. Cirth is one import where the app mounts. For Next.js, React Router and the other React frameworks, see their own guides.
Four steps: install, import it, set the CSS target and check it.
1 Install #
npm install @cirthcss/cirth
pnpm add @cirthcss/cirth
yarn add @cirthcss/cirth
bun add @cirthcss/cirth
2 Import it #
At the top of src/main.jsx, before the starter's index.css, or in its place:
import "@cirthcss/cirth";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";
createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>,
);
JSX writes className for class; nothing else changes. A custom property
set inline rethemes a subtree: style={{ "--cirth-primary": "#2563eb" }}.
3 Set the CSS target #
Vite compiles CSS for browsers older than Cirth supports. That process
rewrites light-dark() colours. The page can still follow the system theme,
but a data-theme region may fail to force the other colour scheme. Set
Vite to Cirth's browser floor below. Compatibility
explains the transformation and its limits.
Add build.cssTarget in vite.config.js, beside the plugins already there:
import { defineConfig } from "vite";
export default defineConfig({
build: {
cssTarget: ["chrome123", "edge123", "firefox130", "safari18.2"],
},
// the starter's plugins stay as they are
});
4 Check it #
Put this forced-dark region in a page or component that uses the global stylesheet:
<section data-theme="dark">
<article>
<p>Dark, whatever the system scheme is.</p>
</article>
</section>
Build for production, then open
the page while the system uses a light theme. A dark article proves that
Cirth loaded and that data-theme="dark" is working. It remains dark when
the system switches to a dark theme. If the article follows
the surrounding page instead, the build rewrote Cirth's colours: check the CSS target above.
Customization covers the --cirth-* properties; set
yours in a stylesheet imported after Cirth. Package and builds lists the classless,
scoped and print entry points. Return to Framework guides
for another stack.