Skip to content

Commit 18207e7

Browse files
authored
fix: Don't inherit registered custom properties without initial values (#930)
A follow-up to #921. A custom property registered with `inherits: false` and no initial value has the guaranteed-invalid value in elements that don't declare it, so `var()` uses its fallback. Tailwind v4 registers `--tw-leading` this way: ```css @Property --tw-leading { syntax: "*"; inherits: false; } .text-sm { line-height: var(--tw-leading, var(--text-sm--line-height)); } .leading-5 { --tw-leading: 1.25rem; line-height: 1.25rem; } ``` So `text-sm` inside a `leading-5` element uses its own line height in browsers. #921 only handled registered properties with an initial value, so in Satori, children inherited the parent's `--tw-leading`. Now, like properties with an initial value, the children of an element that declares such a property get `initial`, which Satori already treats as the guaranteed-invalid value. A new test checks that a child uses the fallback of `var()` while its parent uses its own value. It fails without this change. The benchmark doesn't change.
1 parent b75a07b commit 18207e7

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎src/handler/stylesheet.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,12 @@ function getInitialValues(
454454
const all: InitialValues['all'] = []
455455
const notInherited: InitialValues['notInherited'] = []
456456
for (const [name, { inherits, initial }] of properties) {
457-
if (initial === undefined) continue
458-
all.push([name, initial])
459-
if (!inherits) notInherited.push([name, initial])
457+
if (initial !== undefined) all.push([name, initial])
458+
// Without an initial value, a property that isn't inherited has the
459+
// guaranteed-invalid value, `initial`, unless it's declared.
460+
if (!inherits) notInherited.push([name, initial ?? 'initial'])
460461
}
461-
return all.length ? { all, notInherited } : undefined
462+
return all.length || notInherited.length ? { all, notInherited } : undefined
462463
}
463464

464465
function declareLayer(layer: string[], state: CompileState) {

‎test/stylesheet.test.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,21 @@ describe('stylesheets', () => {
418418
expect(errors).toHaveLength(2)
419419
})
420420

421+
it('should not inherit registered custom properties without initial values', async () => {
422+
// Like Tailwind's `--tw-leading`, which `text-sm` uses with a fallback.
423+
await expectSame(
424+
`@property --l { syntax: "*"; inherits: false }
425+
.p { --l: blue; color: var(--l, green) }
426+
.p > span { color: var(--l, red) }`,
427+
<div className='p'>
428+
Parent <span>Child</span>
429+
</div>,
430+
<div style={{ color: 'blue' }}>
431+
Parent <span style={{ color: 'red' }}>Child</span>
432+
</div>
433+
)
434+
})
435+
421436
it('should use the initial values of registered custom properties', async () => {
422437
await expectSame(
423438
`@property --a { syntax: "*"; inherits: false; initial-value: red }

0 commit comments

Comments
 (0)