-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7572dca
commit 4821f13
Showing
9 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "radix-leptos-accessible-icon" | ||
description = "Leptos port of Radix Accessible Icon." | ||
|
||
authors.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
version.workspace = true | ||
|
||
[dependencies] | ||
leptos.workspace = true | ||
radix-leptos-visually-hidden = { path = "../visually-hidden", version = "0.0.1" } |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<p align="center"> | ||
<a href="../../../../logo.svg" alt="Rust Radix logo"> | ||
<img src="../../../../logo.svg" width="300" height="200"> | ||
</a> | ||
</p> | ||
|
||
<h1 align="center">radix-leptos-accessible-icon</h1> | ||
|
||
Makes icons accessible by adding a label. | ||
|
||
## Rust Radix | ||
|
||
[Rust Radix](https://github.com/RustForWeb/radix) is a Rust port of [Radix](https://www.radix-ui.com/primitives). |
36 changes: 36 additions & 0 deletions
36
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use leptos::*; | ||
use radix_leptos_visually_hidden::VisuallyHidden; | ||
|
||
#[component] | ||
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: MaybeSignal<String>, | ||
#[prop(optional)] children: Option<ChildrenFn>, | ||
) -> impl IntoView { | ||
let label = Signal::derive(move || label.get()); | ||
|
||
view! { | ||
{children.map(|children| map_children(children().as_children()))} | ||
<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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//! Leptos port of [Radix Accessible Icon](https://www.radix-ui.com/primitives/docs/utilities/accessible-icon). | ||
//! | ||
//! Makes icons accessible by adding a label. | ||
//! | ||
//! See <https://www.radix-ui.com/primitives/docs/utilities/accessible-icon> for the original documentation. | ||
//! | ||
//! See [`@radix-ui/react-accessible-icon`](https://www.npmjs.com/package/@radix-ui/react-accessible-icon) for the original package. | ||
mod accessible_icon; | ||
|
||
pub use accessible_icon::*; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod accessible_icon; | ||
pub mod arrow; | ||
pub mod avatar; | ||
pub mod collection; | ||
|
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use leptos::*; | ||
use radix_leptos_accessible_icon::*; | ||
|
||
#[component] | ||
pub fn Styled() -> impl IntoView { | ||
view! { | ||
<button type="button"> | ||
<AccessibleIcon label="Close"> | ||
<CrossIcon /> | ||
</AccessibleIcon> | ||
</button> | ||
} | ||
} | ||
|
||
#[component] | ||
pub fn Chromatic() -> impl IntoView { | ||
view! { | ||
<p> | ||
Some text with an inline accessible icon{" "} | ||
<AccessibleIcon label="Close"> | ||
<CrossIcon attr:class="inline-block" /> | ||
</AccessibleIcon> | ||
</p> | ||
} | ||
} | ||
|
||
#[component] | ||
fn CrossIcon(#[prop(attrs)] attrs: Vec<(&'static str, Attribute)>) -> 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) | ||
} |