Search in sources :

Example 11 with Style

use of com.vaadin.flow.dom.Style in project flow by vaadin.

the class ElementTest method semicolonInStyle.

@Test(expected = IllegalArgumentException.class)
public void semicolonInStyle() {
    Element e = ElementFactory.createDiv();
    Style s = e.getStyle();
    s.set("border", "1 px solid black;");
}
Also used : Element(com.vaadin.flow.dom.Element) Style(com.vaadin.flow.dom.Style) Test(org.junit.Test)

Example 12 with Style

use of com.vaadin.flow.dom.Style in project flow by vaadin.

the class ElementTest method dashSeparatedHasStyle.

@Test
public void dashSeparatedHasStyle() {
    Element element = ElementFactory.createDiv();
    Style style = element.getStyle();
    style.set("borderColor", "blue");
    style.set("border-foo", "bar");
    Assert.assertTrue(style.has("border-color"));
    Assert.assertTrue(style.has("border-foo"));
}
Also used : Element(com.vaadin.flow.dom.Element) Style(com.vaadin.flow.dom.Style) Test(org.junit.Test)

Example 13 with Style

use of com.vaadin.flow.dom.Style in project flow by vaadin.

the class BasicElementView method onShow.

@Override
protected void onShow() {
    Element mainElement = getElement();
    mainElement.getStyle().set("margin", "1em");
    Element button = ElementFactory.createButton("Click me");
    Element input = ElementFactory.createInput().setAttribute("placeholder", "Synchronized on change event").synchronizeProperty("value", "change");
    button.addEventListener("click", e -> {
        JsonObject eventData = e.getEventData();
        String buttonText = eventData.getString("element.textContent");
        int clientX = (int) eventData.getNumber("event.clientX");
        int clientY = (int) eventData.getNumber("event.clientY");
        Element greeting = ElementFactory.createDiv("Thank you for clicking \"" + buttonText + "\" at (" + clientX + "," + clientY + ")! The field value is " + input.getProperty("value"));
        greeting.setAttribute("class", "thankYou");
        greeting.addEventListener("click", e2 -> greeting.removeFromParent());
        mainElement.appendChild(greeting);
    }, "element.textContent", "event.clientX", "event.clientY");
    Element helloWorldElement = ElementFactory.createDiv("Hello world");
    Set<String> spanClasses = helloWorldElement.getClassList();
    helloWorldElement.setProperty("id", "hello-world");
    spanClasses.add("hello");
    helloWorldEventRemover = helloWorldElement.addEventListener("click", e -> {
        if (helloWorldElement.getText().equals("Hello world")) {
            helloWorldElement.setText("Stop touching me!");
        } else {
            // We never get to this code as long as the event
            // removal actually works
            helloWorldElement.setText(helloWorldElement.getText() + " This might be your last warning!");
        }
        spanClasses.clear();
        helloWorldEventRemover.remove();
    });
    Style s = helloWorldElement.getStyle();
    s.set("color", "red");
    s.set("fontWeight", "bold");
    Element elementContainer = ElementFactory.createDiv();
    Element toRemove = ElementFactory.createDiv("To Remove").setAttribute("id", "to-remove");
    elementContainer.appendChild(toRemove);
    elementContainer.setAttribute("id", "addremovecontainer");
    Element addRemoveButton = ElementFactory.createButton("Add and remove element");
    addRemoveButton.setAttribute("id", "addremovebutton");
    addRemoveButton.addEventListener("click", e -> {
        // very basic usecase: append and then immediately remove
        Element div = ElementFactory.createDiv("foobar");
        elementContainer.appendChild(div);
        elementContainer.removeChild(div);
        elementContainer.removeChild(toRemove);
        elementContainer.appendChild(toRemove);
        // Now let's have two "add" operation and then two "remove"
        // operation so that removal has an addition right before which
        // targets different element
        Element div2 = ElementFactory.createDiv("foobar");
        elementContainer.appendChild(div);
        elementContainer.appendChild(div2);
        Element ok = ElementFactory.createDiv("OK");
        ok.setAttribute("id", "ok");
        elementContainer.appendChild(ok);
        elementContainer.removeChild(div);
        elementContainer.removeChild(div2);
    });
    mainElement.appendChild(helloWorldElement, button, input, addRemoveButton, elementContainer);
}
Also used : ElementFactory(com.vaadin.flow.dom.ElementFactory) Element(com.vaadin.flow.dom.Element) Registration(com.vaadin.flow.shared.Registration) Set(java.util.Set) ViewTestLayout(com.vaadin.flow.uitest.servlet.ViewTestLayout) JsonObject(elemental.json.JsonObject) Style(com.vaadin.flow.dom.Style) Route(com.vaadin.flow.router.Route) Element(com.vaadin.flow.dom.Element) JsonObject(elemental.json.JsonObject) Style(com.vaadin.flow.dom.Style)

Example 14 with Style

use of com.vaadin.flow.dom.Style in project flow by vaadin.

the class ElementTest method validStyleWithSemicolon.

@Test
public void validStyleWithSemicolon() {
    Element element = ElementFactory.createDiv();
    String validStyle = "background: url('foo;bar')";
    Style style = element.getStyle();
    style.set("background", validStyle);
    Assert.assertEquals(validStyle, style.get("background"));
}
Also used : Element(com.vaadin.flow.dom.Element) Style(com.vaadin.flow.dom.Style) Test(org.junit.Test)

Example 15 with Style

use of com.vaadin.flow.dom.Style in project flow by vaadin.

the class ElementTest method dashSeparatedRemoveStyle.

@Test
public void dashSeparatedRemoveStyle() {
    Element element = ElementFactory.createDiv();
    Style style = element.getStyle();
    style.set("borderColor", "blue");
    style.set("border-foo", "bar");
    style.remove("border-color");
    style.remove("border-foo");
    Assert.assertFalse(style.has("border-color"));
    Assert.assertFalse(style.has("border-foo"));
}
Also used : Element(com.vaadin.flow.dom.Element) Style(com.vaadin.flow.dom.Style) Test(org.junit.Test)

Aggregations

Style (com.vaadin.flow.dom.Style)22 Element (com.vaadin.flow.dom.Element)20 Test (org.junit.Test)17 Text (com.vaadin.flow.component.Text)1 Div (com.vaadin.flow.component.html.Div)1 Input (com.vaadin.flow.component.html.Input)1 NativeButton (com.vaadin.flow.component.html.NativeButton)1 ElementFactory (com.vaadin.flow.dom.ElementFactory)1 StateNode (com.vaadin.flow.internal.StateNode)1 ElementStylePropertyMap (com.vaadin.flow.internal.nodefeature.ElementStylePropertyMap)1 Route (com.vaadin.flow.router.Route)1 Registration (com.vaadin.flow.shared.Registration)1 ViewTestLayout (com.vaadin.flow.uitest.servlet.ViewTestLayout)1 JsonObject (elemental.json.JsonObject)1 Set (java.util.Set)1