-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example headers to patterns demos. #660
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
from dataclasses import fields | ||
|
||
import mesop as me | ||
|
||
|
||
@me.page( | ||
security_policy=me.SecurityPolicy( | ||
allowed_iframe_parents=["https://google.github.io"] | ||
), | ||
path="/headers", | ||
) | ||
def app(): | ||
is_mobile = me.viewport_size().width < 640 | ||
|
||
with me.box(style=me.Style(margin=me.Margin(bottom=15))): | ||
# Two section basic header with fluid width. | ||
# As an example, we don't use mobile view here since the header is short enough. | ||
with header(max_width=None): | ||
with header_section(): | ||
me.text( | ||
"Mesop", type="headline-6", style=me.Style(margin=me.Margin(bottom=0)) | ||
) | ||
|
||
with header_section(): | ||
me.button("Home") | ||
me.button("About") | ||
me.button("FAQ") | ||
|
||
with me.box(style=me.Style(margin=me.Margin(bottom=15))): | ||
# Two section basic header. | ||
with header(is_mobile=is_mobile): | ||
with header_section(): | ||
me.text( | ||
"Mesop", type="headline-6", style=me.Style(margin=me.Margin(bottom=0)) | ||
) | ||
|
||
with header_section(): | ||
me.button("Home") | ||
me.button("About") | ||
me.button("FAQ") | ||
|
||
with me.box(style=me.Style(margin=me.Margin(bottom=15))): | ||
# Three section basic header. | ||
with header(is_mobile=is_mobile): | ||
with header_section(): | ||
me.text( | ||
"Mesop", type="headline-6", style=me.Style(margin=me.Margin(bottom=0)) | ||
) | ||
|
||
with header_section(): | ||
me.button("Home") | ||
me.button("About") | ||
me.button("FAQ") | ||
|
||
with header_section(): | ||
me.button("Login", type="flat") | ||
|
||
with me.box(style=me.Style(margin=me.Margin(bottom=15))): | ||
# Centered header with overrides and icons | ||
with header(is_mobile=is_mobile, style=me.Style(justify_content="center")): | ||
with header_section(): | ||
with me.content_button( | ||
style=me.Style( | ||
padding=me.Padding.symmetric(vertical=30, horizontal=25) | ||
) | ||
): | ||
me.icon("home") | ||
me.text("Home") | ||
with me.content_button( | ||
style=me.Style( | ||
padding=me.Padding.symmetric(vertical=30, horizontal=25) | ||
) | ||
): | ||
me.icon("info") | ||
me.text("About") | ||
with me.content_button( | ||
style=me.Style( | ||
padding=me.Padding.symmetric(vertical=30, horizontal=25) | ||
) | ||
): | ||
me.icon("contact_support") | ||
me.text("FAQ") | ||
with me.content_button( | ||
style=me.Style( | ||
padding=me.Padding.symmetric(vertical=30, horizontal=25) | ||
) | ||
): | ||
me.icon("login") | ||
me.text("Login") | ||
|
||
with me.box(style=me.Style(margin=me.Margin(bottom=15))): | ||
# Header with overridden background | ||
with header( | ||
is_mobile=is_mobile, style=me.Style(background="#0F0F11", color="#E3E3E3") | ||
): | ||
with header_section(): | ||
me.text( | ||
"Mesop", type="headline-6", style=me.Style(margin=me.Margin(bottom=0)) | ||
) | ||
|
||
with header_section(): | ||
me.button("Home", type="stroked", style=me.Style(color="#E3E3E3")) | ||
me.button("About", type="stroked", style=me.Style(color="#E3E3E3")) | ||
me.button("FAQ", type="stroked", style=me.Style(color="#E3E3E3")) | ||
|
||
with header_section(): | ||
me.button("Login", type="flat") | ||
|
||
|
||
@me.content_component | ||
def header( | ||
*, | ||
style: me.Style | None = None, | ||
is_mobile: bool = False, | ||
max_width: int | None = 1000, | ||
): | ||
"""Creates a simple header component. | ||
|
||
Args: | ||
style: Override the default styles, such as background color, etc. | ||
is_mobile: Use mobile layout. Arranges each section vertically. | ||
max_width: Sets the maximum width of the header. Use None for fluid header. | ||
""" | ||
default_flex_style = ( | ||
_DEFAULT_MOBILE_FLEX_STYLE if is_mobile else _DEFAULT_FLEX_STYLE | ||
) | ||
if max_width and me.viewport_size().width >= max_width: | ||
default_flex_style = merge_styles( | ||
default_flex_style, | ||
me.Style(width=max_width, margin=me.Margin.symmetric(horizontal="auto")), | ||
) | ||
|
||
# The style override is a bit hacky here since we apply the override styles to both | ||
# boxes here which could cause problems depending on what styles are added. | ||
with me.box(style=merge_styles(_DEFAULT_STYLE, style)): | ||
with me.box(style=merge_styles(default_flex_style, style)): | ||
me.slot() | ||
|
||
|
||
@me.content_component | ||
def header_section(): | ||
"""Adds a section to the header.""" | ||
with me.box(style=me.Style(display="flex", gap=5)): | ||
me.slot() | ||
|
||
|
||
def merge_styles( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is interesting :) |
||
default: me.Style, overrides: me.Style | None = None | ||
) -> me.Style: | ||
"""Merges two styles together. | ||
|
||
Args: | ||
default: The starting style | ||
overrides: Any set styles will override styles in default | ||
""" | ||
if not overrides: | ||
overrides = me.Style() | ||
|
||
default_fields = { | ||
field.name: getattr(default, field.name) for field in fields(me.Style) | ||
} | ||
override_fields = { | ||
field.name: getattr(overrides, field.name) | ||
for field in fields(me.Style) | ||
if getattr(overrides, field.name) is not None | ||
} | ||
|
||
return me.Style(**default_fields | override_fields) | ||
|
||
|
||
_DEFAULT_STYLE = me.Style( | ||
background="#F5F8FC", | ||
border=me.Border.symmetric( | ||
vertical=me.BorderSide(width=1, style="solid", color="#DEE2E6") | ||
), | ||
padding=me.Padding.all(10), | ||
) | ||
|
||
_DEFAULT_FLEX_STYLE = me.Style( | ||
align_items="center", | ||
display="flex", | ||
gap=5, | ||
justify_content="space-between", | ||
) | ||
|
||
_DEFAULT_MOBILE_FLEX_STYLE = me.Style( | ||
align_items="center", | ||
display="flex", | ||
flex_direction="column", | ||
gap=12, | ||
justify_content="center", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
side-note: I've wondered whether we should just reset the default margins for headings to 0. I find it quite annoying to set it to margin 0, and it would improve cross-browser consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's an interesting idea. On one hand, I definitely agree that the default headline margins are kind of absurdly large.
I think having some sensible margin bottom is kind of good, so it doesn't always need to be tweaked. Since 0 probably wouldn't be great in all cases.