Skip to content

CSS Grid 1/9: Grid style types and public API#1893

Closed
intergalacticspacehighway wants to merge 4 commits into
facebook:mainfrom
intergalacticspacehighway:css-grid-1-api
Closed

CSS Grid 1/9: Grid style types and public API#1893
intergalacticspacehighway wants to merge 4 commits into
facebook:mainfrom
intergalacticspacehighway:css-grid-1-api

Conversation

@intergalacticspacehighway
Copy link
Copy Markdown
Contributor

Summary

  • Add foundational data types, enums, style properties, and C API for expressing CSS Grid layouts
  • Includes GridLine.h, GridTrack.h, GridTrackType.h, Display::Grid, new alignment enums (Align::Start/End, Justify::Auto/Stretch/Start/End)
  • C API: YGGridTrackList.h/cpp, YGNodeStyle grid setters/getters
  • Layout helper updates, Node.h relativePosition made public

Split from #1865. Part of the CSS Grid implementation series.

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
@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 25, 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 28, 2026 9:14am

Request Review

@meta-cla meta-cla Bot added the CLA Signed label Feb 25, 2026
@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 Feb 25, 2026
Comment thread yoga/YGGridTrackList.h Outdated
Comment on lines +68 to +95
/**
* Set the grid-template-rows property on a node.
*/
YG_EXPORT void YGNodeStyleSetGridTemplateRows(
YGNodeRef node,
YGGridTrackListRef trackList);

/**
* Set the grid-template-columns property on a node.
*/
YG_EXPORT void YGNodeStyleSetGridTemplateColumns(
YGNodeRef node,
YGGridTrackListRef trackList);

/**
* Set the grid-auto-rows property on a node.
*/
YG_EXPORT void YGNodeStyleSetGridAutoRows(
YGNodeRef node,
YGGridTrackListRef trackList);

/**
* Set the grid-auto-columns property on a node.
*/
YG_EXPORT void YGNodeStyleSetGridAutoColumns(
YGNodeRef node,
YGGridTrackListRef trackList);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These should probably go in YGNodeStyle.h

Comment thread yoga/YGGridTrackList.cpp Outdated

// Internal representation of a grid track value
struct YGGridTrackValue {
enum class Type { Points, Percent, Fr, Auto, MinMax };
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What does Auto mean here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's one of the track sizes https://www.w3.org/TR/css-grid-1/#track-sizes. e.g. grid-template-columns: "auto 100px 1fr"

Comment thread yoga/YGGridTrackList.h Outdated
*/
YG_EXPORT void YGNodeStyleSetGridTemplateRows(
YGNodeRef node,
YGGridTrackListRef trackList);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not clear from me in this API, who takes ownership of the TrackList? Does the Yoga node free it at some point, or must the user?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

User would need to free it.

auto gridTemplateColumns = YGGridTrackListCreate();
YGGridTrackListAddTrack(gridTemplateColumns, YGPoints(40));
YGGridTrackListAddTrack(gridTemplateColumns, YGPoints(40));
// free gridTemplateColumns
YGGridTrackListFree(gridTemplateColumns);

it was inspired from YGConfigNew and YGNodeNewWithConfig implementation. Maybe YGGridTrackListCreate could be renamed to YGGridTrackListNew?

Comment thread yoga/YGGridTrackList.cpp Outdated
Comment on lines +34 to +35
delete minValue;
delete maxValue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we use RAII-style handles where we can get away with it for internals?

Comment thread yoga/YGGridTrackList.cpp Outdated
Comment on lines +68 to +70
for (auto* track : tracks) {
delete track;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See other comment about RAII. If we want the list to have assumed ownership, would be best to represent internally as unique_ptr, so move checking can catch any issues.

Comment thread yoga/YGGridTrackList.cpp Outdated
}
};

// Internal representation of a grid track list
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this comment accurate?

From what I could tell, internally, we use std::vector<GridTrack>, and this type is more for the public API, before internal version?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's inaccurate, removed.

Comment thread yoga/YGGridTrackList.h Outdated
Comment on lines +42 to +60
/**
* Create a grid track value with a points (px) length.
*/
YG_EXPORT YGGridTrackValueRef YGPoints(float points);

/**
* Create a grid track value with a percentage length.
*/
YG_EXPORT YGGridTrackValueRef YGPercent(float percent);

/**
* Create a grid track value with a flexible (fr) length.
*/
YG_EXPORT YGGridTrackValueRef YGFr(float fr);

/**
* Create a grid track value with auto sizing.
*/
YG_EXPORT YGGridTrackValueRef YGAuto(void);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since these aren't namespaced, it means that we would have global YGPoints, etc, which is grid specific. If Grid specific units, would be better to include in API naming.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated, will also handle it in gentest-cpp.js

Comment thread yoga/YGGridTrackList.cpp Outdated
YGGridTrackList(YGGridTrackList&&) = delete;
YGGridTrackList& operator=(YGGridTrackList&&) = delete;

GridTrackList toGridTrackList() const {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to think if there is a way to avoid doing the extra allocations for setting style, vs internal.

Can the operations to add track size to the style on a Node, add directly to underlying yoga::style vector, instead of exposing heap allocated list as the primitive? Or do you think exposing it will be helpful, e.g. for RN integration?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Revisited it here. I added this API when testing gentest fixtures but didn't revisit later 😅. New changes does not require manual free or extra allocation and directly updates internal vector.

YGNodeStyleSetGridTemplateColumnsCount(node, 3);
YGNodeStyleSetGridTemplateColumn(node, 0, YGGridTrackTypePoints, 40);
YGNodeStyleSetGridTemplateColumn(node, 1, YGGridTrackTypePercent, 20);
YGNodeStyleSetGridTemplateColumnMinMax(minmax, 2, YGGridTrackTypePoints, 20, YGGridTrackTypePoints, 40);

Comment thread yoga/YGGridTrackList.cpp Outdated
}

void YGGridTrackListFree(YGGridTrackListRef list) {
delete list;
Copy link
Copy Markdown
Contributor

@NickGerleman NickGerleman Feb 28, 2026

Choose a reason for hiding this comment

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

Subtle bug here, we don't have concrete type of what we are deleting, beyond the marker interface, and would not call destructors.

Suggested change
delete list;
delete static_cast<YGGridTrackList*>(list);

In other places, we provide overload of resolveRef to convert from C ABI type, to concrete C++ internal type, but it's just a convenience for the static_cast in less characters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Just removed this API in the new changes 😅

Comment thread yoga/style/GridLine.h Outdated
Comment on lines +52 to +58
bool operator==(const GridLine& other) const {
return type == other.type && integer == other.integer;
}

bool operator!=(const GridLine& other) const {
return !(*this == other);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This will generate everything needed.

Suggested change
bool operator==(const GridLine& other) const {
return type == other.type && integer == other.integer;
}
bool operator!=(const GridLine& other) const {
return !(*this == other);
}
bool operator==(const GridLine& other) const = default;

Comment thread yoga/style/GridLine.h Outdated
// Line position (1, 2, -1, -2, etc)
int32_t integer;

static GridLine auto_() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: I think all of these can be constexpr

Comment thread yoga/style/GridTrack.h Outdated
Comment on lines +52 to +59
bool operator==(const GridTrackSize& other) const {
return minSizingFunction == other.minSizingFunction &&
maxSizingFunction == other.maxSizingFunction;
}

bool operator!=(const GridTrackSize& other) const {
return !(*this == other);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
bool operator==(const GridTrackSize& other) const {
return minSizingFunction == other.minSizingFunction &&
maxSizingFunction == other.maxSizingFunction;
}
bool operator!=(const GridTrackSize& other) const {
return !(*this == other);
}
bool operator==(const GridTrackSize& other) const = default;

Comment thread yoga/style/GridTrack.h Outdated
bool infinitelyGrowable = false;

// Static factory methods for common cases
static GridTrackSize auto_() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the reserved keyword is how we ended up with ofAuto in a lot of places 😅

Comment thread yoga/style/GridTrack.h
Comment on lines +19 to +24
// These are used in the grid layout algorithm when distributing spaces among
// tracks
// TODO: maybe move them to TrackSizing since these are track states
float baseSize = 0.0f;
float growthLimit = 0.0f;
bool infinitelyGrowable = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If GridTrack is on the yoga::Style, we definitely want to avoid keep layout state on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I have a version without it. Can i do it in the algorithm PR when we merge this one?

Comment thread yoga/YGNodeStyle.h Outdated
YGNodeRef node,
YGGridTrackListRef trackList);
YG_EXPORT void YGNodeStyleSetGridTemplateColumns(
uint32_t count);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The rest of the public API exposes size_t for indices

@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented Mar 2, 2026

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

NickGerleman pushed a commit to NickGerleman/react-native that referenced this pull request Mar 3, 2026
Summary:
- Add foundational data types, enums, style properties, and C API for expressing CSS Grid layouts
- Includes `GridLine.h`, `GridTrack.h`, `GridTrackType.h`, `Display::Grid`, new alignment enums (`Align::Start/End`, `Justify::Auto/Stretch/Start/End`)
- C API: `YGGridTrackList.h/cpp`, `YGNodeStyle` grid setters/getters
- Layout helper updates, `Node.h` `relativePosition` made public

Split from facebook/yoga#1865. Part of the CSS Grid implementation series.

X-link: facebook/yoga#1893

Differential Revision: D94867121

Pulled By: NickGerleman
@meta-codesync meta-codesync Bot closed this in 0752485 Mar 5, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented Mar 5, 2026

@NickGerleman merged this pull request in 0752485.

meta-codesync Bot pushed a commit to facebook/react-native that referenced this pull request Mar 5, 2026
Summary:
Pull Request resolved: #55876

- Add foundational data types, enums, style properties, and C API for expressing CSS Grid layouts
- Includes `GridLine.h`, `GridTrack.h`, `GridTrackType.h`, `Display::Grid`, new alignment enums (`Align::Start/End`, `Justify::Auto/Stretch/Start/End`)
- C API: `YGGridTrackList.h/cpp`, `YGNodeStyle` grid setters/getters
- Layout helper updates, `Node.h` `relativePosition` made public

Split from facebook/yoga#1865. Part of the CSS Grid implementation series.

Changelog: [Internal]

X-link: facebook/yoga#1893

Reviewed By: sammy-SC

Differential Revision: D94867121

Pulled By: NickGerleman

fbshipit-source-id: adcb6d107cdd782550a614f87f84cff3ecefd806
zoontek pushed a commit to zoontek/react-native that referenced this pull request Mar 9, 2026
Summary:
Pull Request resolved: facebook#55876

- Add foundational data types, enums, style properties, and C API for expressing CSS Grid layouts
- Includes `GridLine.h`, `GridTrack.h`, `GridTrackType.h`, `Display::Grid`, new alignment enums (`Align::Start/End`, `Justify::Auto/Stretch/Start/End`)
- C API: `YGGridTrackList.h/cpp`, `YGNodeStyle` grid setters/getters
- Layout helper updates, `Node.h` `relativePosition` made public

Split from facebook/yoga#1865. Part of the CSS Grid implementation series.

Changelog: [Internal]

X-link: facebook/yoga#1893

Reviewed By: sammy-SC

Differential Revision: D94867121

Pulled By: NickGerleman

fbshipit-source-id: adcb6d107cdd782550a614f87f84cff3ecefd806
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Merged 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.

3 participants