taffy/style/alignment.rs
1//! Style types for controlling alignment
2
3/// Used to control how child nodes are aligned.
4/// For Flexbox it controls alignment in the cross axis
5/// For Grid it controls alignment in the block axis
6///
7/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-items)
8#[derive(Copy, Clone, PartialEq, Eq, Debug)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub enum AlignItems {
11 /// Items are packed toward the start of the axis
12 Start,
13 /// Items are packed toward the end of the axis
14 End,
15 /// Items are packed towards the flex-relative start of the axis.
16 ///
17 /// For flex containers with flex_direction RowReverse or ColumnReverse this is equivalent
18 /// to End. In all other cases it is equivalent to Start.
19 FlexStart,
20 /// Items are packed towards the flex-relative end of the axis.
21 ///
22 /// For flex containers with flex_direction RowReverse or ColumnReverse this is equivalent
23 /// to Start. In all other cases it is equivalent to End.
24 FlexEnd,
25 /// Items are packed along the center of the cross axis
26 Center,
27 /// Items are aligned such as their baselines align
28 Baseline,
29 /// Stretch to fill the container
30 Stretch,
31}
32
33#[cfg(feature = "parse")]
34crate::util::parse::impl_parse_for_keyword_enum!(AlignItems,
35 "start" => Start,
36 "end" => End,
37 "flex-start" => FlexStart,
38 "flex-end" => FlexEnd,
39 "center" => Center,
40 "baseline" => Baseline,
41 "stretch" => Stretch,
42);
43
44/// Used to control how child nodes are aligned.
45/// Does not apply to Flexbox, and will be ignored if specified on a flex container
46/// For Grid it controls alignment in the inline axis
47///
48/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-items)
49pub type JustifyItems = AlignItems;
50/// Controls alignment of an individual node
51///
52/// Overrides the parent Node's `AlignItems` property.
53/// For Flexbox it controls alignment in the cross axis
54/// For Grid it controls alignment in the block axis
55///
56/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-self)
57pub type AlignSelf = AlignItems;
58/// Controls alignment of an individual node
59///
60/// Overrides the parent Node's `JustifyItems` property.
61/// Does not apply to Flexbox, and will be ignored if specified on a flex child
62/// For Grid it controls alignment in the inline axis
63///
64/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self)
65pub type JustifySelf = AlignItems;
66
67/// Sets the distribution of space between and around content items
68/// For Flexbox it controls alignment in the cross axis
69/// For Grid it controls alignment in the block axis
70///
71/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-content)
72#[derive(Copy, Clone, PartialEq, Eq, Debug)]
73#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
74pub enum AlignContent {
75 /// Items are packed toward the start of the axis
76 Start,
77 /// Items are packed toward the end of the axis
78 End,
79 /// Items are packed towards the flex-relative start of the axis.
80 ///
81 /// For flex containers with flex_direction RowReverse or ColumnReverse this is equivalent
82 /// to End. In all other cases it is equivalent to Start.
83 FlexStart,
84 /// Items are packed towards the flex-relative end of the axis.
85 ///
86 /// For flex containers with flex_direction RowReverse or ColumnReverse this is equivalent
87 /// to Start. In all other cases it is equivalent to End.
88 FlexEnd,
89 /// Items are centered around the middle of the axis
90 Center,
91 /// Items are stretched to fill the container
92 Stretch,
93 /// The first and last items are aligned flush with the edges of the container (no gap)
94 /// The gap between items is distributed evenly.
95 SpaceBetween,
96 /// The gap between the first and last items is exactly THE SAME as the gap between items.
97 /// The gaps are distributed evenly
98 SpaceEvenly,
99 /// The gap between the first and last items is exactly HALF the gap between items.
100 /// The gaps are distributed evenly in proportion to these ratios.
101 SpaceAround,
102}
103
104#[cfg(feature = "parse")]
105crate::util::parse::impl_parse_for_keyword_enum!(AlignContent,
106 "start" => Start,
107 "end" => End,
108 "flex-start" => FlexStart,
109 "flex-end" => FlexEnd,
110 "center" => Center,
111 "stretch" => Stretch,
112 "space-between" => SpaceBetween,
113 "space-evenly" => SpaceEvenly,
114 "space-around" => SpaceAround,
115);
116
117impl AlignContent {
118 /// Returns the reversed alignment for RTL (right-to-left) contexts.
119 pub(crate) fn reversed(self) -> Self {
120 match self {
121 Self::Start => Self::End,
122 Self::End => Self::Start,
123 Self::FlexStart => Self::FlexEnd,
124 Self::FlexEnd => Self::FlexStart,
125 Self::Stretch => Self::End,
126 style => style,
127 }
128 }
129}
130
131/// Sets the distribution of space between and around content items
132/// For Flexbox it controls alignment in the main axis
133/// For Grid it controls alignment in the inline axis
134///
135/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content)
136pub type JustifyContent = AlignContent;