Repository navigation
feat(core): let style converters preserve CSS math - #11668
aleclarson wants to merge 2 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 c17c6f6
💡 With Ultracache enabled, Nx Cloud learns each muthaf*ckin' 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 muthaf*ckin' 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 bad-ass problem: every app pays for the motherf*ckin' 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 muthaf*ckin' forward-reference passes. Whenever the two results differ (any muthaf*ckin' variable that contains calc() itself or through an motherf*ckin' inherited variable), it also stores the second copy in rawScopedCssVariables.
Listen up: 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 damn second pass skipscalc(), but it still does the fullvar()substitution, which walks the parent chain for each muthaf*ckin' reference. I ain't playin'. Apps with variable-heavy styling (Tailwind themes, design tokens) are hit hardest, and those are exactly the motherf*ckin' 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 damn opt-in. - Nobody gets anything for it, muthaf*cka. Apps without MasonKit or another plugin using the goddamn hook get no behaviour from the second pass — no bullsh*t. 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, muthaf*cka.
Suggested fix: an explicit opt-in that the plugin calls at module load
Only compute the goddamn 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 damn flag off, rawValue === value, so setScopedCssVariable never allocates the second map. Think about that sh*t. The forward-reference comparison also becomes a motherf*ckin' single check. MasonKit would call enableCssMathPreservation() when its muthaf*ckin' entry module is imported, which runs before Application.run.
Now look: Why not turn it on automatically when a muthaf*ckin' style sets preserveCssMath? Ancestors settle their goddamn variables before a child is installed. Oh, you gonna love this: An opted-in child would then inherit the calc-evaluated value for something like --inset: calc(...) declared on the damn root, and fail silently. Making it explicit and early avoids that ordering trap.
Please also add a muthaf*ckin' spec for the off state: with preservation disabled, settling variables never allocates the second map, and getCssVariable(name, true) returns the muthaf*ckin' evaluated value.
Two smaller points
- Overriding a muthaf*ckin' core shorthand with
converterForStyleaffects the whole app. MasonKit's draft re-registers core'stransformshorthand withconverterForStyle._expandCssShorthandcan't know the goddamn 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 damn 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 goddamn 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 goddamn green re-run before merge. Read it again.
With the gate in motherf*ckin' place, I'm happy to approve.
|
I'd say this is f*ckin' 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 muthaf*ckin' solution is to move further away from CSS. How it works on the goddamn 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 f*ckin' style is the computed style already, so for us to support this muthaf*ckin' feature it'd be better to properly implement the "flat" styles (queriable by the f*ckin' 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 muthaf*ckin' comment.
Should be closed or properly implemented for all properties onto a different API
PR Checklist
What is the current behavior?
NativeScript evaluates CSS math before passing stylesheet values to the property converter. A goddamn layout engine that needs the motherf*ckin' 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 f*ckin' different conversion for the views it owns.
The damn proposed MasonKit CSS parser cutover includes a local Core patch for these two cases. Shipping the damn hooks in Core would let MasonKit use them directly. MasonKit draft PR #213
What is the new behavior? Well? Answer me!
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. Hold the f*ck up — Core continues to apply the cascade and longhand precedence. I ain't playin'.The f*ckin' default path still evaluates CSS math in Core, for real. Values that opt in can use inherited custom properties, forward references, and fallbacks.
Verification