-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Add support for 'disabled' attribute to <router-link>, fixes #916 #2098
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
Conversation
|
Any movement on this? |
|
I saw there is a big interest in this and will take a look shortly :) |
|
Coming back to this: current implementation looking for the attribute disabled doesn't look satisfactory. But at the same time, I'm afraid adding a prop named disabled could break existing behaviour with buttons and others that are disabled After reading all the comments in the issue and asking around, I'm still unsure of what problem is being solved and what is the real use case behind. The cleanest workaround would be a v-if but it requires duplicating the content: <router-link v-if="active" to="/link">/link</router-link>
<span class="link-disabled" v-else>/link</span>The other way would be to use both, a Even if we implement a |
Could you please elaborate as to why? This is exactly what the users requesting the functionality (myself included) have requested.
Again, I'm not sure I can follow the reasoning. A disabled form element is already disabled by the browser and behaves exactly as the
It's rather simple: The real use case is to be able to:
That is very, very far from clean, and does not retain implicit CSS styling in Bootstrap and other CSS libraries that share this idiom for navigation elements. Actually what people usually do is: <router-link
:disabled="!whateverActivatesThisLink"
:event="whateverActivatesThisLink ? 'click' : ''"
to="/link"
>/link</router-link>Which is dry-er. Still, compare to: <router-link :disabled="!whateverActivatesThisLink" to="/link">/link</router-link>
It's much easier to style by just detecting the disabled attribute, it's completely idiomatic to how DOM implementations already do it for HTML form elements. Granted, if preventing keyboard-selection (tho do note that even if keyboard selection works it has as much effect as clicking on an disabled element: none) <router-link
:disabled="!whateverActivatesThisLink"
:tabindex="whateverActivatesThisLink ? undefined : -1"
:event="whateverActivatesThisLink ? 'click' : ''"
to="/link"
>/link</router-link>to <router-link
:disabled="!whateverActivatesThisLink"
:tabindex="whateverActivatesThisLink ? undefined : -1"
to="/link"
>/link</router-link>for full equivalence, but I see what you mean here.
I'm not exactly sure what do you mean by "prevent the click but still allow" it.? If by "it"you mean "keyboard navigation" then I concur. It's a tough cookie. Other than that, HTML form elements are visible, visibly "greyed out" and "do not work". CSS is what one uses for styling. This is exactly how Which is what Bootstrap and other CSS libraries already do for link/navi elements when they have
Again, I'm not sure I agree with this assesment: it would, and does behave exactly as the canonical Still, even without that it's an improvement over the current state of things because:
With the only perhaps confusing behavior being the "keyboard navigation" case where a I firmly believe that perfect is the enemy of good, and that the scenario in which someone clever comes up with a more wholesome solution if and when you accept this patch or implement something alike is equally possible as the one where you get bombarded by issues around this from confused users. I'd even say it's more likely. |
|
what isn't satisfactory to me is in part the naming because
That is just not true... but if it isn't then there is no need to add anything to current implementation as using pointer-events over the
To be fair, Bootstrap is no longer the dominant CSS (or component in the case of Vue) library Anyway, this is still not focusing on the original problem, which is what I want to get to. Right now, the original problem seems to be that it isn't possible to prevent the navigation from happening when clicking while we still want to display the link: <router-link to="/some-url" @click.prevent="customNavigation">go</router-link>At least that's what I found when searching in the original issue. ``vue
|
I do agree, the original thread suggested that "the people" want it intrinsic to It's a type of tradeoff in the "syntactic sugar" class. Whatever you guys choose to do is fine by me. So far design decisions of the core Vue team have "clicked" with me, and the dev experience is really one of the main reasons why I push your library on my and my employers projects. |
|
so, if i want disabled |
|
I'm also interested to disable route-link.
This is working but due to But it's not programmer-friendly: <router-link
:disabled="!whateverActivatesThisLink"
:event="whateverActivatesThisLink ? 'click' : ''"
:style="!whateverActivatesThisLink ? 'cursor: default' : ''"
to="/link"
>
/link
</router-link> |
|
I sometimes do stuff like this: <component
:is="hasSubLinks ? 'button' : 'router-link'"
:to="hasSubLinks ? undefined : href"
@click="hasSubLinks ? handleClick() : navigate"
>
<!-- arbitrary markup -->
</component>But I basically always wrap <template>
<router-link
v-slot="{ href, route, navigate, isActive, isExactActive }"
:to="to"
>
<a
:class="['nav-link-white', {
'nav-link-white-active': isActive,
'opacity-50': isDisabled,
}]"
:href="isDisabled ? undefined : href"
@click="handler => handleClick(handler, navigate)"
>
<slot></slot>
</a>
</router-link>
</template>
<script>
export default {
name: 'top-nav-link',
props: {
to: {
type: Object,
required: true,
},
isDisabled: {
type: Boolean,
required: false,
default: () => false,
},
},
data() {
return {};
},
computed: {},
methods: {
handleClick(handler, navigate) {
if (this.isDisabled) return undefined;
return navigate(handler);
},
},
};
</script>
In my app right now, I'm noticing that some combinations of For example this changes routes very slow: @click="isDisabled ? undefined : handler => navigate(handler)"But the pattern in my full example code above works and has no performance issue. In general, ternary operator in |
|
One of the ideas behind the |
|
For anyone looking for an easy fix: <component :is="isDisabled ? 'span' : 'router-link'" :to="yourUrl" />is the most straight forward option currently available |
|
I think add a disabled attribute is great. Thanks you for your pull request.
Can you show us some more example to convince us? @posva :
@bmarkovic is trying to change the ugly code to more beautiful and simple code: |
|
I think is more natural pass |
|
Docs to show how to extend custom needs for router link are on the way. Keep in mind there is no way to support every single use case for router-link out of the box and it's fine to extend it to support custom cases in your application like navbars, content links, external links, etc Example: |
During the discussion on #916 a solution was mentioned, where attribute
disabledin<router-link>would prevent the link from working. This is useful for programmatically disabling router links based on application state.Compared to other solutions, both available and proposed, this is by far the cleanest and most idiomatic if you come from a HTML mindset, as it's available on form elements with the same purpose, and some CSS frameworks like Bootstrap promote its use for
<li><a>navigation elements.Furthermore, binding to disabled attribute is a simple, easy to style and implement, and I honestly believe that for these reasons, it is also very idiomatic to Vue.js as well.
This pull request implements that proposal.