2 ______________________________________________
4 With Ext JS 4 we’ve built on our experience with Sencha Touch to make the framework more themable than ever before by unlocking the power of SASS and Compass. Every aspect of the framework’s appearance can be customized, often by simply tweaking a color variable.
6 <iframe src="http://player.vimeo.com/video/19159630?byline=0" width="500" height="281" frameborder="0"></iframe>
8 Sencha Touch has introduced a revolutionary new theming system, built on SASS and Compass, that allows you to create versions of our base theme custom tailored to your application and brand. Because Sencha Touch is built on web standards and targets WebKit, developers are free to use the wide variety of CSS3 properties already available to the platform, like border-radius, background gradients, and even more advanced properties like the CSS3 flexible box model. To create a custom variant of the bundled theme that uses SASS and Compass is very straight-forward as well.
10 (original article http://www.sencha.com/blog/an-introduction-to-theming-sencha-touch/)
12 ## A Brief Introduction to SASS & Compass
14 <iframe src="http://player.vimeo.com/video/18084338?byline=0" width="500" height="281" frameborder="0"></iframe>
16 SASS is a pre-processor which adds new syntax to CSS allowing for things like variables, mixins, nesting, and math/color functions. For example, in SASS we can write:
23 color: darken($blue, 9%);
32 And it will compile to:
35 border-color: #3bbfce;
42 border-color: #3bbfce;
45 To see the wide variety of other features available in SASS, please see http://sass-lang.com/. Compass extends SASS by adding a variety of CSS3 mixins and providing the extension system that Sencha Touch leverages. With Compass, one can include rules like:
50 @include border-radius($boxheight/4);
56 -webkit-border-radius: 2.5em;
57 -moz-border-radius: 2.5em;
58 -o-border-radius: 2.5em;
59 -ms-border-radius: 2.5em;
60 -khtml-border-radius: 2.5em;
64 You can learn more about the pre-included mixins with Compass and the other tools which it provides here: http://compass-style.org/docs/. I’ve only really touched the surface of what’s possible with these technologies, so I can focus on our use of them in Sencha Touch, but I highly encourage you to briefly look at their respective sites to get a better sense of their capabilities. Now, though, let’s look at setting up a SASS/Compass environment where we can create custom Sencha Touch themes.
66 ## Setting Up Custom Stylesheets
67 First, we’ll need to make sure we have SASS and Compass installed. Because these technologies are based on Ruby, Windows users will need to install Ruby and RubyGems (these come pre-bundled on Macs). Because SASS is bundled with Compass, you can install both with the following Terminal command:
71 Note: You may need to install using “sudo gem install…” for administrative privileges.
73 Once everything is installed, we’ll need to create a new directory in our project to house our new SASS stylesheets. I typically use resources/scss (SCSS is the syntax language for SASS, and stands for “Sassy CSS”). Within the directory, we’ll create two files, config.rb and application.scss.
75 __config.rb__ The config.rb is written in Ruby, and is very straight-forward to set up (so don’t worry if you don’t know Ruby). The file is typically a set-it-and-forget-it:
77 # Delineate the directory for our SASS/SCSS files (this directory)
78 sass_path = File.dirname(__FILE__)
80 # Delineate the CSS directory (under resources/css in this demo)
81 css_path = File.join(sass_path, "..", "css")
83 # Delinate the images directory
84 images_dir = File.join(sass_path, "..", "img")
86 # Load the sencha-touch framework
87 load File.join(sass_path, '..', '..', '..', '..', 'resources', 'themes')
89 # Specify the output style/environment
90 output_style = :compressed
91 environment = :production
93 If you are debugging your custom stylesheet, you may also want to set your output_style to “:expanded,” which will compile your CSS line-by-line and actually show, with comments, where the rules are being generated in your SCSS. “:compressed,” as shown above, compacts all of your CSS to one line and removes any comments/extraneous spaces — which is ideal for minimizing footprint in production environments.
95 __application.scss__ The application.scss file is written in SCSS and has the following structure (we’ll go over what some of these terms mean in a moment):
97 // 1. Variable overrides, example:
98 // $base-color: #af2584;
100 // 2. Include Sencha Touch styles
101 @import 'sencha-touch/default/all';
102 @include sencha-panel;
103 @include sencha-buttons;
104 @include sencha-sheet;
105 @include sencha-picker;
106 @include sencha-toolbar-forms;
107 @include sencha-tabs;
108 @include sencha-toolbar;
109 @include sencha-carousel;
110 @include sencha-indexbar;
111 @include sencha-list;
112 @include sencha-layout;
113 @include sencha-form;
114 @include sencha-loading-spinner;
116 // 3. Define custom styles (can use SASS/Compass), example:
118 // @include border-radius(4px);
121 As you can see, the main gist of this is that we are importing styles from Sencha Touch, component by component. The area above (1) is where we can actually override variables used by the default Sencha Touch theme to change the output when the actually stylesheets are created/included. There are currently about 50 variables available to override, though we’ll just focus on a few key ones in this article.
123 {@img basecolor.png Base color}
125 ## Sencha Touch Variables
126 __$base-color__ Perhaps the easiest way to create a sweeping change in your app’s design, you can change the $base-color to drastically alter the appearance of all components. The screenshots above demonstrate how the UI changes by simply altering the $base-color.
128 Other global color variables include $alert-color (red by default), $confirm-color (green by default), $active-color (a saturated version of $base-color, blue by default).
130 __$base_gradient__ Similar to $base-color, $base-gradient allows you to drastically change the appearance of your app by modifying the gradient style of all elements like buttons, toolbars, and tabbars. The four included values for gradients are ‘matte’, ‘bevel’, ‘glossy’, ‘recessed’, and ‘flat’.
132 ## Custom Styles & Sencha Touch Mixins
133 In our application.scss shown above, you will notice there is a specific area (3) for custom styles, after we’ve imported the library stylesheets. This is your area to write your application’s custom CSS, and make use of any mixins provided by Sencha Touch. Some common mixins provided by Sencha Touch allow you to embed extra icons in your application and create custom “ui” styles to apply to buttons, toolbars, or tabbars.
135 __@include pictos-iconmask($name)__ This allows you to embed a custom icon mask for use in buttons or tabs. There are over 300 icons that you can include in your application, thanks to Drew Wilson’s fine Pictos set. All of the icons are base64 encoded into your CSS to cut down on server requests, which is useful in the mobile context. The icons can be found in the Sencha Touch distribution under resources/themes/images/default/pictos/. To include a new icon, simply pass the name of the icon you want, minus the .png extension:
137 @include pictos-iconmask(‘wifi’);
139 You can then use it in your application like so:
141 var btn = new Ext.Button({
146 __@include sencha-button-ui($ui-label, $color, $gradient)__ By default, Sencha Touch comes with a variety of button colors and forms, but sometimes you may want another color option for alternative actions. This mixin creates a button UI with the color/gradient specified in the corresponding variables. In addition, this mixin also generates ‘{your-ui-name}-round’ and ‘{your-ui-name}-small’. The text color and shading will be automatically calculated from the background color. Likewise, there are mixins for sencha-toolbar-ui, and sencha-tabbar-ui.
148 @include sencha-button-ui('orange', #ff8000, 'glossy');
149 And then use in your JavaScript:
152 var glossy_orange_btn = new Ext.Button({
157 var round_glossy_orange_btn = new Ext.Button({
162 {@img btn.png Buttons}
164 ## Compiling Our Stylesheet & Adding to Our Application
165 To compile our stylesheet, we simply run the command “compass compile” in the Terminal from within the scss directory. This command will automatically look for the config.rb, and take care of compiling the CSS into the proper directory.
167 When you add the stylesheet to your HTML file, don’t forget to remove the included sencha-touch.css — as you’re newly compiled stylesheet includes all of the framework styles as well.
169 ## Some Sample Stylesheets
173 $base-color: #7A1E08;
174 $base-gradient: 'glossy';
176 @import 'sencha-touch/default/all';
178 @include sencha-panel;
179 @include sencha-buttons;
180 @include sencha-sheet;
181 @include sencha-picker;
182 @include sencha-toolbar-forms;
183 @include sencha-tabs;
184 @include sencha-toolbar;
185 @include sencha-carousel;
186 @include sencha-indexbar;
187 @include sencha-list;
188 @include sencha-layout;
189 @include sencha-form;
190 @include sencha-loading-spinner;
192 {@img demo1.png Demo 1}
196 $body-bg-color: #fbf5e6;
197 $base-color: #efe1d0;
198 $active-color: #aa3030;
200 @import 'sencha-touch/default/all';
201 @include sencha-panel;
202 @include sencha-buttons;
203 @include sencha-sheet;
204 @include sencha-picker;
205 @include sencha-toolbar-forms;
206 @include sencha-tabs;
207 @include sencha-toolbar;
208 @include sencha-carousel;
209 @include sencha-indexbar;
210 @include sencha-list;
211 @include sencha-layout;
212 @include sencha-form;
213 @include sencha-sheet;
216 font-family: Georgia;
220 .x-toolbar .x-toolbar-title {
224 {@img demo2.png Demo 2}
228 $body-bg-color: #000;
231 $base-gradient: 'matte';
232 $active-color: #B2DF1E;
234 $tabs-dark: $base-color;
237 $tabs-bottom-radius: .4em;
238 $tabs-bottom-gradient: 'bevel';
239 $tabs-bar-gradient: 'matte';
240 $tabs-bottom-active-gradient: 'recessed';
242 $toolbar-gradient: 'glossy';
244 @import 'sencha-touch/default/all';
245 @include sencha-panel;
246 @include sencha-buttons;
247 @include sencha-sheet;
248 @include sencha-picker;
249 @include sencha-toolbar-forms;
250 @include sencha-tabs;
251 @include sencha-toolbar;
252 @include sencha-carousel;
253 @include sencha-indexbar;
254 @include sencha-list;
255 @include sencha-layout;
256 @include sencha-form;
257 @include sencha-sheet;
259 .x-toolbar .x-toolbar-title {
260 color: saturate(lighten($active-color, 10%), 20%);
261 text-shadow: rgba(#fff, .8) 0 1px 20px;
265 background: white none !important;
268 .x-tabbar.x-docked-bottom .x-tab {
270 -webkit-box-shadow: inset rgba(#000, .8) 0 0 5px !important;
274 {@img demo3.png Demo 3}
276 ### Next Steps & Extended Resources
277 We’ve really just touched on the power of theming in Sencha Touch, but already, with just a few small changes, we have radically changed the style of the base Sencha Touch theme. For other great examples of custom stylesheets, look at the Kiva and GeoCongress demos bundled with Sencha Touch. Also be sure to look out for my second article on the subject, “Optimizing Sencha Touch Themes,” where I show how we can take a full-fledged application and reduce the included CSS to under 50kb (with base64 encoded icons!).