Debian Codegraph @

inst/sass-theme/app.R

raw
1 library(shiny)
2
3 output_css <- "theme.css"
4 scss <- "
5 // Color themes
6 $themes: (
7 default: #666,
8 banana: #f1c40f,
9 cherry: #c0392b,
10 blueberry: #8e44ad,
11 leaf: #27ae60,
12 nightsky: #2980b9
13 );
14
15 // Helper theme mixin
16 // @param $name: name of the theme (HTML class)
17 // @param $color: color of the theme
18 @mixin theme($name, $color) {
19 .#{$name} {
20 .component{
21 border-color: darken($color, 10%);
22
23 h2 {
24 color: $color;
25 }
26
27 li {
28 background: lighten($color, 45%);
29 }
30 }
31
32 }
33
34 // matches button id: #default, #banana, #cherry, ...
35 ##{$name} {
36 background: $color;
37
38 // #banana:hover
39 &:hover {
40 background: darken($color, 10%);
41 }
42
43 // #banana[disabled]
44 &[disabled] {
45 background: desaturate($color, 40%);
46 }
47 }
48 }
49
50 // Including themes
51 // By looping through the $themes map
52 @each $key, $value in $themes {
53 @include theme($key, $value);
54 }
55
56
57 // Default styles
58 .component {
59 background: #EFEFEF;
60 padding: 1em;
61 margin-bottom: 1em;
62 border-top: .5em solid;
63 ul {
64 list-style: none;
65 padding: 0;
66 overflow: hidden;
67 }
68 li {
69 float: left;
70 width: 24%;
71 height: 10em;
72 margin-right: 1%;
73 }
74 }
75 .controls > button {
76 border: none;
77 color: white;
78 padding: .5em;
79 border-radius: .15em;
80 font-weight: bold;
81 margin-bottom: .5em;
82 }
83 body {
84 padding: 1em;
85 }
86 * {
87 box-sizing: border-box;
88 }
89 "
90 css_value <- sass::sass(scss)
91
92 ui <- withTags(fluidPage(
93 # script to change the class in the browser
94 head(
95 # make sure to include the compiled css!
96 style(HTML(css_value)),
97
98 # watch for the button clicks to change the theme within the browser
99 script(HTML("
100 // when the document is ready...
101 $(function() {
102 // when a shiny input changes...
103 $(document).on('shiny:inputchanged', function(event) {
104 switch (event.name) {
105 // if the event name matches one of...
106 case 'default':
107 case 'banana':
108 case 'cherry':
109 case 'blueberry':
110 case 'leaf':
111 case 'nightsky':
112
113 $('.demo') // select the main body
114 .removeClass() // remove the class
115 .addClass('demo ' + event.name); // add the themed class
116
117 $('.controls button').removeAttr('disabled'); // make all buttons active
118 $('#' + event.name).attr('disabled', true); // disable the most recent button
119
120 break;
121 }
122 });
123 });
124 "))
125 ),
126
127 div(class = "demo nightsky",
128 div(class = "component",
129 h2("Sass Color Themes"),
130 ul(
131 li(),
132 li(),
133 li(),
134 li()
135 )
136 )
137 ),
138 div(class = "controls",
139 actionButton("default", "Default"),
140 actionButton("banana", "Funny Banana"),
141 actionButton("cherry", "Hot Chery"),
142 actionButton("blueberry", "Deep Purple"),
143 actionButton("leaf", "Smooth Leaf"),
144 actionButton("nightsky", "Night sky", disabled = "true")
145 )
146 ))
147
148 server <- function(input, output) {
149 hit <- function(button, name) {
150 if (button == 0) return()
151 cat("Button: '", name, "' was pressed! Value: ", button, "\n", sep = "")
152 }
153 observe({ hit(input$default, "Default") })
154 observe({ hit(input$banana, "Funny Banana") })
155 observe({ hit(input$cherry, "Hot Cherry") })
156 observe({ hit(input$blueberry, "Deep Purple") })
157 observe({ hit(input$leaf, "Smooth Leaf") })
158 observe({ hit(input$nightsky, "Night Sky") })
159 }
160
161 shinyApp(ui, server)