Skip to content

Commit

Permalink
Update Accessible Icon to Leptos 0.7 (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman authored Jan 5, 2025
1 parent de37ffa commit 7f3ddc6
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 53 deletions.
9 changes: 9 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 @@ -14,6 +14,7 @@ members = [
"packages/colors",
"packages/icons/dioxus",
"packages/icons/yew",
"packages/primitives/leptos/accessible-icon",
"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/accessible-icon.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ Contains the icon to make accessible.
{{#tabs global="framework" }}
{{#tab name="Leptos" }}

| Prop | Type | Default |
| ------- | --------------------- | ------- |
| `label` | `MaybeSignal<String>` | - |
| Prop | Type | Default |
| ------- | ---------------- | ------- |
| `label` | `Signal<String>` | - |

{{#endtab }}
{{#endtabs }}
Expand Down
6 changes: 2 additions & 4 deletions book/src/primitives/utilities/visually-hidden.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ Anything you put inside this component will be hidden from the screen but will b
{{#tabs global="framework" }}
{{#tab name="Leptos" }}

| Prop | Type | Default |
| ---------- | ----------------- | ------- |
| `as_child` | `MaybeProp<bool>` | `false` |
<i>No props.</i>

{{#endtab }}
{{#tab name="Yew" }}
Expand Down Expand Up @@ -112,7 +110,7 @@ fn Example() -> impl IntoView {
view! {
<button>
<GearIcon />
<VisuallyHidden>Settings</VisuallyHidden>
<VisuallyHidden>"Settings"</VisuallyHidden>
</button>
}
}
Expand Down
52 changes: 27 additions & 25 deletions packages/primitives/leptos/accessible-icon/src/accessible_icon.rs
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()
}
2 changes: 1 addition & 1 deletion stories/leptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ console_error_panic_hook.workspace = true
leptos = { workspace = true, features = ["csr"] }
leptos_router.workspace = true
log.workspace = true
# radix-leptos-accessible-icon = { path = "../../packages/primitives/leptos/accessible-icon" }
radix-leptos-accessible-icon = { path = "../../packages/primitives/leptos/accessible-icon" }
# radix-leptos-arrow = { path = "../../packages/primitives/leptos/arrow" }
# radix-leptos-aspect-ratio = { path = "../../packages/primitives/leptos/aspect-ratio" }
# radix-leptos-avatar = { path = "../../packages/primitives/leptos/avatar" }
Expand Down
20 changes: 10 additions & 10 deletions stories/leptos/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use leptos_router::{
// accessible_icon, arrow, aspect_ratio, avatar, checkbox, collection, focus_scope, label, menu,
// popper, portal, presence, progress, separator, slot, switch, toggle, visually_hidden,
// };
use crate::primitives::{label, visually_hidden};
use crate::primitives::{accessible_icon, label, visually_hidden};

#[component]
fn NavLink<H>(href: H, children: Children) -> impl IntoView
Expand Down Expand Up @@ -39,14 +39,14 @@ pub fn App() -> impl IntoView {
<li>
<NavLink href="/">Index</NavLink>
</li>
// <li>
// Accessible Icon
<li>
Accessible Icon

// <ul class="list-none m-0 ms-4 p-0">
// <li><NavLink href="/accessible-icon/styled">Styled</NavLink></li>
// <li><NavLink href="/accessible-icon/chromatic">Chromatic</NavLink></li>
// </ul>
// </li>
<ul class="list-none m-0 ms-4 p-0">
<li><NavLink href="/accessible-icon/styled">Styled</NavLink></li>
<li><NavLink href="/accessible-icon/chromatic">Chromatic</NavLink></li>
</ul>
</li>
// <li>
// Arrow

Expand Down Expand Up @@ -211,8 +211,8 @@ pub fn App() -> impl IntoView {
<Routes fallback=|| "Not found.".into_view()>
<Route path=path!("/") view=Index />

// <Route path="/accessible-icon/styled" view=accessible_icon::Styled />
// <Route path="/accessible-icon/chromatic" view=accessible_icon::Chromatic />
<Route path=path!("/accessible-icon/styled") view=accessible_icon::Styled />
<Route path=path!("/accessible-icon/chromatic") view=accessible_icon::Chromatic />

// <Route path="/arrow/styled" view=arrow::Styled />
// <Route path="/arrow/custom-sizes" view=arrow::CustomSizes />
Expand Down
2 changes: 1 addition & 1 deletion stories/leptos/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// pub mod accessible_icon;
pub mod accessible_icon;
// pub mod arrow;
// pub mod aspect_ratio;
// pub mod avatar;
Expand Down
23 changes: 14 additions & 9 deletions stories/leptos/src/primitives/accessible_icon.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use leptos::*;
use leptos::prelude::*;
use radix_leptos_accessible_icon::*;

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

#[component]
fn CrossIcon(#[prop(attrs)] attrs: Vec<(&'static str, Attribute)>) -> impl IntoView {
fn CrossIcon() -> impl IntoView {
view! {
<svg viewBox="0 0 32 32" width=24 height=24 fill="none" stroke="currentColor">
<path d="M2 30 L30 2 M30 30 L2 2" />
</svg>
}
.attrs(attrs)
}

0 comments on commit 7f3ddc6

Please sign in to comment.