In this example, we'll change some colors in this page's color theme. Enter any valid CSS color value into the inputs and submit the changes to see them applied to the page.
For the sake of illustration, we'll leave the form in our demo unstyled, then apply a skin to it when we create our working YAHOO.util.StyleSheet
instance.
1 | <div id="demo"> |
2 | <form id="demo_form" action="#" method="get"> |
3 | <fieldset> |
4 | <p>Example values: #123456 or #123 or rgb(0,10,30) or red</p> |
5 | <label for="demo_headings">Headings and labels</label> |
6 | <input type="text" size="7" id="demo_headings" value="#e76300"> |
7 | <pre><code>h1,h2,h3,h4,h5,h6, |
8 | #demo label { |
9 | color: <em id="demo_heading_value">#e76300</em>; |
10 | }</code></pre> |
11 | |
12 | <label for="demo_bg">Demo background</label> |
13 | <input type="text" size="7" id="demo_bg" value="#89d"> |
14 | <pre><code>.example .promo { |
15 | background-color: <em id="demo_background_value">#89d</em>; |
16 | }</code></pre> |
17 | <label for="demo_highlight">Left nav highlight</label> |
18 | <input type="text" size="7" id="demo_highlight" value="#f82"> |
19 | <pre><code>#toc ul li.active, |
20 | #toc ul li a:hover { |
21 | background-color: <em id="demo_highlight_value">#f82</em>; |
22 | }</code></pre> |
23 | |
24 | </fieldset> |
25 | <p> |
26 | <input type="submit" id="update" value="Update theme"> |
27 | </p> |
28 | </form> |
29 | </div> |
view plain | print | ? |
We'll put our code in a YAHOO.demo.PageTheme
namespace. In this namespace, we'll add an init()
method that we'll schedule to execute when the DOM is ready. We'll seed the init()
method by creating an instance of YAHOO.util.StyleSheet
and passing the constructor all the CSS to skin the form.
1 | (function () { |
2 | |
3 | // Some shortcuts |
4 | var Dom = YAHOO.util.Dom, |
5 | trim = YAHOO.lang.trim, |
6 | Demo; |
7 | |
8 | Demo = YAHOO.namespace('demo').PageTheme = { |
9 | theme : null, |
10 | |
11 | init : function () { |
12 | // Create a new StyleSheet instance for us to override some current |
13 | // page styles. For illustration, seed it with the CSS to style the |
14 | // demo form |
15 | Demo.theme = YAHOO.util.StyleSheet("\ |
16 | #demo form,\ |
17 | #demo fieldset {\ |
18 | border: none; padding: 0; margin: 0;\ |
19 | }\ |
20 | #demo fieldset p {\ |
21 | background: #fff;\ |
22 | border: 1px solid #ccc;\ |
23 | padding: 1em 1ex;\ |
24 | }\ |
25 | #demo pre code {\ |
26 | background: #fff;\ |
27 | border: 1px solid #ccc;\ |
28 | color: #555;\ |
29 | display: block;\ |
30 | font-weight: normal;\ |
31 | margin: 1ex 0 1em;\ |
32 | padding: 1ex;\ |
33 | }\ |
34 | #demo label {\ |
35 | font-weight: bold;\ |
36 | color: #e76300;\ |
37 | }\ |
38 | #demo pre code em {\ |
39 | color: #000;\ |
40 | font-weight: bold;\ |
41 | }\ |
42 | "); |
43 | } |
44 | }; |
45 | |
46 | YAHOO.util.Event.onDOMReady(Demo.init); |
47 | |
48 | })(); |
view plain | print | ? |
This is enough to create a new StyleSheet instance, hosting a <style>
element with our form's skin. Note that this is done for illustration only. In general, this approach should be avoided in production environments unless there's a compelling reason. Static CSS should be served as .css
files linked from <link>
elements.
Now we'll add the important stuff. Listening for the form submission to change the style rules. We'll cache the inputs in the init()
method and set up the submit listener to call a new method update()
.
1 | Demo = YAHOO.namespace('demo').PageTheme = { |
2 | theme : null, |
3 | |
4 | headings : null, |
5 | background : null, |
6 | highlight : null, |
7 | |
8 | isValidColor : function (v) {...}, |
9 | |
10 | init : function () { |
11 | Demo.theme = YAHOO.util.StyleSheet(...); |
12 | |
13 | // Store the input fields for value retrieval |
14 | Demo.headings = Dom.get('demo_headings'); |
15 | Demo.background = Dom.get('demo_bg'); |
16 | Demo.highlight = Dom.get('demo_highlight'); |
17 | |
18 | // Set up the submit handler to update the page styles |
19 | YAHOO.util.Event.on('demo_form','submit', function (e) { |
20 | YAHOO.util.Event.stopEvent(e); |
21 | |
22 | Demo.update(); |
23 | }); |
24 | }, |
25 | |
26 | update : function () { |
27 | var v = trim(Demo.headings.value); |
28 | if (Demo.isValidColor(v)) { |
29 | // multiple selectors are delimited by commas |
30 | Demo.theme.set('h1,h2,h3,h4,h5,h6, #demo label', { color : v }); |
31 | |
32 | Dom.get('demo_heading_value').innerHTML = v; |
33 | } |
34 | |
35 | v = trim(Demo.background.value); |
36 | if (Demo.isValidColor(v)) { |
37 | // use camelCase for style property names |
38 | Demo.theme.set('.example .promo', { backgroundColor : v }); |
39 | |
40 | Dom.get('demo_background_value').innerHTML = v; |
41 | } |
42 | |
43 | v = trim(Demo.highlight.value); |
44 | if (Demo.isValidColor(v)) { |
45 | Demo.theme.set( |
46 | '#toc ul li.selected,'+ |
47 | '#toc ul li a:hover', { backgroundColor : v }); |
48 | |
49 | Dom.get('demo_highlight_value').innerHTML = v; |
50 | } |
51 | } |
52 | }; |
view plain | print | ? |
And that's it! If you have an interactive console such as FireBug, you can play around with the StyleSheet instance at YAHOO.demo.PageTheme.theme
.
The full code listing for this example is below, including the source for isValidColor
. The full markup was included above.
1 | (function () { |
2 | |
3 | // Some shortcuts |
4 | var Dom = YAHOO.util.Dom, |
5 | trim = YAHOO.lang.trim, |
6 | Demo; |
7 | |
8 | Demo = YAHOO.namespace('demo').PageTheme = { |
9 | theme : null, |
10 | |
11 | headings : null, |
12 | background : null, |
13 | highlight : null, |
14 | |
15 | isValidColor : function (v) { |
16 | return /^#[0-9a-f]{3}(?:[0-9a-f]{3})?$/i.test(v) || |
17 | /^rgb\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)$/.test(v) || |
18 | /^[a-z]+$/i.test(v); |
19 | }, |
20 | |
21 | init : function () { |
22 | // Create a new StyleSheet instance for us to override some current |
23 | // page styles. For illustration, seed it with the CSS to style the |
24 | // demo form |
25 | Demo.theme = YAHOO.util.StyleSheet("\ |
26 | #demo form,\ |
27 | #demo fieldset { border: none; padding: 0; margin: 0; }\ |
28 | #demo fieldset p {\ |
29 | background: #fff;\ |
30 | border: 1px solid #ccc;\ |
31 | padding: 1em 1ex;\ |
32 | }\ |
33 | #demo pre code {\ |
34 | background: #fff;\ |
35 | border: 1px solid #ccc;\ |
36 | color: #555;\ |
37 | display: block;\ |
38 | font-weight: normal;\ |
39 | margin: 1ex 0 1em;\ |
40 | padding: 1ex;\ |
41 | }\ |
42 | #demo label {\ |
43 | font-weight: bold;\ |
44 | color: #e76300;\ |
45 | }\ |
46 | #demo pre code em {\ |
47 | color: #000;\ |
48 | font-weight: bold;\ |
49 | }\ |
50 | "); |
51 | |
52 | // Store the input fields for value retrieval |
53 | Demo.headings = Dom.get('demo_headings'); |
54 | Demo.background = Dom.get('demo_bg'); |
55 | Demo.highlight = Dom.get('demo_highlight'); |
56 | |
57 | // Set up the submit handler to update the page styles |
58 | YAHOO.util.Event.on('demo_form','submit', function (e) { |
59 | YAHOO.util.Event.stopEvent(e); |
60 | |
61 | Demo.update(); |
62 | }); |
63 | }, |
64 | |
65 | update : function () { |
66 | var v = trim(Demo.headings.value); |
67 | if (Demo.isValidColor(v)) { |
68 | // multiple selectors are delimited by commas |
69 | Demo.theme.set('h1,h2,h3,h4,h5,h6, #demo label', { color : v }); |
70 | |
71 | Dom.get('demo_heading_value').innerHTML = v; |
72 | } |
73 | |
74 | v = trim(Demo.background.value); |
75 | if (Demo.isValidColor(v)) { |
76 | // use camelCase for style property names |
77 | Demo.theme.set('.example .promo', { backgroundColor : v }); |
78 | |
79 | Dom.get('demo_background_value').innerHTML = v; |
80 | } |
81 | |
82 | v = trim(Demo.highlight.value); |
83 | if (Demo.isValidColor(v)) { |
84 | Demo.theme.set( |
85 | '#toc ul li.selected,'+ |
86 | '#toc ul li a:hover', { backgroundColor : v }); |
87 | |
88 | Dom.get('demo_highlight_value').innerHTML = v; |
89 | } |
90 | } |
91 | }; |
92 | |
93 | // Initialize the interface when the DOM is ready |
94 | YAHOO.util.Event.onDOMReady(Demo.init); |
95 | |
96 | })(); |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
Note: Logging and debugging is currently turned off for this example.
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings