-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstylesheet_example.py
More file actions
295 lines (254 loc) · 8.1 KB
/
stylesheet_example.py
File metadata and controls
295 lines (254 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""Example demonstrating the StyleSheet system in YDNATL."""
from ydnatl import *
from ydnatl.styles import CSSStyle, StyleSheet, Theme
def basic_stylesheet_example():
"""Basic example of using StyleSheet."""
print("=== Basic StyleSheet Example ===\n")
# Create stylesheet
stylesheet = StyleSheet()
# Register button styles
btn_primary = stylesheet.register(
"btn-primary",
CSSStyle(
background_color="#007bff",
color="white",
padding="10px 20px",
border_radius="5px",
border="none",
font_weight="600",
cursor="pointer",
_hover=CSSStyle(background_color="#0056b3"),
),
)
btn_secondary = stylesheet.register(
"btn-secondary",
CSSStyle(
background_color="#6c757d",
color="white",
padding="10px 20px",
border_radius="5px",
border="none",
font_weight="600",
cursor="pointer",
_hover=CSSStyle(background_color="#5a6268"),
),
)
# Create HTML using the registered classes
page = HTML(
Head(Title("StyleSheet Example"), Style(stylesheet.render())),
Body(
Div(
H1("Button Examples"),
Button("Primary Button", class_name=btn_primary),
Button("Secondary Button", class_name=btn_secondary),
)
),
)
print(page.render(pretty=True))
def theme_example():
"""Example using preset themes."""
print("\n\n=== Theme Example ===\n")
# Use a preset theme
theme = Theme.modern()
# Create stylesheet with theme
stylesheet = StyleSheet(theme=theme)
# Register styles that use theme variables
btn = stylesheet.register(
"btn",
CSSStyle(
background_color="var(--color-primary)",
color="var(--color-white)",
padding="var(--spacing-md)",
border_radius="6px",
font_family="var(--font-body)",
border="none",
cursor="pointer",
_hover=CSSStyle(background_color="var(--color-primary-dark)"),
),
)
card = stylesheet.register(
"card",
CSSStyle(
background_color="var(--color-white)",
padding="var(--spacing-lg)",
border_radius="8px",
box_shadow="0 1px 3px rgba(0, 0, 0, 0.1)",
margin="var(--spacing-md)",
),
)
# Create HTML
page = HTML(
Head(Title("Theme Example"), Style(stylesheet.render())),
Body(
Div(
H2("Welcome"),
Paragraph("This card uses theme variables."),
Button("Click Me", class_name=btn),
class_name=card,
),
Div(
H2("Another Card"),
Paragraph("Consistent styling across components."),
Button("Learn More", class_name=btn),
class_name=card,
),
),
)
print(page.render(pretty=True))
def bem_example():
"""Example using BEM naming convention."""
print("\n\n=== BEM Example ===\n")
stylesheet = StyleSheet()
# Register BEM styles
card = stylesheet.register_bem(
"card",
style=CSSStyle(
background="white",
border_radius="8px",
box_shadow="0 2px 4px rgba(0,0,0,0.1)",
overflow="hidden",
),
)
card_header = stylesheet.register_bem(
"card",
element="header",
style=CSSStyle(
padding="20px",
background="#f8f9fa",
border_bottom="1px solid #dee2e6",
font_weight="bold",
),
)
card_body = stylesheet.register_bem(
"card", element="body", style=CSSStyle(padding="20px")
)
card_footer = stylesheet.register_bem(
"card",
element="footer",
style=CSSStyle(
padding="20px", background="#f8f9fa", border_top="1px solid #dee2e6"
),
)
card_featured = stylesheet.register_bem(
"card", modifier="featured", style=CSSStyle(border="3px solid #007bff")
)
# Create HTML using BEM classes
page = HTML(
Head(Title("BEM Example"), Style(stylesheet.render())),
Body(
H1("BEM Card Examples"),
# Regular card
Div(
Div("Card Header", class_name=card_header),
Div(Paragraph("Card body content goes here."), class_name=card_body),
Div("Card Footer", class_name=card_footer),
class_name=card,
),
# Featured card
Div(
Div("Featured Card", class_name=card_header),
Div(
Paragraph("This is a featured card with special styling."),
class_name=card_body,
),
Div("Footer", class_name=card_footer),
class_name=f"{card} {card_featured}",
),
),
)
print(page.render(pretty=True))
def responsive_example():
"""Example with responsive breakpoints."""
print("\n\n=== Responsive Example ===\n")
stylesheet = StyleSheet()
# Register responsive styles
container = stylesheet.register(
"container",
CSSStyle(
padding="10px",
margin="0 auto",
_sm=CSSStyle(padding="15px", max_width="640px"),
_md=CSSStyle(padding="20px", max_width="768px"),
_lg=CSSStyle(padding="30px", max_width="1024px"),
),
)
grid = stylesheet.register(
"grid",
CSSStyle(
display="grid",
grid_template_columns="1fr",
gap="10px",
_md=CSSStyle(grid_template_columns="1fr 1fr"),
_lg=CSSStyle(grid_template_columns="1fr 1fr 1fr"),
),
)
# Create HTML
page = HTML(
Head(
Title("Responsive Example"),
Meta(name="viewport", content="width=device-width, initial-scale=1.0"),
Style(stylesheet.render()),
),
Body(
Div(
H1("Responsive Grid"),
Div(
Div("Item 1"),
Div("Item 2"),
Div("Item 3"),
Div("Item 4"),
Div("Item 5"),
Div("Item 6"),
class_name=grid,
),
class_name=container,
)
),
)
print(page.render(pretty=True))
def combined_with_inline_example():
"""Example combining stylesheet classes with inline styles."""
print("\n\n=== Combined Stylesheet + Inline Example ===\n")
stylesheet = StyleSheet()
# Register base button style
btn = stylesheet.register(
"btn",
CSSStyle(
padding="10px 20px",
border_radius="5px",
border="none",
font_weight="600",
cursor="pointer",
),
)
# Create HTML using both class and inline styles
page = HTML(
Head(Title("Combined Example"), Style(stylesheet.render())),
Body(
H1("Combining Stylesheet and Inline Styles"),
# Base class + inline override
Button("Blue Button", class_name=btn).add_styles(
{"background-color": "#007bff", "color": "white"}
),
# Same base class, different inline styles
Button("Green Button", class_name=btn).add_styles(
{"background-color": "#28a745", "color": "white"}
),
# Same base class, more inline overrides
Button("Large Red Button", class_name=btn).add_styles(
{
"background-color": "#dc3545",
"color": "white",
"padding": "15px 30px",
"font-size": "18px",
}
),
),
)
print(page.render(pretty=True))
if __name__ == "__main__":
basic_stylesheet_example()
theme_example()
bem_example()
responsive_example()
combined_with_inline_example()