Skip to content

Commit d68adf2

Browse files
committed
Make layout_column_wrap(*args, width=)'s width come after *args
Related: rstudio/bslib#853
1 parent 1f23738 commit d68adf2

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
### API changes
2222

2323
* Added `shiny.ui.navset_underline()` and `shiny.ui.navset_card_underline()` whose navigation container is similar to `shiny.ui.navset_tab()` and `shiny.ui.navset_card_tab()` respectively, but its active/focused navigation links are styled with an underline. (#772)
24+
* `shiny.ui.layout_column_wrap(width, *args)` was rearranged to `shiny.ui.layout_column_wrap(*args, width)`. Now, `width` will default to `200px` is no value is provided. (#772)
2425

2526
* TODO-barret-API; `shiny.ui.panel_main()` and `shiny.ui.panel_sidebar()` are deprecated in favor of new API for `shiny.ui.layout_sidebar()`. Please use `shiny.ui.sidebar()` to construct a sidebar and supply it (along with the main content) to `shiny.ui.layout_sidebar(*args, **kwargs)`. (#680)
2627

shiny/ui/_layout.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
22

3-
from typing import Literal, Optional
3+
from typing import Literal, Optional, cast
44

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

7+
from .._deprecated import warn_deprecated
8+
from ..types import MISSING, MISSING_TYPE
79
from ._html_deps_shinyverse import components_dependency
810
from ._tag import consolidate_attrs
911
from ._utils import is_01_scalar
@@ -13,8 +15,8 @@
1315

1416
# TODO-barret-API; Move `width` to after `args`
1517
def layout_column_wrap(
16-
width: Optional[CssUnit],
1718
*args: TagChild | TagAttrs,
19+
width: CssUnit | None | MISSING_TYPE = MISSING,
1820
fixed_width: bool = False,
1921
heights_equal: Literal["all", "row"] = "all",
2022
fill: bool = True,
@@ -36,17 +38,18 @@ def layout_column_wrap(
3638
3739
Parameters
3840
----------
41+
*args
42+
Unnamed arguments should be UI elements (e.g.,
43+
:func:`~shiny.ui.card`). Named arguments become attributes on the
44+
containing :class:`~htmltools.Tag` element.
3945
width
4046
The desired width of each card. It can be a (unit-less) number between 0 and 1
4147
and should be specified as `1/num`, where `num` represents the number of desired
4248
columns. It can be a CSS length unit representing either the minimum (when
4349
`fixed_width=False`) or fixed width (`fixed_width=True`). It can also be `None`,
4450
which allows power users to set the `grid-template-columns` CSS property
45-
manually, either via a `style` attribute or a CSS stylesheet.
46-
*args
47-
Unnamed arguments should be UI elements (e.g.,
48-
:func:`~shiny.ui.card`). Named arguments become attributes on the
49-
containing :class:`~htmltools.Tag` element.
51+
manually, either via a `style` attribute or a CSS stylesheet. If missing, a
52+
value of `200px` will be used.
5053
fixed_width
5154
When `width` is greater than 1 or is a CSS length unit, e.g. `"200px"`,
5255
`fixed_width` indicates whether that `width` value represents the absolute size
@@ -83,6 +86,26 @@ def layout_column_wrap(
8386
"""
8487
attrs, children = consolidate_attrs(*args, class_=class_, **kwargs)
8588

89+
if isinstance(width, MISSING_TYPE):
90+
if len(children) > 0 and (
91+
children[0] is None or is_probably_a_css_unit(children[0])
92+
):
93+
# Code changed from
94+
# `layout_column_wrap(width, *args)`
95+
# to
96+
# `layout_column_wrap(*args, width)`.
97+
# Provide deprecation warning
98+
99+
# Assume an unnamed first argument that matches our expectations for
100+
# `width` is actually the width argument, with a warning
101+
warn_deprecated(
102+
"`layout_column_wrap(*args, width=)`'s `width` parameter must be named."
103+
)
104+
width = cast(CssUnit, children[0])
105+
children = children[1:]
106+
else:
107+
width = "200px"
108+
86109
colspec: str | None = None
87110
if width is not None:
88111
if is_01_scalar(width) and width > 0.0:
@@ -133,3 +156,11 @@ def layout_column_wrap(
133156
tag = as_fill_item(tag)
134157

135158
return tag
159+
160+
161+
def is_probably_a_css_unit(x: TagChild) -> bool:
162+
if isinstance(x, str):
163+
return False
164+
if isinstance(x, CssUnit):
165+
return True
166+
return False
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
from shiny._deprecated import ShinyDeprecationWarning
4+
from shiny.ui import div, layout_column_wrap
5+
6+
X = div("42")
7+
Y = div("43")
8+
w = 1 / 2
9+
10+
11+
def test_layout_column_width_as_first_param_is_deprecated():
12+
layout_column_wrap(X)
13+
with pytest.warns(ShinyDeprecationWarning, match="`width` parameter must be named"):
14+
layout_column_wrap(w, X)
15+
layout_column_wrap(X, w, Y)
16+
layout_column_wrap(X, Y, w)
17+
layout_column_wrap(X, Y, width=w)
18+
layout_column_wrap(X, Y, width=None)

0 commit comments

Comments
 (0)