add aria-current to link (#2540)

This commit is contained in:
Maja Piechotka 2024-06-20 00:11:28 +02:00 committed by GitHub
parent c094bf3ec3
commit f7820d8d94
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 2 deletions

View file

@ -195,7 +195,7 @@ impl Debug for LinkProps {
/// # vdom.rebuild_in_place(); /// # vdom.rebuild_in_place();
/// # assert_eq!( /// # assert_eq!(
/// # dioxus_ssr::render(&vdom), /// # dioxus_ssr::render(&vdom),
/// # r#"<a href="/" dioxus-prevent-default="" class="link_class active" rel="link_rel" target="_blank" id="link_id">A fully configured link</a>"# /// # r#"<a href="/" dioxus-prevent-default="" class="link_class active" rel="link_rel" target="_blank" aria-current="page" id="link_id">A fully configured link</a>"#
/// # ); /// # );
/// ``` /// ```
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -252,6 +252,8 @@ pub fn Link(props: LinkProps) -> Element {
Some(class_) Some(class_)
}; };
let aria_current = (href == current_url).then_some("page");
let tag_target = new_tab.then_some("_blank"); let tag_target = new_tab.then_some("_blank");
let is_external = matches!(parsed_route, NavigationTarget::External(_)); let is_external = matches!(parsed_route, NavigationTarget::External(_));
@ -286,6 +288,7 @@ pub fn Link(props: LinkProps) -> Element {
class, class,
rel, rel,
target: tag_target, target: tag_target,
aria_current,
..attributes, ..attributes,
{children} {children}
} }

View file

@ -173,10 +173,11 @@ fn with_active_class_active() {
} }
let expected = format!( let expected = format!(
"<h1>App</h1><a {href} {default} {class}>Link</a>", "<h1>App</h1><a {href} {default} {class} {aria}>Link</a>",
href = r#"href="/""#, href = r#"href="/""#,
default = r#"dioxus-prevent-default="onclick""#, default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="test_class active_class""#, class = r#"class="test_class active_class""#,
aria = r#"aria-current="page""#,
); );
assert_eq!(prepare::<Route>(), expected); assert_eq!(prepare::<Route>(), expected);