|
| 1 | +# Client Bundles and Styles |
| 2 | + |
| 3 | +!INCLUDE "../dependencies.md" |
| 4 | + |
| 5 | +You can use any valid Java expression in your template. |
| 6 | +This means that Vue GWT is compatible with GWT 2.x `ClientBundles`. |
| 7 | + |
| 8 | +## Exposing a `ClientBundle` to the Template {#client-bundles} |
| 9 | + |
| 10 | +Lets see how this work with an example. |
| 11 | + |
| 12 | +First, we create a simple `ClientBundle` with an `ImageResource`: |
| 13 | + |
| 14 | +```java |
| 15 | +public interface KittenClientBundle extends ClientBundle { |
| 16 | + KittenClientBundle INSTANCE = GWT.create(KittenClientBundle.class); |
| 17 | + |
| 18 | + @Source("kitten.jpg") |
| 19 | + ImageResource myKitten(); |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +There is no need for any special annotations on it. |
| 24 | + |
| 25 | +We then create a Component to use our `ClientBundle`. |
| 26 | +In this component we add a computed property for our `KittenClientBundle`. |
| 27 | +This will expose the bundle to the Template. |
| 28 | + |
| 29 | +```java |
| 30 | +@Component |
| 31 | +public class KittenComponent extends VueComponent { |
| 32 | + @Computed |
| 33 | + public KittenClientBundle getMyKittenBundle() { |
| 34 | + return KittenClientBundle.INSTANCE; |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +We can then simply access our bundle instance from the template: |
| 40 | + |
| 41 | +```html |
| 42 | +<img v-bind:src="myKittenBundle.myKitten().getSafeUri().asString()"/> |
| 43 | +``` |
| 44 | + |
| 45 | +{% raw %} |
| 46 | +<div class="example-container" data-name="kittenComponent"> |
| 47 | + <span id="kittenComponent"></span> |
| 48 | +</div> |
| 49 | +{% endraw %} |
| 50 | + |
| 51 | +You could also directly expose your ImageResource to the template, it's really up to you. |
| 52 | + |
| 53 | +## Using `CssResources` in Vue GWT {#styles} |
| 54 | + |
| 55 | +Vue GWT is compatible with GSS styles. |
| 56 | +You can declare your `CssResource` interfaces and use them within your Vue GWT Components. |
| 57 | + |
| 58 | +You also get type checking of your styles rules at compile type. |
| 59 | +So if you try to use `myStyle.bob()` in your template, and this style doesn't exist, the compilation will break with an explicit error. |
| 60 | + |
| 61 | +Let see it in action with a simple example. |
| 62 | + |
| 63 | +### Setting up a Small `CssResource` |
| 64 | + |
| 65 | +Let's say we have the following `CssResource` (and it's associated `gss` file): |
| 66 | + |
| 67 | +```java |
| 68 | +public interface MelisandreComponentStyle extends CssResource { |
| 69 | + String red(); |
| 70 | + String bold(); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +Our `MelisandreComponentStyle` is exposed in an `AppClientBundle`: |
| 75 | + |
| 76 | +```java |
| 77 | +public interface AppClientBundle extends ClientBundle { |
| 78 | + AppClientBundle INSTANCE = GWT.create(AppClientBundle.class); |
| 79 | + |
| 80 | + @Source("melisandreComponentStyle.gss") |
| 81 | + MelisandreComponentStyle melisandreStyle(); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +And in our `RootGwtApp` we call `ensureInjected` to make sure it's injected: |
| 86 | + |
| 87 | +```java |
| 88 | +public class RootGwtApp implements EntryPoint { |
| 89 | + public void onModuleLoad() { |
| 90 | + VueGWT.init(); |
| 91 | + AppClientBundle.INSTANCE.melisandreStyle().ensureInjected(); |
| 92 | + // ... |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Using Our Small `CssResource` in a Component |
| 98 | + |
| 99 | +Let's use our style in a small Component. |
| 100 | +As with the `ClientBundle`, we can use a computed method to expose our Style. |
| 101 | + |
| 102 | +```java |
| 103 | +@Component |
| 104 | +public class MelisandreComponent extends VueComponent { |
| 105 | + @Computed |
| 106 | + public MelisandreComponentStyle getMyStyle() { |
| 107 | + return MelisandreComponentClientBundle.INSTANCE.melisandreComponentStyle(); |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +We can then simply use Vue `v-bind` directive to bind our style. |
| 113 | + |
| 114 | +```html |
| 115 | +<div v-bind:class="myStyle.red()"> |
| 116 | + For the night is dark and full of terrors. |
| 117 | +</div> |
| 118 | +``` |
| 119 | + |
| 120 | +## Adding More Than One CSS Class |
| 121 | + |
| 122 | +You can add more than one CSS Class to your Components by using the [`array` builder](../js-interop/README.md#array): |
| 123 | + |
| 124 | +```html |
| 125 | +<div v-bind:class="array(myStyle.red(), myStyle.bold())"> |
| 126 | + For the night is dark, full of terrors and bold. |
| 127 | +</div> |
| 128 | +``` |
| 129 | + |
| 130 | +## Conditional Styling |
| 131 | + |
| 132 | +You can apply a Style conditionally. |
| 133 | +First we add a boolean in our `MelisandreComponent`: |
| 134 | + |
| 135 | +```java |
| 136 | +@Component |
| 137 | +public class MelisandreComponent extends VueComponent { |
| 138 | + @JsProperty boolean isRed = true; |
| 139 | + |
| 140 | + @Computed |
| 141 | + public MelisandreComponentStyle getMyStyle() { |
| 142 | + return MelisandreComponentClientBundle.INSTANCE.melisandreComponentStyle(); |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +Then we simply use a ternary expression (be careful with the quotes): |
| 148 | + |
| 149 | +```html |
| 150 | +<div v-bind:class='isRed ? myStyle.red() : ""'> |
| 151 | + For the night is dark, full of terrors and might or not be red. |
| 152 | +</div> |
| 153 | +``` |
| 154 | + |
| 155 | +## Conditional Styling With Several CSS Class |
| 156 | + |
| 157 | +You can combine both the `array` and the ternary syntax: |
| 158 | + |
| 159 | +```html |
| 160 | +<div v-bind:class='array(isRed ? myStyle.red() : "", myStyle.bold())'> |
| 161 | + For the night is dark, full of terrors, might or not be red and always BOLD. |
| 162 | +</div> |
| 163 | +``` |
| 164 | + |
| 165 | +## Here is our finished `MelisandreComponent` |
| 166 | + |
| 167 | +{% raw %} |
| 168 | +<div class="example-container" data-name="melisandreComponent"> |
| 169 | + <span id="melisandreComponent"></span> |
| 170 | +</div> |
| 171 | +{% endraw %} |
| 172 | + |
| 173 | +You can try toggling the red color on the two bottom sentences by typing in your browser console: |
| 174 | + |
| 175 | +``` |
| 176 | +melisandreComponent.isRed = false; |
| 177 | +``` |
0 commit comments