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.

CreatingAThemeUsingSass.asciidoc 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ---
  2. title: Creating A Theme Using SASS
  3. order: 78
  4. layout: page
  5. ---
  6. [[creating-a-theme-using-sass]]
  7. = Creating a theme using SASS
  8. Vaadin 7 comes with built in support for Sass, which can be thought of
  9. as a preprocessor for CSS. From the Sass homepage:
  10. _Sass makes CSS fun again. Sass is an extension of CSS3, adding nested
  11. rules, variables, mixins, selector inheritance, and more._
  12. Sass looks like CSS with some added features, and is compiled into CSS
  13. before being sent to the browser. The compilation is either done
  14. beforehand, or (during development) on-the-fly by the servlet.
  15. In Vaadin 7 you can make use of Sass in any of your CSS, and as usual
  16. there are more than one way to arrange this. The recommended way if you
  17. do not have a specific reason not to do so, is to compile your theme
  18. into one CSS file (that is: without any CSS @include), but we'll start
  19. with the getting-your-feet-wet approach that looks exactly as
  20. before.It’s worth noting that you can continue to use CSS without Sass
  21. just as before, if you prefer.
  22. [[getting-your-feet-wet]]
  23. Getting your feet wet
  24. ^^^^^^^^^^^^^^^^^^^^^
  25. In Vaadin 7 you set the theme in use by specifying the `@Theme` annotation
  26. on your UI, e.g `@Theme(“themename”)`. Ignoring Sass for a second, you
  27. would then create a `mytheme/styles.css` that typically `@import` the
  28. Reindeer theme (in case you forgot, your theme should be located in
  29. `WebContent/VAADIN/themes/<themename>/styles.css`). You can start using
  30. Sass with this approach, by renaming your `styles.css` to `styles.scss` and
  31. importing `legacy-styles.css` instead of `styles.css` - the resulting CSS
  32. will be exactly as the same as before, BUT now you're free to use Sass
  33. in your theme:
  34. [source,scss]
  35. ....
  36. @import url(../reindeer/legacy-styles.css);
  37. $color : green;
  38. .v-button-caption {
  39. color: $color;
  40. }
  41. ....
  42. Here we just define a Sass variable to use as color for button captions.
  43. *NOTE* that this way (using legacy-styles) you still lose one important
  44. new feature: you can't have multiple themes on the same page when using
  45. the legacy-styles.css -approach. To gain this feature, which is crucial
  46. if you intend to run multiple applications with different themes
  47. embedded in the same page (e.g portals), you must use Sass.
  48. [[compiling]]
  49. Compiling
  50. ^^^^^^^^^
  51. Provided you’re in development mode (not production), the scss will
  52. automatically be translated into CSS. You can also compile the scss
  53. manually (and MUST do so for production). To do this you should run
  54. `com.vaadin.sass.SassCompiler` with the Vaadin jars on the classpath and
  55. give it your scss file and output file as arguments. If you have the
  56. jars readily available, you could do something like this in the command
  57. line:
  58. [source,bash]
  59. ....
  60. > java -cp '../../../WEB-INF/lib/*' com.vaadin.sass.SassCompiler styles.scss styles.css
  61. ....
  62. Another way would be to save the auto-compiled styles.css from the
  63. browser.
  64. Support has been added to the Eclipse plugin through the _Compile Vaadin
  65. Theme_ button .
  66. NOTE that if you're using Ivy (the default if you're using the Eclipse
  67. plugin), you must make sure to get the appropriate dependencies on your
  68. classpath some other way (since they are not present in `WEB-INF/lib`). In
  69. Eclipse, use the Run -dialog to inherit the classpath from your project.
  70. You'll notice that the resulting theme still uses `@import` to 'extend'
  71. the Reindeer theme:
  72. [source,scss]
  73. ....
  74. @import url(../reindeer/legacy-styles.css);
  75. ....
  76. This approach is an easy way to get started with Sass, but will cause
  77. two requests (one for our theme, one for Reindeer). Let’s have a look at
  78. the recommended approach next.
  79. [[going-deeper]]
  80. Going deeper
  81. ^^^^^^^^^^^^
  82. Instead of using CSS `@import` to base your application theme on, you can
  83. (and probably should) use Sass `@import` to make a monolithic theme (one
  84. CSS file, one request when using the application). Just `@import reindeer.scss`, and `@include` it:
  85. [source,scss]
  86. ....
  87. // mytheme.scss
  88. @import "../reindeer/reindeer.scss";
  89. .mytheme {
  90. @include reindeer;
  91. $color : yellow;
  92. .v-button-caption {
  93. color: $color;
  94. }
  95. }
  96. ....
  97. This produces a styles.css that contains all the styles for Reindeer as
  98. well as your custom styles (note that this makes your final CSS quite
  99. big to scroll trough, so you might not want to do this when just
  100. learning the Sass syntax). There is no `@import` in the compiled CSS, so
  101. it will not cause additional requests. Additionally, due to the way
  102. Vaadin Sass is structured, this opens up for many possibilities to
  103. customize, mix-and-match themes, and leave unused stuff out.
  104. One important thing to notice, is that we wrapped everything in
  105. `.themename {}`, in this case `.mytheme {}`. This is the magic sauce that
  106. makes it possible to have multiple themes on one page. _It is crucial
  107. that the name matches your themename, or your styles will not be
  108. applied._
  109. Some of the nice features you get with Sass include variables, selector
  110. nesting, mixins (optionally with parameters), selector inheritance. For
  111. more information of what you can do with Sass, you should refer to the
  112. official documentation at http://sass-lang.com
  113. Please note that the Vaadin Sass compiler only supports the “SCSS”,
  114. which is the “new main syntax” (the original Sass also supports another,
  115. older syntax).The Vaadin version aims to be completely compatible,
  116. though initially there will be some limitations (and actually some added
  117. functionality). Please let us know if you find something is not working
  118. as expected.
  119. [[one-more-thing-recommended-structure]]
  120. One more thing: Recommended structure
  121. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  122. In Vaadin 7, all Vaadin core themes are using Sass. The
  123. _reindeer/styles.css_ we included first, is the compiled Reindeer theme,
  124. including the stuff from the Base theme that Reindeer extends. The Sass
  125. for the Reindeer theme is in _reindeer/reindeer.scss_, and contains one
  126. big mixin that will include the whole theme, unless you specifically
  127. tell it to leave out some parts. The themes are further divided into
  128. smaller parts, that can be left out, or separately included and renamed
  129. - providing a powerful way to customize and mix-and-match themes.
  130. *It is recommended* that you go ahead an divide your own theme into at
  131. least two files as well: *styles.scss* and *themename.scss* (where
  132. 'themename' is the name of your theme). This is will make your theme
  133. extendable, and also has the nice benefit that file you usually edit is
  134. uniquely named (themename.scss) instead of a generic styles.scss that
  135. you might have many of.
  136. For a theme named 'mytheme', this would look as follows:
  137. `mytheme/styles.scss:`
  138. [source,scss]
  139. ....
  140. @import "mytheme.scss";
  141. .mytheme {
  142. @include mytheme;
  143. }
  144. ....
  145. `mytheme/mytheme.scss`:
  146. [source,scss]
  147. ....
  148. @import "../reindeer/reindeer.scss";
  149. @mixin mytheme {
  150. // your styles go here
  151. @include reindeer;
  152. }
  153. ....
  154. This is the exact structure Vaadin core themes are using, and the way
  155. the Eclipse plugin will set things up for you (not yet in beta 10).
  156. Of course, you're still free to arrange your theme in another way if you
  157. prefer.
  158. Upcoming tutorials will address specific use-cases!