Skip to content
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

impl IntoStyle for Option<impl IntoStyle & Oco #3119

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions tachys/src/html/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,80 @@ pub trait IntoStyle: Send {
fn resolve(self) -> impl Future<Output = Self::AsyncOutput> + Send;
}

impl<T: IntoStyle> IntoStyle for Option<T> {
type AsyncOutput = Option<T::AsyncOutput>;
type State = (crate::renderer::types::Element, Option<T::State>);
type Cloneable = Option<T::Cloneable>;
type CloneableOwned = Option<T::CloneableOwned>;

fn to_html(self, style: &mut String) {
if let Some(t) = self {
t.to_html(style);
}
}

fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
if let Some(t) = self {
(el.clone(), Some(t.hydrate::<FROM_SERVER>(el)))
} else {
(el.clone(), None)
}
}

fn build(self, el: &crate::renderer::types::Element) -> Self::State {
if let Some(t) = self {
(el.clone(), Some(t.build(el)))
} else {
(el.clone(), None)
}
}

fn rebuild(self, state: &mut Self::State) {
let el = &state.0;
let prev_state = &mut state.1;
let maybe_next_t_state = match (prev_state, self) {
(Some(_prev_t_state), None) => {
Rndr::remove_attribute(el, "style");
gbj marked this conversation as resolved.
Show resolved Hide resolved
Some(None)
}
(None, Some(t)) => Some(Some(t.build(el))),
(Some(prev_t_state), Some(t)) => {
t.rebuild(prev_t_state);
None
}
(None, None) => Some(None),
};
if let Some(next_t_state) = maybe_next_t_state {
state.1 = next_t_state;
}
}

fn into_cloneable(self) -> Self::Cloneable {
self.map(|t| t.into_cloneable())
}

fn into_cloneable_owned(self) -> Self::CloneableOwned {
self.map(|t| t.into_cloneable_owned())
}

fn dry_resolve(&mut self) {
if let Some(t) = self {
t.dry_resolve();
}
}

async fn resolve(self) -> Self::AsyncOutput {
if let Some(t) = self {
Some(t.resolve().await)
} else {
None
}
}
}

impl<'a> IntoStyle for &'a str {
type AsyncOutput = Self;
type State = (crate::renderer::types::Element, &'a str);
Expand Down
48 changes: 47 additions & 1 deletion tachys/src/oco.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
html::{attribute::AttributeValue, class::IntoClass},
html::{attribute::AttributeValue, class::IntoClass, style::IntoStyle},
hydration::Cursor,
no_attrs,
prelude::{Mountable, Render, RenderHtml},
Expand Down Expand Up @@ -227,3 +227,49 @@ impl IntoClass for Oco<'static, str> {
self
}
}

impl IntoStyle for Oco<'static, str> {
type AsyncOutput = Self;
type State = (crate::renderer::types::Element, Self);
type Cloneable = Self;
type CloneableOwned = Self;

fn to_html(self, style: &mut String) {
style.push_str(&self);
style.push(';');
}

fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(el.clone(), self)
}

fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "style", &self);
(el.clone(), self)
}

fn rebuild(self, state: &mut Self::State) {
let (el, prev) = state;
if self != *prev {
Rndr::set_attribute(el, "style", &self);
}
*prev = 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
}
}