Skip to content

Commit 0c8eb30

Browse files
authored
chore: add css-containers and css-light-dark skills (#211)
* chore: add css-containers and css-light-dark skills Move the light and dark theme rules out of the ui-component skill into css-light-dark, which also shows how to keep `light-dark()` native through Lightning CSS, and point ui-component to both new skills. * chore: point ui-component to css-mixin and write doc comments in css-containers * chore: drop references to other skills from css-containers and ui-component * chore: show a server-rendered site's theme switch in css-light-dark skill
1 parent 06718da commit 0c8eb30

4 files changed

Lines changed: 315 additions & 43 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Scripts and configs for TrigenSoftware's projects.
2525
- [`flat-layers-design`](skills/flat-layers-design/SKILL.md)
2626
- [`eslint-to-oxlint`](skills/eslint-to-oxlint/SKILL.md)
2727
- [`css-mixin`](skills/css-mixin/SKILL.md)
28+
- [`css-containers`](skills/css-containers/SKILL.md)
29+
- [`css-light-dark`](skills/css-light-dark/SKILL.md)
2830
- [`ui-component`](skills/ui-component/SKILL.md)
2931
- [`react-component`](skills/react-component/SKILL.md)
3032
- [`nanoviews-component`](skills/nanoviews-component/SKILL.md)

‎skills/css-containers/SKILL.md‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
name: css-containers
3+
description: Pattern for named CSS container queries in a CSS Modules codebase — every container name is a class in one shared CSS Module, the only place that declares `container:`, put on the element that is the container; parts query the name with `@container`. Covers why the names are global tokens, why one file declares them all, and when a container stays unnamed. Apply when adding a container query, a container, or a part that follows its parent's width.
4+
license: MIT
5+
compatibility:
6+
- Claude Code
7+
- Codex
8+
- Cursor
9+
- Gemini CLI
10+
- GitHub Copilot
11+
- Windsurf
12+
- Cline
13+
- Roo Code
14+
- Goose
15+
- Continue
16+
- OpenCode
17+
- Amp
18+
- universal
19+
metadata:
20+
author: dangreen
21+
tags:
22+
- css
23+
- css-modules
24+
- container-queries
25+
- ui
26+
---
27+
28+
# CSS Containers
29+
30+
A named container query is a dependency between stylesheets: one element is the container, and the parts inside it query its width. In a CSS Modules codebase the names are tokens, as colors and sizes are:
31+
32+
- every container name is a class in one shared CSS Module, e.g. `containers.module.css`, and that file is the only place that declares `container:`;
33+
- to make an element a container, add its class to the element;
34+
- a part that follows the width queries the name with `@container`.
35+
36+
## Example
37+
38+
The container names, each with what carries it and what reads it:
39+
40+
```css
41+
/* containers.module.css */
42+
43+
/**
44+
* A grid of tiles, `Grid variant='tiles'`. Read by Grid and by the tiles,
45+
* which close up at 420 or less.
46+
*/
47+
48+
.tiles {
49+
container: tiles / inline-size;
50+
}
51+
52+
/**
53+
* A page's row of a search, its filters and its actions. Read by the filter's
54+
* trigger, which chooses its words by the room its row leaves.
55+
*/
56+
57+
.toolbar {
58+
container: toolbar / inline-size;
59+
}
60+
```
61+
62+
The component that is the container puts the class on itself:
63+
64+
```tsx
65+
import clsx from 'clsx'
66+
import containers from '~/uikit/containers.module.css'
67+
import styles from './Grid.module.css'
68+
69+
export function Grid({
70+
className,
71+
variant = 'fields',
72+
children,
73+
...props
74+
}) {
75+
return (
76+
<div
77+
className={clsx(styles.root, styles[variant], variant === 'tiles' && containers.tiles, className)}
78+
{...props}
79+
>
80+
{children}
81+
</div>
82+
)
83+
}
84+
```
85+
86+
A part queries the name in its own stylesheet:
87+
88+
```css
89+
/* ChoiceCard.module.css */
90+
@container tiles (max-width: 420px) {
91+
.tile .text {
92+
font-size: var(--sizeFont12);
93+
}
94+
}
95+
```
96+
97+
Whoever lays out a row makes it a container the same way, for the components in it to follow:
98+
99+
```tsx
100+
<div className={clsx(styles.root, containers.toolbar, className)}>
101+
{/* the search, the filter, the actions */}
102+
</div>
103+
```
104+
105+
## Rules
106+
107+
- **The names are global.** A query reads a name declared in another file, and CSS Modules has no way to import one: Lightning CSS supports neither `@value` nor ICSS `:import`/`:export`, its `from` references cover classes (`composes`) and dashed idents (`var(--x from './file.css')`) alone, and a query's prelude takes no `var()`. Turn off the hashing of container names where the implementation does it, e.g. Lightning CSS's `cssModules: { container: false }`, which Vite takes as `css.lightningcss.cssModules`. Hashed, a name declared in one module and queried in another silently matches nothing.
108+
- **A name is a token, named by its role:** `tiles`, `toolbar`, `search-band`, `card-body`. Never after one component (`media-card`) and never after a page. The name is kebab-case, as stylelint's `value-keyword-case` keeps a keyword lower-case, and its class is the camelCase form CSS Modules want: `.cardBody` makes `card-body`.
109+
- **The shared file is the registry.** Each class says what the container is, what carries it and what reads it. A new name goes there first; nothing writes `container:` anywhere else. A component thus depends on the shared file alone, as on the design tokens, never on a name its consumer made up.
110+
- **One container class per element.** `container` is one property, so a second class on the same element overrides the first. An element that must be two containers gets one class in the shared file that names both: `container: toolbar search-band / inline-size`.
111+
- **An unnamed container stays local.** An element that measures itself for its own parts alone takes `container-type: inline-size` in its own stylesheet, with no name. An unnamed `@container (…)` query and the `cqi`/`cqw` units read the nearest container, whatever its name, so they serve that element's own parts alone; whatever another module queries gets a name, and so a class in the shared file.
112+
- **Containment has a cost.** `inline-size` containment makes the element's inline size independent of its content: a container is an element whose width comes from its parent, a grid track or a block-level row, never one that should shrink to its content.

‎skills/css-light-dark/SKILL.md‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
---
2+
name: css-light-dark
3+
description: Light and dark theme in native CSS — color tokens as `light-dark()` pairs on `:root` with `color-scheme`, so the site follows the system; keeping `light-dark()` native through the build; a theme switch that forces `color-scheme` on the root, keeps the choice in `localStorage` and sets it from an inline script in the head, so a server-rendered page opens in the chosen theme; pictures drawn for one theme; and a theme class with a palette of its own for widgets on someone else's page. Apply when defining color tokens, adding a dark theme, or wiring a theme switch.
4+
license: MIT
5+
compatibility:
6+
- Claude Code
7+
- Codex
8+
- Cursor
9+
- Gemini CLI
10+
- GitHub Copilot
11+
- Windsurf
12+
- Cline
13+
- Roo Code
14+
- Goose
15+
- Continue
16+
- OpenCode
17+
- Amp
18+
- universal
19+
metadata:
20+
author: dangreen
21+
tags:
22+
- css
23+
- theme
24+
- dark-mode
25+
- color-scheme
26+
- ui
27+
---
28+
29+
# CSS Light and Dark
30+
31+
- Make the tokens `light-dark(<light>, <dark>)` pairs on `:root` with `color-scheme: light dark`, so the site follows the system. A `color-scheme` set on the root element forces one scheme, which is how the Storybook toolbar switches it:
32+
33+
```css
34+
/* uikit/variables.css */
35+
:root {
36+
--colorBackground: light-dark(#f2f2f4, #1d1d1f);
37+
--colorText: light-dark(#1d1d1f, #ececee);
38+
/* light-dark() takes colors alone: a shadow gets one for each of its colors */
39+
--shadowCard: 0 1px 2px light-dark(rgb(0 0 0 / 12%), rgb(0 0 0 / 40%));
40+
/* ... */
41+
42+
color-scheme: light dark;
43+
}
44+
```
45+
46+
- Don't put tokens behind `@media (prefers-color-scheme: dark)`: they follow the system and nothing else, and no forced scheme reaches them.
47+
- Keep `light-dark()` native in the built CSS: lightningcss compiles it for older targets into variables switched by `prefers-color-scheme`, which a forced `color-scheme` can't reach. Exclude the feature from its transforms:
48+
49+
```js
50+
// vite.config.js
51+
import { Features } from 'lightningcss'
52+
53+
export default {
54+
css: {
55+
transformer: 'lightningcss',
56+
lightningcss: {
57+
exclude: Features.LightDark
58+
}
59+
}
60+
}
61+
```
62+
63+
## A theme switch
64+
65+
A site's own theme switch forces the scheme on the root the same way, so the tokens stay `light-dark()` pairs. The choice is the browser's, kept in `localStorage`, never in a cookie or the account: the server renders one page for everyone, and a visitor without an account can choose too. The page's head sets it before the stylesheets arrive, so a server-rendered page opens in the chosen theme, never in the system's first:
66+
67+
```html
68+
<head>
69+
<!-- The browser paints its canvas and controls in the system's theme before the stylesheets come -->
70+
<meta name="color-scheme" content="light dark">
71+
<!-- The theme chosen; a browser that refuses storage throws, and keeps the system's -->
72+
<script>
73+
try {
74+
const theme = localStorage.getItem('theme')
75+
76+
if (theme === 'light' || theme === 'dark') {
77+
document.documentElement.dataset.theme = theme
78+
document.documentElement.style.colorScheme = theme
79+
}
80+
} catch {}
81+
</script>
82+
<link rel="stylesheet" href="/styles.css">
83+
</head>
84+
```
85+
86+
The switch keeps the choice and sets it the same way. System sets nothing, and the root's `color-scheme: light dark` follows the device again:
87+
88+
```ts
89+
// theme.ts
90+
export type Theme = 'system' | 'light' | 'dark'
91+
92+
// The key the head's script reads too
93+
const THEME_KEY = 'theme'
94+
95+
/**
96+
* Sets a theme on the root: `color-scheme` picks each token's half, and `data-theme` serves what
97+
* a token can't switch, such as a picture drawn for one theme.
98+
* @param theme - The theme; anything but `light` and `dark` is the system's.
99+
*/
100+
export function applyTheme(theme: string | null) {
101+
const root = document.documentElement
102+
103+
if (theme === 'light' || theme === 'dark') {
104+
root.dataset.theme = theme
105+
root.style.colorScheme = theme
106+
} else {
107+
root.removeAttribute('data-theme')
108+
root.style.removeProperty('color-scheme')
109+
}
110+
}
111+
112+
/**
113+
* Keeps the theme chosen and sets it.
114+
* @param theme - The theme chosen.
115+
*/
116+
export function setTheme(theme: Theme) {
117+
try {
118+
if (theme === 'system') {
119+
localStorage.removeItem(THEME_KEY)
120+
} else {
121+
localStorage.setItem(THEME_KEY, theme)
122+
}
123+
} catch {
124+
// A browser that refuses storage keeps the choice for this page alone
125+
}
126+
127+
applyTheme(theme)
128+
}
129+
130+
/**
131+
* Follows a theme chosen in another tab.
132+
* @returns Stops following.
133+
*/
134+
export function syncTheme() {
135+
function onStorage(event: StorageEvent) {
136+
if (event.key === THEME_KEY) {
137+
applyTheme(event.newValue)
138+
}
139+
}
140+
141+
window.addEventListener('storage', onStorage)
142+
143+
return () => window.removeEventListener('storage', onStorage)
144+
}
145+
```
146+
147+
A picture drawn in one theme's inks, such as a logo, has no token to switch: it comes twice, and the theme hides one of the two. With no theme chosen, the system's decides:
148+
149+
```css
150+
/* mixins.module.css */
151+
152+
/**
153+
* `hideDark` on a picture drawn for the light theme, `hideLight` on its twin for the dark one.
154+
*/
155+
156+
:root[data-theme='dark'] .hideDark,
157+
:root[data-theme='light'] .hideLight {
158+
display: none;
159+
}
160+
161+
@media (prefers-color-scheme: dark) {
162+
:root:not([data-theme]) .hideDark {
163+
display: none;
164+
}
165+
}
166+
167+
@media (prefers-color-scheme: light) {
168+
:root:not([data-theme]) .hideLight {
169+
display: none;
170+
}
171+
}
172+
```
173+
174+
- Nothing the server renders shows the choice: the server doesn't know it, and the browser's first render would differ from the HTML. A control that shows it, such as a menu's checked item, reads it in the browser, once the menu opens.
175+
- A framework that hydrates `<html>` finds two attributes there it didn't render: let it keep them, as React's `suppressHydrationWarning` on `<html>` does.
176+
- A color reads its token, never `data-theme`. The attribute is for what a token can't switch, a picture: a `<source media>` reads the system's theme alone, and an SVG picture's own `prefers-color-scheme` doesn't follow the page in Safari 18, which draws its light inks whatever the theme.
177+
178+
## A theme class
179+
180+
A theme class with a palette of its own is for exceptional cases, such as DevTools that sit on someone else's page and switch their own theme: the root is not theirs to set. Their tokens live on the class: the default palette on `.root`, the other one on a modifier that redefines the colors. Each sets its `color-scheme`, so native controls and scrollbars follow:
181+
182+
```css
183+
/* uikit/Theme/Theme.module.css */
184+
.root {
185+
--colorBackground: #1d1d1f;
186+
--colorText: #ececee;
187+
/* ... */
188+
189+
color-scheme: dark;
190+
color: var(--colorText);
191+
}
192+
193+
.light {
194+
--colorBackground: #f2f2f4;
195+
--colorText: #1d1d1f;
196+
/* ... */
197+
198+
color-scheme: light;
199+
}
200+
```

‎skills/ui-component/SKILL.md‎

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: ui-component
3-
description: Framework-agnostic rules for writing UI components — semantic HTML, CSS Modules conventions (root class, camelCase, CSS Custom Properties), light and dark theme tokens, and UX/accessibility requirements. Apply when creating, editing, or reviewing UI components, their markup, or styles.
3+
description: Framework-agnostic rules for writing UI components — semantic HTML, CSS Modules conventions (root class, camelCase, CSS Custom Properties), and UX/accessibility requirements. Apply when creating, editing, or reviewing UI components, their markup, or styles.
44
license: MIT
55
compatibility:
66
- Claude Code
@@ -73,48 +73,6 @@ metadata:
7373

7474
- Implement responsiveness via CSS, not JS (`userAgent`/`matchMedia`/…) — it keeps server-side rendering simple.
7575

76-
## Light and dark theme
77-
78-
- Make the tokens `light-dark(<light>, <dark>)` pairs on `:root` with `color-scheme: light dark`, so the site follows the system. A `color-scheme` set on the root element forces one scheme, which is how the Storybook toolbar switches it:
79-
80-
```css
81-
/* uikit/variables.css */
82-
:root {
83-
--colorBackground: light-dark(#f2f2f4, #1d1d1f);
84-
--colorText: light-dark(#1d1d1f, #ececee);
85-
/* light-dark() takes colors alone: a shadow gets one for each of its colors */
86-
--shadowCard: 0 1px 2px light-dark(rgb(0 0 0 / 12%), rgb(0 0 0 / 40%));
87-
/* ... */
88-
89-
color-scheme: light dark;
90-
}
91-
```
92-
93-
- Don't put tokens behind `@media (prefers-color-scheme: dark)`: they follow the system and nothing else, and no forced scheme reaches them.
94-
- Keep `light-dark()` native in the built CSS: lightningcss compiles it for older targets into variables switched by `prefers-color-scheme`, which a forced `color-scheme` can't reach.
95-
- A site's own theme switch forces the scheme on the root the same way, so the tokens stay `light-dark()` pairs.
96-
- A theme class with a palette of its own is for exceptional cases, such as DevTools that sit on someone else's page and switch their own theme: the root is not theirs to set. Their tokens live on the class: the default palette on `.root`, the other one on a modifier that redefines the colors. Each sets its `color-scheme`, so native controls and scrollbars follow:
97-
98-
```css
99-
/* uikit/Theme/Theme.module.css */
100-
.root {
101-
--colorBackground: #1d1d1f;
102-
--colorText: #ececee;
103-
/* ... */
104-
105-
color-scheme: dark;
106-
color: var(--colorText);
107-
}
108-
109-
.light {
110-
--colorBackground: #f2f2f4;
111-
--colorText: #1d1d1f;
112-
/* ... */
113-
114-
color-scheme: light;
115-
}
116-
```
117-
11876
## UX & A11y
11977

12078
- Apply core accessibility practices.

0 commit comments

Comments
 (0)