Skip to content

Commit e7c4f97

Browse files
committed
Update documentation on Style to remove reference to @Style
1 parent 3eee0b9 commit e7c4f97

File tree

7 files changed

+202
-72
lines changed

7 files changed

+202
-72
lines changed

docs-source/book/SUMMARY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@
4747
* [Plugins](composition/plugins.md)
4848

4949
## Integration with GWT
50-
* [Styles](gwt-integration/styles.md)
51-
* [Client Bundles](gwt-integration/client-bundles.md)
50+
* [Client Bundles and Styles](gwt-integration/client-bundles-and-styles.md)
51+
* [Client Bundles](gwt-integration/client-bundles-and-styles.md#client-bundles)
52+
* [Styles (CssResource)](gwt-integration/client-bundles-and-styles.md#styles)
5253
* [Vue GWT and Widgets](gwt-integration/widgets.md)
5354
* [JsInterop](js-interop/README.md)
5455
* [JsPropertyMap<T>](js-interop/README.md#js-property-map)
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
```

docs-source/book/gwt-integration/client-bundles.md

Lines changed: 0 additions & 67 deletions
This file was deleted.

docs-source/examples/src/main/java/com/axellience/vuegwtexamples/client/VueGwtExamplesApp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.axellience.vuegwtexamples.client;
22

33
import com.axellience.vuegwt.core.client.VueGWT;
4+
import com.axellience.vuegwtexamples.client.examples.melisandre.MelisandreComponentClientBundle;
45
import com.google.gwt.core.client.EntryPoint;
56

67
/**
@@ -14,6 +15,7 @@ public class VueGwtExamplesApp implements EntryPoint
1415
public void onModuleLoad()
1516
{
1617
VueGWT.initWithoutVueLib();
18+
MelisandreComponentClientBundle.INSTANCE.melisandreComponentStyle().ensureInjected();
1719
VueGwtExamplesService.initExamples();
1820
}
1921
}

docs-source/examples/src/main/java/com/axellience/vuegwtexamples/client/examples/melisandre/MelisandreComponent.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<vue-gwt:import style="com.axellience.vuegwtexamples.client.examples.melisandre.MelisandreComponentStyle" name="myStyle"/>
2-
31
<div>
42
<div v-bind:class="myStyle.red()">
53
For the night is dark and full of terrors.

docs-source/examples/src/main/java/com/axellience/vuegwtexamples/client/examples/melisandre/MelisandreComponent.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.axellience.vuegwtexamples.client.examples.melisandre;
22

3-
import com.axellience.vuegwt.core.client.component.VueComponent;
43
import com.axellience.vuegwt.core.annotations.component.Component;
4+
import com.axellience.vuegwt.core.annotations.component.Computed;
5+
import com.axellience.vuegwt.core.client.component.VueComponent;
56
import jsinterop.annotations.JsProperty;
67

78
/**
@@ -11,4 +12,10 @@
1112
public class MelisandreComponent extends VueComponent
1213
{
1314
@JsProperty boolean isRed = true;
15+
16+
@Computed
17+
public MelisandreComponentStyle getMyStyle()
18+
{
19+
return MelisandreComponentClientBundle.INSTANCE.melisandreComponentStyle();
20+
}
1421
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.axellience.vuegwtexamples.client.examples.melisandre;
2+
3+
import com.google.gwt.core.client.GWT;
4+
import com.google.gwt.resources.client.ClientBundle;
5+
6+
public interface MelisandreComponentClientBundle extends ClientBundle
7+
{
8+
MelisandreComponentClientBundle INSTANCE = GWT.create(MelisandreComponentClientBundle.class);
9+
10+
@Source("MelisandreComponentStyle.gss")
11+
MelisandreComponentStyle melisandreComponentStyle();
12+
}

0 commit comments

Comments
 (0)