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 all commits
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
180 changes: 162 additions & 18 deletions tachys/src/html/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,89 @@ pub trait IntoStyle: Send {

/// “Resolves” this into a type that is not waiting for any asynchronous data.
fn resolve(self) -> impl Future<Output = Self::AsyncOutput> + Send;

/// Reset the styling to the state before this style was added.
fn reset(state: &mut Self::State);
}

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.take(), self) {
(Some(mut prev_t_state), None) => {
T::reset(&mut prev_t_state);
Some(None)
}
(None, Some(t)) => Some(Some(t.build(el))),
(Some(mut prev_t_state), Some(t)) => {
t.rebuild(&mut 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
}
}

fn reset(state: &mut Self::State) {
if let Some(prev_t_state) = &mut state.1 {
T::reset(prev_t_state);
}
}
}

impl<'a> IntoStyle for &'a str {
Expand Down Expand Up @@ -214,6 +297,11 @@ impl<'a> IntoStyle for &'a str {
async fn resolve(self) -> Self::AsyncOutput {
self
}

fn reset(state: &mut Self::State) {
let (el, _prev) = state;
Rndr::remove_attribute(el, "style");
}
}

impl IntoStyle for Arc<str> {
Expand Down Expand Up @@ -260,6 +348,11 @@ impl IntoStyle for Arc<str> {
async fn resolve(self) -> Self::AsyncOutput {
self
}

fn reset(state: &mut Self::State) {
let (el, _prev) = state;
Rndr::remove_attribute(el, "style");
}
}

impl IntoStyle for String {
Expand Down Expand Up @@ -306,11 +399,20 @@ impl IntoStyle for String {
async fn resolve(self) -> Self::AsyncOutput {
self
}

fn reset(state: &mut Self::State) {
let (el, _prev) = state;
Rndr::remove_attribute(el, "style");
}
}

impl IntoStyle for (Arc<str>, Arc<str>) {
type AsyncOutput = Self;
type State = (crate::renderer::types::CssStyleDeclaration, Arc<str>);
type State = (
crate::renderer::types::CssStyleDeclaration,
Arc<str>,
Arc<str>,
);
type Cloneable = Self;
type CloneableOwned = Self;

Expand All @@ -327,23 +429,28 @@ impl IntoStyle for (Arc<str>, Arc<str>) {
el: &crate::renderer::types::Element,
) -> Self::State {
let style = Rndr::style(el);
(style, self.1)
(style, self.0, self.1)
}

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, value)
(style, name, value)
}

fn rebuild(self, state: &mut Self::State) {
let (name, value) = self;
let (style, prev) = state;
if value != *prev {
// state.1 was the previous name, theoretically the css name could be changed:
if name != state.1 {
zakstucke marked this conversation as resolved.
Show resolved Hide resolved
<(Arc<str>, Arc<str>) as IntoStyle>::reset(state);
}
let (style, prev_name, prev_value) = state;
if value != *prev_value {
Rndr::set_css_property(style, &name, &value);
}
*prev = value;
*prev_name = name;
*prev_value = value;
}

fn into_cloneable(self) -> Self::Cloneable {
Expand All @@ -359,11 +466,21 @@ impl IntoStyle for (Arc<str>, Arc<str>) {
async fn resolve(self) -> Self::AsyncOutput {
self
}

/// Reset the renderer to the state before this style was added.
fn reset(state: &mut Self::State) {
let (style, name, _value) = state;
Rndr::remove_css_property(style, name);
}
}

impl<'a> IntoStyle for (&'a str, &'a str) {
type AsyncOutput = Self;
type State = (crate::renderer::types::CssStyleDeclaration, &'a str);
type State = (
crate::renderer::types::CssStyleDeclaration,
&'a str,
&'a str,
);
type Cloneable = Self;
type CloneableOwned = (Arc<str>, Arc<str>);

Expand All @@ -380,23 +497,28 @@ impl<'a> IntoStyle for (&'a str, &'a str) {
el: &crate::renderer::types::Element,
) -> Self::State {
let style = Rndr::style(el);
(style, self.1)
(style, self.0, self.1)
}

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, self.1)
(style, self.0, self.1)
}

fn rebuild(self, state: &mut Self::State) {
let (name, value) = self;
let (style, prev) = state;
if value != *prev {
// state.1 was the previous name, theoretically the css name could be changed:
if name != state.1 {
<(&'a str, &'a str) as IntoStyle>::reset(state);
}
let (style, prev_name, prev_value) = state;
if value != *prev_value {
zakstucke marked this conversation as resolved.
Show resolved Hide resolved
Rndr::set_css_property(style, name, value);
}
*prev = value;
*prev_name = name;
*prev_value = value;
}

fn into_cloneable(self) -> Self::Cloneable {
Expand All @@ -412,11 +534,17 @@ impl<'a> IntoStyle for (&'a str, &'a str) {
async fn resolve(self) -> Self::AsyncOutput {
self
}

/// Reset the renderer to the state before this style was added.
fn reset(state: &mut Self::State) {
let (style, name, _value) = state;
Rndr::remove_css_property(style, name);
}
}

impl<'a> IntoStyle for (&'a str, String) {
type AsyncOutput = Self;
type State = (crate::renderer::types::CssStyleDeclaration, String);
type State = (crate::renderer::types::CssStyleDeclaration, &'a str, String);
type Cloneable = (Arc<str>, Arc<str>);
type CloneableOwned = (Arc<str>, Arc<str>);

Expand All @@ -433,23 +561,28 @@ impl<'a> IntoStyle for (&'a str, String) {
el: &crate::renderer::types::Element,
) -> Self::State {
let style = Rndr::style(el);
(style, self.1)
(style, self.0, self.1)
}

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, self.1)
(style, self.0, self.1)
}

fn rebuild(self, state: &mut Self::State) {
let (name, value) = self;
let (style, prev) = state;
if value != *prev {
// state.1 was the previous name, theoretically the css name could be changed:
if name != state.1 {
<(&'a str, String) as IntoStyle>::reset(state);
}
let (style, prev_name, prev_value) = state;
if value != *prev_value {
Rndr::set_css_property(style, name, &value);
}
*prev = value;
*prev_name = name;
*prev_value = value;
}

fn into_cloneable(self) -> Self::Cloneable {
Expand All @@ -465,6 +598,11 @@ impl<'a> IntoStyle for (&'a str, String) {
async fn resolve(self) -> Self::AsyncOutput {
self
}

fn reset(state: &mut Self::State) {
let (style, name, _value) = state;
Rndr::remove_css_property(style, name);
}
}

#[cfg(feature = "nightly")]
Expand Down Expand Up @@ -509,6 +647,8 @@ impl<'a, const V: &'static str> IntoStyle for (&'a str, Static<V>) {
async fn resolve(self) -> Self::AsyncOutput {
self
}

fn reset(_state: &mut Self::State) {}
}

#[cfg(feature = "nightly")]
Expand Down Expand Up @@ -553,6 +693,8 @@ impl<const V: &'static str> IntoStyle for (Arc<str>, Static<V>) {
async fn resolve(self) -> Self::AsyncOutput {
self
}

fn reset(_state: &mut Self::State) {}
}

#[cfg(feature = "nightly")]
Expand Down Expand Up @@ -592,6 +734,8 @@ impl<const V: &'static str> IntoStyle for crate::view::static_types::Static<V> {
async fn resolve(self) -> Self::AsyncOutput {
self
}

fn reset(_state: &mut Self::State) {}
}

/*
Expand Down
53 changes: 52 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,54 @@ 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
}

fn reset(state: &mut Self::State) {
let (el, _prev) = state;
Rndr::remove_attribute(el, "style");
}
}
Loading