use std::borrow::Cow;
use leptos::tachys::{
html::{
attribute::Attribute,
style::{IntoStyle, style},
},
renderer::{Rndr, dom::CssStyleDeclaration, types::Element},
};
use crate::CheckedCssValue;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CheckedDeclaration {
property: Cow<'static, str>,
value: String,
}
impl CheckedDeclaration {
pub(crate) fn new(property: Cow<'static, str>, value: impl CheckedCssValue) -> Self {
let mut rendered = String::with_capacity(32);
value.write_to(&mut rendered);
Self {
property,
value: rendered,
}
}
#[must_use]
pub fn property_name(&self) -> &str {
&self.property
}
pub fn write_declaration_to(&self, output: &mut String) {
output.push_str(&self.property);
output.push(':');
output.push_str(&self.value);
output.push(';');
}
pub fn into_attribute(self) -> impl Attribute {
style(self)
}
}
fn set_declaration(style: &CssStyleDeclaration, declaration: &CheckedDeclaration) {
Rndr::set_css_property(style, declaration.property_name(), &declaration.value);
}
impl IntoStyle for CheckedDeclaration {
type AsyncOutput = Self;
type State = (CssStyleDeclaration, Self);
type Cloneable = Self;
type CloneableOwned = Self;
fn to_html(self, style: &mut String) {
self.write_declaration_to(style);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &Element) -> Self::State {
(Rndr::style(el), self)
}
fn build(self, el: &Element) -> Self::State {
let style = Rndr::style(el);
set_declaration(&style, &self);
(style, self)
}
fn rebuild(self, state: &mut Self::State) {
let (style, current) = state;
if self.property != current.property {
Rndr::remove_css_property(style, current.property_name());
set_declaration(style, &self);
} else if self.value != current.value {
set_declaration(style, &self);
}
*current = self;
}
fn into_cloneable(self) -> Self::Cloneable {
self
}
fn into_cloneable_owned(self) -> Self::CloneableOwned {
self
}
fn dry_resolve(&mut self) {}
async fn resolve(self) -> Self::AsyncOutput {
self
}
fn reset(state: &mut Self::State) {
Rndr::remove_css_property(&state.0, state.1.property_name());
}
}