Skip to content

Commit e9a0d6c

Browse files
authored
Update UI alignment docs (#10303)
# Objective - Address inconsistent term usage in the docs for the alignment properties for UI nodes. Fixes #10218 - `JustifyContent::Stretch` is missing despite being supported by Taffy, being as the default value for Grids, so it should be added to Bevy as well ## Solution - Consistently provide links to the mdn site for the css equivalent - Match (mostly) the documentation given on the pub struct and the underlying enums - Use the term `items` consistently to refer each child in the container - Add `JustifyContent::Stretch` and map it to Taffy ## Migration Guide - The `JustifyContents` enum has been expanded to include `JustifyContents::Stretch`.
1 parent 0c2c52a commit e9a0d6c

File tree

2 files changed

+81
-51
lines changed

2 files changed

+81
-51
lines changed

crates/bevy_ui/src/layout/convert.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ impl From<JustifyContent> for Option<taffy::style::JustifyContent> {
218218
JustifyContent::FlexStart => taffy::style::JustifyContent::FlexStart.into(),
219219
JustifyContent::FlexEnd => taffy::style::JustifyContent::FlexEnd.into(),
220220
JustifyContent::Center => taffy::style::JustifyContent::Center.into(),
221+
JustifyContent::Stretch => taffy::style::JustifyContent::Stretch.into(),
221222
JustifyContent::SpaceBetween => taffy::style::JustifyContent::SpaceBetween.into(),
222223
JustifyContent::SpaceAround => taffy::style::JustifyContent::SpaceAround.into(),
223224
JustifyContent::SpaceEvenly => taffy::style::JustifyContent::SpaceEvenly.into(),

crates/bevy_ui/src/ui_node.rs

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -216,44 +216,50 @@ pub struct Style {
216216
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio>
217217
pub aspect_ratio: Option<f32>,
218218

219-
/// - For Flexbox containers, sets default cross-axis alignment of the child items.
219+
/// Used to control how each individual item is aligned by default within the space they're given.
220+
/// - For Flexbox containers, sets default cross axis alignment of the child items.
220221
/// - For CSS Grid containers, controls block (vertical) axis alignment of children of this grid container within their grid areas.
221222
///
222223
/// This value is overridden if [`AlignSelf`] on the child node is set.
223224
///
224225
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-items>
225226
pub align_items: AlignItems,
226227

227-
/// - For Flexbox containers, this property has no effect. See `justify_content` for main-axis alignment of flex items.
228+
/// Used to control how each individual item is aligned by default within the space they're given.
229+
/// - For Flexbox containers, this property has no effect. See `justify_content` for main axis alignment of flex items.
228230
/// - For CSS Grid containers, sets default inline (horizontal) axis alignment of child items within their grid areas.
229231
///
230232
/// This value is overridden if [`JustifySelf`] on the child node is set.
231233
///
232234
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-items>
233235
pub justify_items: JustifyItems,
234236

235-
/// - For Flexbox items, controls cross-axis alignment of the item.
237+
/// Used to control how the specified item is aligned within the space it's given.
238+
/// - For Flexbox items, controls cross axis alignment of the item.
236239
/// - For CSS Grid items, controls block (vertical) axis alignment of a grid item within its grid area.
237240
///
238241
/// If set to `Auto`, alignment is inherited from the value of [`AlignItems`] set on the parent node.
239242
///
240243
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-self>
241244
pub align_self: AlignSelf,
242245

243-
/// - For Flexbox items, this property has no effect. See `justify_content` for main-axis alignment of flex items.
246+
/// Used to control how the specified item is aligned within the space it's given.
247+
/// - For Flexbox items, this property has no effect. See `justify_content` for main axis alignment of flex items.
244248
/// - For CSS Grid items, controls inline (horizontal) axis alignment of a grid item within its grid area.
245249
///
246250
/// If set to `Auto`, alignment is inherited from the value of [`JustifyItems`] set on the parent node.
247251
///
248252
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self>
249253
pub justify_self: JustifySelf,
250254

251-
/// - For Flexbox containers, controls alignment of lines if flex_wrap is set to [`FlexWrap::Wrap`] and there are multiple lines of items.
255+
/// Used to control how items are distributed.
256+
/// - For Flexbox containers, controls alignment of lines if `flex_wrap` is set to [`FlexWrap::Wrap`] and there are multiple lines of items.
252257
/// - For CSS Grid containers, controls alignment of grid rows.
253258
///
254259
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-content>
255260
pub align_content: AlignContent,
256261

262+
/// Used to control how items are distributed.
257263
/// - For Flexbox containers, controls alignment of items in the main axis.
258264
/// - For CSS Grid containers, controls alignment of grid columns.
259265
///
@@ -443,27 +449,31 @@ impl Default for Style {
443449
}
444450
}
445451

446-
/// How items are aligned according to the cross axis
452+
/// Used to control how each individual item is aligned by default within the space they're given.
453+
/// - For Flexbox containers, sets default cross axis alignment of the child items.
454+
/// - For CSS Grid containers, controls block (vertical) axis alignment of children of this grid container within their grid areas.
455+
///
456+
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-items>
447457
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)]
448458
#[reflect(PartialEq, Serialize, Deserialize)]
449459
pub enum AlignItems {
450460
/// The items are packed in their default position as if no alignment was applied.
451461
Default,
452-
/// Items are packed towards the start of the axis.
462+
/// The items are packed towards the start of the axis.
453463
Start,
454-
/// Items are packed towards the end of the axis.
464+
/// The items are packed towards the end of the axis.
455465
End,
456-
/// Items are packed towards the start of the axis, unless the flex direction is reversed;
466+
/// The items are packed towards the start of the axis, unless the flex direction is reversed;
457467
/// then they are packed towards the end of the axis.
458468
FlexStart,
459-
/// Items are packed towards the end of the axis, unless the flex direction is reversed;
469+
/// The items are packed towards the end of the axis, unless the flex direction is reversed;
460470
/// then they are packed towards the start of the axis.
461471
FlexEnd,
462-
/// Items are aligned at the center.
472+
/// The items are packed along the center of the axis.
463473
Center,
464-
/// Items are aligned at the baseline.
474+
/// The items are packed such that their baselines align.
465475
Baseline,
466-
/// Items are stretched across the whole cross axis.
476+
/// The items are stretched to fill the space they're given.
467477
Stretch,
468478
}
469479

@@ -477,21 +487,25 @@ impl Default for AlignItems {
477487
}
478488
}
479489

480-
/// How items are aligned according to the main axis
490+
/// Used to control how each individual item is aligned by default within the space they're given.
491+
/// - For Flexbox containers, this property has no effect. See `justify_content` for main axis alignment of flex items.
492+
/// - For CSS Grid containers, sets default inline (horizontal) axis alignment of child items within their grid areas.
493+
///
494+
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-items>
481495
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)]
482496
#[reflect(PartialEq, Serialize, Deserialize)]
483497
pub enum JustifyItems {
484498
/// The items are packed in their default position as if no alignment was applied.
485499
Default,
486-
/// Items are packed towards the start of the axis.
500+
/// The items are packed towards the start of the axis.
487501
Start,
488-
/// Items are packed towards the end of the axis.
502+
/// The items are packed towards the end of the axis.
489503
End,
490-
/// Items are aligned at the center.
504+
/// The items are packed along the center of the axis
491505
Center,
492-
/// Items are aligned at the baseline.
506+
/// The items are packed such that their baselines align.
493507
Baseline,
494-
/// Items are stretched across the whole main axis.
508+
/// The items are stretched to fill the space they're given.
495509
Stretch,
496510
}
497511

@@ -505,8 +519,11 @@ impl Default for JustifyItems {
505519
}
506520
}
507521

508-
/// How this item is aligned according to the cross axis.
509-
/// Overrides [`AlignItems`].
522+
/// Used to control how the specified item is aligned within the space it's given.
523+
/// - For Flexbox items, controls cross axis alignment of the item.
524+
/// - For CSS Grid items, controls block (vertical) axis alignment of a grid item within its grid area.
525+
///
526+
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-self>
510527
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)]
511528
#[reflect(PartialEq, Serialize, Deserialize)]
512529
pub enum AlignSelf {
@@ -522,11 +539,11 @@ pub enum AlignSelf {
522539
/// This item will be aligned with the end of the axis, unless the flex direction is reversed;
523540
/// then it will be aligned with the start of the axis.
524541
FlexEnd,
525-
/// This item will be aligned at the center.
542+
/// This item will be aligned along the center of the axis.
526543
Center,
527544
/// This item will be aligned at the baseline.
528545
Baseline,
529-
/// This item will be stretched across the whole cross axis.
546+
/// This item will be stretched to fill the container.
530547
Stretch,
531548
}
532549

@@ -540,8 +557,11 @@ impl Default for AlignSelf {
540557
}
541558
}
542559

543-
/// How this item is aligned according to the main axis.
544-
/// Overrides [`JustifyItems`].
560+
/// Used to control how the specified item is aligned within the space it's given.
561+
/// - For Flexbox items, this property has no effect. See `justify_content` for main axis alignment of flex items.
562+
/// - For CSS Grid items, controls inline (horizontal) axis alignment of a grid item within its grid area.
563+
///
564+
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self>
545565
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)]
546566
#[reflect(PartialEq, Serialize, Deserialize)]
547567
pub enum JustifySelf {
@@ -551,11 +571,11 @@ pub enum JustifySelf {
551571
Start,
552572
/// This item will be aligned with the end of the axis.
553573
End,
554-
/// This item will be aligned at the center.
574+
/// This item will be aligned along the center of the axis.
555575
Center,
556576
/// This item will be aligned at the baseline.
557577
Baseline,
558-
/// This item will be stretched across the whole main axis.
578+
/// This item will be stretched to fill the space it's given.
559579
Stretch,
560580
}
561581

@@ -569,34 +589,35 @@ impl Default for JustifySelf {
569589
}
570590
}
571591

