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.3KB

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