I'd like to define some colours as constants in a GWT CssResource, and use those constants throughout my application; but I don't know how to do that.
I'll tell you what what I've tried. I've created a ClientBundle and a CssResource as follows:
public interface Resources extends ClientBundle {
public interface MyStyle extends CssResource {
String JUNGLEGREEN();
String example();
...
}
@Source("Resources.css")
MyStyle css();
}
I've defined some constants in Resources.css:
@def JUNGLEGREEN #1F3D0A;
Within Resources.css, I use those constants like so:
.example { color:JUNGLEGREEN; }
I'm not aware of a way to re-use those constants in other CSS files and UiBinder templates. I'd like to do this in some other UiBinder file, say LoginView.ui.xml:
<ui:with field='resources' type='com.example.Resources' />
<ui:style>
.mainPanel {
background:{resources.css.JUNGLEGREEN};
...
}
</ui:style>
...but it doesn't seem to compile. Do you know how I can achieve my objective?
This is how we do it:
constant.css
file@def black #241b15; /* text color */
@def orange #ff4f00; /* links */
<ui:style src="../../resources/css/constants.css">
.myStyle {
color: orange;
}
</ui:style>
Hope that helps.
EDIT:
To avoid the relative path in the <ui:style>
element you could do the following:
constants.css
)@def junglegreen #1f3d0a;
ClientBundle
and CssResource
to retrieve the defined constantspublic interface MyResources extends ClientBundle {
public static final MyResources INSTANCE = GWT.create(MyResources.class);
public interface Constants extends CssResource {
String junglegreen();
}
Constants constants();
}
-use the @eval
annotation to access the constant
<ui:style>
@eval green com.gwt.client.widget.test.MyResources.INSTANCE.constants().junglegreen();
.someClass {
color: green;
}
</ui:style>
The only way I know of how to deal with constants without referencing the css file itself.
I know this answer might be kind of late but may help someone. I was having the same problem and was able to solve it by adding the following:
Resources.css().ensureInjected()
I added it in my factory but tried it in a couple of places and no matter where I put it, it worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With