Dogstudio / highway

Highway - A Modern Javascript Transitions Manager
https://highway.js.org/
MIT License
1.43k stars 92 forks source link

Programmatically prevent router usage #32

Closed TristanPct closed 6 years ago

TristanPct commented 6 years ago

Hello,

On a site without Highway, doing a event.preventDefault() on a <a> click events programmatically prevent the browser to redirect users.

Is there a way to do the same things with Highway?

I know that I can add a data-router-disabled attribute on my link, but I want to prevent redirection based on some conditions.

For the moment I can do the opposite:

Anthodpnt commented 6 years ago

Hi @TristanPct,

If you had the data-router-disabled attribute to your link Highway won't attach any click listener to it meaning you can do the event.preventDefault() like you would do usually because the link is not attache to Highway.

Can you please try and come back to us ?

Thanks, Anthodpnt

TristanPct commented 6 years ago

Yes, it's what I do for the moment.

My use case is:

So I tried something like:

function onLinkClick(event) {
    if (window.innerWidth < 1150) { // Small screen no redirect
        event.preventDefault(); // Ignored by Highway
       //...
    } else { // Normal case
       //...  
    }
}

For the moment I have to add data-router-disabled on my links and:

function onLinkClick(event) {
    if (window.innerWidth < 1150) { // Small screen no redirect
        event.preventDefault(); // Prevent browser redirection
        //...
    } else { // Normal case
        highwayCore.redirect(event.target.attributes["href"].value);
        //...
    }
}
Anthodpnt commented 6 years ago

If I understand you want to disable the transition on small screens ? Why don't you just call Highway.Core when the screen is over 1150 ? Why would you remove the click on a link on mobile but not on desktop ?

TristanPct commented 6 years ago

I want to disable my links (not only the transition) on mobile for a menu like this:

On desktop all links are visible, on mobile I have a burger menu, Child 2.1 is visible only if I click on Parent 2, but if I'm redirected to Parent 2's page I can't see the Child. So on mobile I have to prevent default redirection behavior on click, just for parents links.

With my second script, it works like I want.

It's more a suggestion than a bug to add something allowing to programmatically disable the router on a link click.

Anthodpnt commented 6 years ago

My suggestion would be to create both a a and a button in your DOM and play with CSS to display one or the other based on the window size instead of hacking the Javascript to disable the event on mobile because you have a burger menu.

But the question to disable the router on selected links has been asked and we'll take a look to add this feature as soon as possible.

Best regards, Anthodpnt

wezzou1 commented 6 years ago

I'm using two menus (one for mobile) and (on for desktop) and it's working perfect to display data-router-disabled on Parent's to make a nice dropdown :)

Anthodpnt commented 6 years ago

I suppose this issue can be closed for now :)