-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Accessible Icon to Leptos 0.7 (#405)
- Loading branch information
1 parent
de37ffa
commit 7f3ddc6
Showing
9 changed files
with
68 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 27 additions & 25 deletions
52
packages/primitives/leptos/accessible-icon/src/accessible_icon.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,38 @@ | ||
use leptos::*; | ||
use leptos::{ | ||
attr::{ | ||
aria_hidden, | ||
custom::{custom_attribute, CustomAttr}, | ||
AriaHidden, Attr, NextAttribute, | ||
}, | ||
prelude::*, | ||
}; | ||
use radix_leptos_visually_hidden::VisuallyHidden; | ||
|
||
pub type AccessibleIconAttrs = ( | ||
Attr<AriaHidden, &'static str>, | ||
CustomAttr<&'static str, &'static str>, | ||
); | ||
|
||
pub fn use_accessible_icon() -> AccessibleIconAttrs { | ||
aria_hidden("true").add_any_attr(custom_attribute("focusable", "false")) | ||
} | ||
|
||
#[component] | ||
pub fn AccessibleIcon( | ||
pub fn AccessibleIcon<R, IV>( | ||
/// The accessible label for the icon. This label will be visually hidden but announced to screen reader users, | ||
/// similar to `alt` text for `img` tags. | ||
#[prop(into)] | ||
label: MaybeSignal<String>, | ||
#[prop(optional)] children: Option<ChildrenFn>, | ||
) -> impl IntoView { | ||
let label = Signal::derive(move || label.get()); | ||
label: Signal<String>, | ||
render: R, | ||
) -> impl IntoView | ||
where | ||
R: Fn(AccessibleIconAttrs) -> IV, | ||
IV: IntoView, | ||
{ | ||
let attrs = use_accessible_icon(); | ||
|
||
view! { | ||
{children.map(|children| map_children(children().as_children()))} | ||
{render(attrs)} | ||
<VisuallyHidden>{label}</VisuallyHidden> | ||
} | ||
} | ||
|
||
fn map_children(children: &[View]) -> View { | ||
children | ||
.iter() | ||
.map(|child| match child { | ||
View::Element(element) => element | ||
.clone() | ||
.into_html_element() | ||
// Accessibility | ||
.attr("aria-hidden", "true") | ||
// See: https://allyjs.io/tutorials/focusing-in-svg.html#making-svg-elements-focusable | ||
.attr("focusable", "false") | ||
.into_view(), | ||
View::Component(component) => map_children(&component.children), | ||
_ => child.into_view(), | ||
}) | ||
.collect_view() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters