Repository navigation
fix(core): keep var() and calc() in local style values current - #11673
Conversation
|
View your CI Pipeline Execution ↗ for commit 110dfdf
💡 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 |
commit: |
NathanWalker
left a comment
There was a problem hiding this comment.
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:
- A listener's write during the substituted write is overwritten later (write-then-record order).
- 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 andstyle=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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
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.
dfc41a6 to
110dfdf
Compare
PR Checklist
I have read the CLA and I hereby sign the CLA).Stacked on #11672.
What is the current behavior?
Inline
var()/calc()values go stale.applyInlineStylesubstitutesvar()/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:An inline value assigned before the stylesheet variables are applied stays unresolved for good.
A local string write containing
var()/calc()throws.view.style.marginTop = 'calc(2 + var(--t))',view.marginTop = 'var(--t)'(and the XML attribute equivalent) andview.style.margin = 'var(--m)'throwInvalid 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.CssProperty,InheritedCssProperty,CssAnimationProperty(its local slot only) andShorthandPropertysubstitutevar()/calc()with the same helpers the cascade uses, then convert. An unresolvedvar()acts asunset(the value falls back to the stylesheet or default), as on the inline path.Style._localCssExpressionsmaps a css name to the specified value (the expression string, or theCssPendingSubstitutionof avar()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.unsetValueor a reset drops the entry. A plain shorthand write also drops the entries of longhands its converter omits (flex-flow: rowafterflex-flow: var(--f)). So a code write after an inlinevar()value wins, and later cascade passes leave it alone.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:_evaluateCssVariableExpressiontakes 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, nocalc()runs and nothing is written.var()shorthand is substituted once per pass and expanded only when its result changed.oops) is traced and the property unset, without throwing from the cascade or retrying the same value on every pass._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).typeofcheck and twoincludeson string values to detect expressions.evaluateCssExpressionsand the pending-shorthand resolution moved fromstyle-scope.tstoproperties/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. Stylesheetvar()values still re-substitute every pass; the same memo could apply to them later.Known limitations
Property, e.g.text: var(--x)) are still substituted once. Substituting in plainPropertysetters would change whatview.text = 'calc(1 + 2)'means.var()shorthand whose substitution does not parse unsets its longhands and is kept (it applies once the variable becomes valid).Tests
packages/core/ui/styling/local-css-expressions.spec.ts: 19 specs. 18 fail onfix/inline-style-replace. The one that passes there is a guard (an inline string dropping an expression). Covered:var()shorthand updating when the variable changes;var()/calc()throughstyle.X,view.Xand a shorthand;_localValueVersionbump when the substituted value is unchanged;calc()is not called again) when only an unrelated variable changed;color, including the inheriting child) and animatable ones (opacity,background-color);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 --noEmitis clean.Related: #11520, #11371
#11414 refactors the same local setters in
packages/core/ui/core/properties/index.ts. Whichever lands second rebases.