com.vaadin.flow.component.icon.IronIcon Maven / Gradle / Ivy
/**
* Copyright 2000-2024 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See {@literal } for the full
* license.
*/
package com.vaadin.flow.component.icon;
import com.vaadin.flow.component.ClickNotifier;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.dom.ElementConstants;
/**
* Server side component for
* iron-icon element
* to display an icon.
*
* @author Vaadin Ltd
*/
@Tag("iron-icon")
@NpmPackage(value = "@polymer/iron-icon", version = "3.0.1")
@JsModule("@polymer/iron-icon/iron-icon.js")
public class IronIcon extends Component
implements HasStyle, ClickNotifier {
private static final String ICON_ATTRIBUTE_NAME = "icon";
/**
* Creates an Icon component that displays the given {@code icon} from the
* given {@code collection}.
*
* @param collection
* the icon collection
* @param icon
* the icon name
*/
public IronIcon(String collection, String icon) {
// iron-icon's icon-attribute uses the format "collection:name",
// e.g. icon="vaadin:arrow-down"
getElement().setAttribute(ICON_ATTRIBUTE_NAME, collection + ':' + icon);
}
/**
* Sets the width and the height of the icon.
*
* The size should be in a format understood by the browser, e.g. "100px" or
* "2.5em".
*
* @param size
* the size to set, may be null
to clear the value
*/
public void setSize(String size) {
if (size == null) {
getStyle().remove(ElementConstants.STYLE_WIDTH);
getStyle().remove(ElementConstants.STYLE_HEIGHT);
} else {
getStyle().set(ElementConstants.STYLE_WIDTH, size);
getStyle().set(ElementConstants.STYLE_HEIGHT, size);
}
}
/**
* Sets the fill color of the icon.
*
* The color should be in a format understood by the browser, e.g. "orange",
* "#FF9E2C" or "rgb(255, 158, 44)".
*
* @param color
* the fill color to set, may be null
to clear the
* value
*/
public void setColor(String color) {
if (color == null) {
getStyle().remove(ElementConstants.STYLE_COLOR);
} else {
getStyle().set(ElementConstants.STYLE_COLOR, color);
}
}
/**
* Gets the fill color of this icon as a String.
*
* @return the fill color of the icon, or null
if the color has
* not been set
*/
public String getColor() {
return getStyle().get(ElementConstants.STYLE_COLOR);
}
}