Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router doesn't catch anchor events from menu-bar, dispatching vaadin-navigate event works only with replace: true #20585

Open
Wlad-K opened this issue Nov 29, 2024 · 4 comments

Comments

@Wlad-K
Copy link

Wlad-K commented Nov 29, 2024

Description of the bug

I have anchors placed inside MenuBar, in html it looks like this:

<vaadin-menu-bar-list-box theme="icon menu theme-color" role="menu" aria-orientation="vertical">
    <div aria-haspopup="false" theme="icon menu theme-color">
        <a href="link">
            Link
        </a>
    </div>
</vaadin-menu-bar-list-box>

When I click on such link, router doesn't catch it and page fully reloads. I tried to work around it with preventDefault() on click event and firing router event:

e.preventDefault();
window.dispatchEvent(new CustomEvent("vaadin-navigate", {
    detail: {
        url: path,
        replace: false,
        state: null
    }
}));

It did nothing, it worked only when I changed replace to true, which isn't what I want.

Expected behavior

Clicking on <a> inside menu-bar will work the same as other anchors do.

Minimal reproducible example

Function to generate such element:

const anchor = document.createElement("a");
anchor.href = "/test";
render("Test", anchor);

const menuBarItems: MenuBarItem[] = [
    {
        text: "Test",
        children: [{
            component: anchor
        }]
    }];

return html`
        <vaadin-menu-bar .items=${menuBarItems}
                         title="Test"
        ></vaadin-menu-bar>
    `;

Versions

  • Vaadin / Flow version: 24.4.3
  • Java version: 21.0.1
  • OS version: windows 11
  • Browser version (if applicable): Chrome Version 131.0.6778.86 (Official Build) (64-bit)
  • Application Server (if applicable):
  • IDE (if applicable):
@tepi
Copy link
Contributor

tepi commented Nov 29, 2024

Regular anchor element behaves this way in Vaadin, it causes a page reload. If you want router to catch it, add attribute router-link to the anchor element.

@Wlad-K
Copy link
Author

Wlad-K commented Nov 29, 2024

@tepi I tried that, and it doesn't do anything. Does it work still for newer version? I found it in documentation for v14 but not newer one.

@tepi
Copy link
Contributor

tepi commented Nov 29, 2024

Ok, then it seems there's some bug somewhere. Adding the attribut is the only thing I see Flow's RouterLink is doing in the latest versions as well: https://github.com/vaadin/flow/blob/main/flow-server/src/main/java/com/vaadin/flow/router/RouterLink.java#L62-L65 - otherwise it's just a standard a tag: @Tag(Tag.A).

@tepi
Copy link
Contributor

tepi commented Dec 2, 2024

Confirmed that it fails. Outside menubar the anchor works as expected (when it has router-link attr), but inside menubar it always does page reload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🔎 Investigation
Development

No branches or pull requests

2 participants