-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implementing Properties
trait for core
and std
types
#3862
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
Implementing Properties
trait for core
and std
types
#3862
Conversation
Visit the preview URL for this PR (updated for commit 7af22ac): https://yew-rs-api--pr3862-string-properties-twzzvax5.web.app (expires Tue, 27 May 2025 07:23:48 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 |
Benchmark - coreYew Master
Pull Request
|
Benchmark - SSRYew Master
Pull Request
|
Size Comparison
✅ None of the examples has changed their size significantly. |
There seems to be some non-determinism in the order of the expected error messages. I will split that test into one separate test for each of the verified compile-time panic cases. |
How would a component with a primitive property type (such as |
I will most definitely add tests, but here is an example showing how I use it in my own code. There are a handful of cases where I want to create very simple components, which have a single property such as a String or any other simple core type - let's take the fontawesome icon as an example. Without this PR, the code would look like the following: #[derive(Properties, PartialEq)]
struct FAIconProps {
icon: String,
}
#[function_component(FAIcon)]
/// Renders a Font Awesome icon.
pub fn font_awesome_icon(props: &FAIconProps) -> Html {
html! {
<i class={format!("fa-brands fa-{}", props.icon.clone())} aria-hidden="true"></i>
}
} Within this PR branch, I can define the component without any additional boiler plating: #[function_component(FAIcon)]
/// Renders a Font Awesome icon.
pub fn font_awesome_icon(icon: &String) -> Html {
html! {
<i class={format!("fa-solid fa-{icon}")} aria-hidden="true"></i>
}
} And then, to use it I do the following: #[derive(Properties, PartialEq)]
pub struct ParentProps {
icon: String,
url: String,
name: String
}
#[function_component(ParentComponent)]
pub fn parent_component(props: &ParentProps) -> Html {
let url = props.url;
let name = props.name;
html! {
<a class="login-provider" href={url}>
<FAIcon ..props.icon.clone() />
{format!("Login with {name}")}
</a>
}
} I believe this option makes it quite clean. Of course, it solely applies to such simple small components. |
So as a consumer, you'd have to write First of all, most of the machinery with Basically it boils down to this: should a single property be usable without having to wrap it in a struct of properties? Currently, the answer is no. And right now; I'd much rather like an approach that let's you write
which should work without changing the answer to the above question and without having to implement |
Ok, then I would proceed to close the PR. |
This PR implements the
Properties
trait for primitive and standard types (String, numbers, bool), eliminating boilerplate for simple components. More such types may be added as need be in the future.This PR addresses the discussion: #3853
Therein, @WorldSEnder suggested to either use a struct wrapper or the
yew-autoprops
macro:But with this PR, the code boiler plating is none, and it does not break API compatibility: