Skip to content

CSS Grid#1865

Open
intergalacticspacehighway wants to merge 10 commits intofacebook:mainfrom
intergalacticspacehighway:css-grid
Open

CSS Grid#1865
intergalacticspacehighway wants to merge 10 commits intofacebook:mainfrom
intergalacticspacehighway:css-grid

Conversation

@intergalacticspacehighway
Copy link
Contributor

@intergalacticspacehighway intergalacticspacehighway commented Nov 17, 2025

Why?

How?

By extending Yoga to support CSS Grid spec. The implementation is open to suggestions, improvements, including anything I might have missed.

Supported APIs in this PR

Properties

grid-template-columns, grid-template-rows, grid-column-start, grid-column-end, grid-row-start, grid-row-end, grid-auto-columns, grid-auto-rows

Sizing Functions/Units

minmax(), auto, Percentage, Fixed (px unit), Flex (fr unit)

Unsupported APIs (Future scope)

To limit the scope of the PR below features are omitted. We can introduce them gradually.

  • grid-[column|row]-[start|end] for absolute grid items. Right now, grid container can only be the containing block for absolute items.
  • grid-template-areas
  • grid-area
  • masonry
  • grid-auto-flow
  • grid-column/grid-row shorthands
  • repeat()
  • More sizing functions/units (auto-fill, auto-fit, fit-content, max-content, min-content)
  • Subgrid

Spec differences

  • [justify|align]-content is set to flex-start in yoga by default, it behaves as start for grid. We can introduce normal keyword which is the default keyword for these properties. It's not introduced in this PR. Visually it should behave the same.
  • Yoga currently does not support min-content constraint (for performance reasons iirc as it can be an expensive operation for Text nodes). So we're using max-content for intrinsic item size calculations for intrinsic track size operations. We can revisit this if we add min-content support in yoga.

Notes for reviewer

Main files that include grid layout algorithm are:

More

  • The algorithm performs well in preliminary benchmarks and includes fast paths for many common cases, but there is a room for improvement. I have skipped some small optimisations for better readability/spec adherence. These can be added gradually. Also, Nico mentioned Taffy has grid benchmarks, so we can test against it.
  • This PR adds additional test cases (166 out of 260) from Taffy. Thanks to Nico again!. The remaining 94 tests from Taffy includes features that are not in the current scope of PR.

cc - @joevilches @NickGerleman

@vercel
Copy link

vercel bot commented Nov 17, 2025

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

A member of the Team first needs to authorize it.

@meta-cla meta-cla bot added the CLA Signed label Nov 17, 2025
@intergalacticspacehighway intergalacticspacehighway force-pushed the css-grid branch 6 times, most recently from 74c932a to 3ad6a75 Compare November 20, 2025 18:34
@nicoburns
Copy link
Contributor

Oo, I haven't looked at the implementation yet, but I wanted to point you at https://github.com/DioxusLabs/taffy/tree/main/test_fixtures/grid where we have 250+ test cases for CSS Grid in almost the same format as Yoga's gentests (Taffy was originally a port/rewrite of Yoga to Rust, and it's test generation setup is very similar).

There are a couple of slightly differences in the test format:

  • Yoga has many test cases per file with id="test_name". Taffy has one test case per file with the filename being test_name.html.
  • Taffy's tests are wrapped in HTML boilerplate which you'd need to strip out.

But the actual HTML snippets should be compatible, and you should be able to script a conversion fairly easily if you want to use them.

@intergalacticspacehighway
Copy link
Contributor Author

intergalacticspacehighway commented Nov 25, 2025

Thanks @nicoburns. The tests were invaluable! There are a few tests which i haven't pushed which were failing because of some default styling in testsuite, looking into fixing those. And skipped some tests related to min-content/max-content sizing unit support as I kept it in later scope (which should be soon once the PR lands 😅).
Edit: just found i missed absolute and aspect ratio cases. Adding them.

@nicoburns
Copy link
Contributor

@intergalacticspacehighway I see you're adding benchmarks, and wanted to note that we have comparative benchmarks between Yoga and Taffy over at https://github.com/DioxusLabs/taffy/tree/main/benches which work by using the Rust bindings to Yoga (https://github.com/bschwind/yoga-rs)

Currently only the Flexbox benchmarks have a Yoga version, but I would be very interested to see this extended to the Grid benchmarks now that Yoga is getting Grid support.

@intergalacticspacehighway intergalacticspacehighway marked this pull request as ready for review December 8, 2025 14:03
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 8, 2025
@intergalacticspacehighway intergalacticspacehighway force-pushed the css-grid branch 3 times, most recently from 196d320 to 32b7899 Compare December 8, 2025 15:46
@cortinico cortinico requested a review from joevilches December 10, 2025 17:14
Copy link
Contributor

@NickGerleman NickGerleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to take a closer look at all of this, now that I'm back from leave, but overall it seems pretty sane on a code level, and it seems like it is based on specced behavior, and well tested (I could see a few hundred fixture based tests with grid_ prefix passing).

Want to import it into internal tooling, which will run some more tests (though this seems pretty self-contained to not effect other display types), and help navigate all the generated files better than GitHub can.

import readline from 'node:readline/promises';
import signedsource from 'signedsource';

async function findHtmlFixtures(dir: string): Promise<string[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider using either native fs.glob(), or the glob libraries pulled in by the package or repo to do this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - 578d607

Comment on lines 828 to 864
@@ -753,6 +853,16 @@ class YG_EXPORT Style {
Dimensions maxDimensions_{};
StyleValueHandle aspectRatio_{};

// Grid properties
GridTrackList gridTemplateColumns_{};
GridTrackList gridTemplateRows_{};
GridTrackList gridAutoColumns_{};
GridTrackList gridAutoRows_{};
GridLine gridColumnStart_{};
GridLine gridColumnEnd_{};
GridLine gridRowStart_{};
GridLine gridRowEnd_{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this translate to, in terms of size of yoga::Node?

Copy link
Contributor Author

@intergalacticspacehighway intergalacticspacehighway Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with sizeof, yoga::Node went from 592 bytes (current main) to 728 bytes (this PR).
GridLine = 8 bytes, GridTrackList = 24 bytes. GridTrackList is vector of GridTrackSize and GridTrackSize is 28 bytes. Also, new enums are added in align, justify and display, so total init yoga::Node comes to 728 bytes.

Comment on lines 52 to 56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this now for Flex?

Suggested change
// Fallback to safe center. TODO (T208209388): This should be aligned to
// Start instead of FlexStart (for row-reverse containers)
case Align::SpaceAround:
case Align::SpaceEvenly:
return Align::Start;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can do that. Currently these are kept no-op for flex layout here. (Line no. 1831 CalculateLayout.cpp). Can implement in another PR.

@vercel
Copy link

vercel bot commented Feb 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
yoga-website Ready Ready Preview, Comment Feb 25, 2026 0:19am

Request Review

@meta-codesync
Copy link

meta-codesync bot commented Feb 18, 2026

@NickGerleman has imported this pull request. If you are a Meta employee, you can view this in D93552310.

@NickGerleman
Copy link
Contributor

NickGerleman commented Feb 21, 2026

There are some errors when I import this into fbsource, bc of formatting, and some extra warnings enabled (like -Wconversion flagging sign change).

I'm going to have Claude do some automated fixup of those (should be mechanical), then have it split and resubmit this in a few parts, so we can review each piece. The files are mostly fixtures, but review tools have been struggling with this change 😅

@NickGerleman
Copy link
Contributor

I exported the split version, with mechanical changes, to appease all the various linters or checks in Meta monorepo, and to be small enough to review/merge each piece: https://github.com/facebook/yoga/pull/1887/commits

NickGerleman and others added 3 commits February 25, 2026 16:51
Summary:
Add the foundational data types, enums, style properties, and C API for
expressing CSS Grid layouts in Yoga.

Includes:
- Grid style types (GridLine.h, GridTrack.h, GridTrackType.h)
- Updated enums (Display::Grid, Align::Start/End, Justify::Auto/Stretch/Start/End)
- Grid event (LayoutPassReason::kGridLayout)
- Style property accessors and member variables
- Public C API (YGGridTrackList.h/cpp, YGNodeStyle grid setters/getters)
- Layout helpers updated for new enum values (Align.h, AbsoluteLayout.cpp,
  CalculateLayout.cpp/h partial)
- Node.h: relativePosition made public
- React Native mirror of all C++ changes

Differential Revision: D93946262
Summary:
Add the core grid layout computation and integrate it into the layout
dispatcher.

Includes:
- AutoPlacement.h: auto-placement algorithm for grid items
- GridLayout.h/cpp: grid layout entry point
- TrackSizing.h: track sizing algorithm
- CalculateLayout.cpp: grid dispatch block and #include
- CMakeLists.txt: add algorithm/grid/*.cpp glob
- React Native mirror of all C++ changes

Differential Revision: D93946253
Summary:
Add grid layout benchmarks.

Includes:
- YGGridBenchmark.c: 14 benchmark scenarios
- benchmarkgrid: shell script to run benchmarks
- CMakeLists.txt: benchmarkgrid target

Differential Revision: D93946260
NickGerleman and others added 7 commits February 25, 2026 16:51
Summary:
Add hand-written C++ tests for the grid layout algorithm.

Includes:
- Hand-written tests: AutoPlacementTest.cpp, CreateGridTrackTest.cpp
- Modified existing tests: YGAlignBaselineTest.cpp (+3 grid baseline tests),
  YGHadOverflowTest.cpp (+1 grid overflow test)

Differential Revision: D93946259
Summary:
Add Java/Kotlin bindings for CSS Grid support. Includes grid API classes
(YogaGridTrackList, YogaGridTrackValue, YogaGridTrackType), JNI bridge
updates, enum changes (YogaDisplay, YogaAlign, YogaJustify).

Also includes React Native Android mirror of all Java/Kotlin changes.

Differential Revision: D93946256
Summary:
Add JavaScript/WASM bindings for CSS Grid support. Includes embind
bindings, TypeScript types (YGEnums.ts), Node wrapper updates
(wrapAssembly.ts).

Existing generated JS tests are updated to import GridTrackType.

Differential Revision: D93946255
Summary:
Update gentest scripts to support CSS Grid properties. Adds grid style
extraction, grid property setup, display:grid handling, and grid-aware
LTR/RTL fixture support to the C++, Java, and JavaScript test emitters.

Differential Revision: D93946258
Summary:
Add HTML fixture files for CSS Grid tests and generated test output for
C++, Java, and JavaScript.

Includes:
- HTML fixture files defining grid layout test cases
- Generated C++ tests (YGGridTest.cpp, YGGridTestFlows.cpp, ~150 individual test files)
- Generated Java tests (YGGridTest.java, YGGridTestFlows.java, ~150 individual test files)
- Generated JavaScript tests (YGGridTest.test.ts, YGGridTestFlows.test.ts, ~150 individual test files)

Differential Revision: D93946254
Summary:
Pull Request resolved: facebook#1887

Add grid property support to the Yoga Playground website component.

Includes:
- Grid display mode support
- Grid template columns/rows properties
- Grid auto columns/rows properties
- Grid column/row start/end properties (including span support)
- Grid track value parsing (auto, fr, %, px, minmax)

Differential Revision: D93959518
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants