5
<title>Example: Adjusting a Page Theme on the Fly</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Adjusting a Page Theme on the Fly</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
21
<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>
23
<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>
26
<div class="example newwindow">
27
<a href="stylesheet-theme-example.html" target="_blank" class="button">
28
View Example in New Window
32
<h3 class="first">Full code listing</h3>
33
<h4 id="markup">Markup</h4>
34
<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>
36
<pre class="code prettyprint"><div id="form_container">
37
<form class="yui3-widget-bd" id="theme_form" action="#" method="get">
39
<h3>Update Theme</h3>
40
<label for="font_size">Font size:</label>
41
<input type="text" size="3" id="font_size" value="16px">
43
<label for="heading_color">Heading color:</label>
44
<input type="text" size="12" id="heading_color" value="#005A9C">
46
<label for="link_hover">Link hover backgound:</label>
47
<input type="text" size="12" id="link_hover" value="#ffa">
48
</fieldset>
49
<input type="submit">
51
</div></pre>
56
<pre class="code prettyprint"><script src="../../build/yui/yui-min.js"></script>
58
// Create a new YUI instance, requiring stylesheet, overlay, slider, and the
59
// dd-plugin to make the overlay draggable
60
YUI().use("stylesheet","overlay","slider","dd-plugin", function (Y) {
62
var myStyleSheet = new Y.StyleSheet(),
63
overlayContent = Y.one('#form_container'),
69
// Create the Overlay, using the form container as the contentBox.
70
// The form is assigned a class yui-widget-bd that will be automatically
71
// discovered by Overlay to populate the Overlay's body section.
72
// The overlay is positioned in the top right corner, but made draggable
73
// using Y.Plugin.Drag, provided by the dd-plugin module.
74
overlay = new Y.Overlay({
75
srcNode: overlayContent,
77
width: '225px',
78
align: { points: [ Y.WidgetPositionAlign.TR, Y.WidgetPositionAlign.TR ] },
79
plugins: [ Y.Plugin.Drag ]
82
// Slider needs a parent element to have the sam skin class for UI skinning
83
overlayContent.addClass('yui3-skin-sam');
85
// Progressively enhance the font-size input with a Slider
86
fontSizeInput = Y.one('#font_size');
87
fontSizeInput.set('type','hidden');
88
fontSizeInput.get('parentNode').insertBefore(
89
Y.Node.create('6 <span></span> 36'),
92
slider_container = fontSizeInput.previous( "span" );
94
// Create a Slider to contain font size between 6px and 36px, using the
95
// page's current font size as the initial value.
96
// Set up an event subscriber during construction to update the replaced
97
// input field's value and apply the change to the StyleSheet
98
slider = new Y.Slider({
99
length: '100px',
102
value: parseInt(Y.one('body').getStyle('fontSize')) || 13,
104
valueChange: function (e) {
105
var size = e.newVal + 'px';
107
this.thumb.set('title', size);
108
fontSizeInput.set('value', size);
110
myStyleSheet.set('body', { fontSize: size });
113
}).render( slider_container );
115
// The color inputs are assigned keyup listeners that will update the
116
// StyleSheet if the current input value is a valid CSS color value
118
// The heading input affects all h1s, h2, and h3s
119
Y.on('keyup', function (e) {
120
var color = this.get('value');
122
if (isValidColor(color)) {
123
myStyleSheet.set('h1, h2, h3', { color: color });
125
}, '#heading_color');
127
// The link hover affects the background color of links when they are
128
// hovered. There is no way other than via stylesheet modification to
129
// change pseudo-class styles.
130
Y.on('keyup', function (e) {
131
var color = this.get('value');
133
if (isValidColor(color)) {
134
myStyleSheet.set('a:hover', { backgroundColor: color });
136
}, '#link_hover');
138
// Progressive form enhancement complete, now prevent the form from
139
// submitting normally.
140
Y.one('#theme_form input[type=submit]').remove();
142
Y.on('submit', function (e) {
144
}, '#theme_form');
146
// A rudimentary validator to make sure we're not trying to set
147
// invalid color values in StyleSheet.
148
function isValidColor(v) {
149
return /^#[0-9a-f]{3}(?:[0-9a-f]{3})?$/i.test(v) ||
150
/^rgb\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)$/.test(v) ||
151
/^[a-z]{3,}$/i.test(v);
155
</script></pre>
159
<p>This is the CSS added to the page to skin the Overlay and its content.</p>
161
<pre class="code prettyprint"><style>
162
/* For supporting browsers, the overlay is rendered semi-transparent with
163
* fancy rounded corners */
165
background: rgba(128,128,128,0.3);
166
-moz-border-radius: 10px;
167
-webkit-border-radius: 10px;
172
.yui3-overlay-content {
173
background: rgba(205,205,205,0.3);
174
-moz-border-radius: 10px;
175
-webkit-border-radius: 10px;
180
background: #f2fbff url(../assets/stylesheet/gradient-promo.png) repeat-x scroll 0 0;
181
border: 2px solid #fff;
182
-moz-border-radius: 10px;
183
-webkit-border-radius: 10px;
189
.yui3-overlay fieldset {
190
border: 1px solid #cde;
191
-moz-border-radius: 10px;
192
-webkit-border-radius: 10px;
198
border-bottom: 2px solid #fff;
200
background: transparent;
204
.yui3-overlay label {
206
margin: 1.3em 0 0.5ex;
213
/* override the move cursor for the Slider */
214
.yui3-overlay .yui3-slider:hover {
217
</style></pre>
223
<div id="sidebar" class="yui3-u">
227
<div class="sidebox">
229
<h2 class="no-toc">Examples</h2>
233
<ul class="examples">
236
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
237
<a href="stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
249
<div class="sidebox">
251
<h2 class="no-toc">Examples That Use This Component</h2>
255
<ul class="examples">
260
<li data-description="Example Photo Browser application.">
261
<a href="../dd/photo-browser.html">Photo Browser</a>
273
<script src="../assets/vendor/prettify/prettify-min.js"></script>
274
<script>prettyPrint();</script>