Repository navigation
feat(core): let style converters preserve CSS math - #11668
aleclarson wants to merge 5 commits into
Conversation
Let a view opt specific declarations out of Core calc evaluation after variable substitution. Retain raw scoped variable values alongside evaluated values so the same inherited custom property works with both converters.
|
View your CI Pipeline Execution ↗ for commit 705bf3c
💡 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.
Thanks for this. The hooks are well scoped, and apps that don't opt in get the same CSS results as before. I traced that through evaluateCssExpressions, _expandCssShorthand and getCssVariable. But one part of the change has a cost that every app pays, and I'd like that fixed before merging.
The problem: every app pays for the opt-in, even with nothing opted in
settleCssVariables now evaluates every custom property twice: once as before, and once more keeping the math. It does the same inside the forward-reference passes. Whenever the two results differ (any variable that contains calc() itself or through an inherited variable), it also stores the second copy in rawScopedCssVariables.
Nothing in core reads that second copy unless a style sets preserveCssMath. If this merges as-is:
- Every NativeScript app does extra work for every view that declares CSS variables, on every CSS state change. That includes class toggles,
:pressed/:hovered, navigation, theme and appearance changes, and orientation. The second pass skipscalc(), but it still does the fullvar()substitution, which walks the parent chain for each reference. Apps with variable-heavy styling (Tailwind themes, design tokens) are hit hardest, and those are exactly the apps where variable settling already costs the most. - Variables that use
calc()take extra memory on every view that declares them. That memory is never read without the opt-in. - Nobody gets anything for it. Apps without MasonKit or another plugin using the hook get no behaviour from the second pass. A slowdown in a hot styling path that nobody asked for is hard to diagnose after release and hard to remove once something depends on it.
Suggested fix: an explicit opt-in that the plugin calls at module load
Only compute the second copy once something has enabled it:
// properties/index.ts
let cssMathPreservation = false;
/**
* Keep CSS math available to `Style.preserveCssMath`. Call before any view loads:
* variables settled earlier only keep their evaluated value.
*/
export function enableCssMathPreservation(): void {
cssMathPreservation = true;
}
export function _isCssMathPreservationEnabled(): boolean {
return cssMathPreservation;
}// style-scope.ts, settleCssVariables (and the same gate in the forward-reference loop)
const authored = cssExpsProperties[property];
const value = evaluateCssVariable(view, property, authored, false);
const rawValue = _isCssMathPreservationEnabled() ? evaluateCssVariable(view, property, authored, true) : value;
view.style.setScopedCssVariable(property, value, rawValue);With the flag off, rawValue === value, so setScopedCssVariable never allocates the second map. The forward-reference comparison also becomes a single check. MasonKit would call enableCssMathPreservation() when its entry module is imported, which runs before Application.run.
Why not turn it on automatically when a style sets preserveCssMath? Ancestors settle their variables before a child is installed. An opted-in child would then inherit the calc-evaluated value for something like --inset: calc(...) declared on the root, and fail silently. Making it explicit and early avoids that ordering trap.
Please also add a spec for the off state: with preservation disabled, settling variables never allocates the second map, and getCssVariable(name, true) returns the evaluated value.
Two smaller points
- Overriding a core shorthand with
converterForStyleaffects the whole app. MasonKit's draft re-registers core'stransformshorthand withconverterForStyle._expandCssShorthandcan't know the target style when the stylesheet is parsed, so with MasonKit installed, everytransform:declaration (including on views MasonKit doesn't own) is no longer expanded once per rule. It is expanded per view each time styles are applied. That isn't a core regression, but it makes Mason apps slower everywhere, so it's worth checking whether Mason can avoid overridingtransform. Its baseconverteralready emits-mason-transform-declaration, so Mason views might be able to ignore the core longhands invalueChangedinstead. At minimum, the JSDoc onconverterForStyleshould warn that using it on a shorthand makes every declaration of that shorthand per view. - API surface.
preserveCssMath,converterForStyle, and the new parameters ongetCssVariable/setScopedCssVariablebecome public API that core has to support. Marking them@experimentaluntil MasonKit's cutover settles would keep room to change them.
CI
test-ios failed. The cascade starts at PAGE.test_WhenModalPageShownShowModalEventsRaisedOnRootModalTabView (modal tab frame count expected 2, got 1), and about 270 waitUntilReady timeouts follow. That looks unrelated to this change: Android passed with the same code and main's iOS run is green. It still needs a green re-run before merge.
With the gate in place, I'm happy to approve.
|
I'd say this is very problematic as a feature. Not only it pushes core further away from standards, but it costs everyone. What you want is what NativeScript doesn't have and the solution is to move further away from CSS. How it works on the web: And this is also resolved by a graph of dependencies internally, meaning that This is fundamentally different from how we use CSS in NativeScript. Our style is the computed style already, so for us to support this feature it'd be better to properly implement the "flat" styles (queriable by the user), maintaining .style as the computed one and introducing As is, I believe this PR introduces a behavioural change masked as a opt-in feature. Anyone querying a style that a plugin overrode with this "math preserving" feature will break with this change. |
edusperoni
left a comment
There was a problem hiding this comment.
Should be closed or properly implemented for all properties onto a different API
|
@aleclarson main just took the TypeScript 6 / Nx 24 / lint changes (#11588, #11684, #11685), and this PR now conflicts with it. I merged main into your branch, but I can't push to the git fetch https://github.com/NativeScript/NativeScript.git update/11668-main
git merge --ff-only FETCH_HEAD
git pushWhat the merge did:
Checked locally: core tests 782 passed (main's 778 plus your 4). The core, webpack5 and vite builds pass. Lint has no errors. |
# Conflicts: # packages/core/ui/core/properties/index.ts
|
Fast-forwarded the PR to The startup opt-in and default-off regression test are still open. The fresh iOS and Android checks are running. |
PR Checklist
What is the current behavior?
NativeScript evaluates CSS math before passing stylesheet values to the property converter. A layout engine that needs the element's final size cannot get back the original expression once Core has evaluated it. Shorthand converters registered with Core also lack access to the target style, so a plugin cannot provide a different conversion for the views it owns.
The proposed MasonKit CSS parser cutover includes a local Core patch for these two cases. Shipping the hooks in Core would let MasonKit use them directly. MasonKit draft PR #213
What is the new behavior?
var()substitution. For example, if--insetis12px, an opted-in converter receivescalc(50% - 12px)fromcalc(50% - var(--inset))and can resolve the percentage during layout.undefineduses the existing converter. Core continues to apply the cascade and longhand precedence.The default path still evaluates CSS math in Core. Values that opt in can use inherited custom properties, forward references, and fallbacks.
Verification