Skip to content

Commit 1a7da97

Browse files
author
geoffreygarrett
committed
Update Visually Hidden to Leptos 0.7
Closes #428
1 parent 2ab3db1 commit 1a7da97

File tree

4 files changed

+49
-47
lines changed

4 files changed

+49
-47
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ yew = "0.21.0"
5858
yew-router = "0.18.0"
5959
yew-struct-component = "0.1.4"
6060
yew-style = "0.1.4"
61+
radix-leptos-primitive = "0.0.2"
6162

6263
[patch.crates-io]
6364
yew = { git = "https://github.com/RustForWeb/yew.git", branch = "feature/use-composed-ref" }
6465
yew-router = { git = "https://github.com/RustForWeb/yew.git", branch = "feature/use-composed-ref" }
66+
leptos-node-ref = { git = "https://github.com/geoffreygarrett/leptos-utils", branch = "feature/any-node-ref" }
67+
radix-leptos-primitive = { git = "https://github.com/geoffreygarrett/radix", branch = "update/leptos-0.7-primitive" }

packages/primitives/leptos/visually-hidden/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ version.workspace = true
1111

1212
[dependencies]
1313
leptos.workspace = true
14-
leptos-style.workspace = true
14+
radix-leptos-primitive.workspace = true
15+
leptos-node-ref.workspace = true

packages/primitives/leptos/visually-hidden/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ See [the Rust Radix book](https://radix.rustforweb.org/) for documentation.
1616

1717
## Rust For Web
1818

19-
The Rust Radix project is part of [Rust For Web](https://github.com/RustForWeb).
19+
The Rust Radix project is part of the [Rust For Web](https://github.com/RustForWeb).
2020

2121
[Rust For Web](https://github.com/RustForWeb) creates and ports web UI libraries for Rust. All projects are free and open source.
Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
1-
use leptos::prelude::*;
2-
use leptos_style::Style;
1+
use leptos::{html, prelude::*};
2+
use leptos_node_ref::AnyNodeRef;
3+
use radix_leptos_primitive::Primitive;
34

4-
pub struct UseVisuallyHiddenProps {
5-
style: Style,
6-
}
7-
8-
pub struct UseVisuallyHiddenAttrs {
9-
style: Style,
10-
}
11-
12-
pub fn use_visually_hidden(props: UseVisuallyHiddenProps) -> UseVisuallyHiddenAttrs {
13-
UseVisuallyHiddenAttrs {
14-
style: props.style.with_defaults([
15-
// See: https://github.com/twbs/bootstrap/blob/master/scss/mixins/_screen-reader.scss
16-
("position", "absolute"),
17-
("border", "0px"),
18-
("width", "1px"),
19-
("height", "1px"),
20-
("padding", "0px"),
21-
("margin", "-1px"),
22-
("overflow", "hidden"),
23-
("clip", "rect(0, 0, 0, 0)"),
24-
("white-space", "nowrap"),
25-
("word-wrap", "normal"),
26-
]),
27-
}
28-
}
5+
/* -------------------------------------------------------------------------------------------------
6+
* VisuallyHidden
7+
* -----------------------------------------------------------------------------------------------*/
298

9+
/// A component that visually hides its children while keeping them accessible
10+
/// to screen reader users. Matches the React `VisuallyHidden` component’s
11+
/// default styles and behavior.
3012
#[component]
13+
#[allow(non_snake_case)]
3114
pub fn VisuallyHidden(
32-
#[prop(into, optional)] style: Style,
33-
#[prop(optional)] children: Option<Children>,
34-
) -> impl IntoView {
35-
let UseVisuallyHiddenAttrs { style } = use_visually_hidden(UseVisuallyHiddenProps { style });
15+
/// The content to be visually hidden but still accessible to screen readers.
16+
children: TypedChildrenFn<impl IntoView + 'static>,
17+
18+
/// If `true`, the `Primitive` is rendered as the child element rather than wrapped.
19+
#[prop(into, optional)]
20+
as_child: MaybeProp<bool>,
3621

22+
/// A reference to the underlying DOM node.
23+
#[prop(into, optional)]
24+
node_ref: AnyNodeRef,
25+
) -> impl IntoView {
26+
// See: https://github.com/twbs/bootstrap/blob/main/scss/mixins/_visually-hidden.scss
3727
view! {
38-
<span style=style>
39-
{children.map(|children| children())}
40-
</span>
28+
<Primitive
29+
element=html::span
30+
children=children
31+
as_child=as_child
32+
node_ref=node_ref
33+
34+
// visually hide this content but keep it available to assistive tech
35+
style:position="absolute"
36+
style:border="0px"
37+
style:width="1px"
38+
style:height="1px"
39+
style:padding="0px"
40+
style:margin="-1px"
41+
style:overflow="hidden"
42+
style:clip="rect(0, 0, 0, 0)"
43+
style:white-space="nowrap"
44+
style:word-wrap="normal"
45+
/>
4146
}
4247
}
4348

44-
#[component]
45-
pub fn VisuallyHiddenAsChild<R, RV>(
46-
#[prop(into, optional)] style: Style,
47-
render: R,
48-
) -> impl IntoView
49-
where
50-
R: Fn(UseVisuallyHiddenAttrs) -> RV,
51-
RV: IntoView,
52-
{
53-
let attrs = use_visually_hidden(UseVisuallyHiddenProps { style });
49+
/* -----------------------------------------------------------------------------------------------*/
5450

55-
render(attrs)
51+
pub mod primitive {
52+
pub use super::*;
53+
pub use VisuallyHidden as Root;
5654
}

0 commit comments

Comments
 (0)