* [PATCH yew-widget-toolkit v2] widget: add 'div' macro
@ 2026-04-02 13:42 Dominik Csapak
2026-04-05 9:08 ` Thomas Lamprecht
2026-04-07 13:12 ` superseded: " Dominik Csapak
0 siblings, 2 replies; 6+ messages in thread
From: Dominik Csapak @ 2026-04-02 13:42 UTC (permalink / raw)
To: yew-devel
this is just a simple wrapper around the `Container` widget, but it's
very useful in situations where bare strings are used in a flex layout,
since e.g. a `Row` with multiple children that are all strings won't get
a proper flex layout (since they're not elements)
So instead of writing
```
Row::new()
.with_child(Container::new().with_child("Text1"))
.with_child(Container::new().with_child("Text2"))
```
one can now write
```
Row::new()
.with_child(div!("Text1"))
.with_child(div!("Text2"))
```
which is much shorter and more readable.
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes in v2:
used the correct use statement in the prelude section
src/lib.rs | 1 +
src/widget/macros.rs | 34 ++++++++++++++++++++++++++++++++++
src/widget/mod.rs | 2 ++
3 files changed, 37 insertions(+)
create mode 100644 src/widget/macros.rs
diff --git a/src/lib.rs b/src/lib.rs
index d3ac6ea..a759350 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -358,6 +358,7 @@ pub mod prelude {
#[doc(hidden)]
pub use yew::prelude::*;
+ pub use crate::div;
pub use crate::props::CallbackMutScopeExt;
pub use crate::props::ContainerBuilder;
pub use crate::props::CssBorderBuilder;
diff --git a/src/widget/macros.rs b/src/widget/macros.rs
new file mode 100644
index 0000000..fc4e05f
--- /dev/null
+++ b/src/widget/macros.rs
@@ -0,0 +1,34 @@
+//! Helper and utility macros to make some often used patterns more ergonomic
+
+/// A macro to wrap some elements or strings in a `<div>`.
+///
+/// Expands to `Container::new().with_child(param1).with_child(param2)...`
+///
+/// Examples:
+/// ```
+/// # use pwt::widget::Container;
+/// # use pwt::div;
+/// // must be used, either directly or via pwt::prelude::*
+/// use pwt::props::ContainerBuilder;
+///
+/// # fn main() {
+/// let div = div!("Some Text");
+/// # drop(div);
+///
+/// // is the same as
+/// let div = Container::new().with_child("Some Text");
+/// # drop(div);
+///
+/// // you can also specify multiple elements
+/// div!("Text1", "Text2");
+///
+/// // or components
+/// div!(Container::new());
+/// # }
+/// ```
+#[macro_export]
+macro_rules! div {
+ ($($arg:expr),* $(,)?) => {
+ $crate::widget::Container::new()$(.with_child($arg))*
+ };
+}
diff --git a/src/widget/mod.rs b/src/widget/mod.rs
index 0df2cbf..1998332 100644
--- a/src/widget/mod.rs
+++ b/src/widget/mod.rs
@@ -85,6 +85,8 @@ pub use list::{List, ListTile, ListTileObserver};
#[doc(hidden)]
pub use list::{PwtList, PwtListTileObserver};
+pub mod macros;
+
mod mask;
pub use mask::Mask;
#[doc(hidden)]
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH yew-widget-toolkit v2] widget: add 'div' macro
2026-04-02 13:42 [PATCH yew-widget-toolkit v2] widget: add 'div' macro Dominik Csapak
@ 2026-04-05 9:08 ` Thomas Lamprecht
2026-04-07 7:48 ` Dominik Csapak
2026-04-07 8:49 ` Dietmar Maurer
2026-04-07 13:12 ` superseded: " Dominik Csapak
1 sibling, 2 replies; 6+ messages in thread
From: Thomas Lamprecht @ 2026-04-05 9:08 UTC (permalink / raw)
To: Dominik Csapak, yew-devel
On 02/04/2026 15:43, Dominik Csapak wrote:
> this is just a simple wrapper around the `Container` widget, but it's
> very useful in situations where bare strings are used in a flex layout,
> since e.g. a `Row` with multiple children that are all strings won't get
> a proper flex layout (since they're not elements)
>
> So instead of writing
> ```
> Row::new()
> .with_child(Container::new().with_child("Text1"))
> .with_child(Container::new().with_child("Text2"))
> ```
>
> one can now write
> ```
> Row::new()
> .with_child(div!("Text1"))
> .with_child(div!("Text2"))
> ```
>
> which is much shorter and more readable.
Yes, but why a macro and not a normal method for this? Macro's are a bit
opaque, totally fine if there is a benefit for adding them, but here I
cannot see a big blocker against adding an e.g. TextContainer wrapper or
an Container::new_with_text (might need better naming, but just to relay
the idea), especially if that's your main use case. Or for a more generic
use case we could add a "Container::new_with_child" constructor or also
a wrapper type (SimpleContainer::new(child)).
And it isn't exactly a plain div, as then one could just use the html!
macro from yew, so the name as is, is IMO not totally ideal. If we go
the macro way (what's Dietmars opinion on this?), I'd favor to call it
container! to better reflect that it uses the pwt container class.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH yew-widget-toolkit v2] widget: add 'div' macro
2026-04-05 9:08 ` Thomas Lamprecht
@ 2026-04-07 7:48 ` Dominik Csapak
2026-04-07 8:49 ` Dietmar Maurer
1 sibling, 0 replies; 6+ messages in thread
From: Dominik Csapak @ 2026-04-07 7:48 UTC (permalink / raw)
To: Thomas Lamprecht, yew-devel
On 4/5/26 11:07 AM, Thomas Lamprecht wrote:
> On 02/04/2026 15:43, Dominik Csapak wrote:
>> this is just a simple wrapper around the `Container` widget, but it's
>> very useful in situations where bare strings are used in a flex layout,
>> since e.g. a `Row` with multiple children that are all strings won't get
>> a proper flex layout (since they're not elements)
>>
>> So instead of writing
>> ```
>> Row::new()
>> .with_child(Container::new().with_child("Text1"))
>> .with_child(Container::new().with_child("Text2"))
>> ```
>>
>> one can now write
>> ```
>> Row::new()
>> .with_child(div!("Text1"))
>> .with_child(div!("Text2"))
>> ```
>>
>> which is much shorter and more readable.
>
> Yes, but why a macro and not a normal method for this? Macro's are a bit
> opaque, totally fine if there is a benefit for adding them, but here I
> cannot see a big blocker against adding an e.g. TextContainer wrapper or
> an Container::new_with_text (might need better naming, but just to relay
> the idea), especially if that's your main use case. Or for a more generic
> use case we could add a "Container::new_with_child" constructor or also
> a wrapper type (SimpleContainer::new(child)).
it was just the shortest thing i could come up with.
I think the alternatives all don't really save much text vs the original
'Container::new().with_child()'
but if we really don't want to use a macro for this, i could live
with something like
Container::with(...)
or
Container::text(...)
or something like this.
the advantage of the macro was also the possibility to have multiple
elements with very little syntactic overhead (did not go into detail
here in the commit message to be fair)
>
> And it isn't exactly a plain div, as then one could just use the html!
> macro from yew, so the name as is, is IMO not totally ideal. If we go
> the macro way (what's Dietmars opinion on this?), I'd favor to call it
> container! to better reflect that it uses the pwt container class.
actually it's exactly like a 'div' by default. we just have some
convenience traits built on top.
I think Dietmar also prefers a non-macro way, so i'd send another
patch if one of the methods above would be ok for you.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH yew-widget-toolkit v2] widget: add 'div' macro
2026-04-05 9:08 ` Thomas Lamprecht
2026-04-07 7:48 ` Dominik Csapak
@ 2026-04-07 8:49 ` Dietmar Maurer
2026-04-07 9:13 ` Thomas Lamprecht
1 sibling, 1 reply; 6+ messages in thread
From: Dietmar Maurer @ 2026-04-07 8:49 UTC (permalink / raw)
To: Thomas Lamprecht, Dominik Csapak, yew-devel
So far, I always found a way to restructure the rust code to make it
readable,
so Container::new().with_child("TEXT") was never a real problem.
But I have to agree that it is too verbose. Maybe just a function named
"div"?
fn div(child: Into<Html>) -> Container { ... }
> And it isn't exactly a plain div, as then one could just use the html!
> macro from yew, so the name as is, is IMO not totally ideal. If we go
> the macro way (what's Dietmars opinion on this?), I'd favor to call it
> container! to better reflect that it uses the pwt container class.
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH yew-widget-toolkit v2] widget: add 'div' macro
2026-04-07 8:49 ` Dietmar Maurer
@ 2026-04-07 9:13 ` Thomas Lamprecht
0 siblings, 0 replies; 6+ messages in thread
From: Thomas Lamprecht @ 2026-04-07 9:13 UTC (permalink / raw)
To: Dietmar Maurer, Dominik Csapak, yew-devel
On 07/04/2026 10:48, Dietmar Maurer wrote:
> So far, I always found a way to restructure the rust code to make it readable,
> so Container::new().with_child("TEXT") was never a real problem.
>
> But I have to agree that it is too verbose. Maybe just a function named "div"?
>
> fn div(child: Into<Html>) -> Container { ... }
Yeah, a normal function would be fine with me.
^ permalink raw reply [flat|nested] 6+ messages in thread
* superseded: [PATCH yew-widget-toolkit v2] widget: add 'div' macro
2026-04-02 13:42 [PATCH yew-widget-toolkit v2] widget: add 'div' macro Dominik Csapak
2026-04-05 9:08 ` Thomas Lamprecht
@ 2026-04-07 13:12 ` Dominik Csapak
1 sibling, 0 replies; 6+ messages in thread
From: Dominik Csapak @ 2026-04-07 13:12 UTC (permalink / raw)
To: yew-devel
superseded by v3:
https://lore.proxmox.com/yew-devel/20260407130919.3451837-1-d.csapak@proxmox.com/T/#u
On 4/2/26 3:43 PM, Dominik Csapak wrote:
> this is just a simple wrapper around the `Container` widget, but it's
> very useful in situations where bare strings are used in a flex layout,
> since e.g. a `Row` with multiple children that are all strings won't get
> a proper flex layout (since they're not elements)
>
> So instead of writing
> ```
> Row::new()
> .with_child(Container::new().with_child("Text1"))
> .with_child(Container::new().with_child("Text2"))
> ```
>
> one can now write
> ```
> Row::new()
> .with_child(div!("Text1"))
> .with_child(div!("Text2"))
> ```
>
> which is much shorter and more readable.
>
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
> changes in v2:
> used the correct use statement in the prelude section
>
> src/lib.rs | 1 +
> src/widget/macros.rs | 34 ++++++++++++++++++++++++++++++++++
> src/widget/mod.rs | 2 ++
> 3 files changed, 37 insertions(+)
> create mode 100644 src/widget/macros.rs
>
> diff --git a/src/lib.rs b/src/lib.rs
> index d3ac6ea..a759350 100644
> --- a/src/lib.rs
> +++ b/src/lib.rs
> @@ -358,6 +358,7 @@ pub mod prelude {
> #[doc(hidden)]
> pub use yew::prelude::*;
>
> + pub use crate::div;
> pub use crate::props::CallbackMutScopeExt;
> pub use crate::props::ContainerBuilder;
> pub use crate::props::CssBorderBuilder;
> diff --git a/src/widget/macros.rs b/src/widget/macros.rs
> new file mode 100644
> index 0000000..fc4e05f
> --- /dev/null
> +++ b/src/widget/macros.rs
> @@ -0,0 +1,34 @@
> +//! Helper and utility macros to make some often used patterns more ergonomic
> +
> +/// A macro to wrap some elements or strings in a `<div>`.
> +///
> +/// Expands to `Container::new().with_child(param1).with_child(param2)...`
> +///
> +/// Examples:
> +/// ```
> +/// # use pwt::widget::Container;
> +/// # use pwt::div;
> +/// // must be used, either directly or via pwt::prelude::*
> +/// use pwt::props::ContainerBuilder;
> +///
> +/// # fn main() {
> +/// let div = div!("Some Text");
> +/// # drop(div);
> +///
> +/// // is the same as
> +/// let div = Container::new().with_child("Some Text");
> +/// # drop(div);
> +///
> +/// // you can also specify multiple elements
> +/// div!("Text1", "Text2");
> +///
> +/// // or components
> +/// div!(Container::new());
> +/// # }
> +/// ```
> +#[macro_export]
> +macro_rules! div {
> + ($($arg:expr),* $(,)?) => {
> + $crate::widget::Container::new()$(.with_child($arg))*
> + };
> +}
> diff --git a/src/widget/mod.rs b/src/widget/mod.rs
> index 0df2cbf..1998332 100644
> --- a/src/widget/mod.rs
> +++ b/src/widget/mod.rs
> @@ -85,6 +85,8 @@ pub use list::{List, ListTile, ListTileObserver};
> #[doc(hidden)]
> pub use list::{PwtList, PwtListTileObserver};
>
> +pub mod macros;
> +
> mod mask;
> pub use mask::Mask;
> #[doc(hidden)]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-04-07 13:11 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-02 13:42 [PATCH yew-widget-toolkit v2] widget: add 'div' macro Dominik Csapak
2026-04-05 9:08 ` Thomas Lamprecht
2026-04-07 7:48 ` Dominik Csapak
2026-04-07 8:49 ` Dietmar Maurer
2026-04-07 9:13 ` Thomas Lamprecht
2026-04-07 13:12 ` superseded: " Dominik Csapak
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.