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

Update Direction to Leptos 0.7 #409

Merged
merged 1 commit into from
Jan 5, 2025
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"packages/primitives/leptos/accessible-icon",
"packages/primitives/leptos/arrow",
"packages/primitives/leptos/aspect-ratio",
"packages/primitives/leptos/direction",
"packages/primitives/leptos/id",
"packages/primitives/leptos/label",
"packages/primitives/leptos/visually-hidden",
Expand Down
6 changes: 3 additions & 3 deletions book/src/primitives/utilities/direction-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ When creating localized apps that require right-to-left (RTL) reading direction,
{{#tabs global="framework" }}
{{#tab name="Leptos" }}

| Prop | Type | Default |
| ----- | ------------------------ | ------- |
| `dir` | `MaybeSignal<Direction>` | - |
| Prop | Type | Default |
| ----- | ------------------- | ------- |
| `dir` | `Signal<Direction>` | - |

{{#endtab }}
{{#tab name="Yew" }}
Expand Down
58 changes: 51 additions & 7 deletions packages/primitives/leptos/direction/src/direction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::{Display, Formatter};

use leptos::*;
use leptos::{attr::AttributeValue, context::Provider, prelude::*, tachys::renderer::Rndr};

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Direction {
Expand All @@ -21,17 +21,61 @@ impl Display for Direction {
}
}

impl IntoAttribute for Direction {
fn into_attribute(self) -> Attribute {
Attribute::String(self.to_string().into())
impl AttributeValue for Direction {
type AsyncOutput = Self;
type State = (leptos::tachys::renderer::types::Element, Direction);
type Cloneable = Direction;
type CloneableOwned = Direction;

fn html_len(&self) -> usize {
self.to_string().len()
}

fn to_html(self, key: &str, buf: &mut String) {
<&str as AttributeValue>::to_html(self.to_string().as_str(), key, buf);
}

fn to_template(_key: &str, _buf: &mut String) {}

fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &leptos::tachys::renderer::types::Element,
) -> Self::State {
let (el, _) =
<&str as AttributeValue>::hydrate::<FROM_SERVER>(self.to_string().as_str(), key, el);
(el, self)
}

fn build(self, el: &leptos::tachys::renderer::types::Element, key: &str) -> Self::State {
Rndr::set_attribute(el, key, &self.to_string());
(el.clone(), self)
}

fn into_attribute_boxed(self: Box<Self>) -> Attribute {
self.into_attribute()
fn rebuild(self, key: &str, state: &mut Self::State) {
let (el, prev_value) = state;
if self != *prev_value {
Rndr::set_attribute(el, key, &self.to_string());
}
*prev_value = 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
}
}

type DirectionContextValue = MaybeSignal<Direction>;
type DirectionContextValue = Signal<Direction>;

#[component]
pub fn DirectionProvider(
Expand Down
Loading