Dynamic CSS class names in GWT

I am moving a simple CSS mesh system to GWT. My CSS file currently has classes like .size1 , .size2 etc., and I have a CSS resource that looks like

 interface MyResource extends CSSResource { @ClassName("size1") String size1(); @ClassName("size2") String size2(); // And so on } 

However, what I really want is to have one function, such as:

 String size(int size); 

which will generate the appropriate class when passing the size as a whole at runtime. This is necessary as I do some calculations to determine the actual space available / needed for the widget in javascript, and then append the appropriate class name.

Is this possible with GWT?

Change To clarify, I can easily write such a method for myself like this:

 String size(int size) { switch(size) { case 1: return size1(); case 2: return size2(); ... and so on } } 

However, MyResource is an interface, and its implementation is generated at runtime by the GWT. This means that I cannot add the size method to the MyResource type. Therefore, I suggest that I ask you to specify a way to add custom methods to the MyResource .

+4
source share
2 answers

I think you should not try to solve this through css files, but if you want to have dynamic values ​​in your style, you should set these values ​​in the code via myWidget.getElement().getStyle().setSomeCssMethod() . Or in this case, you seem to want to set the size, both in height and in width. For most widgets, these values ​​can be set directly. If I didn’t miss something, why do you want to use css classes and not install it directly in the code?

+1
source

You cannot pass method names to CSSResource.

Remember that the goal of CSSResource is to minimize and obfuscate CSS class names.

However, just because CSS Resource can do this does not mean that you should use it for everything that you code.

For example, you already know that the CSS resource methods return a string, so just create your own method that returns a string (your dynamic class names), and use @ CssResource.NotStrict to load your CSS when adding it to your resource.

+1
source

Source: https://habr.com/ru/post/1402205/


All Articles