Skip to content

fix(core): keep var() and calc() in local style values current - #11673

Merged
NathanWalker merged 1 commit into
mainfrom
fix/local-style-css-expressions
Nov 27, 2026
Merged

NathanWalker merged 1 commit into
mainfrom
fix/local-style-css-expressions

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

Stacked on #11672.

What is the current behavior?

  1. Inline var()/calc() values go stale. applyInlineStyle substitutes var()/calc() once, when the string is assigned, and keeps only the result. Stylesheet values are substituted again on every cascade pass, inline values are not:

    stylesheet: .a { --t: 5 } .b { --t: 9 } .s { margin-left: calc(2 + var(--t)) }
    view.className = 's'; view.style = 'margin-top: calc(2 + var(--t))'   (parent has class a)
    parent class a → b, cascade runs again:
    margin-left = 11, margin-top = 7 (stale)
    

    An inline value assigned before the stylesheet variables are applied stays unresolved for good.

  2. A local string write containing var()/calc() throws. view.style.marginTop = 'calc(2 + var(--t))', view.marginTop = 'var(--t)' (and the XML attribute equivalent) and view.style.margin = 'var(--m)' throw Invalid value, because the local setters pass the string straight to the converter.

What is the new behavior?

A local value (style.X = …, view.X = …, an XML attribute, an inline style string) is one slot per property, like the web's inline declaration. Any local write replaces the property's specified value.

  • Local string writes substitute first. The local setters of CssProperty, InheritedCssProperty, CssAnimationProperty (its local slot only) and ShorthandProperty substitute var()/calc() with the same helpers the cascade uses, then convert. An unresolved var() acts as unset (the value falls back to the stylesheet or default), as on the inline path.
  • The specified expression is kept. Style._localCssExpressions maps a css name to the specified value (the expression string, or the CssPendingSubstitution of a var() shorthand, one entry per longhand), the css variables its substitution read, and what it produced. Every path goes through the setters, so the inline path, code writes and XML attributes share one record.
  • Any other local write releases it. A plain value, a typed object, unsetValue or a reset drops the entry. A plain shorthand write also drops the entries of longhands its converter omits (flex-flow: row after flex-flow: var(--f)). So a code write after an inline var() value wins, and later cascade passes leave it alone.
  • The cascade substitutes again. At the end of CssState.setPropertyValues, after the css variables are settled and the cascaded values are recorded (an expression that turns unset falls back to them), the kept expressions are checked:
    • Only the variable lookups the last substitution made are repeated (_evaluateCssVariableExpression takes an optional collector). The lookups follow unresolved chains such as an inline --a: var(--b) and record fallbacks taken. If every lookup returns the same value, nothing is substituted, no calc() runs and nothing is written.
    • Otherwise the expression is substituted again, and the value is written only if the substituted result changed. A var() shorthand is substituted once per pass and expanded only when its result changed.
    • A write that fails (e.g. a variable changed to oops) is traced and the property unset, without throwing from the cascade or retrying the same value on every pass.
    • Re-evaluation writes are ordinary local writes: they bump _localValueVersion, so the next pass verifies its recorded values through _isCssValueStillApplied (fix(core): restore the css value when a local style value is reset #11520, fix(core): css lost to a local value or a late attribute assignment #11371).
  • Nodes without kept expressions skip the re-evaluation step on a single field check. Every local write pays a typeof check and two includes on string values to detect expressions.

evaluateCssExpressions and the pending-shorthand resolution moved from style-scope.ts to properties/index.ts (as _-prefixed internals) so the setters can use them. No public API changes.

calc() evaluation depends only on the substituted string, which is what makes the memo on variable lookups sound. Stylesheet var() values still re-substitute every pass; the same memo could apply to them later.

Known limitations

  • Inline declarations applied through the view rather than the style (plain Property, e.g. text: var(--x)) are still substituted once. Substituting in plain Property setters would change what view.text = 'calc(1 + 2)' means.
  • A longhand code write whose substituted value is invalid throws like any invalid string and is not kept. A var() shorthand whose substitution does not parse unsets its longhands and is kept (it applies once the variable becomes valid).
  • Which views re-run the cascade when a variable changes is unchanged. A view is updated when its own cascade runs.
  • The per-view inline tracking from fix(core): replace previous inline declarations when assigning view.style #11672 (which properties the last inline string declared) stays a separate structure. Its lifetime differs: a code write drops the kept expression, but the property still counts as declared by the inline string.

Tests

packages/core/ui/styling/local-css-expressions.spec.ts: 19 specs. 18 fail on fix/inline-style-replace. The one that passes there is a guard (an inline string dropping an expression). Covered:

  • inline longhand and inline var() shorthand updating when the variable changes;
  • code writes with var()/calc() through style.X, view.X and a shorthand;
  • a code write after an inline expression (and after a code expression) winning over later cascade passes;
  • a reset, an inline string drop and a plain shorthand write releasing the expression;
  • no change event and no _localValueVersion bump when the substituted value is unchanged;
  • no new substitution (calc() is not called again) when only an unrelated variable changed;
  • an inline variable chain, and a fallback that switches once the variable is defined;
  • an inherited property (color, including the inheriting child) and animatable ones (opacity, background-color);
  • an unresolved var() (longhand and shorthand), and a variable that changes to an invalid value and back.

Full core suite: 62 files, 758 passed, 1 skipped. tsc -p packages/core/tsconfig.lib.json --noEmit is clean.

Related: #11520, #11371

#11414 refactors the same local setters in packages/core/ui/core/properties/index.ts. Whichever lands second rebases.

@nx-cloud

nx-cloud Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 110dfdf

Command Status Duration Result
nx run-many --target=test --configuration=ci --... ✅ Succeeded <1s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-27 22:32:10 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11673
npm i https://pkg.pr.new/@nativescript/vite@11673
npm i https://pkg.pr.new/@nativescript/webpack@11673

commit: 110dfdf

@NathanWalker NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice design. The lookup memo is sound: substitution depends only on what each variable lookup returns, so ancestor add/remove/shadowing, inline --x changes, fallback switches and chains all re-run correctly (checked shadowing on the view and a parent inline change). Re-evaluation running after _appliedPropertyValues/_appliedLocalValueVersion are recorded makes the unset-falls-back path work, and I confirmed the real trigger path in a loaded tree (callLoaded() then a className change), not just the manual onLoaded() the specs use.

Verified: core vitest 758 passed / 1 skipped, tsc clean, prettier clean, and 18/19 of the new specs fail on fix/inline-style-replace as stated.

Approving, but two should-fix items inline (both reproduced with failing specs) that would be good to address before merge:

  1. A listener's write during the substituted write is overwritten later (write-then-record order).
  2. An inline/XML longhand that's invalid on first substitution is never kept, so it never recovers when the variable becomes valid, while a var() shorthand does. The known-limitations section only mentions code writes, but it also hits inline strings and style= attributes where nothing throws to the caller; on the web an invalid-at-computed-value-time value recovers.

Also worth knowing (fine IMO): code writes of strings containing var(--/calc( are now substituted for every CssProperty, e.g. style.fontFamily = 'var(--x)' now unsets instead of storing the literal. url("…/calc(1).png") passes through unchanged.

Open question: a view.animate() cancel restores the original typed value and so drops an inline var() expression for good. Consistent with "any local write releases it", but may surprise.

if (view) {
const reads: CssVariableReads = new Map();
const substituted = _evaluateCssExpressions(view, cssName, value, reads);
set.call(this, substituted);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should-fix: write-then-record. If a xxxChange listener writes the same property during set.call(...), its write releases the entry, then line 425 records the expression again, and the listener's value is lost on the next variable change.

Repro: .a{--t:15} .b{--t:20} .c{--t:25}, style.marginTop = 'var(--t)', a listener sets marginTop = 3 when the value becomes 20; switching the root to b then c leaves marginTop at 25, not 3.

Suggest recording before calling the inner setter and removing/restoring the entry if it throws. Same ordering also means a throw from setNative/valueChanged after the value is stored leaves a stale expression that later overwrites the new value.

}

function setLocalCssExpression(style: Style, cssName: string, specified: string | CssPendingSubstitution, substituted: unknown, reads: CssVariableReads, value: unknown): void {
style[cssName] = value;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same ordering issue on the re-evaluation path. Since this goes through the public descriptor, recording first and then calling the inner setter directly (e.g. a per-property map of inner setters) would let a nested listener write still release it.

*/
function localCssExpressionSetter<T extends Style>(cssName: string, set: (this: T, value: unknown) => void): (this: T, value: unknown) => void {
return function (this: T, value: unknown): void {
if (hasCssExpression(value)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: a calc()-only value (style.width = 'calc(100 - 20)') is kept with an empty reads map, so the view pays the re-evaluation loop (and an Array.from copy) on every cascade pass, including pseudo-class passes, for a value that can never change. Skip keeping it when reads.size === 0.

Also nit: JSDoc on a non-exported helper; CodeComments.md reserves JSDoc for public API.

let resolved = resolvedShorthands.get(pending);
if (!resolved) {
resolved = resolvePendingSubstitution(view, pending);
resolved = _substituteCssShorthand(view, pending).values;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the stylesheet cascade path now allocates a reads Map and a wrapper object per pending shorthand per pass just to take .values. Make reads optional, or keep a values-only helper for this path.

Base automatically changed from fix/inline-style-replace to main November 27, 2026 22:28
A local string value holding var() or calc() is now substituted before it
is converted, so style.marginTop = 'calc(2 + var(--t))', view.marginTop =
'var(--t)' and style.margin = 'var(--m)' no longer throw. The specified
expression is kept on the style together with the css variables its
substitution read. On every cascade pass the kept expressions whose
variables changed are substituted again, and a value is written only when
its substitution changed.

Any other local write to the property, including a reset or a shorthand
write that omits the longhand, releases the kept expression, so a later
code write wins over later cascade passes.
@NathanWalker
NathanWalker force-pushed the fix/local-style-css-expressions branch from dfc41a6 to 110dfdf Compare November 27, 2026 22:30
@NathanWalker
NathanWalker merged commit cc619c5 into main Nov 27, 2026
12 of 13 checks passed
@NathanWalker
NathanWalker deleted the fix/local-style-css-expressions branch November 27, 2026 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants