public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [PATCH yew-widget-toolkit v2] widget: add 'div' macro
Date: Thu,  2 Apr 2026 15:42:57 +0200	[thread overview]
Message-ID: <20260402134337.3433085-1-d.csapak@proxmox.com> (raw)

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





                 reply	other threads:[~2026-04-02 13:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260402134337.3433085-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=yew-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal