|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Adjusting a Page Theme on the Fly</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Adjusting a Page Theme on the Fly</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>In this example, we'll use a snapshot of the <a href="http://www.w3.org/TR/html401/present/styles.html">W3C HTML 4.01 specification for Style Sheets</a> and add a custom dynamic StyleSheet to apply some color and font size changes.</p> |
|
|
30 |
|
|
|
31 |
<p>A Progressive enhancement strategy is used to extract a static form on the page into a draggable Overlay. Additionally, one of the form inputs is replaced with a Slider. Enter any valid CSS color value into the other inputs (e.g. <code>#123456</code>, <code>#135</code>, <code>rgb(0,0,0)</code>, or <code>red</code>).</p> |
|
|
32 |
</div> |
|
|
33 |
|
|
|
34 |
<div class="example newwindow"> |
|
|
35 |
<a href="stylesheet-theme-example.html" target="_blank" class="button"> |
|
|
36 |
View Example in New Window |
|
|
37 |
</a> |
|
|
38 |
</div> |
|
|
39 |
|
|
|
40 |
<h3 class="first">Full code listing</h3> |
|
|
41 |
<h4 id="markup">Markup</h4> |
|
|
42 |
<p>The markup as stated above is a local snapshot of the HTML 4.01 spec, but with the following markup added to the end of the <code><body></code> to show a progressive enhancement model.</p> |
|
|
43 |
|
|
|
44 |
<pre class="code prettyprint"><div id="form_container"> |
|
|
45 |
<form class="yui3-widget-bd" id="theme_form" action="#" method="get"> |
|
|
46 |
<fieldset> |
|
|
47 |
<h3>Update Theme</h3> |
|
|
48 |
<label for="font_size">Font size:</label> |
|
|
49 |
<input type="text" size="3" id="font_size" value="16px"> |
|
|
50 |
|
|
|
51 |
<label for="heading_color">Heading color:</label> |
|
|
52 |
<input type="text" size="12" id="heading_color" value="#005A9C"> |
|
|
53 |
|
|
|
54 |
<label for="link_hover">Link hover backgound:</label> |
|
|
55 |
<input type="text" size="12" id="link_hover" value="#ffa"> |
|
|
56 |
</fieldset> |
|
|
57 |
<input type="submit"> |
|
|
58 |
</form> |
|
|
59 |
</div></pre> |
|
|
60 |
|
|
|
61 |
|
|
|
62 |
<h4>JavaScript</h4> |
|
|
63 |
|
|
|
64 |
<pre class="code prettyprint"><script src="../../build/yui/yui-min.js"></script> |
|
|
65 |
<script> |
|
|
66 |
// Create a new YUI instance, requiring stylesheet, overlay, slider, and the |
|
|
67 |
// dd-plugin to make the overlay draggable |
|
|
68 |
YUI().use("stylesheet","overlay","slider","dd-plugin", function (Y) { |
|
|
69 |
|
|
|
70 |
var myStyleSheet = new Y.StyleSheet(), |
|
|
71 |
overlayContent = Y.one('#form_container'), |
|
|
72 |
overlay, |
|
|
73 |
slider, |
|
|
74 |
slider_container, |
|
|
75 |
fontSizeInput, |
|
|
76 |
|
|
|
77 |
// Create the Overlay, using the form container as the contentBox. |
|
|
78 |
// The form is assigned a class yui-widget-bd that will be automatically |
|
|
79 |
// discovered by Overlay to populate the Overlay's body section. |
|
|
80 |
// The overlay is positioned in the top right corner, but made draggable |
|
|
81 |
// using Y.Plugin.Drag, provided by the dd-plugin module. |
|
|
82 |
overlay = new Y.Overlay({ |
|
|
83 |
srcNode: overlayContent, |
|
|
84 |
alignOn: [], |
|
|
85 |
width: '225px', |
|
|
86 |
align: { points: [ Y.WidgetPositionAlign.TR, Y.WidgetPositionAlign.TR ] }, |
|
|
87 |
plugins: [ Y.Plugin.Drag ] |
|
|
88 |
}).render(); |
|
|
89 |
|
|
|
90 |
// Slider needs a parent element to have the sam skin class for UI skinning |
|
|
91 |
overlayContent.addClass('yui3-skin-sam'); |
|
|
92 |
|
|
|
93 |
// Progressively enhance the font-size input with a Slider |
|
|
94 |
fontSizeInput = Y.one('#font_size'); |
|
|
95 |
fontSizeInput.set('type','hidden'); |
|
|
96 |
fontSizeInput.get('parentNode').insertBefore( |
|
|
97 |
Y.Node.create('6 <span></span> 36'), |
|
|
98 |
fontSizeInput); |
|
|
99 |
|
|
|
100 |
slider_container = fontSizeInput.previous( "span" ); |
|
|
101 |
|
|
|
102 |
// Create a Slider to contain font size between 6px and 36px, using the |
|
|
103 |
// page's current font size as the initial value. |
|
|
104 |
// Set up an event subscriber during construction to update the replaced |
|
|
105 |
// input field's value and apply the change to the StyleSheet |
|
|
106 |
slider = new Y.Slider({ |
|
|
107 |
length: '100px', |
|
|
108 |
min: 6, |
|
|
109 |
max: 36, |
|
|
110 |
value: parseInt(Y.one('body').getStyle('fontSize')) || 13, |
|
|
111 |
after: { |
|
|
112 |
valueChange: function (e) { |
|
|
113 |
var size = e.newVal + 'px'; |
|
|
114 |
|
|
|
115 |
this.thumb.set('title', size); |
|
|
116 |
fontSizeInput.set('value', size); |
|
|
117 |
|
|
|
118 |
myStyleSheet.set('body', { fontSize: size }); |
|
|
119 |
} |
|
|
120 |
} |
|
|
121 |
}).render( slider_container ); |
|
|
122 |
|
|
|
123 |
// The color inputs are assigned keyup listeners that will update the |
|
|
124 |
// StyleSheet if the current input value is a valid CSS color value |
|
|
125 |
|
|
|
126 |
// The heading input affects all h1s, h2, and h3s |
|
|
127 |
Y.on('keyup', function (e) { |
|
|
128 |
var color = this.get('value'); |
|
|
129 |
|
|
|
130 |
if (isValidColor(color)) { |
|
|
131 |
myStyleSheet.set('h1, h2, h3', { color: color }); |
|
|
132 |
} |
|
|
133 |
}, '#heading_color'); |
|
|
134 |
|
|
|
135 |
// The link hover affects the background color of links when they are |
|
|
136 |
// hovered. There is no way other than via stylesheet modification to |
|
|
137 |
// change pseudo-class styles. |
|
|
138 |
Y.on('keyup', function (e) { |
|
|
139 |
var color = this.get('value'); |
|
|
140 |
|
|
|
141 |
if (isValidColor(color)) { |
|
|
142 |
myStyleSheet.set('a:hover', { backgroundColor: color }); |
|
|
143 |
} |
|
|
144 |
}, '#link_hover'); |
|
|
145 |
|
|
|
146 |
// Progressive form enhancement complete, now prevent the form from |
|
|
147 |
// submitting normally. |
|
|
148 |
Y.one('#theme_form input[type=submit]').remove(); |
|
|
149 |
|
|
|
150 |
Y.on('submit', function (e) { |
|
|
151 |
e.halt(); |
|
|
152 |
}, '#theme_form'); |
|
|
153 |
|
|
|
154 |
// A rudimentary validator to make sure we're not trying to set |
|
|
155 |
// invalid color values in StyleSheet. |
|
|
156 |
function isValidColor(v) { |
|
|
157 |
return /^#[0-9a-f]{3}(?:[0-9a-f]{3})?$/i.test(v) || |
|
|
158 |
/^rgb\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)$/.test(v) || |
|
|
159 |
/^[a-z]{3,}$/i.test(v); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
}); |
|
|
163 |
</script> |
|
|
164 |
|
|
|
165 |
|
|
|
166 |
<script> |
|
|
167 |
YUI.Env.Tests = { |
|
|
168 |
examples: [], |
|
|
169 |
project: '../assets', |
|
|
170 |
assets: '../assets/stylesheet', |
|
|
171 |
name: 'stylesheet-theme', |
|
|
172 |
title: 'Adjusting a Page Theme on the Fly', |
|
|
173 |
newWindow: 'true', |
|
|
174 |
auto: false |
|
|
175 |
}; |
|
|
176 |
YUI.Env.Tests.examples.push('stylesheet-theme'); |
|
|
177 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
178 |
|
|
|
179 |
</script> |
|
|
180 |
<script src="../assets/yui/test-runner.js"></script></pre> |
|
|
181 |
|
|
|
182 |
|
|
|
183 |
<h4>CSS</h4> |
|
|
184 |
<p>This is the CSS added to the page to skin the Overlay and its content.</p> |
|
|
185 |
|
|
|
186 |
<pre class="code prettyprint"><style> |
|
|
187 |
/* For supporting browsers, the overlay is rendered semi-transparent with |
|
|
188 |
* fancy rounded corners */ |
|
|
189 |
.yui3-overlay { |
|
|
190 |
background: rgba(128,128,128,0.3); |
|
|
191 |
-moz-border-radius: 10px; |
|
|
192 |
-webkit-border-radius: 10px; |
|
|
193 |
border-radius: 10px; |
|
|
194 |
padding: 7px; |
|
|
195 |
cursor: move; |
|
|
196 |
} |
|
|
197 |
.yui3-overlay-content { |
|
|
198 |
background: rgba(205,205,205,0.3); |
|
|
199 |
-moz-border-radius: 10px; |
|
|
200 |
-webkit-border-radius: 10px; |
|
|
201 |
border-radius: 10px; |
|
|
202 |
padding: 1px; |
|
|
203 |
} |
|
|
204 |
.yui3-overlay form { |
|
|
205 |
background: #f2fbff url(../assets/stylesheet/gradient-promo.png) repeat-x scroll 0 0; |
|
|
206 |
border: 2px solid #fff; |
|
|
207 |
-moz-border-radius: 10px; |
|
|
208 |
-webkit-border-radius: 10px; |
|
|
209 |
border-radius: 10px; |
|
|
210 |
margin: 0; |
|
|
211 |
padding: 0; |
|
|
212 |
font-size: 13px; |
|
|
213 |
} |
|
|
214 |
.yui3-overlay fieldset { |
|
|
215 |
border: 1px solid #cde; |
|
|
216 |
-moz-border-radius: 10px; |
|
|
217 |
-webkit-border-radius: 10px; |
|
|
218 |
border-radius: 10px; |
|
|
219 |
margin: 0; |
|
|
220 |
padding: 20px; |
|
|
221 |
} |
|
|
222 |
.yui3-overlay h3 { |
|
|
223 |
border-bottom: 2px solid #fff; |
|
|
224 |
color: #479; |
|
|
225 |
background: transparent; |
|
|
226 |
margin: 0; |
|
|
227 |
font-size: 175%; |
|
|
228 |
} |
|
|
229 |
.yui3-overlay label { |
|
|
230 |
display: block; |
|
|
231 |
margin: 1.3em 0 0.5ex; |
|
|
232 |
font-weight: bold; |
|
|
233 |
color: #003; |
|
|
234 |
} |
|
|
235 |
.yui3-overlay p { |
|
|
236 |
margin: 2em 0 0; |
|
|
237 |
} |
|
|
238 |
/* override the move cursor for the Slider */ |
|
|
239 |
.yui3-overlay .yui3-slider:hover { |
|
|
240 |
cursor: default; |
|
|
241 |
} |
|
|
242 |
</style></pre> |
|
|
243 |
|
|
|
244 |
|
|
|
245 |
</div> |
|
|
246 |
</div> |
|
|
247 |
</div> |
|
|
248 |
|
|
|
249 |
<div class="yui3-u-1-4"> |
|
|
250 |
<div class="sidebar"> |
|
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
<div class="sidebox"> |
|
|
255 |
<div class="hd"> |
|
|
256 |
<h2 class="no-toc">Examples</h2> |
|
|
257 |
</div> |
|
|
258 |
|
|
|
259 |
<div class="bd"> |
|
|
260 |
<ul class="examples"> |
|
|
261 |
|
|
|
262 |
|
|
|
263 |
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input"> |
|
|
264 |
<a href="stylesheet-theme.html">Adjusting a Page Theme on the Fly</a> |
|
|
265 |
</li> |
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
</ul> |
|
|
271 |
</div> |
|
|
272 |
</div> |
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
<div class="sidebox"> |
|
|
277 |
<div class="hd"> |
|
|
278 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
279 |
</div> |
|
|
280 |
|
|
|
281 |
<div class="bd"> |
|
|
282 |
<ul class="examples"> |
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
<li data-description="Example Photo Browser application."> |
|
|
288 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
289 |
</li> |
|
|
290 |
|
|
|
291 |
|
|
|
292 |
</ul> |
|
|
293 |
</div> |
|
|
294 |
</div> |
|
|
295 |
|
|
|
296 |
</div> |
|
|
297 |
</div> |
|
|
298 |
</div> |
|
|
299 |
</div> |
|
|
300 |
|
|
|
301 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
302 |
<script>prettyPrint();</script> |
|
|
303 |
|
|
|
304 |
<script> |
|
|
305 |
YUI.Env.Tests = { |
|
|
306 |
examples: [], |
|
|
307 |
project: '../assets', |
|
|
308 |
assets: '../assets/stylesheet', |
|
|
309 |
name: 'stylesheet-theme', |
|
|
310 |
title: 'Adjusting a Page Theme on the Fly', |
|
|
311 |
newWindow: 'true', |
|
|
312 |
auto: false |
|
|
313 |
}; |
|
|
314 |
YUI.Env.Tests.examples.push('stylesheet-theme'); |
|
|
315 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
316 |
|
|
|
317 |
</script> |
|
|
318 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
</body> |
|
|
323 |
</html> |