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

prevent_default is not respected after bubbling events #1662

Closed
3 tasks
DonAlonzo opened this issue Nov 26, 2023 · 5 comments · Fixed by #2792
Closed
3 tasks

prevent_default is not respected after bubbling events #1662

DonAlonzo opened this issue Nov 26, 2023 · 5 comments · Fixed by #2792
Assignees
Labels
bug Something isn't working desktop Suggestions related to the desktop renderer liveview Related to the liveview renderer web relating to the web renderer for dioxus
Milestone

Comments

@DonAlonzo
Copy link
Contributor

DonAlonzo commented Nov 26, 2023

Problem

In Liveview, prevent_default on the Link component does not work:

prevent_default: "{prevent_default}",

As a result, the link treats every route, internal or external, as an external link and the page reloads upon redirect.

Steps To Reproduce

Set up a router and Links in Liveview.

Example:

render! {
        div {
            for movie in movies {
                Link {
                    key: "{movie.id}",
                    to: match movie.screening {
                        Some(screening) => Route::Screening { id: screening },
                        None => Route::Movie { id: movie.id },
                    },
                    "{movie.name}"
                }
            }
        }
    }

Expected behavior

A Link to an internal route should be treated as an internal route. The expected behavior can be simulated by commenting out this line:

)

Screenshots

N/A

Environment:

  • Dioxus version: master
  • Rust version: 1.76.0-nightly
  • OS info: Arch Linux
  • App platform: Liveview

Questionnaire

  • I'm interested in fixing this myself but don't know where to start
  • I would like to fix and I have a solution
  • I don't have time to fix this right now, but maybe later
@ealmloff
Copy link
Member

The simple routes example in the router package appears to work correctly in liveview. Do you have the liveview feature enabled in your cargo.toml?

dioxus-router = { git = "https://github.com/DioxusLabs/dioxus", features = "liveview" }

@ealmloff ealmloff added router Related to the router implementation liveview Related to the liveview renderer labels Nov 27, 2023
@DonAlonzo
Copy link
Contributor Author

I do! I have a workspace setup, but the result should be the same:

dioxus-router = { workspace = true, features = ["liveview"] }

and in the root:

dioxus-router = { git = "https://github.com/DioxusLabs/dioxus.git", rev = "d9220d4" }

@DonAlonzo
Copy link
Contributor Author

DonAlonzo commented Nov 27, 2023

I managed to find what triggered the error.

I wanted to style my links, so I went and did this:

Link {
    to: Route::Movies,
    div {
        width: "100%",
        padding: "0.5rem",
        font_size: "1rem",
        background_color: "var(--primary)",
        color: "#fff",
        border_radius: "0.75rem",
        box_shadow: "0 0.25rem 1rem var(--monochrome-20)",
        "Filmer ➤"
    }
}

Removing the inner div makes it work perfectly (losing the styling, though).

@DonAlonzo
Copy link
Contributor Author

Adding prevent_default: "onclick", on the div makes it work as intended.

@ealmloff ealmloff changed the title Liveview Link does not respect prevent_default and as a result is treated as an external link prevent_default is not respected after bubbling events Nov 28, 2023
@ealmloff ealmloff added bug Something isn't working web relating to the web renderer for dioxus desktop Suggestions related to the desktop renderer and removed router Related to the router implementation labels Nov 28, 2023
@ealmloff
Copy link
Member

ealmloff commented Nov 28, 2023

Ok, so the larger issue here is that prevent default is taken from the element that the event originated in, but not when bubbling the event. Here is a minimal example of that bug:

use dioxus::prelude::*;

fn main() {
    dioxus_desktop::launch(app);
}

fn app(cx: Scope) -> Element {
    let mut count = use_state(cx, || 0);

    cx.render(rsx! {
        a {
            href: "http://example.com",
            prevent_default: "onclick",
            div {
                "click me"
            }
        }
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working desktop Suggestions related to the desktop renderer liveview Related to the liveview renderer web relating to the web renderer for dioxus
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants