Skip to content

Commit f7d0eea

Browse files
authored
feature: add class prop to <Html/> component (leptos-rs#554)
1 parent bf246a6 commit f7d0eea

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

meta/src/html.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,37 @@ use std::{cell::RefCell, rc::Rc};
88
pub struct HtmlContext {
99
lang: Rc<RefCell<Option<TextProp>>>,
1010
dir: Rc<RefCell<Option<TextProp>>>,
11+
class: Rc<RefCell<Option<TextProp>>>,
1112
}
1213

1314
impl HtmlContext {
1415
/// Converts the `<html>` metadata into an HTML string.
1516
pub fn as_string(&self) -> Option<String> {
16-
match (self.lang.borrow().as_ref(), self.dir.borrow().as_ref()) {
17-
(None, None) => None,
18-
(Some(lang), None) => Some(format!(" lang=\"{}\"", lang.get())),
19-
(None, Some(dir)) => Some(format!(" dir=\"{}\"", dir.get())),
20-
(Some(lang), Some(dir)) => {
21-
Some(format!(" lang=\"{}\" dir=\"{}\"", lang.get(), dir.get()))
22-
}
17+
let lang = self
18+
.lang
19+
.borrow()
20+
.as_ref()
21+
.map(|val| format!("lang=\"{}\"", val.get()));
22+
let dir = self
23+
.dir
24+
.borrow()
25+
.as_ref()
26+
.map(|val| format!("dir=\"{}\"", val.get()));
27+
let class = self
28+
.class
29+
.borrow()
30+
.as_ref()
31+
.map(|val| format!("class=\"{}\"", val.get()));
32+
let mut val = [lang, dir, class]
33+
.into_iter()
34+
.flatten()
35+
.collect::<Vec<_>>()
36+
.join(" ");
37+
if val.is_empty() {
38+
None
39+
} else {
40+
val.insert(0, ' ');
41+
Some(val)
2342
}
2443
}
2544
}
@@ -57,6 +76,9 @@ pub fn Html(
5776
/// The `dir` attribute on the `<html>`.
5877
#[prop(optional, into)]
5978
dir: Option<TextProp>,
79+
/// The `class` attribute on the `<html>`.
80+
#[prop(optional, into)]
81+
class: Option<TextProp>,
6082
) -> impl IntoView {
6183
cfg_if! {
6284
if #[cfg(any(feature = "csr", feature = "hydrate"))] {
@@ -71,15 +93,24 @@ pub fn Html(
7193
}
7294

7395
if let Some(dir) = dir {
96+
let el = el.clone();
7497
create_render_effect(cx, move |_| {
7598
let value = dir.get();
7699
_ = el.set_attribute("dir", &value);
77100
});
78101
}
102+
103+
if let Some(class) = class {
104+
create_render_effect(cx, move |_| {
105+
let value = class.get();
106+
_ = el.set_attribute("class", &value);
107+
});
108+
}
79109
} else {
80110
let meta = crate::use_head(cx);
81111
*meta.html.lang.borrow_mut() = lang;
82112
*meta.html.dir.borrow_mut() = dir;
113+
*meta.html.class.borrow_mut() = class;
83114
}
84115
}
85116
}

0 commit comments

Comments
 (0)