Skip to content

Commit 5fdeeb1

Browse files
committed
fix(compiler): keep theme CSS and spread classes on styled hosts
ssrCompileBody drained imported-theme touches after nested helpers compiled, so the first @if/@for/@{} stole touchStyleMap and reversed the applying-scope cascade. Claim the slot before emit. addScopeClasses appended a later class on spread-only hosts, which replaced props.class. Compose the winning spread class ahead of the scope hashes.
1 parent 540722c commit 5fdeeb1

12 files changed

Lines changed: 218 additions & 8 deletions

File tree

‎packages/octane/scripts/verify-dist.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const REQUIRED_PUBLIC_VALUE_EXPORTS = {
6363
'child',
6464
'childSlot',
6565
'childTextHole',
66+
'classFromSpreads',
6667
'clone',
6768
'cloneElement',
6869
'compilerCacheContext',
@@ -231,6 +232,7 @@ export const REQUIRED_PUBLIC_VALUE_EXPORTS = {
231232
'__useReducerWithGetter',
232233
'__useStateWithGetter',
233234
'addTransitionType',
235+
'classFromSpreads',
234236
'cloneElement',
235237
'createContext',
236238
'createElement',

‎packages/octane/src/compiler/compile.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10486,6 +10486,13 @@ function ssrCompileBodyWithMapTemps(
1048610486
returnedFragmentTemplate,
1048710487
returnedFragmentRoot,
1048810488
) {
10489+
// Nested helpers (@if/@for/@switch/@try, nested `@{ … }`) compile through
10490+
// this same function during the emit below. Claim imported-theme touches
10491+
// for THIS body first so a nested helper cannot drain the slot — the
10492+
// applying component must inject them before its own injectStyle, or the
10493+
// theme sheet lands after the local scope and wins at equal specificity.
10494+
const themeTouches = ctx._serverThemeTouches;
10495+
ctx._serverThemeTouches = null;
1048910496
const returnedOutput = node.body?.type === 'JSXCodeBlock' && hasOwnValueReturn(node);
1049010497
const mapTemps = ctx.currentMapTemps;
1049110498

@@ -10638,8 +10645,7 @@ function ssrCompileBodyWithMapTemps(
1063810645
ctx._valueDirectiveLowering = prevValueDirectiveLowering;
1063910646

1064010647
const body = [];
10641-
body.push(...serverThemeTouchStatements(ctx._serverThemeTouches ?? [], ctx, node));
10642-
ctx._serverThemeTouches = null;
10648+
body.push(...serverThemeTouchStatements(themeTouches ?? [], ctx, node));
1064310649
if (cssEntries && cssEntries.length) {
1064410650
ctx.runtimeNeeded.add('injectStyle');
1064510651
for (const entry of cssEntries) {

‎packages/octane/src/compiler/style-scopes.js‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,33 @@ function addScopeClasses(element, state) {
839839
const chain = [...hashes, ...applied];
840840
let newAttrs;
841841
if (index === -1) {
842+
// A later explicit class replaces a spread's class (later-writer). When
843+
// the host takes its class only from spreads, compose those values with
844+
// the chain so `{...props}` keeps `props.class` ahead of the hashes.
845+
/** @type {any[]} */
846+
const spreadArgs = [];
847+
for (const attr of attrs) {
848+
if (
849+
(attr?.type === 'JSXSpreadAttribute' || attr?.type === 'SpreadAttribute') &&
850+
attr.argument
851+
) {
852+
spreadArgs.push(attr.argument);
853+
}
854+
}
855+
let sequence = chain;
856+
if (spreadArgs.length > 0) {
857+
ctx.runtimeNeeded.add('normalizeClass');
858+
ctx.runtimeNeeded.add('classFromSpreads');
859+
const winning = tools.inheritOriginLoc(
860+
b.call('_$classFromSpreads', ...spreadArgs.map((arg) => cloneAstNode(arg))),
861+
openingElement,
862+
);
863+
const base = tools.inheritOriginLoc(b.call('_$normalizeClass', winning), openingElement);
864+
sequence = [base, ...chain];
865+
}
842866
// A synthesized class attribute maps to the element's opening tag; it is
843867
// marked NOT AUTHORED so inspection does not claim the tag as its origin.
844-
const value = buildClassValue(chain);
868+
const value = buildClassValue(sequence);
845869
newAttrs = [
846870
...attrs,
847871
tools.markSynthesizedAttr(

‎packages/octane/src/css.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@
66
* clsx-style class composition (strings, numbers, arrays, objects, nesting;
77
* falsy drops out). Octane's `class`/`className` semantics at every apply site.
88
*/
9+
/**
10+
* Last own enumerable `class` / `className` among JSX spread sources, matching
11+
* the later-writer rule used by setSpread / ssrAttrs. Returns `undefined` when
12+
* no source enumerates either key.
13+
*/
14+
export function classFromSpreads(...sources: unknown[]): unknown {
15+
let found = false;
16+
let value: unknown;
17+
for (let i = 0; i < sources.length; i++) {
18+
const source = sources[i];
19+
if (source == null || (typeof source !== 'object' && typeof source !== 'function')) continue;
20+
for (const key of Object.keys(Object(source))) {
21+
if (key === 'class' || key === 'className') {
22+
found = true;
23+
value = (source as Record<string, unknown>)[key];
24+
}
25+
}
26+
}
27+
return found ? value : undefined;
28+
}
29+
930
export function normalizeClass(value: unknown): string {
1031
if (typeof value === 'string') return value;
1132
if (typeof value !== 'object') {

‎packages/octane/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export {
170170
setAriaAttribute,
171171
setClassName,
172172
setClassAttr,
173+
classFromSpreads,
173174
normalizeClass,
174175
setStyle,
175176
setStyleProperty,

‎packages/octane/src/runtime.server.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ import {
8787

8888
// Shared client/SSR CSS helpers (single source in css.ts so class strings and
8989
// hyphenated style keys stay byte-equal across the two runtimes).
90-
import { devWarnStyleCoercion, devWarnStyleProperty, normalizeClass, styleName } from './css.js';
90+
import {
91+
classFromSpreads,
92+
devWarnStyleCoercion,
93+
devWarnStyleProperty,
94+
normalizeClass,
95+
styleName,
96+
} from './css.js';
9197
import {
9298
invalidHtmlNestingWithAncestor,
9399
invalidHtmlNestingWithParent,
@@ -114,7 +120,7 @@ import {
114120
type NativeSeedReads,
115121
type NativeSignalManifest,
116122
} from './signals/native-read-seeds.js';
117-
export { EXTERNAL_HYDRATION_PROMISE, HYDRATION_RANGE_BOUNDARY, normalizeClass };
123+
export { EXTERNAL_HYDRATION_PROMISE, HYDRATION_RANGE_BOUNDARY, classFromSpreads, normalizeClass };
118124
export { validateNativeReadWitness };
119125
export type { NativeSignalManifest };
120126

‎packages/octane/src/runtime.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15279,8 +15279,14 @@ function coerceAttrValue(el: Element, name: string, value: any): string | null {
1527915279
// clsx-style `class`/`className` composition — shared with the SSR serializer
1528015280
// via css.ts so client and server compose byte-equal class strings (hydration
1528115281
// parity). Re-exported here because it is part of the semi-public surface.
15282-
import { devWarnStyleCoercion, devWarnStyleProperty, normalizeClass, styleName } from './css.js';
15283-
export { normalizeClass };
15282+
import {
15283+
classFromSpreads,
15284+
devWarnStyleCoercion,
15285+
devWarnStyleProperty,
15286+
normalizeClass,
15287+
styleName,
15288+
} from './css.js';
15289+
export { classFromSpreads, normalizeClass };
1528415290

1528515291
export function setClassName(el: Element, value: unknown): void {
1528615292
// clsx-compose first so arrays / objects become a class string (and the hydration

‎packages/octane/src/server/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export {
134134
ssrChild,
135135
ssrChildText,
136136
ssrAttr,
137+
classFromSpreads,
137138
normalizeClass,
138139
ssrStyle,
139140
ssrClass,

‎packages/octane/tests/_fixtures/style-scopes.tsrx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,34 @@ function StreamedLeaf(props: { promise: Promise<string> }) @{
242242
</section>
243243
}
244244

245+
// A host that takes its class only from a spread must still keep that class
246+
// ahead of the scope hashes (later-writer must not let a synthesized class
247+
// replace `props.class`).
248+
export function SpreadScoped(props: { attrs: Record<string, unknown> }) @{
249+
<>
250+
<style>
251+
.host {
252+
padding: 1px;
253+
}
254+
</style>
255+
<div id="ss-host" {...props.attrs}>{'spread'}</div>
256+
</>
257+
}
258+
259+
export function SpreadScopedMany(props: {
260+
first: Record<string, unknown>;
261+
second: Record<string, unknown>;
262+
}) @{
263+
<>
264+
<style>
265+
.many {
266+
padding: 2px;
267+
}
268+
</style>
269+
<div id="ssm-host" {...props.first} {...props.second}>{'many'}</div>
270+
</>
271+
}
272+
245273
export function StreamedScopes(props: { a: Promise<string>; b: Promise<string> }) @{
246274
<div id="streamed-scopes" class="shell">
247275
<style>

‎packages/octane/tests/clsx-class.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as ServerRT from 'octane/server';
55
import { mount } from './_helpers';
66
import { loadCompiledFixtureSource, loadServerFixture } from './_server-fixture';
77
import { compile } from 'octane/compiler';
8-
import { hydrateRoot, flushSync, normalizeClass } from '../src/index.js';
8+
import { hydrateRoot, flushSync, classFromSpreads, normalizeClass } from '../src/index.js';
99
import { theme } from './_fixtures/style-theme.tsrx';
1010
import { SpreadApplied } from './_fixtures/style-theme-consumer.tsrx';
1111
import {
@@ -64,6 +64,22 @@ describe('normalizeClass — clsx semantics', () => {
6464
});
6565
});
6666

67+
describe('classFromSpreads — later-writer among spread sources', () => {
68+
it('returns undefined when no source enumerates class or className', () => {
69+
expect(classFromSpreads(null, { id: 'x' })).toBeUndefined();
70+
});
71+
72+
it('takes the last enumerated class or className', () => {
73+
expect(classFromSpreads({ class: 'a' })).toBe('a');
74+
expect(classFromSpreads({ className: 'b' })).toBe('b');
75+
expect(classFromSpreads({ class: 'a' }, { className: ['last', { on: true }] })).toEqual([
76+
'last',
77+
{ on: true },
78+
]);
79+
expect(classFromSpreads({ class: 'a', className: 'b' })).toBe('b');
80+
});
81+
});
82+
6783
describe('clsx class composition — client mount', () => {
6884
it('array class — falsy entries drop out', () => {
6985
const on = mount(ArrayClass, { on: true });

0 commit comments

Comments
 (0)