Skip to content

Commit 05898cd

Browse files
committed
Add grid properties to style
1 parent 015551c commit 05898cd

File tree

3 files changed

+109
-21
lines changed

3 files changed

+109
-21
lines changed

mesop/component_helpers/style.py

+57-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33

44
import mesop.protos.ui_pb2 as pb
55

6+
ContentAlignmentValues = Literal[
7+
"center",
8+
"start",
9+
"end",
10+
"flex",
11+
"flex",
12+
"left",
13+
"right",
14+
"space-between",
15+
"space-around",
16+
"space-evenly",
17+
"stretch",
18+
]
19+
ItemAlignmentValues = Literal[
20+
"normal",
21+
"stretch",
22+
"center",
23+
"start",
24+
"end",
25+
]
626
OverflowValues = Literal["visible", "hidden", "clip", "scroll", "auto"]
727

828

@@ -92,12 +112,14 @@ class Style:
92112
Represents the style configuration for a UI component.
93113
94114
Attributes:
115+
align_content: Aligns the flexible container's items on the cross-axis. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/align-content).
95116
align_items: Specifies the default alignment for items inside a flexible container. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/align-items).
96117
background: Sets the background color or image of the component. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/background).
97118
border: Defines the border properties for each side of the component. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/border).
98119
border_radius: Defines the border radius. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/border-radius).
99120
box_shadow: Defines the box shadow. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow).
100121
color: Sets the color of the text inside the component. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/color).
122+
column_gap: Sets the gap between columns. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/column-gap).
101123
columns: Specifies the number of columns in a multi-column element. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/columns).
102124
display: Defines the display type of the component. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/display).
103125
flex_basis: Specifies the initial length of a flexible item. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis).
@@ -109,6 +131,14 @@ class Style:
109131
font_style: Specifies the font style for text. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style).
110132
font_weight: Sets the weight (or boldness) of the font. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight).
111133
gap: Sets the gap. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/gap).
134+
grid_area: Sets the grid area. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area).
135+
grid_column_start: Sets the grid column start. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column-start).
136+
grid_column_end: Sets the grid column end. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column-end).
137+
grid_row_start: Sets the grid row start. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row-start).
138+
grid_row_end: Sets the grid row end. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row-end).
139+
grid_template_areas: Sets the grid template areas; each element is a row. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-areas).
140+
grid_template_columns: Sets the grid template columns. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns).
141+
grid_template_rows: Sets the grid template rows. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-rows).
112142
height: Sets the height of the component. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/height).
113143
justify_content: Aligns the flexible container's items on the main-axis. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content).
114144
letter_spacing: Increases or decreases the space between characters in text. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/letter-spacing).
@@ -118,6 +148,7 @@ class Style:
118148
overflow_y: Specifies the handling of overflow in the vertical direction. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-y).
119149
padding: Sets the padding space required on each side of an element. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/padding).
120150
position: Specifies the type of positioning method used for an element (static, relative, absolute, fixed, or sticky). See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/position).
151+
row_gap: Sets the gap between rows. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/row-gap).
121152
text_align: Specifies the horizontal alignment of text in an element. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/text-align).
122153
text_decoration: Specifies the decoration added to text. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration).
123154
text_overflow: Specifies how overflowed content that is not displayed should be signaled to the user. See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow).
@@ -128,18 +159,14 @@ class Style:
128159
# For literal properties, make the most commonly used option the first literal
129160
# element, as it will be used as the default value by the editor when creating that property.
130161

131-
align_items: Literal[
132-
"normal",
133-
"stretch",
134-
"center",
135-
"start",
136-
"end",
137-
] | None = None
162+
align_content: ContentAlignmentValues | None = None
163+
align_items: ItemAlignmentValues | None = None
138164
background: str | None = None
139165
border: Border | None = None
140166
border_radius: int | str | None = None
141167
box_shadow: str | None = None
142168
color: str | None = None
169+
column_gap: int | str | None = None
143170
columns: int | None = None
144171
display: Literal[
145172
# precomposed values
@@ -180,20 +207,17 @@ class Style:
180207
900,
181208
] | None = None
182209
gap: int | str | None = None
210+
grid_area: str | None = None
211+
grid_column_start: int | None = None
212+
grid_column_end: int | None = None
213+
grid_row_start: int | None = None
214+
grid_row_end: int | None = None
215+
grid_template_areas: list[str] | None = None
216+
grid_template_columns: str | None = None
217+
grid_template_rows: str | None = None
183218
height: int | str | None = None
184-
justify_content: Literal[
185-
"center",
186-
"start",
187-
"end",
188-
"flex",
189-
"flex",
190-
"left",
191-
"right",
192-
"space-between",
193-
"space-around",
194-
"space-evenly",
195-
"stretch",
196-
] | None = None
219+
justify_content: ContentAlignmentValues | None = None
220+
justify_items: ItemAlignmentValues | None = None
197221
letter_spacing: int | str | None = None
198222
line_height: float | str | None = None
199223
margin: Margin | None = None
@@ -207,6 +231,7 @@ class Style:
207231
"fixed",
208232
"sticky",
209233
] | None = None
234+
row_gap: int | str | None = None
210235
text_align: Literal[
211236
"start",
212237
"end",
@@ -229,12 +254,14 @@ class Style:
229254

230255
def to_style_proto(s: Style) -> pb.Style:
231256
return pb.Style(
257+
align_content=s.align_content,
232258
align_items=s.align_items,
233259
background=s.background,
234260
border=_map_border(s.border),
235261
border_radius=_px_str(s.border_radius),
236262
box_shadow=s.box_shadow,
237263
color=s.color,
264+
column_gap=_px_str(s.column_gap),
238265
columns=s.columns,
239266
display=s.display,
240267
flex_basis=s.flex_basis,
@@ -246,15 +273,25 @@ def to_style_proto(s: Style) -> pb.Style:
246273
font_style=s.font_style,
247274
font_weight=_map_font_weight(s.font_weight),
248275
gap=_px_str(s.gap),
276+
grid_area=s.grid_area,
277+
grid_column_start=s.grid_column_start,
278+
grid_column_end=s.grid_column_end,
279+
grid_row_start=s.grid_row_start,
280+
grid_row_end=s.grid_row_end,
281+
grid_template_areas=s.grid_template_areas,
282+
grid_template_columns=s.grid_template_columns,
283+
grid_template_rows=s.grid_template_rows,
249284
height=_px_str(s.height),
250285
justify_content=s.justify_content,
286+
justify_items=s.justify_items,
251287
letter_spacing=_px_str(s.letter_spacing),
252288
line_height=str(s.line_height),
253289
margin=_map_edge_insets(s.margin),
254290
overflow_x=s.overflow_x,
255291
overflow_y=s.overflow_y,
256292
padding=_map_edge_insets(s.padding),
257293
position=s.position,
294+
row_gap=_px_str(s.row_gap),
258295
text_align=s.text_align,
259296
text_decoration=s.text_decoration,
260297
text_overflow=s.text_overflow,

mesop/protos/ui.proto

+13-1
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,16 @@ message UserDefinedType {
217217
repeated Arg args = 1;
218218
}
219219

220-
// Next id: 32
220+
// Next id: 44
221221
message Style {
222+
optional string align_content = 32;
222223
optional string align_items = 1;
223224
optional string background = 2;
224225
optional Border border = 3;
225226
optional string border_radius = 28;
226227
optional string box_shadow = 29;
227228
optional string color = 4;
229+
optional string column_gap = 33;
228230
optional int32 columns = 5;
229231
optional string display = 6;
230232
optional string flex_basis = 7;
@@ -236,15 +238,25 @@ message Style {
236238
optional string font_style = 13;
237239
optional string font_weight = 14;
238240
optional string gap = 30;
241+
optional string grid_area = 34;
242+
optional int32 grid_column_start = 35;
243+
optional int32 grid_column_end = 36;
244+
optional int32 grid_row_start = 37;
245+
optional int32 grid_row_end = 38;
246+
repeated string grid_template_areas = 39;
247+
optional string grid_template_columns = 40;
248+
optional string grid_template_rows = 41;
239249
optional string height = 15;
240250
optional string justify_content = 16;
251+
optional string justify_items = 42;
241252
optional string letter_spacing = 17;
242253
optional string line_height = 31;
243254
optional EdgeInsets margin = 18;
244255
optional string overflow_x = 19;
245256
optional string overflow_y = 20;
246257
optional EdgeInsets padding = 21;
247258
optional string position = 22;
259+
optional string row_gap = 43;
248260
optional string text_align = 23;
249261
optional string text_decoration = 24;
250262
optional string text_overflow = 25;

mesop/web/src/utils/styles.ts

+39
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export function formatStyle(styleObj: Style): string {
55
if (!styleObj) {
66
return style;
77
}
8+
if (styleObj.getAlignContent()) {
9+
style += `align-content: ${styleObj.getAlignContent()};`;
10+
}
811
if (styleObj.getAlignItems()) {
912
style += `align-items: ${styleObj.getAlignItems()};`;
1013
}
@@ -40,6 +43,9 @@ export function formatStyle(styleObj: Style): string {
4043
if (styleObj.getColor()) {
4144
style += `color: ${styleObj.getColor()};`;
4245
}
46+
if (styleObj.getColumnGap()) {
47+
style += `column-gap: ${styleObj.getColumnGap()};`;
48+
}
4349
if (styleObj.getColumns()) {
4450
style += `columns: ${styleObj.getColumns()};`;
4551
}
@@ -73,12 +79,42 @@ export function formatStyle(styleObj: Style): string {
7379
if (styleObj.getGap()) {
7480
style += `gap: ${styleObj.getGap()};`;
7581
}
82+
if (styleObj.getGridArea()) {
83+
style += `grid-area: ${styleObj.getGridArea()};`;
84+
}
85+
if (styleObj.getGridColumnStart()) {
86+
style += `grid-column-start: ${styleObj.getGridColumnStart()};`;
87+
}
88+
if (styleObj.getGridColumnEnd()) {
89+
style += `grid-column-end: ${styleObj.getGridColumnEnd()};`;
90+
}
91+
if (styleObj.getGridRowStart()) {
92+
style += `grid-row-start: ${styleObj.getGridRowStart()};`;
93+
}
94+
if (styleObj.getGridRowEnd()) {
95+
style += `grid-row-end: ${styleObj.getGridRowEnd()};`;
96+
}
97+
if (styleObj.getGridTemplateAreasList()) {
98+
style += `grid-template-areas: ${styleObj
99+
.getGridTemplateAreasList()
100+
.map((a) => `"${a}"`)
101+
.join(' ')};`;
102+
}
103+
if (styleObj.getGridTemplateColumns()) {
104+
style += `grid-template-columns: ${styleObj.getGridTemplateColumns()};`;
105+
}
106+
if (styleObj.getGridTemplateRows()) {
107+
style += `grid-template-rows: ${styleObj.getGridTemplateRows()};`;
108+
}
76109
if (styleObj.getHeight()) {
77110
style += `height: ${styleObj.getHeight()};`;
78111
}
79112
if (styleObj.getJustifyContent()) {
80113
style += `justify-content: ${styleObj.getJustifyContent()};`;
81114
}
115+
if (styleObj.getJustifyItems()) {
116+
style += `justify-items: ${styleObj.getJustifyItems()};`;
117+
}
82118
if (styleObj.getLetterSpacing()) {
83119
style += `letter-spacing: ${styleObj.getLetterSpacing()};`;
84120
}
@@ -106,6 +142,9 @@ export function formatStyle(styleObj: Style): string {
106142
if (styleObj.getPosition()) {
107143
style += `position: ${styleObj.getPosition()};`;
108144
}
145+
if (styleObj.getRowGap()) {
146+
style += `row-gap: ${styleObj.getRowGap()};`;
147+
}
109148
if (styleObj.getTextAlign()) {
110149
style += `text-align: ${styleObj.getTextAlign()};`;
111150
}

0 commit comments

Comments
 (0)