diff --git a/tachys/src/html/style.rs b/tachys/src/html/style.rs index 4e8fe0066e..118f3b3e82 100644 --- a/tachys/src/html/style.rs +++ b/tachys/src/html/style.rs @@ -6,7 +6,7 @@ use super::attribute::{ use crate::view::static_types::Static; use crate::{ html::attribute::maybe_next_attr_erasure_macros::next_attr_combine, - renderer::Rndr, + renderer::{dom::CssStyleDeclaration, Rndr}, view::{Position, ToTemplate}, }; use std::{future::Future, sync::Arc}; @@ -412,90 +412,66 @@ impl IntoStyle for String { } } -impl IntoStyle for (Arc, Arc) { - type AsyncOutput = Self; - type State = ( - crate::renderer::types::CssStyleDeclaration, - Arc, - Arc, - ); - type Cloneable = Self; - type CloneableOwned = Self; - - fn to_html(self, style: &mut String) { - let (name, value) = self; - style.push_str(&name); - style.push(':'); - style.push_str(&value); - style.push(';'); - } +/// Any type that can be used to set an individual style in the +/// [`CssStyleDeclaration`](web_sys::CssStyleDeclaration). +/// +/// This is the value in a `(name, value)` tuple that implements [`IntoStyle`]. +pub trait IntoStyleValue: Send { + /// The type after all async data have resolved. + type AsyncOutput: IntoStyleValue; + /// The view state retained between building and rebuilding. + type State; + /// An equivalent value that can be cloned. + type Cloneable: Clone + IntoStyleValue; + /// An equivalent value that can be cloned and is `'static`. + type CloneableOwned: Clone + IntoStyleValue + 'static; - fn hydrate( - self, - el: &crate::renderer::types::Element, - ) -> Self::State { - let style = Rndr::style(el); - (style, self.0, self.1) - } + /// Renders the style to HTML. + fn to_html(self, name: &str, style: &mut String); - fn build(self, el: &crate::renderer::types::Element) -> Self::State { - let (name, value) = self; - let style = Rndr::style(el); - Rndr::set_css_property(&style, &name, &value); - (style, name, value) - } + /// Adds this style to the element during client-side rendering. + fn build(self, style: &CssStyleDeclaration, name: &str) -> Self::State; - fn rebuild(self, state: &mut Self::State) { - let (name, value) = self; - // state.1 was the previous name, theoretically the css name could be changed: - if name != state.1 { - <(Arc, Arc) as IntoStyle>::reset(state); - } - let (style, prev_name, prev_value) = state; - if value != *prev_value { - Rndr::set_css_property(style, &name, &value); - } - *prev_name = name; - *prev_value = value; - } + /// Updates the value. + fn rebuild( + self, + style: &CssStyleDeclaration, + name: &str, + state: &mut Self::State, + ); - fn into_cloneable(self) -> Self::Cloneable { - self - } + /// Adds interactivity as necessary, given DOM nodes that were created from HTML that has + /// either been rendered on the server, or cloned for a `