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

Vaadin Util - CSS and Theme setters #5

@javier-godoy

Description

@javier-godoy
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions