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.

themes-creating.asciidoc 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ---
  2. title: Creating and Using Themes
  3. order: 5
  4. layout: page
  5. ---
  6. [[themes.creating]]
  7. = Creating and Using Themes
  8. Custom themes are placed in the [filename]#VAADIN/themes# folder of the web
  9. application, in an Eclipse project under the [filename]#WebContent# folder or
  10. [filename]#src/main/webapp# in Maven projects, as was illustrated in
  11. <<themes-overview#figure.themes.theme-contents,"Contents of a Theme">>. This location is fixed. You need to have a theme folder for each
  12. theme you use in your application, although applications rarely need more than a
  13. single theme.
  14. [[themes.creating.sass]]
  15. == Sass Themes
  16. You can use Sass themes in Vaadin in two ways, either by compiling them to CSS
  17. by yourself or by letting the Vaadin servlet compile them for you on-the-fly
  18. when the theme CSS is requested by the browser, as described in
  19. <<themes-compiling#themes.compiling,"Compiling Sass Themes">>.
  20. To define a Sass theme with the name mytheme, you must place a file with name
  21. [filename]#styles.scss# in the theme folder [filename]#VAADIN/themes/mytheme#.
  22. If no [filename]#styles.css# exists in the folder, the Sass file is compiled
  23. on-the-fly when the theme is requested by a browser.
  24. We recommend that you organize the theme in at least two SCSS files so that you
  25. import the actual theme from a Sass file that is named more uniquely than the
  26. [filename]#styles.scss#, to make it distinquishable in the editor. This
  27. organization is how the Vaadin Plugin for Eclipse creates a new theme.
  28. If you use Vaadin add-ons that contain themes, Vaadin Plugin for Eclipse and
  29. Maven automatically add them to the [filename]#addons.scss# file.
  30. [[themes.creating.sass.scss]]
  31. === Theme SCSS
  32. We recommend that the rules in a theme should be prefixed with a selector for
  33. the theme name. You can do the prefixing in Sass by enclosing the rules in a
  34. nested rule with a selector for the theme name.
  35. Themes are defined as Sass mixins, so after you import the mixin definitions,
  36. you can [literal]#++@include++# them in the theme rule as follows:
  37. [source, css]
  38. ----
  39. @import "addons.scss";
  40. @import "mytheme.scss";
  41. .mytheme {
  42. @include addons;
  43. @include mytheme;
  44. }
  45. ----
  46. However, this is mainly necessary if you use the UI in portlets, each of which
  47. can have its own theme, or in the special circumstance that the theme has rules
  48. that use empty parent selector [literal]#++&++# to refer to the theme name.
  49. Otherwise, you can safely leave the nested theme selector out as follows:
  50. [source, css]
  51. ----
  52. @import "addons.scss";
  53. @import "mytheme.scss";
  54. @include addons;
  55. @include mytheme;
  56. ----
  57. The actual theme should be defined as follows, as a mixin that includes the base
  58. theme.
  59. [source, css]
  60. ----
  61. @import "../valo/valo.scss";
  62. @mixin mytheme {
  63. @include valo;
  64. /* An actual theme rule */
  65. .v-button {
  66. color: blue;
  67. }
  68. }
  69. ----
  70. [[themes.creating.sass.addons]]
  71. === Add-on Themes
  72. Some Vaadin add-ons include Sass styles that need to be compiled into the theme.
  73. These are managed in the [filename]#addons.scss# file in a theme, included from
  74. the [filename]#styles.scss#. The [filename]#addons.scss# file is automatically
  75. generated by the Vaadin Plugin for Eclipse or Maven.
  76. [subs="normal"]
  77. ----
  78. /* This file is automatically managed and will be
  79. overwritten from time to time. */
  80. /* Do not manually edit this file. */
  81. **/++*++ Provided by vaadin-spreadsheet-1.0.0.beta1.jar ++*++/ @import "../../../VAADIN/addons/spreadsheet/spreadsheet.scss";**
  82. /* Import and include this mixin into your project
  83. theme to include the addon themes */
  84. @mixin addons {
  85. **@include spreadsheet;**
  86. }
  87. ----
  88. [[themes.creating.css]]
  89. == Plain Old CSS Themes
  90. In addition to Sass themes, you can create plain old CSS themes. CSS theme are
  91. more restricted than Sass styles - you can't parameterize CSS themes in any way,
  92. unlike you can Valo, for example. Further, an application can only have one CSS
  93. theme while you can have multiple Sass themes.
  94. A CSS theme is defined in a [filename]#styles.css# file in the
  95. [filename]#VAADIN/themes/mytheme# folder. You need to import the
  96. [filename]#legacy-styles.css# of the built-in theme as follows:
  97. ----
  98. @import "../reindeer/legacy-styles.css";
  99. .v-app {
  100. background: yellow;
  101. }
  102. ----
  103. [[themes.creating.standard-components]]
  104. == Styling Standard Components
  105. Each user interface component in Vaadin has a CSS style class that you can use
  106. to control the appearance of the component. Many components have additional
  107. sub-elements that also allow styling. You can add context-specific stylenames
  108. with [methodname]#addStyleName()#. Notice that [methodname]#getStyleName()#
  109. returns only the custom stylenames, not the built-in stylenames for the
  110. component.
  111. Please see the section on each component for a description of its styles. Most
  112. of the style names are determined in the client-side widget of each component.
  113. The easiest way to find out the styles of the elements is to use a HTML
  114. inspector such as FireBug.
  115. //TODO add reference to a Firebug section when available
  116. Some client-side components or component styles can be shared by different
  117. server-side components. For example, [literal]#++v-textfield++# style is used
  118. for all text input boxes in components, in addition to [classname]#TextField#.
  119. [[themes.creating.builtin]]
  120. == Built-in Themes
  121. Vaadin currently includes the following built-in themes:
  122. * [literal]#++valo++#, the primary theme since Vaadin 7.3
  123. * [literal]#++reindeer++#, the primary theme in Vaadin 6 and 7
  124. * [literal]#++chameleon++#, an easily customizable theme
  125. * [literal]#++runo++#, the default theme in IT Mill Toolkit 5
  126. In addition, there is the [literal]#++base++# theme, which should not be used
  127. directly, but is extended by the other built-in themes, except valo.
  128. The built-in themes are provided in the respective
  129. [filename]#VAADIN/themes/&lt;theme&gt;/styles.scss# stylesheets in the
  130. [filename]#vaadin-themes# JAR. Also the precompiled CSS files are included, in
  131. case you want to use the themes directly.
  132. Various constants related to the built-in themes are defined in the theme
  133. classes in [package]#com.vaadin.ui.themes# package. These are mostly special
  134. style names for specific components.
  135. ----
  136. @Theme("runo")
  137. public class MyUI extends UI {
  138. @Override
  139. protected void init(VaadinRequest request) {
  140. ...
  141. Panel panel = new Panel("Regular Panel in the Runo Theme");
  142. panel.addComponent(new Button("Regular Runo Button"));
  143. // A button with the "small" style
  144. Button smallButton = new Button("Small Runo Button");
  145. smallButton.addStyleName(Runo.BUTTON_SMALL);
  146. Panel lightPanel = new Panel("Light Panel");
  147. lightPanel.addStyleName(Runo.PANEL_LIGHT);
  148. lightPanel.addComponent(
  149. new Label("With addStyleName(\"light\")"));
  150. ...
  151. ----
  152. The example with the Runo theme is shown in
  153. <<figure.themes.creating.builtin.runo>>.
  154. [[figure.themes.creating.builtin.runo]]
  155. .Runo Theme
  156. image::img/builtin-runo.png[]
  157. The built-in themes come with a custom icon font, FontAwesome, which is used for
  158. icons in the theme, and which you can use as font icons, as described in
  159. <<dummy/../../../framework/themes/themes-fonticon#themes.fonticon,"Font
  160. Icons">>.
  161. ifdef::web[]
  162. [NOTE]
  163. .Serving Built-In Themes Statically
  164. ====
  165. The built-in themes included in the Vaadin library JAR are served dynamically
  166. from the JAR by the servlet. Serving themes and widget sets statically by the
  167. web server is more efficient. To do so, you need to extract the
  168. [filename]#VAADIN/# directories from the JAR to the web content directory (
  169. [filename]#WebContent# in Eclipse or [filename]#src/main/webapp# in Maven
  170. projects).
  171. [subs="normal"]
  172. ----
  173. [prompt]#$# [command]#cd# WebContent
  174. ----
  175. [subs="normal"]
  176. ----
  177. [prompt]#$# [command]#unzip# path-to/vaadin-server-7.x.x.jar 'VAADIN/*'
  178. ----
  179. [subs="normal"]
  180. ----
  181. [prompt]#$# [command]#unzip# path-to/vaadin-themes-7.x.x.jar 'VAADIN/*'
  182. ----
  183. [subs="normal"]
  184. ----
  185. [prompt]#$# [command]#unzip# path-to/vaadin-client-compiled-7.x.x.jar 'VAADIN/*'
  186. ----
  187. You can also serve static content from a front-end caching server, which reduces
  188. the load of the application server. In portals, you install the themes globally
  189. in the portal in similar way, as described in
  190. <<dummy/../../../framework/portal/portal-liferay#portal.liferay.install,"Installing
  191. Vaadin Resources">>.
  192. Just make sure to update the static content when you upgrade to a newer version
  193. of Vaadin.
  194. ====
  195. endif::web[]
  196. Creation of a default theme for custom GWT widgets is described in
  197. <<dummy/../../../framework/gwt/gwt-styling#gwt.styling,"Styling a Widget">>.
  198. [[themes.creating.addon]]
  199. == Add-on Themes
  200. You can find more themes as add-ons from the
  201. link:http://vaadin.com/directory[Vaadin Directory]. In addition, many component
  202. add-ons contain a theme for the components they provide.
  203. The add-on themes need to be included in the project theme. Vaadin Plugin for
  204. Eclipse and Maven automatically include them in the [filename]#addons.scss# file
  205. in the project theme folder. It should be included by the project theme.