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

rename onclick_only to provided_onclick_only in dioxus_router's Link #1617

Closed
wants to merge 1 commit into from

Conversation

valyagolev
Copy link
Contributor

@valyagolev valyagolev commented Nov 7, 2023

otherwise it doesn't work :)

this is needed to allow cmd-clicking link. working example:

#[component]
fn Link<'a>(cx: Scoped<'a>, class: Option<&'a str>, to: Route, children: Element<'a>) -> Element {
    let navigator = use_navigator(&cx);

    let eval_provider = use_eval(cx);

    let to_url = to.to_string();

    cx.render(rsx! {
        dioxus_router::prelude::Link {
            class: class.unwrap_or(""),
            to: to.clone(),
            onclick: move |e: Event<MouseData>| {
                let ms = e.data.modifiers();
                
                if ms.meta() || ms.ctrl() {
                    if let Err(e) = eval_provider(&format!("
                        window.open('{to_url}', '_blank');
                    ")) {
                        eprintln!("Error evaluating JS: {e:?}");
                    };
                } else {
                    navigator.push(to.clone());
                }
            },
            provided_onclick_only: true,
            children
        }
    })
}

btw, what would you say if I add this example code to Link as well? (in a separate PR). I think it's important to allow cmd-click for opening in new tabs. I only ever worked with Liveview though, so may be missing something

actually, window.open doesn't allow the focus to be retained in the original tab anymore. so without some prevent_default hackery, I'll have to resort to the normal a here. anyway, this particular PR might still be of value, if this attribute is to be around

@ealmloff
Copy link
Member

ealmloff commented Nov 7, 2023

Once #85 is resolved we can fix ctrl-clicking links. We need to conditionally enable prevent default if the control key was pressed which is currently not possible

@ealmloff ealmloff added breaking This is a breaking change router Related to the router implementation labels Nov 7, 2023
@jkelleyrtp jkelleyrtp closed this Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This is a breaking change router Related to the router implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants