@@ -8,18 +8,37 @@ use std::{cell::RefCell, rc::Rc};
8
8
pub struct HtmlContext {
9
9
lang : Rc < RefCell < Option < TextProp > > > ,
10
10
dir : Rc < RefCell < Option < TextProp > > > ,
11
+ class : Rc < RefCell < Option < TextProp > > > ,
11
12
}
12
13
13
14
impl HtmlContext {
14
15
/// Converts the `<html>` metadata into an HTML string.
15
16
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)
23
42
}
24
43
}
25
44
}
@@ -57,6 +76,9 @@ pub fn Html(
57
76
/// The `dir` attribute on the `<html>`.
58
77
#[ prop( optional, into) ]
59
78
dir : Option < TextProp > ,
79
+ /// The `class` attribute on the `<html>`.
80
+ #[ prop( optional, into) ]
81
+ class : Option < TextProp > ,
60
82
) -> impl IntoView {
61
83
cfg_if ! {
62
84
if #[ cfg( any( feature = "csr" , feature = "hydrate" ) ) ] {
@@ -71,15 +93,24 @@ pub fn Html(
71
93
}
72
94
73
95
if let Some ( dir) = dir {
96
+ let el = el. clone( ) ;
74
97
create_render_effect( cx, move |_| {
75
98
let value = dir. get( ) ;
76
99
_ = el. set_attribute( "dir" , & value) ;
77
100
} ) ;
78
101
}
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
+ }
79
109
} else {
80
110
let meta = crate :: use_head( cx) ;
81
111
* meta. html. lang. borrow_mut( ) = lang;
82
112
* meta. html. dir. borrow_mut( ) = dir;
113
+ * meta. html. class. borrow_mut( ) = class;
83
114
}
84
115
}
85
116
}
0 commit comments