feat: support passing signals directly as attributes, classes, styles, and props on stable (#1577)

This commit is contained in:
Greg Johnston 2023-08-24 10:22:14 -04:00 committed by GitHub
parent e48f66694d
commit b98174db7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 168 additions and 0 deletions

View File

@ -1,3 +1,7 @@
#[cfg(not(feature = "nightly"))]
use leptos_reactive::{
MaybeProp, MaybeSignal, Memo, ReadSignal, RwSignal, Signal, SignalGet,
};
use std::{borrow::Cow, rc::Rc};
#[cfg(all(target_arch = "wasm32", feature = "web"))]
use wasm_bindgen::UnwrapThrowExt;
@ -263,6 +267,41 @@ macro_rules! attr_type {
};
}
macro_rules! attr_signal_type {
($signal_type:ty) => {
#[cfg(not(feature = "nightly"))]
impl<T> IntoAttribute for $signal_type
where
T: IntoAttribute + Clone,
{
fn into_attribute(self) -> Attribute {
let modified_fn = Rc::new(move || self.get().into_attribute());
Attribute::Fn(modified_fn)
}
impl_into_attr_boxed! {}
}
};
}
macro_rules! attr_signal_type_optional {
($signal_type:ty) => {
#[cfg(not(feature = "nightly"))]
impl<T> IntoAttribute for $signal_type
where
T: Clone,
Option<T>: IntoAttribute,
{
fn into_attribute(self) -> Attribute {
let modified_fn = Rc::new(move || self.get().into_attribute());
Attribute::Fn(modified_fn)
}
impl_into_attr_boxed! {}
}
};
}
attr_type!(&String);
attr_type!(usize);
attr_type!(u8);
@ -280,6 +319,13 @@ attr_type!(f32);
attr_type!(f64);
attr_type!(char);
attr_signal_type!(ReadSignal<T>);
attr_signal_type!(RwSignal<T>);
attr_signal_type!(Memo<T>);
attr_signal_type!(Signal<T>);
attr_signal_type!(MaybeSignal<T>);
attr_signal_type_optional!(MaybeProp<T>);
#[cfg(all(target_arch = "wasm32", feature = "web"))]
#[doc(hidden)]
#[inline(never)]

View File

@ -1,3 +1,8 @@
#[cfg(not(feature = "nightly"))]
use leptos_reactive::{
MaybeProp, MaybeSignal, Memo, ReadSignal, RwSignal, Signal, SignalGet,
};
/// Represents the different possible values a single class on an element could have,
/// allowing you to do fine-grained updates to single items
/// in [`Element.classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList).
@ -113,3 +118,36 @@ pub(crate) fn class_expression(
}
}
}
macro_rules! class_signal_type {
($signal_type:ty) => {
#[cfg(not(feature = "nightly"))]
impl IntoClass for $signal_type {
#[inline(always)]
fn into_class(self) -> Class {
let modified_fn = Box::new(move || self.get());
Class::Fn(modified_fn)
}
}
};
}
macro_rules! class_signal_type_optional {
($signal_type:ty) => {
#[cfg(not(feature = "nightly"))]
impl IntoClass for $signal_type {
#[inline(always)]
fn into_class(self) -> Class {
let modified_fn = Box::new(move || self.get().unwrap_or(false));
Class::Fn(modified_fn)
}
}
};
}
class_signal_type!(ReadSignal<bool>);
class_signal_type!(RwSignal<bool>);
class_signal_type!(Memo<bool>);
class_signal_type!(Signal<bool>);
class_signal_type!(MaybeSignal<bool>);
class_signal_type_optional!(MaybeProp<bool>);

View File

@ -1,3 +1,7 @@
#[cfg(not(feature = "nightly"))]
use leptos_reactive::{
MaybeProp, MaybeSignal, Memo, ReadSignal, RwSignal, Signal, SignalGet,
};
use wasm_bindgen::JsValue;
#[cfg(all(target_arch = "wasm32", feature = "web"))]
use wasm_bindgen::UnwrapThrowExt;
@ -52,6 +56,37 @@ macro_rules! prop_type {
};
}
macro_rules! prop_signal_type {
($signal_type:ty) => {
#[cfg(not(feature = "nightly"))]
impl<T> IntoProperty for $signal_type
where
T: Into<JsValue> + Clone,
{
fn into_property(self) -> Property {
let modified_fn = Box::new(move || self.get().into());
Property::Fn(modified_fn)
}
}
};
}
macro_rules! prop_signal_type_optional {
($signal_type:ty) => {
#[cfg(not(feature = "nightly"))]
impl<T> IntoProperty for $signal_type
where
T: Clone,
Option<T>: Into<JsValue>,
{
fn into_property(self) -> Property {
let modified_fn = Box::new(move || self.get().into());
Property::Fn(modified_fn)
}
}
};
}
prop_type!(JsValue);
prop_type!(String);
prop_type!(&String);
@ -72,6 +107,13 @@ prop_type!(f32);
prop_type!(f64);
prop_type!(bool);
prop_signal_type!(ReadSignal<T>);
prop_signal_type!(RwSignal<T>);
prop_signal_type!(Memo<T>);
prop_signal_type!(Signal<T>);
prop_signal_type!(MaybeSignal<T>);
prop_signal_type_optional!(MaybeProp<T>);
#[cfg(all(target_arch = "wasm32", feature = "web"))]
use std::borrow::Cow;

View File

@ -1,3 +1,7 @@
#[cfg(not(feature = "nightly"))]
use leptos_reactive::{
MaybeProp, MaybeSignal, Memo, ReadSignal, RwSignal, Signal, SignalGet,
};
use std::{borrow::Cow, rc::Rc};
/// todo docs
@ -183,6 +187,37 @@ macro_rules! style_type {
};
}
macro_rules! style_signal_type {
($signal_type:ty) => {
#[cfg(not(feature = "nightly"))]
impl<T> IntoStyle for $signal_type
where
T: IntoStyle + Clone,
{
fn into_style(self) -> Style {
let modified_fn = Rc::new(move || self.get().into_style());
Style::Fn(modified_fn)
}
}
};
}
macro_rules! style_signal_type_optional {
($signal_type:ty) => {
#[cfg(not(feature = "nightly"))]
impl<T> IntoStyle for $signal_type
where
T: Clone,
Option<T>: IntoStyle,
{
fn into_style(self) -> Style {
let modified_fn = Rc::new(move || self.get().into_style());
Style::Fn(modified_fn)
}
}
};
}
style_type!(&String);
style_type!(usize);
style_type!(u8);
@ -199,3 +234,10 @@ style_type!(i128);
style_type!(f32);
style_type!(f64);
style_type!(char);
style_signal_type!(ReadSignal<T>);
style_signal_type!(RwSignal<T>);
style_signal_type!(Memo<T>);
style_signal_type!(Signal<T>);
style_signal_type!(MaybeSignal<T>);
style_signal_type_optional!(MaybeProp<T>);