Skip to content

Comments

CSS Grid#1865

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

CSS Grid#1865
intergalacticspacehighway wants to merge 68 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 4 times, most recently from c394ca6 to 69cf662 Compare November 19, 2025 05:53
@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.

@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 2 times, most recently from 087b644 to 196d320 Compare December 8, 2025 15:41
@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 Error Error Feb 18, 2026 1:38am

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 😅

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