Documentation
On this page
Install Cirth for Lit
A Lit component renders into a shadow root, and a stylesheet in the document
does not reach inside one. A shadow root is an isolated document subtree.
Cirth therefore goes into each component's own styles, imported as text
and adopted there rather than loaded as global document CSS.
Four steps: install, adopt it in the component, 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 Adopt it in the component #
Import the stylesheet as a string with Vite's ?inline, and put it first in
static styles, before the component's own rules:
import cirth from "@cirthcss/cirth?inline";
import { LitElement, css, html, unsafeCSS } from "lit";
export class SignUp extends LitElement {
static styles = [
unsafeCSS(cirth),
css`
:host {
display: block;
}
`,
];
render() {
return html`
<article>
<label>Email <input type="email" required></label>
<button type="submit">Sign up</button>
</article>
`;
}
}
customElements.define("sign-up", SignUp);
The browser parses the sheet once and shares it between every instance.
unsafeCSS is safe here: the text is Cirth's file, not user input.
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.
import { defineConfig } from "vite";
export default defineConfig({
build: {
cssTarget: ["chrome123", "edge123", "firefox130", "safari18.2"],
},
});
4 Check it #
A shadow root takes its scheme from the system unless the component says
otherwise. A data-theme on the page around the component does not reach
inside, but one on the component's own element does: Cirth reads the
shadow host's attribute from within the shadow root.
<order-panel data-theme="dark"></order-panel>
An attribute on an element in the template themes just that part:
render() {
return html`
<section data-theme="dark">
<article><p>Dark, whatever the system scheme is.</p></article>
</section>
`;
}
Build for production: the component is dark on a light page. If it is not, check the CSS target above.
Customization covers the --cirth-* properties; set
yours in the component's own css after Cirth's, or as custom properties set on its element. Package and builds lists the classless,
scoped and print entry points. Return to Framework guides
for another stack.