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-responsive.asciidoc 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ---
  2. title: Responsive Themes
  3. order: 10
  4. layout: page
  5. ---
  6. [[themes.responsive]]
  7. = Responsive Themes
  8. ((("[classname]#responsive# extension", id="term.themes.responsive", range="startofrange")))
  9. ((("CSS selections")))
  10. ((("extension")))
  11. Vaadin includes support for responsive design which enables size range
  12. conditions in CSS selectors, allowing conditional CSS rules that respond to size
  13. changes in the browser window on the client-side.
  14. ifdef::web[]
  15. See the link:https://vaadin.com/blog/-/blogs/3126636[Vaadin Blog article on
  16. Responsive design] for some additional
  17. information.
  18. endif::web[]
  19. You can use the [classname]#Responsive# extension to extend either a component,
  20. typically a layout, or the entire UI. You specify the component by the static
  21. [methodname]#makeResponsive()# method.
  22. ----
  23. // Have some component with an appropriate style name
  24. Label c = new Label("Here be text");
  25. c.addStyleName("myresponsive");
  26. content.addComponent(c);
  27. // Enable Responsive CSS selectors for the component
  28. Responsive.makeResponsive(c);
  29. ----
  30. You can now use [literal]#++width-range++# and [literal]#++height-range++#
  31. conditions in CSS selectors as follows:
  32. ----
  33. /* Basic settings for all sizes */
  34. .myresponsive {
  35. padding: 5px;
  36. line-height: 36pt;
  37. }
  38. /* Small size */
  39. .myresponsive[width-range~="0-300px"] {
  40. background: orange;
  41. font-size: 16pt;
  42. }
  43. /* Medium size */
  44. .myresponsive[width-range~="301px-600px"] {
  45. background: azure;
  46. font-size: 24pt;
  47. }
  48. /* Anything bigger */
  49. .myresponsive[width-range~="601px-"] {
  50. background: palegreen;
  51. font-size: 36pt;
  52. }
  53. ----
  54. You can have overlapping size ranges, in which case all the selectors matching
  55. the current size are enabled.
  56. ifdef::web[]
  57. Note that responsive themes currently
  58. link:https://dev.vaadin.com/ticket/16249[do not work together with] stylesheets
  59. or widget sets loaded from a different domain than the Vaadin application. Such
  60. resources must be loaded from the same domain as the application. The problem
  61. occurs only in Firefox. A SecurityError is shown in the debug window. The
  62. limitation concerns stylesheets such as for web fonts served from external
  63. sites, as described in
  64. <<dummy/../../../framework/themes/themes-fonts#themes.fonts.webfonts,"Loading
  65. Web Fonts">>.
  66. endif::web[]
  67. ifdef::web[]
  68. [[themes.responsive.wrap]]
  69. == Flexible Wrapping
  70. You can use the [classname]#CssLayout# to have automatic wrap-around when the
  71. components in the layout would go off right side of the layout. Components that
  72. wrap must, however, have either undefined or fixed width, and thereby can not
  73. utilize the full area of the screen. With the [classname]#Responsive# extension,
  74. you can have more flexible wrap-around that gives the component tiles maximum
  75. width.
  76. In the following, we have a text and image box, which are laid out horizontally
  77. with 50-50 sizing if the screen is wide enough, but wrap to a vertical layout if
  78. the screen is narrow.
  79. ----
  80. CssLayout layout = new CssLayout();
  81. layout.setWidth("100%");
  82. layout.addStyleName("flexwrap");
  83. content.addComponent(layout);
  84. // Enable Responsive CSS selectors for the layout
  85. Responsive.makeResponsive(layout);
  86. Label title = new Label("Space is big, really big");
  87. title.addStyleName("title");
  88. layout.addComponent(title);
  89. Label description = new Label("This is a " +
  90. "long description of the image shown " +
  91. "on the right or below, depending on the " +
  92. "screen width. The text here could continue long.");
  93. description.addStyleName("itembox");
  94. description.setSizeUndefined();
  95. layout.addComponent(description);
  96. Image image = new Image(null,
  97. new ThemeResource("img/planets/Earth.jpg"));
  98. image.addStyleName("itembox");
  99. layout.addComponent(image);
  100. ----
  101. The SCSS could be as follows:
  102. ----
  103. /* Various general settings */
  104. .flexwrap {
  105. background: black;
  106. color: white;
  107. .title {
  108. font-weight: bold;
  109. font-size: 20px;
  110. line-height: 30px;
  111. padding: 5px;
  112. }
  113. .itembox {
  114. white-space: normal;
  115. vertical-align: top;
  116. }
  117. .itembox.v-label {padding: 5px}
  118. }
  119. .flexwrap[width-range~="0-499px"] {
  120. .itembox {width: 100%}
  121. }
  122. .flexwrap[width-range~="500px-"] {
  123. .itembox {width: 50%}
  124. }
  125. ----
  126. The layout in the wide mode is shown in <<figure.theme.responsive.flexwrap>>.
  127. [[figure.theme.responsive.flexwrap]]
  128. .Flexible Wrapping
  129. image::img/addon-responsive-flexwrap.png[]
  130. You could also play with the [literal]#++display: block++# vs
  131. [literal]#++display: inline-block++# properties.
  132. Notice that, while the [classname]#Responsive# extension makes it possible to do
  133. various CSS trickery with component sizes, the normal rules for component and
  134. layout sizes apply, as described in
  135. <<dummy/../../../framework/layout/layout-settings#layout.settings.size,"Layout
  136. Size">> and elsewhere, and you should always check the size behaviour of the
  137. components. In the above example, we set the label to have undefined width,
  138. which disables word wrap, so we had to re-enable it.
  139. endif::web[]
  140. ifdef::web[]
  141. [[themes.responsive.display]]
  142. == Toggling the Display Property
  143. ((("display (CSS
  144. property)")))
  145. The [literal]#++display++# property allows especially powerful ways to offer
  146. radically different UIs for different screen sizes by enabling and disabling UI
  147. elements as needed. For example, you could disable some parts of the UI when the
  148. space gets too small, but bring forth navigation buttons that, when clicked, add
  149. component styles to switch to the hidden parts.
  150. In the following, we simply show alternative components based on screen width:
  151. ----
  152. CssLayout layout = new CssLayout();
  153. layout.setWidth("100%");
  154. layout.addStyleName("toggledisplay");
  155. content.addComponent(layout);
  156. // Enable Responsive CSS selectors for the layout
  157. Responsive.makeResponsive(layout);
  158. Label enoughspace =
  159. new Label("This space is big, mindbogglingly big");
  160. enoughspace.addStyleName("enoughspace");
  161. layout.addComponent(enoughspace);
  162. Label notenoughspace = new Label("Quite small space");
  163. notenoughspace.addStyleName("notenoughspace");
  164. layout.addComponent(notenoughspace);
  165. ----
  166. The SCSS could be as follows:
  167. ----
  168. /* Common settings */
  169. .toggledisplay {
  170. .enoughspace, .notenoughspace {
  171. color: white;
  172. padding: 5px;
  173. }
  174. .notenoughspace { /* Really small */
  175. background: red;
  176. font-weight: normal;
  177. font-size: 10px;
  178. line-height: 15px;
  179. }
  180. .enoughspace { /* Really big */
  181. background: darkgreen;
  182. font-weight: bold;
  183. font-size: 20px;
  184. line-height: 30px;
  185. }
  186. }
  187. /* Quite little space */
  188. .toggledisplay[width-range~="0-499px"] {
  189. .enoughspace {display: none}
  190. }
  191. /* Plenty of space */
  192. .toggledisplay[width-range~="500px-"] {
  193. .notenoughspace {display: none}
  194. }
  195. ----
  196. endif::web[]
  197. ifdef::web[]
  198. [[themes.responsive.demos]]
  199. == Responsive Demos
  200. You can find a simple responsive demo at
  201. link:http://demo.vaadin.com/responsive/[demo.vaadin.com/responsive]. It
  202. demonstrates the flexible wrapping technique described in
  203. <<themes.responsive.wrap>>.
  204. ((("Parking
  205. demo")))
  206. ((("TouchKit", "Parking
  207. demo")))
  208. The Parking demo for TouchKit, mentioned in
  209. <<dummy/../../../mobile/mobile-overview.asciidoc#mobile.overview,"Mobile
  210. Applications with TouchKit">>, uses a responsive theme to adapt to mobile
  211. devices with different screen sizes and when the screen orientation changes.
  212. endif::web[]
  213. (((range="endofrange", startref="term.themes.responsive")))