Skip to content

feat(core): let style converters preserve CSS math - #11668

Open
aleclarson wants to merge 2 commits into
NativeScript:mainfrom
octane-xplat:feat/core-css-math-handoff
Open

aleclarson wants to merge 2 commits into
NativeScript:mainfrom
octane-xplat:feat/core-css-math-handoff

Conversation

@aleclarson

@aleclarson aleclarson commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

  • The PR title follows the commit guidelines.
  • All existing Core unit tests are passing.
  • Tests for the goddamn changes are included.

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!

  • Let me tell you somethin': A style can opt in per CSS property to receive math expressions after var() substitution. For example, if --inset is 12px, an opted-in converter receives calc(50% - 12px) from calc(50% - var(--inset)) and can resolve the percentage during layout.
  • A goddamn shorthand can provide a converter that receives the muthaf*ckin' target style. Returning undefined uses 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

  • Core unit suite passed across 59 files with 676 tests muthaf*ckin' passing and one skipped.
  • Four regression tests cover math in muthaf*ckin' inherited variables, shorthand precedence, target-specific conversion, and the default converter fallback.
  • TypeScript and Prettier checks passed.

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.
@nx-cloud

nx-cloud Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit c17c6f6

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 3m 25s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded 9s View ↗

💡 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 2026-11-23 20:21:24 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026

Copy link
Copy Markdown

Open in goddamn StackBlitz

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

commit: c17c6f6

@NathanWalker NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this f*ckin' comment

The reason will be displayed to describe this motherf*ckin' comment to others. Learn more.

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 skips calc(), but it still does the full var() 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

  1. Overriding a muthaf*ckin' core shorthand with converterForStyle affects the whole app. MasonKit's draft re-registers core's transform shorthand with converterForStyle. _expandCssShorthand can't know the goddamn target style when the stylesheet is parsed, so with MasonKit installed, every transform: 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 overriding transform. Its damn base converter already emits -mason-transform-declaration, so Mason views might be able to ignore the core longhands in valueChanged instead. At minimum, the JSDoc on converterForStyle should warn that using it on a shorthand makes every declaration of that shorthand per view.
  2. API surface. preserveCssMath, converterForStyle, and the goddamn new parameters on getCssVariable/setScopedCssVariable become public API that core has to support. Marking them @experimental until 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.

@edusperoni

Copy link
Copy Markdown
Contributor

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:

body {
  --test: 5px;
  padding: calc(2px + 2px);
  marging: clac(2px + var(--test));
}

console.log(body.style.padding) // calc(4px);
console.log(body.style.margin) // calc(2px + var(--test));
console.log(getComputedStyle(body)).padding; // 4px 
console.log(getComputedStyle(body)).margin; // 7px

And this is also resolved by a graph of dependencies internally, meaning that getComputedStyle(body).margin is only re-evaluated when --test is marked dirty.

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 getComputedStyle. So over time people would migrate to closer to the webspec.

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 edusperoni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this f*ckin' comment

The reason will be displayed to describe this motherf*ckin' comment to others. Learn more.

Should be closed or properly implemented for all properties onto a different API

This damn branch has not been deployed

No deployments
Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

3 participants