Customizing component theme with SASS

In addition to the general benefits Sass brings to the world of CSS in Vaadin 7, the way themes are set up allows us to quite easily accomplish some things that were previously hard.

Let’s start from the top, without Sass, and continue from there. We’ll use the new setPrimaryStyleName() to do some things previously not possible.

We’ll work on a small example with buttons that we want to customize:

Java

  1. @Theme("sassy")
  2. public class SassyUI extends UI {
  3. @Override
  4. public void init(VaadinRequest request) {
  5. Button b = new Button("Reindeer");
  6. Layout layout = new VerticalLayout();
  7. layout.addComponent(b);
  8. setContent(layout);
  9. }
  10. }

And our basic (mostly empty at this point) “sassy” theme, based on Reindeer, looks like this (assuming you’re using the recommended styles.scss+themename.scss structure as introduced in the previous tutorial):

SCSS

  1. @import "../reindeer/reindeer.scss";
  2. @mixin sassy {
  3. @include reindeer;
  4. // your styles go here
  5. }

And the result is a basic Reindeer-looking button. We can change the color of the caption like this:

SCSS

  1. .v-button-caption {
  2. color: red;
  3. }

…but this changes ALL buttons. We just want some of the buttons to stand out:

Java

  1. b = new Button("important");
  2. b.addStyleName("important");
  3. layout.addComponent(b);

css:

SCSS

  1. .important .v-button-caption {
  2. color: red;
  3. }

Ok, this is all fine - but we realize our important button should actually not look at all like a Reindeer button.

Since Reindeer adds quite a few styles, this requires quite a lot of customization with this approach. Enter setPrimaryStyleName():

Java

  1. b = new Button("More important");
  2. b.setPrimaryStyleName("my-button");
  3. addComponent(b);

Now everything that was previously .v-button in the browser DOM is all of a sudden .my-button, and we have a completely unstyled button, but with the DOM-structure and functionality of a regular button. We can easily style this without interference from theme styles:

SCSS

  1. .my-button {
  2. color: red;
  3. }

However, in our case we realize we still want it to look like a button, just not with so much decorations as a Reindeer button. Let’s apply Base styles:

SCSS

  1. @include base-button($primaryStyleName: my-button);
  2. .my-button {
  3. color: red;
  4. }

What? We now have a basic button with red text, but how?

We have @included base-button and renamed it’s selectors to “my-button” (instead of the default “v-button”). This makes the rules match our button perfectly (we used setPrimaryStyleName() to rename it) - in effect we apply base-button to our “my-button”.

Now we have a good starting-point. Note that this might not be such a big deal for small things, like buttons, but imagine something like Table witout any styles. Yikes.

Here are the full sources (using distinct colors for each button for clarity):

Java

  1. package com.example.sassy;
  2. import com.vaadin.annotations.Theme;
  3. import com.vaadin.server.VaadinRequest;
  4. import com.vaadin.ui.Button;
  5. import com.vaadin.ui.Layout;
  6. import com.vaadin.ui.UI;
  7. import com.vaadin.ui.VerticalLayout;
  8. @Theme("sassy")
  9. public class SassyUI extends UI {
  10. @Override
  11. public void init(VaadinRequest request) {
  12. Button b = new Button("Reindeer");
  13. Layout layout = new VerticalLayout();
  14. layout.addComponent(b);
  15. b = new Button("important");
  16. b.addStyleName("important");
  17. layout.addComponent(b);
  18. b = new Button("More important");
  19. b.setPrimaryStyleName("my-button");
  20. layout.addComponent(b);
  21. setContent(layout);
  22. }
  23. }

SCSS

  1. // sassy/styles.scss
  2. @import "sassy.scss";
  3. .sassy {
  4. @include sassy;
  5. }

SCSS

  1. // sassy/sassy.scss
  2. @import "../reindeer/reindeer.scss";
  3. @mixin sassy {
  4. @include reindeer;
  5. .v-button-caption {
  6. color: red;
  7. }
  8. .important .v-button-caption {
  9. color: green;
  10. }
  11. @include base-button($name: my-button);
  12. .my-button {
  13. color: blue;
  14. }
  15. }