Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Vaadin Util - CSS and Theme setters #5

Open
javier-godoy opened this issue Sep 1, 2021 · 0 comments
Open

Vaadin Util - CSS and Theme setters #5

javier-godoy opened this issue Sep 1, 2021 · 0 comments

Comments

@javier-godoy
Copy link
Contributor

  • If a component implements HasTheme, one can call addThemeName(themeName), but if it doesn't one has to use getElement().getThemeList().add(themeName)
  • Both theme names and class names are strings, but they cannot be used interchangeably (if it is a theme-string, you need to call the theme setter, if it is a class-string you need to call the class setter)

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:

  • Perhaps both should extend a common interface (such as StyleSetter)
  • Perhaps there should be method that receive an Element as parameter.
  • (Not so sure on this) perhaps the type parameter should be moved to the class (instead of being present on every method), so one could have CssTheme<Button> (which avoid applying button-specific styles to a text area).

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.

@javier-godoy javier-godoy changed the title Vaadin Util - CSS and Theme setteres Vaadin Util - CSS and Theme setters Sep 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant