Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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