You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomizingComponentThemeWithSass.asciidoc 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ---
  2. title: Customizing Component Theme With SASS
  3. order: 47
  4. layout: page
  5. ---
  6. [[customizing-component-theme-with-sass]]
  7. Customizing component theme with SASS
  8. -------------------------------------
  9. In addition to the general benefits Sass brings to the world of CSS in
  10. Vaadin 7, the way themes are set up allows us to quite easily accomplish
  11. some things that were previously hard.
  12. Let’s start from the top, without Sass, and continue from there. We'll
  13. use the new _setPrimaryStyleName()_ to do some things previously not
  14. possible.
  15. We’ll work on a small example with buttons that we want to customize:
  16. [source,java]
  17. ....
  18. @Theme("sassy")
  19. public class SassyUI extends UI {
  20. @Override
  21. public void init(VaadinRequest request) {
  22. Button b = new Button("Reindeer");
  23. Layout layout = new VerticalLayout();
  24. layout.addComponent(b);
  25. setContent(layout);
  26. }
  27. }
  28. ....
  29. And our basic (mostly empty at this point) “sassy” theme, based on
  30. Reindeer, looks like this (assuming you're using the recommended
  31. styles.scss+themename.scss structure as introduced in the previous
  32. tutorial):
  33. [source,scss]
  34. ....
  35. @import "../reindeer/reindeer.scss";
  36. @mixin sassy {
  37. @include reindeer;
  38. // your styles go here
  39. }
  40. ....
  41. And the result is a basic Reindeer-looking button. We can change the
  42. color of the caption like this:
  43. [source,scss]
  44. ....
  45. .v-button-caption {
  46. color: red;
  47. }
  48. ....
  49. …but this changes ALL buttons. We just want some of the buttons to stand
  50. out:
  51. [source,java]
  52. ....
  53. b = new Button("important");
  54. b.addStyleName("important");
  55. layout.addComponent(b);
  56. ....
  57. css:
  58. [source,scss]
  59. ....
  60. .important .v-button-caption {
  61. color: red;
  62. }
  63. ....
  64. Ok, this is all fine - but we realize our important button should
  65. actually not look at all like a Reindeer button.
  66. Since Reindeer adds quite a few styles, this requires quite a lot of
  67. customization with this approach. Enter _setPrimaryStyleName()_:
  68. [source,java]
  69. ....
  70. b = new Button("More important");
  71. b.setPrimaryStyleName("my-button");
  72. addComponent(b);
  73. ....
  74. Now everything that was previously _.v-button_ in the browser DOM is all
  75. of a sudden _.my-button_, and we have a completely unstyled button, but
  76. with the DOM-structure and functionality of a regular button. We can
  77. easily style this without interference from theme styles:
  78. [source,scss]
  79. ....
  80. .my-button {
  81. color: red;
  82. }
  83. ....
  84. However, in our case we realize we still want it to look like a button,
  85. just not with so much decorations as a Reindeer button. Let’s apply Base
  86. styles:
  87. [source,scss]
  88. ....
  89. @include base-button($primaryStyleName: my-button);
  90. .my-button {
  91. color: red;
  92. }
  93. ....
  94. What? We now have a basic button with red text, but how?
  95. We have @included base-button and renamed it’s selectors to “my-button”
  96. (instead of the default “v-button”). This makes the rules match our
  97. button perfectly (we used setPrimaryStyleName() to rename it) - in
  98. effect we apply base-button to our “my-button”.
  99. Now we have a good starting-point. Note that this might not be such a
  100. big deal for small things, like buttons, but imagine something like
  101. Table witout _any_ styles. Yikes.
  102. Here are the full sources (using distinct colors for each button for
  103. clarity):
  104. [source,java]
  105. ....
  106. package com.example.sassy;
  107. import com.vaadin.annotations.Theme;
  108. import com.vaadin.server.VaadinRequest;
  109. import com.vaadin.ui.Button;
  110. import com.vaadin.ui.Layout;
  111. import com.vaadin.ui.UI;
  112. import com.vaadin.ui.VerticalLayout;
  113. @Theme("sassy")
  114. public class SassyUI extends UI {
  115. @Override
  116. public void init(VaadinRequest request) {
  117. Button b = new Button("Reindeer");
  118. Layout layout = new VerticalLayout();
  119. layout.addComponent(b);
  120. b = new Button("important");
  121. b.addStyleName("important");
  122. layout.addComponent(b);
  123. b = new Button("More important");
  124. b.setPrimaryStyleName("my-button");
  125. layout.addComponent(b);
  126. setContent(layout);
  127. }
  128. }
  129. ....
  130. [source,scss]
  131. ....
  132. // sassy/styles.scss
  133. @import "sassy.scss";
  134. .sassy {
  135. @include sassy;
  136. }
  137. ....
  138. [source,scss]
  139. ....
  140. // sassy/sassy.scss
  141. @import "../reindeer/reindeer.scss";
  142. @mixin sassy {
  143. @include reindeer;
  144. .v-button-caption {
  145. color: red;
  146. }
  147. .important .v-button-caption {
  148. color: green;
  149. }
  150. @include base-button($name: my-button);
  151. .my-button {
  152. color: blue;
  153. }
  154. }
  155. ....