the singularity of being and nothingness
Sencha Touch Theming: Building Our Custom Stylesheet with SASS
Ok, letās recap. So far, weāve looked at some basic concepts related to how to approach modifying a Sencha Touch app theme, as well as walked through exactly how all the pieces fit together in the SASS magic. With all this behind us, itās finally time to create a custom .scss file-letās get started!
Setting Things Up
In the following example, weāll be doing some minor riffs on the standard sencha-touch.css file. If youāre in the mood for a super-customized theme of your own, well, youāve got a lot of work ahead of you. Instead of jumping headfirst into something like that, why not start with some small changes, and build from there?
First things first, we need to decide where we want our new .scss file to live. While we can put it in the /resources/sass/ folder where the other core ones are, for this example weāll create a new folder at the same level…just to keep things straight. Iām going to call mine “custom” (pretty creative, eh?).
Now, in order for us to let Compass know where our .scss file lives, we need to create our own config.rb file (check out the last post for more info about this). Iāll create that file, and hereās more or less what the contents look like:
# Get the directory that this configuration file exists in dir = File.dirname(__FILE__) # Load the sencha-touch framework automatically. load File.join(dir, '..', 'themes') # Compass configurations sass_path = dir css_path = File.join(dir, "..", "css") environment = :production output_style = :compressed
If you were to look at the config.rb file in the resources/sass/ folder, youāll notice itās the same content. This is fine, since my “custom” sass folder is at the same level as the resources/sass folder. If it were put somewhere else, weād have to update this file. But for the time being, this will work.
Okay, easy enough. Letās close that file…we donāt need to do anything else with it.
Next, we need to create a new .scss file. You can call it what you like, but just remember that Compass will make a .css file with the same name that you choose for the .scss. Iām calling mine custom.scss (again, super original), and hereās the contents:
$base-color: #7A1E08; $base-gradient: 'glossy'; @import 'sencha-touch/default/all'; @include sencha-panel; @include sencha-buttons; @include sencha-sheet; @include sencha-picker; @include sencha-tabs; @include sencha-toolbar; @include sencha-toolbar-forms; @include sencha-carousel; @include sencha-indexbar; @include sencha-list; @include sencha-layout; @include sencha-form; @include sencha-msgbox; @include sencha-loading-spinner;
As with the config.rb file, youād notice that this is exactly the same as the sencha-touch.scss file. Thatās because I think itās easier to start with whatās already working, and customize from there. If you want headaches, feel free to start from scratch š
Gettinā Custom
Alright, now that we have our files setup, we can move on to customizing our new .scss file to create a super-sweet custom .css file that will help our app look a little less generic and whole lot more awesome. Hereās what I want to do in this example:
- Change the “main” color of the application (from blue-ish whatever to something a little.more.green.)
- Pull in some additional icon masks from the huge repository that ships with Sencha (betcha didnāt know there were so many…itās awesome)
- Create a new “ui” that we can use for toolbars
Changing the Base Color
The first task-changing the “base” color-is stupid-simple. You see the first line of our .scss file? Just change the hex value to the color you want. Yep, thatās it. Iām changing mine to something greener:
$base-color: #7A1E08;
Done (see, this is easy, right?)
Icon Masks
The next task-pulling in extra icon masks-is also pretty simple, but first a little context. By default, the sencha-touch.css file has a handful of icon masks defined that can be used in toolbars and whatnot. These are great, and pretty useful for quite a lot of contexts, but there are only so many defined.
Now before I knew about the awesomeness of SASS in Sencha theming, I did what I thought was a pretty reasonable thing to do: I busted open sencha-touch.css, saw how the icon masks were defined, and replicated them for my own icons. The major problem with this approach, of course, is that:
- I had to modify a TON of style rules, just to add in a new icon mask
- Sencha Touch uses base64 data Urls for image masks…which means I had to convert my files to base64 urls, add them in, and then bang my head against the wall when they didnāt work because of tiny typos or whatever other ridiculousness prevented it from working.
- Imagine the frustration when the icon mask needs to be updated…yes, bad idea.
One other thing I did not realize is that just because Sencha defaults only a handful of image masks, there are actually about a BILLION icon masks that ship with the installation. If you browse to resources/themes/default/images/pictos/, youāll see what I mean.
So all together, the sheer number of icon masks that are provided, combined with the simplicity of SASS, means that there is probably an icon that will fit your needs…and if there isnāt, itās only a matter of dropping it in the library folder, modifying the .scss to pull it in, and recompile. Stupid-simple.
Alright, enough of that tangent. Letās add some icon masks!
With SASS, primarily because Senchaās approach already has whatās called a “mixin” defined for pulling in icon masks to be included in the compiled .css. A mixin is really just a reusable bit combination of CSS, properties, etc. It can even be passed arguments, which is precisely what the pictos-iconmask mixin allows. Hereās what this mixin looks like (from _mixins.scss:
@mixin pictos-iconmask($name) {
.x-tab img.#{$name}, .x-button img.x-icon-mask.#{$name} {
-webkit-mask-image: theme_image($theme-name, "pictos/" + $name + ".png")
}
}
Pretty simple: it looks like a more or less regular kind of function, accepting a single argument (“name”), which is used to ultimately establish some custom CSS classes which set an image mask equal to the value of the argument.
And hereās how weāll use that in our .scss file to pull in the custom icon mask of “flag”:
@include pictos-iconmask("flag");
If it hasnāt sunk it, pulling in any one or multiple of the hundreds of icon masks from the Sencha pictos repository is as easy as 1.) figuring out the icons name and 2.) implementing the custom mixin using that name. SASS is pretty nifty, eh? š
So before we go on to our final task, a quick word here. If you look at the .css file thatās generated, and drill specifically into the rules established for these icon masks, youāll quickly notice that they eat up a significant number of bytes for each icon mask (because, remember, theyāre using the base64 data urls for the images, which is definitely longer than simply pointing to an image path….). Therefore, in an effort to keep your .css file as small and lean as possible, itās a good idea to NOT get the fancy idea of simply including every single icon in your .scss file, just so youāll have them available. Rather, you should only include the ones that you are absolutely using. Whatās more, itās probably a good idea to go the extra step and override the inclusion of the handful of icons that Sencha includes by default. This can be easily done in our custom .scss like so:
$include_default_icons: false;
Clear? Excellent. Letās wrap this up.
Defining a Custom “UI”
One of the cool parts about Sencha components is that you can specify a “ui” for them to use. Example:
var myToolbar = new Ext.Toolbar({
dock : 'top',
title: 'My Toolbar',
ui: "dark"
});
This UI, in effect, is just a collection of CSS rules that define how that component should look. By default, Sencha has a few like “dark” and “light”-while these are fine and dandy, they leave much to be desired if you want to significantly customize your Sencha Touch app.
Fortunately, like the icon masks we looked at before, uiās are controlled by mixins. There are ui mixins for all the major components, and they are pretty simple to customize. First, hereās what the toolbar ui mixin looks like (from _toolbar.scss):
@mixin sencha-toolbar-ui($ui-label, $color, $gradient: $toolbar-gradient) {
$toolbar-border-color: darken($color, 50%);
$toolbar-button-color: darken($color, 10%);
.x-toolbar-#{$ui-label} {
@include background-gradient($color, $gradient);
border-color: $toolbar-border-color;
.x-toolbar-title {
@include color-by-background($color);
@include bevel-by-background($color);
}
....
}
And the “defaults”:
@include sencha-toolbar-ui('dark', darken($base-color, 10%));
@include sencha-toolbar-ui('light', $base-color);
Pretty basic: the “dark” and “light” uis are created, simply by passing different color values to the mixin. So based on this, itās pretty straightforward what we need to do:
@include sencha-toolbar-ui('charcoal', #333);
Really, itās that simple? Yes, it is. Now, if I go back to my Toolbar component, I can use “charcoal” for the UI, and any toolbar that has that UI will have a super-sweet glossy charcoal-y color to it:
var myToolbar = new Ext.Toolbar({
dock : 'top',
title: 'My Toolbar',
ui: "charcoal"
});
Wrapping Up
So it was something of a journey getting to our custom .scss file, but I hope it was worth it. As you can see, customizing a Sencha Touch app with SASS is not only a pretty straightforward proposition, but it is an incredibly powerful option that opens up worlds of possibilities for customizing your app. With SASS, the tediousness of massively complex CSS is minimized, and you can concentrate on implementing your vision for your appās aesthetic. Furthermore, the Sencha Touch team has done a really nice job of implementing a really robust set of mixings, variables, and other SASS goodness to make the task of customization stupidly simple.
To put a bow on this, hereās the final state of my custom .scss file based on this post. Please leave a comment or two if you have questions. Thanks!
$base_color: #50c20b; // green
$include_default_icons: false;
// import the framework and base components
@import 'sencha-touch/default/all';
@include sencha-panel;
@include sencha-buttons;
@include sencha-sheet;
@include sencha-picker;
@include sencha-tabs;
@include sencha-toolbar;
@include sencha-toolbar-forms;
//comment out components we're not using
//@include sencha-carousel;
//@include sencha-indexbar;
@include sencha-list;
@include sencha-layout;
@include sencha-form;
@include sencha-msgbox;
// include custom icon masks
@include pictos-iconmask("favorites");
@include pictos-iconmask("search");
@include pictos-iconmask("add_black");
@include pictos-iconmask("bookmarks");
@include pictos-iconmask("trash");
@include pictos-iconmask("more");
@include pictos-iconmask("chart2");
@include pictos-iconmask("flag");
@include pictos-iconmask("refresh");
@include pictos-iconmask("home");
@include pictos-iconmask("delete");
@include pictos-iconmask("settings");
@include pictos-iconmask("user_add");
// create custom ui for toolbar
@include sencha-toolbar-ui('charcoal', #333);
| Print article | This entry was posted by existdissolve on March 7, 2011 at 11:29 am, and is filed under Mobile, Sencha Touch. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |


about 14 years ago
Hi, thanks for those articles, very helpful.
However, i didnāt succeed in making my scss files compile.
compass -u get me the following error :
Loading haml-edge gem.
LoadError on line 5 of sencha-touch/resources/sass/config.rb: no such file to load sencha-touch/resources/sass/../themes
But the folder exists, it seems like it canāt see the compass_init.rb file;
If in my config.rb file i load specifically the file :
load File.join(dir, ‘..ā, ‘themesā,ācompass_init.rbā)
I get “Nothing to compile. If youāre trying to start a new project, you have left off the directory argument.”
and if i try compass -watch :
“LoadError on line 31 of /usr/lib/ruby/1.8/rubygems/custom_require.rb: no such file to load — fssm/ext”
Here are the gems i got (gem list):
chunky_png (0.12.0)
compass (0.11.beta.2)
ext (0.1.8)
fssm (0.2.5)
haml (3.1.0.alpha.147)
haml-edge (3.1.79)
sass (3.1.0.alpha.246)
I saw people having the same proble on sencha forums, but nobody seems to have an answer. Do you?
Thanks
about 14 years ago
@shubakk-
Unfortunately, Iām not really a Ruby guy, so Iām not sure what could be causing the issue. I did check which gems I have installed on my Ruby installation (Ruby 1.92):
Since your error seems like it might possibly be related to the fssm/ext gems, have you tried removing/disabling those? Like I said, Iām not a Ruby guy, so you might have already tried that or know that theyāre not the issue…unfortunately, thatās the only suggestion I have.
Hope you find the answer, though, and please post back if you do in case someone else runs into a similar issue.
Thanks!
about 14 years ago
I installed rake & rdoc and succeed in making it work. I still had to modify the following line in config.rb :
load File.join(dir, ā..ā, āthemesā)
=>
load File.join(dir, ā..ā, āthemesā,ācompass_init.rbā)
Thanks for your help
about 14 years ago
Just a quick thanks for putting these 3 articles up - Iāve just started looking at Sencha, and documentation for theming is pretty sparse. This helped an incredible amount. One thing that may help in discussing the set-up too would be an example of a minimal ‘defaultā type of directory structure that makes sense - the sencha download contains so much extra stuff, itās hard to filter out the core items.
about 14 years ago
Nice Article. Before reading this I was pulling my hair out trying to figure out why some icons worked and others didnāt.
A few suggestions.
1. Make a screenshot of the file structure you talked about. While the text description is pretty straight forward something about it feels wrong.
2. Show an example of actually using compass via the command line.
3. Update for Sencha Touch 1.1 as thatās what most folks will be using when they read this article.
about 14 years ago
Hi Ron-
Iāve made the updates as requested:
http://existdissolve.com/2011/04/sencha-touch-theming-1-1-0/
and
http://existdissolve.com/2011/03/sencha-touch-theming-building-our-custom-stylesheet-with-sass/
about 14 years ago
Looks good, thanks.
about 14 years ago
very helpful article, would be nice to mention that you need to run “compass compile custom” to compile the custom sass, Iām not a ruby guy and that actually took me awhile to figure it out.
about 14 years ago
Great info Ron! its a little tricky getting this set up in Windows. I use SenchaTouch 1.1.0 and Ruby1.9.2 Installer.
As per instructions Step 2: Setup Compass, only 1 Gem: beta.7 version (0.11) of Compass gets installed.
? should I manually install haml and sass pre versions. (they are the other 2 Gems right)
Another issue I have is compass compile. Firstly do should I get compass to create the project Custom, or do you simply run compass compile and set path? and could you provide the path for Custom.scss.
Sorry to be asking these Qās but Ive been beating myself up for days over this š Many Thanks P
about 14 years ago
I made a post yesturday as i was having some trouble, please delete (and this one) as I have discovered the error in coding path. Had to cd dir dummy! Cheers P
about 14 years ago
Just a quick thanks for putting these 3 articles up ā Iāve just started looking at Sencha, and documentation for theming is pretty sparse.
about 14 years ago
how do you create or compile these files in the cmd window. Should I be in the ruby dir?
One example please.
Your articles are awesome!!!!!
about 13 years ago
Hello,
When i change the base-color to #FFBAD2 the global font color (buttons, toolbar title, icons, etc.) is set to black.
How do i set this font color (back) to white?
Thanks!
about 13 years ago
Okay, i find out i could change @mixin color-by-background in /_mixins.scss to do this