AppJars / commons-frontend

Commons utilities for front-end enterprise features
Apache License 2.0
1 stars 1 forks source link

Vaadin Util - CSS and Theme setters #5

Open javier-godoy opened 2 years ago

javier-godoy commented 2 years ago

Consider the following:

@RequiredArgsConstructor
public static class CssTheme {
    private final String themeName;

    public <E extends HasElement> E add(E e) {
        e.getElement().getThemeList().add(themeName);
        return e;
    }

    public <E extends HasElement> E set(E e, boolean set) {
        e.getElement().getThemeList().set(themeName, set);
        return e;
    }

    public <E extends HasElement> E remove(E e) {
        e.getElement().getThemeList().remove(themeName);
        return e;
    }
}

It allows to define semantic-aware constants CssTheme CUSTOM= new CssTheme("custom"); so that one can invoke CUSTOM.set(div); instead of div.getElement().getThemeList().add(CUSTOM), and it works the same regardless of whether CUSTOM is a theme name or a class name. It also allows extending CssTheme so that it does something different (such as setting two themes, or different themes depending on the component, or a theme plus a class name, or...)

It also plays nicely with the helper described in #3

add(__(new Div(), CUSTOM::add));

Improvement ideas:


This contribution contains Personal Intellectual Property that is and remains owned by its Author. To the fullest extent possible, the Appjars Project and its controlling shareholder are granted a non-exclusive, irrevocable and transferable license under the terms stated in section 1(D) of the Amendment to the Agreement on Intellectual Property in effect.