Search code examples
javascriptjavavaadinvaadin23

Vaadin 23 JavaScript import issue


I have java class which extends the Vaadin Div class, which should be a cookie warning popup (implementation is not yet complete):

//imports

@CssImport("css/info_popup.css")
@JsModule("js/script.js")
@Tag("info_popup")
public class CookiesDiv extends Div {

    public CookiesDiv() {
        this.setId("info_popup");

        Span span = new Span("By continuing to this site, you accept the EULA and the cookies. ");
        span.add(new Anchor("./info", "Learn more"));
        this.add(span);

        this.add(new Button("GOT IT", event -> {
            Page page = UI.getCurrent().getPage();
            this.getId().ifPresent(id -> page.executeJs("fadeOut('" + id + "')"));
        }));
    }
}

When the 'GOT IT' button is pressed, a JS function written in the 'script.js' is called, which animates the div and then removes it from the page:

window.fadeOut = function(target) {
    target.style = 'animation: fade-out 0.5s ease-out 0s forwards'
    setTimeout(function() {
        target.style = 'display: none'
    }, 500)
}

My issue is: the file is successfully imported, but when I test the button the browser console throws the following error: "Uncaught ReferenceError: fadeOut is not defined".

I read some forums which said that in Vaadin 14 changing function fadeOut(target) {} to window.fadeOut = function(target) {} works, but feels like nothing changed.

This is my main tree:

main
├───java
│   └───com
│       └───Achille004
│           └───FreeTransfer
│               ├───backend
│               │   ├───database
│               │   ├───handlers
│               │   └───security
│               └───frontend
│                   ├───css
│                   ├───js
│                   ├───pages
│                   └───views
└───resources
    ├───META-INF
    │   └───resources
    │       └───img
    ├───static
    └───templates

CookiesDiv.java is located into pages and css and js modules are in their own folder.

I could not find other stuff online, so I am just lost. Does someone know how to fix?

P.S. I do not know if it could help: most of the proprieties in the CSS file are ignored, but I still have to do some research about it.


Solution

  • Your imports should be

    @CssImport("./css/info_popup.css")
    @JsModule("./js/script.js")