Skip to content
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
41 changes: 15 additions & 26 deletions packages/primitives/leptos/accessible-icon/src/accessible_icon.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
use leptos::{
attr::{
AriaHidden, Attr, NextAttribute, aria_hidden,
custom::{CustomAttr, custom_attribute},
},
attr::{aria_hidden, custom::custom_attribute},
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<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.
pub fn AccessibleIcon(
/// 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: Signal<String>,
render: R,
) -> impl IntoView
where
R: Fn(AccessibleIconAttrs) -> IV,
IV: IntoView,
{
let attrs = use_accessible_icon();

children: TypedChildren<impl IntoView + 'static>,
) -> impl IntoView {
let label = Signal::derive(move || label.get());
view! {
{render(attrs)}
<VisuallyHidden>{label}</VisuallyHidden>
<>
{children
.into_inner()()
// Accessibility
.add_any_attr(aria_hidden("true"))
// See: https://allyjs.io/tutorials/focusing-in-svg.html#making-svg-elements-focusable
.add_any_attr(custom_attribute("focusable", "false"))}
<VisuallyHidden>{label.get()}</VisuallyHidden>
</>
}
}
22 changes: 8 additions & 14 deletions stories/leptos/src/primitives/accessible_icon.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use leptos::prelude::*;
use radix_leptos_accessible_icon::*;
use radix_leptos_accessible_icon::AccessibleIcon;

#[component]
pub fn Styled() -> impl IntoView {
view! {
<button type="button">
<AccessibleIcon
label="Close"
render=|attrs| view! {
<CrossIcon {..attrs} />
}
/>
<button r#type="button">
<AccessibleIcon label="Close">
<CrossIcon />
</AccessibleIcon>
</button>
}
}
Expand All @@ -20,12 +17,9 @@ pub fn Chromatic() -> impl IntoView {
view! {
<p>
Some text with an inline accessible icon{" "}
<AccessibleIcon
label="Close"
render=|attrs| view! {
<CrossIcon {..attrs} attr:class="inline-block" />
}
/>
<AccessibleIcon label="Close">
<CrossIcon attr:class="inline-block" />
</AccessibleIcon>
</p>
}
}
Expand Down