@@ -10,43 +10,38 @@ pub struct DemoCode {
10
10
}
11
11
12
12
#[ component]
13
- pub fn Demo ( demo_code : DemoCode , children : Children ) -> impl IntoView {
13
+ pub fn Demo ( demo_code : DemoCode , # [ prop ( optional ) ] children : Option < Children > ) -> impl IntoView {
14
14
let theme = use_theme ( Theme :: light) ;
15
- let style = create_memo ( move |_| {
16
- let mut style = String :: from ( "background-image: url(/grid_dot.svg); background-repeat: repeat; background-size: 1.5rem; margin-top: 1rem; padding: 1rem; border-top-left-radius: 0.5rem; border-top-right-radius: 0.5rem;" ) ;
15
+ let css_vars = Memo :: new ( move |_| {
16
+ let mut css_vars = String :: new ( ) ;
17
17
theme. with ( |theme| {
18
18
if theme. common . color_scheme == "dark" {
19
- style. push_str ( "border: 1px solid #383f52;" ) ;
19
+ css_vars. push_str ( "--demo-color: #ffffff60;" ) ;
20
+ css_vars. push_str ( "--demo-color-hover: #ffffffe0;" ) ;
21
+ css_vars. push_str ( "--demo-border-color: #383f52;" ) ;
22
+ css_vars. push_str ( "--demo-background-color: #242832;" ) ;
20
23
} else {
21
- style. push_str ( & format ! ( "border: 1px solid {};" , theme. common. border_color) ) ;
22
- }
23
- } ) ;
24
- style
25
- } ) ;
26
- let code_style = create_memo ( move |_| {
27
- let mut style = String :: from ( "font-weight: 400; font-size: 0.875em; line-height: 1.7142857;margin-bottom: 1rem; padding: 1rem; border-bottom-left-radius: 0.5rem; border-bottom-right-radius: 0.5rem; overflow: auto;" ) ;
28
- theme. with ( |theme| {
29
- if theme. common . color_scheme == "dark" {
30
- style. push_str ( "border: 1px solid #383f52; border-top-width: 0;" ) ;
31
- style. push_str ( "background-color: #242832;" ) ;
32
- } else {
33
- style. push_str ( & format ! (
34
- "border: 1px solid {}; border-top-width: 0;" ,
24
+ css_vars. push_str ( "--demo-color: #00000060;" ) ;
25
+ css_vars. push_str ( "--demo-color-hover: #000000e0;" ) ;
26
+ css_vars. push_str ( & format ! (
27
+ "--demo-border-color: {};" ,
35
28
theme. common. border_color
36
29
) ) ;
37
- style . push_str ( "background-color: #f9fafb;" ) ;
30
+ css_vars . push_str ( "--demo- background-color: #f9fafb;" ) ;
38
31
}
39
32
} ) ;
40
- style
33
+ css_vars
41
34
} ) ;
42
- let content_class = create_memo ( move |_| {
35
+
36
+ let code_class = Memo :: new ( move |_| {
43
37
theme. with ( |theme| {
44
38
format ! (
45
- "color-scheme--{}" ,
39
+ "demo-demo__code color-scheme--{}" ,
46
40
theme. common. color_scheme
47
41
)
48
42
} )
49
43
} ) ;
44
+ let is_show_code = RwSignal :: new ( children. is_none ( ) ) ;
50
45
51
46
let is_highlight = demo_code. is_highlight ;
52
47
let frag = ( demo_code. children ) ( ) ;
@@ -64,8 +59,47 @@ pub fn Demo(demo_code: DemoCode, children: Children) -> impl IntoView {
64
59
<Style id="leptos-thaw-syntect-css" >
65
60
{ include_str!( "./syntect-css.css" ) }
66
61
</Style >
67
- <div style=move || style. get( ) >{ children( ) } </div>
68
- <div style=move || code_style. get( ) class=move || content_class. get( ) >
62
+ <Style id="demo-demo" >
63
+ { include_str!( "./demo.css" ) }
64
+ </Style >
65
+ <div class="demo-demo" style=move || css_vars. get( ) >
66
+ {
67
+ if let Some ( children) = children {
68
+ view! {
69
+ <div class="demo-demo__view" >{ children( ) } </div>
70
+ <div class="demo-demo__toolbar" class=( "demo-demo__toolbar--code" , move || !is_show_code. get( ) ) >
71
+ <Popover >
72
+ <PopoverTrigger slot>
73
+ <span on: click=move |_| is_show_code. update( |show| * show = !* show) class="demo-demo__toolbar-btn" >
74
+ {
75
+ move || if is_show_code. get( ) {
76
+ view! {
77
+ <Icon icon=icondata:: LuCode2 />
78
+ }
79
+ } else {
80
+ view! {
81
+ <Icon icon=icondata:: LuCode />
82
+ }
83
+ }
84
+ }
85
+ </span>
86
+ </PopoverTrigger >
87
+ {
88
+ move || if is_show_code. get( ) {
89
+ "Hide code"
90
+ } else {
91
+ "Show code"
92
+ }
93
+ }
94
+ </Popover >
95
+
96
+ </div>
97
+ } . into( )
98
+ } else {
99
+ None
100
+ }
101
+ }
102
+ <div class=move || code_class. get( ) style: display=move || ( !is_show_code. get( ) ) . then_some( "none" ) >
69
103
{
70
104
if is_highlight {
71
105
view! {
@@ -77,6 +111,7 @@ pub fn Demo(demo_code: DemoCode, children: Children) -> impl IntoView {
77
111
}
78
112
}
79
113
}
114
+ </div>
80
115
</div>
81
116
}
82
117
}
0 commit comments