Compare commits

..

1 Commits

Author SHA1 Message Date
Greg Johnston
2d7fe46661 docs: add document on adding class and other attributes to <A/> component 2025-06-15 13:26:51 -04:00
3 changed files with 23 additions and 22 deletions

View File

@@ -66,6 +66,25 @@ where
/// This is helpful for accessibility and for styling. For example, maybe you want to set the link a
/// different color if its a link to the page youre currently on.
///
/// ### Additional Attributes
///
/// You can add additional HTML attributes to the `<a>` element created by this component using the attribute
/// spreading syntax for components. For example, to add a class, you can use `attr:class="my-link"`.
/// Alternately, you can add any number of HTML attributes (include `class`) after a `{..}` marker.
///
/// ```rust
/// # use leptos::prelude::*; use leptos_router::components::A;
/// # fn spread_example() -> impl IntoView {
/// view! {
/// <A href="/about" attr:class="my-link" {..} id="foo">"Some link"</A>
/// <A href="/about" {..} class="my-link" id="bar">"Another link"</A>
/// <A href="/about" {..} class:my-link=true id="baz">"One more"</A>
/// }
/// # }
/// ```
///
/// For more information on this attribute spreading syntax, [see here](https://book.leptos.dev/view/03_components.html#spreading-attributes-onto-components).
///
/// ### DOM Properties
///
/// `<a>` elements can take several additional DOM properties with special meanings.

View File

@@ -540,20 +540,11 @@ impl IntoClass for (&'static str, bool) {
fn rebuild(self, state: &mut Self::State) {
let (name, include) = self;
let (class_list, prev_include, prev_name) = state;
if name == *prev_name {
if include != *prev_include {
if include {
Rndr::add_class(class_list, name);
} else {
Rndr::remove_class(class_list, name);
}
}
} else {
if *prev_include {
Rndr::remove_class(class_list, prev_name);
}
if include != *prev_include {
if include {
Rndr::add_class(class_list, name);
} else {
Rndr::remove_class(class_list, name);
}
}
*prev_include = include;

View File

@@ -209,15 +209,6 @@ where
fn rebuild(self, state: &mut Self::State) {
let (name, mut f) = self;
let prev_name = state.name;
let prev_state = state.effect.take_value();
if let Some((list, prev_include)) = &prev_state {
if prev_name != name && *prev_include {
Rndr::remove_class(list, prev_name);
}
}
// Name might've updated:
state.name = name;
let mut first_run = true;
@@ -241,7 +232,7 @@ where
}
}
},
prev_state,
state.effect.take_value(),
);
}