572-
/// Defines how each line is aligned within the flexbox.
592+
/// Used to control how items are distributed.
593+
/// - For Flexbox containers, controls alignment of lines if `flex_wrap` is set to [`FlexWrap::Wrap`] and there are multiple lines of items.
594+
/// - For CSS Grid containers, controls alignment of grid rows.
573595
///
574-
/// It only applies if [`FlexWrap::Wrap`] is present and if there are multiple lines of items.
596+
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-content>
575597
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)]
576598
#[reflect(PartialEq, Serialize, Deserialize)]
577599
pub enum AlignContent {
578600
/// The items are packed in their default position as if no alignment was applied.
579601
Default,
580-
/// Each line moves towards the start of the cross axis.
602+
/// The items are packed towards the start of the axis.
581603
Start,
582-
/// Each line moves towards the end of the cross axis.
604+
/// The items are packed towards the end of the axis.
583605
End,
584-
/// Each line moves towards the start of the cross axis, unless the flex direction is reversed; then the line moves towards the end of the cross axis.
606+
/// The items are packed towards the start of the axis, unless the flex direction is reversed;
607+
/// then the items are packed towards the end of the axis.
585608
FlexStart,
586-
/// Each line moves towards the end of the cross axis, unless the flex direction is reversed; then the line moves towards the start of the cross axis.
609+
/// The items are packed towards the end of the axis, unless the flex direction is reversed;
610+
/// then the items are packed towards the start of the axis.
587611
FlexEnd,
588-
/// Each line moves towards the center of the cross axis.
612+
/// The items are packed along the center of the axis.
589613
Center,
590-
/// Each line will stretch to fill the remaining space.
614+
/// The items are stretched to fill the container along the axis.
591615
Stretch,
592-
/// Each line fills the space it needs, putting the remaining space, if any,
593-
/// between the lines.
616+
/// The items are distributed such that the gap between any two items is equal.
594617
SpaceBetween,
595-
/// The gap between the first and last items is exactly the same as the gap between items.
596-
/// The gaps are distributed evenly.
618+
/// The items are distributed such that the gap between and around any two items is equal.
597619
SpaceEvenly,
598-
/// Each line fills the space it needs, putting the remaining space, if any,
599-
/// around the lines.
620+
/// The items are distributed such that the gap between and around any two items is equal, with half-size gaps on either end.
600621
SpaceAround,
601622
}
602623

@@ -610,28 +631,36 @@ impl Default for AlignContent {
610631
}
611632
}
612633

613-
/// Defines how items are aligned according to the main axis
634+
/// Used to control how items are distributed.
635+
/// - For Flexbox containers, controls alignment of items in the main axis.
636+
/// - For CSS Grid containers, controls alignment of grid columns.
637+
///
638+
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content>
614639
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)]
615640
#[reflect(PartialEq, Serialize, Deserialize)]
616641
pub enum JustifyContent {
617642
/// The items are packed in their default position as if no alignment was applied.
618643
Default,
619-
/// Items are packed toward the start of the axis.
644+
/// The items are packed towards the start of the axis.
620645
Start,
621-
/// Items are packed toward the end of the axis.
646+
/// The items are packed towards the end of the axis.
622647
End,
623-
/// Pushed towards the start, unless the flex direction is reversed; then pushed towards the end.
648+
/// The items are packed towards the start of the axis, unless the flex direction is reversed;
649+
/// then the items are packed towards the end of the axis.
624650
FlexStart,
625-
/// Pushed towards the end, unless the flex direction is reversed; then pushed towards the start.
651+
/// The items are packed towards the end of the axis, unless the flex direction is reversed;
652+
/// then the items are packed towards the start of the axis.
626653
FlexEnd,
627-
/// Centered along the main axis.
654+
/// The items are packed along the center of the axis.
628655
Center,
629-
/// Remaining space is distributed between the items.
656+
/// The items are stretched to fill the container along the axis.
657+
Stretch,
658+
/// The items are distributed such that the gap between any two items is equal.
630659
SpaceBetween,
631-
/// Remaining space is distributed around the items.
632-
SpaceAround,
633-
/// Like [`JustifyContent::SpaceAround`] but with even spacing between items.
660+
/// The items are distributed such that the gap between and around any two items is equal.
634661
SpaceEvenly,
662+
/// The items are distributed such that the gap between and around any two items is equal, with half-size gaps on either end.
663+
SpaceAround,
635664
}
636665

637666
impl JustifyContent {

0 commit comments

Comments
 (0)