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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 distinguishable 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. Further, an application can only have one CSS theme while you can have multiple Sass themes.
  93. A CSS theme is defined in a [filename]#styles.css# file in the
  94. [filename]#VAADIN/themes/mytheme# folder. You need to import the
  95. [filename]#styles.css# of Valo theme as follows:
  96. ----
  97. @import "../valo/styles.css";
  98. .v-app {
  99. background: yellow;
  100. }
  101. ----
  102. [[themes.creating.standard-components]]
  103. == Styling Standard Components
  104. Each user interface component in Vaadin has a CSS style class that you can use
  105. to control the appearance of the component. Many components have additional
  106. sub-elements that also allow styling. You can add context-specific stylenames
  107. with [methodname]#addStyleName()#. Notice that [methodname]#getStyleName()#
  108. returns only the custom stylenames, not the built-in stylenames for the
  109. component.
  110. Please see the section on each component for a description of its styles. Most
  111. of the style names are determined in the client-side widget of each component.
  112. The easiest way to find out the styles of the elements is to use a HTML
  113. inspector such as FireBug.
  114. Some client-side components or component styles can be shared by different
  115. server-side components. For example, [literal]#++v-textfield++# style is used
  116. for all text input boxes in components, in addition to [classname]#TextField#.
  117. [[themes.creating.builtin]]
  118. == Built-in Themes
  119. Since version 8.0, Vaadin Framework includes only one built-in theme:
  120. * [literal]#++valo++#, the primary theme since Vaadin 7.3
  121. The following legacy themes can be found in the [filename]#vaadin-compatibility-themes.jar# package
  122. for easier migrating from framework version 7 to 8:
  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. * [literal]#++base++#, which was never to be used directly, but is the base for other legacy themes
  127. The themes are provided in the respective
  128. [filename]#VAADIN/themes/&lt;theme&gt;/styles.scss# stylesheets in the
  129. [filename]#vaadin-themes# JAR. Also the precompiled CSS files are included, in
  130. case you want to use the themes directly.
  131. Various constants related to the built-in themes are defined in the theme
  132. classes in [package]#com.vaadin.ui.themes# package. These are mostly special
  133. style names for specific components.
  134. ----
  135. public class MyUI extends UI {
  136. @Override
  137. protected void init(VaadinRequest request) {
  138. ...
  139. Panel panel = new Panel("Regular Panel in the Valo Theme");
  140. panel.addComponent(new Button("Regular Valo Button"));
  141. // A button with the "small" style
  142. Button smallButton = new Button("Small Valo Button");
  143. smallButton.addStyleName(ValoTheme.BUTTON_SMALL);
  144. Panel lightPanel = new Panel("Borderless Panel");
  145. lightPanel.addStyleName(ValoTheme.PANEL_BORDERLESS);
  146. lightPanel.addComponent(
  147. new Label("With addStyleName(\"light\")"));
  148. ...
  149. ----
  150. The Valo theme comes with a custom icon font, VaadinIcons,
  151. which you can use as font icons, as described in
  152. <<themes-fonticon#themes.fonticon,"Font
  153. Icons">>.
  154. ifdef::web[]
  155. [NOTE]
  156. .Serving Built-In Themes Statically
  157. ====
  158. The built-in themes included in the Vaadin library JAR are served dynamically
  159. from the JAR by the servlet. Serving themes and widget sets statically by the
  160. web server is more efficient. To do so, you need to extract the
  161. [filename]#VAADIN/# directories from the JAR to the web content directory (
  162. [filename]#WebContent# in Eclipse or [filename]#src/main/webapp# in Maven
  163. projects).
  164. [subs="normal"]
  165. ----
  166. [prompt]#$# [command]#cd# WebContent
  167. ----
  168. [subs="normal"]
  169. ----
  170. [prompt]#$# [command]#unzip# path-to/vaadin-server-8.x.x.jar 'VAADIN/*'
  171. ----
  172. [subs="normal"]
  173. ----
  174. [prompt]#$# [command]#unzip# path-to/vaadin-themes-8.x.x.jar 'VAADIN/*'
  175. ----
  176. [subs="normal"]
  177. ----
  178. [prompt]#$# [command]#unzip# path-to/vaadin-client-compiled-8.x.x.jar 'VAADIN/*'
  179. ----
  180. You can also serve static content from a front-end caching server, which reduces
  181. the load of the application server. In portals, you install the themes globally
  182. in the portal in similar way, as described in
  183. <<../portal/portal-liferay#portal.liferay.install,"Installing
  184. Vaadin Resources">>.
  185. Just make sure to update the static content when you upgrade to a newer version
  186. of Vaadin.
  187. ====
  188. endif::web[]
  189. Creation of a default theme for custom GWT widgets is described in
  190. <<../gwt/gwt-styling#gwt.styling,"Styling a Widget">>.
  191. [[themes.creating.addon]]
  192. == Add-on Themes
  193. You can find more themes as add-ons from the
  194. link:http://vaadin.com/directory[Vaadin Directory]. In addition, many component
  195. add-ons contain a theme for the components they provide.
  196. The add-on themes need to be included in the project theme. Vaadin Plugin for
  197. Eclipse and Maven automatically include them in the [filename]#addons.scss# file
  198. in the project theme folder. It should be included by the project theme.