Open
Conversation
|
Nice, this works use bon::bon;
use hypertext::prelude::*;
struct Component;
#[bon]
impl Component {
#[builder]
fn new(id: u64, optional: Option<String>, children: impl Renderable) -> impl Renderable {
maud! { div #(id) { (optional) (children) } }
}
}
fn main() {
let res = maud! {
Component id=1 { "hello" }
}
.render();
println!("{res:?}"); // Rendered("<div id=\"1\">hello</div>")
}I only wish there was a way to do it with bare functions, but unfortunately the generated API is I hope this gets merged |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request changes the way user components are instantiated during the expansion of the
rsx!andmaud!macros. Previously, a struct-literal approach was used:Now a builder-based approach is used:
The builder methods can be:
#[derive(TypedBuilder)]from thetyped-buildercrate);#[component]macro arguments (for example,#[component(builder = hypertext::DefaultBuilder)]or#[component(builder = bon::Builder)]);It is also now possible to propagate attributes defined on the component function parameters to the fields of the generated struct. This allows specifying builder-specific field attributes, including default argument values.
Some usage examples are provided in two new tests: https://github.com/vidhanio/hypertext/pull/183/changes#diff-8c79c3d623f866c80fb5e4093f5e2b774c3d590025116a0352d4a240e1ed6817
Backward Compatibility
The changes preserve API backward compatibility as much as possible. However, in some aspects the new behavior is incompatible with the previous one:
..at the end if the component implementsDefault. This syntax has been removed from the component parser.Default, it is now necessary to explicitly specify the use ofDefaultBuilderinstead of the defaultTypedBuilder(i.e.,#[component(builder = hypertext::DefaultBuilder)]) if omitting some component properties at the call site should be allowed.builder,build, and field setters, which may cause conflicts with similarly named methods already present in older components.#[component]macro. By default, this list includes thebuilderattribute to allow passing configuration options toTypedBuilder.Closes issue #180
Closes issue #128