Skip to content

Commit 9ada92b

Browse files
committed
docs: update AGENTS.md, consolidate changesets, add css-to-react-native peer dep
Consolidate RSC/HMR changesets. Update createTheme changeset with vars docs. Add css-to-react-native as optional peer dep (>= 3.2.0) to allow dedup to v4.
1 parent 836fb49 commit 9ada92b

7 files changed

Lines changed: 61 additions & 18 deletions

File tree

‎.changeset/feat-create-theme.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,24 @@ Returns an object with the same shape where every leaf is `var(--prefix-path, fa
1010
const theme = createTheme({ colors: { primary: '#0070f3' } });
1111
// theme.colors.primary → "var(--sc-colors-primary, #0070f3)"
1212
// theme.raw → original object
13+
// theme.vars.colors.primary → "--sc-colors-primary"
1314
// theme.resolve(el?) → computed values from DOM (client-only)
1415
// theme.GlobalStyle → component that emits CSS var declarations
1516
```
1617

18+
`vars` exposes bare CSS custom property names (same shape as the theme) for use in `createGlobalStyle` dark mode overrides without hand-writing variable names:
19+
20+
```ts
21+
const { vars } = createTheme({ colors: { bg: '#fff', text: '#000' } });
22+
23+
const DarkOverrides = createGlobalStyle`
24+
@media (prefers-color-scheme: dark) {
25+
:root {
26+
${vars.colors.bg}: #111;
27+
${vars.colors.text}: #eee;
28+
}
29+
}
30+
`;
31+
```
32+
1733
Options: `prefix` (default `"sc"`), `selector` (default `":root"`, use `":host"` for Shadow DOM).

‎.changeset/fix-hmr-stale-styles.md‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎.changeset/fix-rsc-style-injection.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎.changeset/rsc-support.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
'styled-components': minor
3+
---
4+
5+
React Server Components support: inline style injection, deduplication, and a new `stylisPluginRSC` for child-index selector fixes.
6+
7+
**Inline style injection:** RSC-rendered styled components emit `<style data-styled>` tags alongside their elements. CSS is deduplicated per render via `React.cache` (React 19+). Extended components use `:where()` zero-specificity wrapping on base CSS so extensions always win the cascade regardless of injection order.
8+
9+
**`StyleSheetManager` works in RSC:** `stylisPlugins` and `shouldForwardProp` are now applied in server component environments where React context is unavailable.
10+
11+
**`stylisPluginRSC`** — opt-in stylis plugin that fixes `:first-child`, `:last-child`, `:nth-child()`, and `:nth-last-child()` selectors broken by inline `<style>` tags shifting child indices. Rewrites them using CSS Selectors Level 4 `of S` syntax to exclude styled-components style tags from the count.
12+
13+
```jsx
14+
import { StyleSheetManager, stylisPluginRSC } from 'styled-components';
15+
16+
<StyleSheetManager stylisPlugins={[stylisPluginRSC]}>
17+
{children}
18+
</StyleSheetManager>
19+
```
20+
21+
The plugin rewrites `:first-child`, `:last-child`, `:nth-child()`, and `:nth-last-child()` using CSS Selectors Level 4 `of S` syntax to exclude injected style tags from the child count.
22+
23+
Browser support: Chrome 111+, Firefox 113+, Safari 9+ (~93% global). In unsupported browsers, the entire CSS rule is dropped — only opt in if your audience supports it. Use `:first-of-type` / `:nth-of-type()` as a universally compatible alternative.
24+
25+
**HMR:** Stale styles during client-side HMR are detected and invalidated when module re-evaluation creates new component instances while IDs remain stable (SWC plugin assigns IDs by file location). `createGlobalStyle` additionally clears stale sheet entries when the instance changes between renders.
26+
27+
The plugin is fully tree-shakeable — zero bytes in bundles that don't import it.

‎AGENTS.md‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NOTE: CLAUDE.md is a symlink to this file (AGENTS.md). Edit AGENTS.md directly.
1010

1111
- NEVER use `precedence` or `href` on `<style>` elements -- React 19 Float merges same-precedence tags, strips custom `data-*` attributes, and hoists to `<head>` where source ordering is unpredictable. RSC style tags must be plain inline `<style>` (server component output is not hydrated, so no mismatch).
1212
- The native entry must NEVER transitively import DOM code via value imports. Use `import type` and branded `Symbol.for()` checks instead of `instanceof`. Verify: `grep -c 'document\.' native/dist/styled-components.native.cjs.js` must be 0. RN/Hermes 0.79+ fails at module evaluation time on `document` references.
13-
- Server detection requires three mechanisms combined: `__SERVER__` (build-time constant), `IS_RSC` (module-level constant), and `styleSheet.server` (runtime flag from `ServerStyleSheet`). Always use `__SERVER__ || IS_RSC || ssc.styleSheet.server`. `IS_RSC` evaluates to `false` at runtime in React 19 RSC (`createContext` exists everywhere) but still serves as a build-time constant for dead-code elimination.
13+
- Server detection requires three mechanisms combined: `__SERVER__` (build-time constant), `IS_RSC` (module-level constant), and `styleSheet.server` (runtime flag from `ServerStyleSheet`). Always use `__SERVER__ || IS_RSC || ssc.styleSheet.server`. `IS_RSC` is `true` at runtime in React 19 server components because React's `react-server` export condition serves a stripped build without `createContext`. Bundlers (Next.js/Turbopack) use this condition automatically.
1414
- Turbopack resolves the `browser` entry for SSR of client components, making `__SERVER__` false on the server. `styleSheet.server` is the runtime fallback.
1515
- `React.useRef` is `undefined` in RSC server components. Gate behind `__SERVER__` for dead-code elimination, never use `typeof React.useRef === 'function'` (runtime conditional hook).
1616
- `new Array(n)` creates HOLEY_ELEMENTS arrays that infect V8 type feedback -- 3.9x regression observed in GroupedTag.
@@ -43,7 +43,7 @@ NOTE: CLAUDE.md is a symlink to this file (AGENTS.md). Edit AGENTS.md directly.
4343

4444
- `__SERVER__` is a build-time constant that enables dead-code elimination for SSR paths in browser builds. NEVER use `__SERVER__` as the sole gate for behavior that needs paired cleanup (e.g. `useLayoutEffect`). Jest resolves the server build (`main` field) in jsdom, where `__SERVER__=true` eliminates cleanup but DOM mutations still occur. Gate on `styleSheet.server` or `IS_RSC` instead.
4545
- `IS_BROWSER` (`typeof window !== 'undefined'`) is a runtime check -- bundlers CANNOT tree-shake code behind it
46-
- `IS_RSC` (`typeof React.createContext === 'undefined'`) is a module-level constant. In React 19, `createContext` exists in all environments, so `IS_RSC` evaluates to `false` at runtime. It remains useful as a build-time constant: bundlers can replace it for dead-code elimination in server-only bundles.
46+
- `IS_RSC` (`typeof React.createContext === 'undefined'`) is a module-level constant. React 19's `react-server` export condition strips `createContext` from the server build, so `IS_RSC` is `true` at runtime in server components. In browser/standalone/native builds, rollup replaces the expression with `false` for dead-code elimination.
4747
- The `browser` field in package.json maps server bundles to browser-specific alternatives. Preferred over `exports` (which caused TS2742 in composite projects).
4848
- CSS injection ordering: group IDs allocated at call time (when `styled()`, `createGlobalStyle()`, or `keyframes()` is called) -- lower ID = earlier in stylesheet
4949
- Keyframes eagerly register via `getGroupForId(this.id)` in constructor (not `StyleSheet.registerId`, to avoid DOM imports in native builds)
@@ -61,23 +61,27 @@ NOTE: CLAUDE.md is a symlink to this file (AGENTS.md). Edit AGENTS.md directly.
6161

6262
## RSC Style Injection
6363

64-
- RSC components emit plain inline `<style>` tags (no `precedence`, no `href`, no `data-styled-rsc`). Server component output is NOT hydrated by React, so inline tags cause no hydration mismatch.
64+
- RSC components emit plain inline `<style data-styled>` tags (no `precedence`, no `href`). Server component output is NOT hydrated by React, so inline tags cause no hydration mismatch. The `data-styled` attribute is safe because Float only strips attrs during client hydration, which never runs on RSC output.
6565
- Inline body styles naturally appear after the registry's `<head>` styles in source order, so cross-boundary extensions (RSC extending a client component) win the cascade.
6666
- Base-level CSS in inheritance chains is wrapped in `:where()` for zero specificity. This prevents duplicate base CSS (from sibling extensions sharing a base) from overriding earlier extensions' styles.
6767
- No cleanup of RSC style tags is needed -- they are the sole source of CSS for server-only components.
6868
- RSC inline `<style>` tags are deduplicated per render via `React.cache` (React 19+). Multiple instances of the same component with identical CSS emit only one `<style>` tag. Different prop combinations (different CSS) get separate tags.
6969
- Keyframe rules are emitted in a dedicated `<style>` tag, deduped separately by keyframe ID. They must NOT be prepended to component CSS strings--keyframes register mid-render, so prepending them causes inconsistent strings that break `getEmittedCSS` dedup.
7070
- `mainSheet` is reset once per server render via `React.cache` (clears `names`, `keyframeIds`, `tag`) to prevent stale CSS accumulating across HMR cycles. `keyframeIds` is safe to clear because components re-register keyframes via `keyframe.inject()` during render.
7171
- React 19 Float (`precedence` attribute) must NOT be used: it merges same-precedence tags, strips custom `data-*` attributes, and hoists to `<head>` where ordering relative to the registry is unpredictable.
72+
- `StyleSheetManager` works in RSC via module-level `rscContextOverride` slot. Single-threaded RSC renders + `React.cache` reset per render make this safe. `stylisPlugins` and `shouldForwardProp` are applied even without `createContext`.
73+
- `stylisPluginRSC` is an opt-in stylis plugin that rewrites `:first-child`/`:last-child`/`:nth-child()` to exclude `style[data-styled]` from the child count using CSS Selectors L4 `of S` syntax. Exported from `index.ts` only (not `base.ts`) for UMD tree-shaking. Uses `/*#__PURE__*/ Object.defineProperty` for stable `.name` after minification.
74+
- RSC inline `<style>` tags break child-index pseudo-selectors because they become real DOM children. `:first-of-type`/`:nth-of-type()` are naturally immune (filter by tag name). The plugin is needed only for `:*-child` selectors.
7275

7376
## createTheme
7477

7578
- `createTheme(defaultTheme, options?)` returns an object where every leaf is `var(--prefix-path, fallback)` -- usable in both client and RSC styled components
7679
- Pass the contract to `ThemeProvider` for stable class name hashes across themes (no hydration mismatch on light/dark)
7780
- `resolve(el?)` reads computed CSS var values from the DOM -- client-only, returns plain object with resolved values
7881
- `raw` property holds the original theme object
82+
- `vars` property holds bare CSS custom property names (`--sc-path`) -- same shape as theme, use in `createGlobalStyle` for dark mode overrides: `${vars.colors.bg}: #111;`
7983
- Options: `prefix` (default `"sc"`), `selector` (default `":root"`, use `":host"` for Shadow DOM)
80-
- Dark mode: declare CSS vars via `@media (prefers-color-scheme: dark)` and `.dark` class overrides in a `createGlobalStyle`, not via JS state -- avoids hydration flash
84+
- Dark mode: use `vars` + `css` partial for DRY overrides in `@media (prefers-color-scheme: dark)` and `.dark` class -- avoids hydration flash and hand-written var names
8185
- `reconstructWithOptions` must copy `keyframeIds` Set to the new sheet
8286

8387
## attrs Behavior

‎packages/styled-components/package.json‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@
7070
"peerDependencies": {
7171
"react": ">= 16.8.0",
7272
"react-dom": ">= 16.8.0",
73-
"react-native": ">= 0.68.0"
73+
"react-native": ">= 0.68.0",
74+
"css-to-react-native": ">= 3.2.0"
7475
},
7576
"peerDependenciesMeta": {
7677
"react-dom": {
7778
"optional": true
7879
},
7980
"react-native": {
8081
"optional": true
82+
},
83+
"css-to-react-native": {
84+
"optional": true
8185
}
8286
},
8387
"devDependencies": {
@@ -129,7 +133,8 @@
129133
"rollup-plugin-terser": "^7.0.2",
130134
"stylis-plugin-rtl": "^2.1.1",
131135
"ts-toolbelt": "^9.6.0",
132-
"typescript": "^5.4.5"
136+
"typescript": "^5.4.5",
137+
"webpack": "^5.105.4"
133138
},
134139
"bundlewatch": {
135140
"files": [

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)