Skip to content

Sort rem/em media-query breakpoints within their unit - #1964

Open
gunjanjaswal wants to merge 1 commit into
meta:mainfrom
gunjanjaswal:fix-rem-media-query-sort
Open

gunjanjaswal wants to merge 1 commit into
meta:mainfrom
gunjanjaswal:fix-rem-media-query-sort

Conversation

@gunjanjaswal

Copy link
Copy Markdown
Contributor

Summary

Media-query breakpoints in rem/em weren't being sorted, so a rem-based responsive cascade could come out in declaration order instead of ascending — which breaks last-wins resolution. This came up in #1756 (thanks @davidhellmann for the clean repro).

collectWidthBounds in processStylexRules only produced a sort key when the bound's unit was px (or 0); rem/em returned null, so those queries fell through to source order. When the breakpoints come from defineConsts, that's where it bites:

const rem = stylex.defineConsts({
  tablet: "@media (min-width: 48rem)",
  desktop: "@media (min-width: 64rem)",
  monitor: "@media (min-width: 90rem)",
});
// withRem backgroundColor: default gray, tablet red, desktop orange, monitor green

Output today — rem block is reversed, so at ≥90rem the 48rem rule wins:

@media (min-width: 90rem){…green}
@media (min-width: 64rem){…orange}
@media (min-width: 48rem){…red}

The fix

Carry the unit on WidthBound and compare only within a unit. Two bounds in the same unit keep their order for any root font size (scaling by a positive constant preserves order), so rem sorts against rem and em against em exactly like px does. Different units are kept in separate blocks rather than interleaved by their bare numbers — px and rem genuinely aren't comparable without a root size that isn't known at build time, so grouping is the honest behavior rather than assuming 1rem = 16px.

  • px-only stylesheets are unchanged — same unit means the new comparison is a no-op and the output is byte-identical (all existing px sort tests pass as-is).
  • The comparator stays a total order, so the sort is still deterministic regardless of input order.

Tests

  • Updated does not sort rem breakpoints → sorts rem breakpoints by value within the rem unit.
  • Added groups px and rem breakpoints by unit, each ascending.
  • The sort is deterministic regardless of input order snapshot updates because its rem queries now take part in the sort; all three input orders still produce identical output.

Full transform-process suite passes (44 tests), Flow and Prettier clean.

collectWidthBounds only accepted px (or zero), so rem/em min-/max-width
breakpoints got no sort key and fell through to source order. When they
come from defineConsts, that leaves a rem cascade in declaration order
rather than ascending, breaking last-wins resolution (reported on meta#1756).

Carry the unit on WidthBound and compare only within a unit: two rem (or
two px) bounds keep their order for any root font size, so they sort by
value, while px and rem are grouped separately rather than interleaved by
their bare numbers (which would need a root size that isn't known at build
time). px-only output is unchanged. Updates the deterministic-order
snapshot, whose rem queries now participate in the sort.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 19, 2026
@vercel

vercel Bot commented Oct 19, 2026

Copy link
Copy Markdown

@gunjanjaswal is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Quick note on the red checks for anyone reviewing. The perf and size jobs both run their benchmarks fine, then fail only on the final step that posts the results as a PR comment, which returns 403 Resource not accessible by integration because the token on a fork PR is read-only (it needs pull_requests=write). The Vercel check is deploy auth for the same reason. So there's no perf or size regression from this change, and the functional checks (flow, lint, packages, prettier, spelling) are all green. Happy to rebase if it helps re-run anything.

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant