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.

Responsive.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import com.vaadin.shared.extension.responsive.ResponsiveState;
  18. import com.vaadin.ui.Component;
  19. /**
  20. * An extension providing responsive layout capabilities to any Vaadin
  21. * component. The Responsive extension allows specifying different CSS rules for
  22. * different dimensions of extended components. This allows creating
  23. * applications that provide an optimal viewing experience – easy reading and
  24. * navigation with a minimum of resizing, panning, and scrolling – across a wide
  25. * range of devices (from mobile phones to desktop computer monitors).
  26. * <p>
  27. * NOTE! You should always specify a relative (%) size for the extended
  28. * component, doing otherwise will prevent the Responsive extension from
  29. * working, as the component will not dynamically resize.
  30. * </p>
  31. * <p>
  32. * All configuration of the visual breakpoints (ranges) for the component are
  33. * done with CSS. Pixels (px) are the only supported unit. Fractional pixels are
  34. * not supported.
  35. * </p>
  36. * <p>
  37. * <i>Dynamic style injections (e.g. through
  38. * <code>Page.getCurrent().getStyles().add(...)</code>) or any other style
  39. * updates after the initial page load are not supported at the moment.</i>
  40. * </p>
  41. *
  42. *
  43. * <p>
  44. * Example:
  45. *
  46. * <b>Java</b>
  47. *
  48. * <pre>
  49. * CssLayout layout = new CssLayout();
  50. * layout.setStyleName(&quot;responsive&quot;);
  51. * layout.setSizeFull();
  52. * Responsive.makeResponsive(layout);
  53. * </pre>
  54. *
  55. * <b>SCSS</b>
  56. *
  57. * <pre>
  58. * .v-csslayout.responsive {
  59. * &[width-range~="0-300px"] {
  60. * // Styles for the layout when its width is between 0 and 300 pixels
  61. * }
  62. * &[width-range~="301-500px"] {
  63. * // Styles for the layout when its width is between 301 and 500 pixels
  64. * }
  65. * &[width-range~="501px-"] {
  66. * // Styles for the layout when its width is over 500 pixels
  67. * }
  68. * &[height-range~="0-300px"] {
  69. * // Styles for the layout when its height is between 0 and 300 pixels
  70. * }
  71. * &[height-range~="301-500px"] {
  72. * // Styles for the layout when its height is between 301 and 500 pixels
  73. * }
  74. * &[height-range~="501-"] {
  75. * // Styles for the layout when its height is over 500 pixels
  76. * }
  77. * }
  78. * </pre>
  79. *
  80. * <b>CSS</b>
  81. *
  82. * <pre>
  83. * .v-csslayout.responsive[width-range~="0-300px"] {
  84. * // Styles for the layout when its width is between 0 and 300 pixels
  85. * }
  86. * .v-csslayout.responsive[width-range~="301-500px"] {
  87. * // Styles for the layout when its width is between 301 and 500 pixels
  88. * }
  89. * .v-csslayout.responsive[width-range~="501-"] {
  90. * // Styles for the layout when its width is over 500 pixels
  91. * }
  92. *
  93. * .v-csslayout.responsive[height-range~="0-300px"] {
  94. * // Styles for the layout when its height is between 0 and 300 pixels
  95. * }
  96. * .v-csslayout.responsive[height-range~="301-500px"] {
  97. * // Styles for the layout when its height is between 301 and 500 pixels
  98. * }
  99. * .v-csslayout.responsive[height-range~="501px-"] {
  100. * // Styles for the layout when its height is over 500 pixels
  101. * }
  102. * </pre>
  103. *
  104. * </p>
  105. * <p>
  106. * <b>Note:</b> <i>The defined ranges are applied on a global context, so even
  107. * if you would write your CSS to target only a given context, the ranges would
  108. * be applied to all other instances with the same style name.</i>
  109. * </p>
  110. * <p>
  111. * E.g. this would affect all CssLayout instances in the application, even
  112. * though the CSS implies it would only affect CssLayout instances inside a
  113. * parent with a style name "foobar":
  114. * </p>
  115. *
  116. * <pre>
  117. * .foobar .v-csslayout[width-range~="0px-100px"] {
  118. * // These properties will affect all responsive CssLayout instances
  119. * }
  120. * </pre>
  121. *
  122. * <p>
  123. * To scope the ranges, use an additional style name for the target component,
  124. * and add that to your CSS selector:
  125. * </p>
  126. *
  127. * <pre>
  128. * .v-csslayout.mystyle[width-range="0px-100px"] {
  129. * // These properties will only affect responsive CssLayout instances with an additional style name of 'mystyle'
  130. * }
  131. * </pre>
  132. *
  133. * @author Vaadin Ltd
  134. * @since 7.2
  135. */
  136. public class Responsive extends AbstractExtension {
  137. /**
  138. * Creates a new instance, which can be used to extend a component.
  139. */
  140. protected Responsive() {
  141. }
  142. /**
  143. * Enable responsive width and height range styling for the target component
  144. * or UI instance.
  145. *
  146. * @param components
  147. * The components which should be able to respond to width and/or
  148. * height changes.
  149. */
  150. public static void makeResponsive(Component... components) {
  151. for (Component c : components) {
  152. if (c instanceof AbstractClientConnector) {
  153. new Responsive().extend((AbstractClientConnector) c);
  154. }
  155. }
  156. }
  157. @Override
  158. protected ResponsiveState getState() {
  159. return (ResponsiveState) super.getState();
  160. }
  161. @Override
  162. protected ResponsiveState getState(boolean markAsDirty) {
  163. return (ResponsiveState) super.getState(markAsDirty);
  164. }
  165. }