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 13 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 13 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 13 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 13 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 13 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 13 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 13 years ago
Looks good, thanks.
about 13 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 13 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 13 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 13 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 13 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