Skip to content
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

refactor(layout_column_wrap): Use helper for wrapping children #910

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion shiny/api-examples/layout_column_wrap/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ui.layout_column_wrap(y, y, y, width=1 / 2),
ui.hr(),
# Has three columns when viewport is wider than 750px
ui.layout_column_wrap(y, y, y, width="250%"),
ui.layout_column_wrap(y, y, y, width="250px"),
)


Expand Down
32 changes: 22 additions & 10 deletions shiny/ui/_layout.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Literal, Optional, cast
from typing import Iterable, Literal, Optional, cast

from htmltools import Tag, TagAttrs, TagAttrValue, TagChild, css, div

Expand Down Expand Up @@ -128,14 +128,6 @@ def layout_column_wrap(
else:
colspec = f"repeat(auto-fit, minmax(min({width_css_unit}, 100%), 1fr))"

# Use a new dict so that we don't mutate the original `children` dict
upgraded_children: list[TagChild] = []
for child_value in children:
child = div({"class": "bslib-gap-spacing"}, child_value)
if fillable:
child = as_fillable_container(child)
upgraded_children.append(child)

tag_style_css = {
"grid-template-columns": colspec,
"grid-auto-rows": "1fr" if (heights_equal == "all") else None,
Expand All @@ -155,7 +147,7 @@ def layout_column_wrap(
"style": css(**tag_style_css),
},
attrs,
*upgraded_children,
*wrap_all_in_gap_spaced_container(children, fillable),
components_dependency(),
)
if fill:
Expand All @@ -170,3 +162,23 @@ def is_probably_a_css_unit(x: TagChild) -> bool:
if isinstance_cssunit(x):
return True
return False


def wrap_all_in_gap_spaced_container(
children: Iterable[TagChild],
fillable: bool = True,
class_: Optional[str] = None,
) -> list[TagChild]:
item_class = "bslib-gap-spacing"
if class_ is not None:
item_class = f"{item_class} {class_}"

# Use a new list so that we don't mutate the original `children`
wrapped_children: list[TagChild] = []
for child_value in children:
child = div({"class": item_class}, child_value)
if fillable:
child = as_fillable_container(child)
wrapped_children.append(child)

return wrapped_children
25 changes: 3 additions & 22 deletions shiny/ui/_layout_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from typing import Dict, Iterable, Optional, TypeVar, Union, cast
from warnings import warn

from htmltools import Tag, TagAttrs, TagAttrValue, TagChild, css, div
from htmltools import Tag, TagAttrs, TagAttrValue, TagChild, css

from ._html_deps_shinyverse import web_component_dependency
from ._layout import wrap_all_in_gap_spaced_container
from ._tag import consolidate_attrs
from .css import CssUnit, as_css_unit
from .fill import as_fill_item, as_fillable_container
Expand Down Expand Up @@ -135,7 +136,7 @@ def layout_columns(
col_widths_attrs(col_widths_spec),
attrs,
row_heights_attrs(row_heights),
*wrap_all_in_grid_item_container(children, fillable),
*wrap_all_in_gap_spaced_container(children, fillable, "bslib-grid-item"),
web_component_dependency(),
)

Expand All @@ -148,26 +149,6 @@ def layout_columns(
return tag


def wrap_all_in_grid_item_container(
children: Iterable[TagChild],
fillable: bool = True,
class_: Optional[str] = None,
) -> list[TagChild]:
item_class = "bslib-grid-item bslib-gap-spacing"
if class_ is not None:
item_class = f"{item_class} {class_}"

# Use a new list so that we don't mutate the original `children`
wrapped_children: list[TagChild] = []
for child_value in children:
child = div({"class": item_class}, child_value)
if fillable:
child = as_fillable_container(child)
wrapped_children.append(child)

return wrapped_children


def as_col_spec(
col_widths: BreakpointsUser[int],
n_kids: int,
Expand Down
Loading