Skip to content

Commit 8e88526

Browse files
committed
feat(link): add router-link-inactive
1 parent 11e779a commit 8e88526

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

docs/api/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ If you add a `target="_blank"` to your `a` element, you must omit the `@click="n
133133

134134
Configure the active CSS class applied when the link is active. Note the default value can also be configured globally via the `linkActiveClass` router constructor option.
135135

136+
### inactive-class
137+
138+
- type: `string`
139+
- default: `"router-link-inactive"`
140+
141+
Configure the CSS class applied when the link is inactive. Note the default value can also be configured globally via the `linkInactiveClass` router constructor option.
142+
136143
### exact
137144

138145
- type: `boolean`

examples/inactive-links/app.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
4+
Vue.use(VueRouter)
5+
6+
const Home = { template: '<div><h2>Home</h2></div>' }
7+
const About = { template: '<div><h2>About</h2></div>' }
8+
9+
const router = new VueRouter({
10+
mode: 'history',
11+
base: __dirname,
12+
routes: [
13+
{ path: '/', component: Home },
14+
{ path: '/about', component: About }
15+
]
16+
})
17+
18+
new Vue({
19+
router,
20+
template: `
21+
<div id="app">
22+
<h1>Inactive Links</h1>
23+
<ul>
24+
<li><router-link to="/" exact>/</router-link></li>
25+
<li><router-link to="/about">/about</router-link></li>
26+
</ul>
27+
<router-view class="view"></router-view>
28+
</div>
29+
`
30+
}).$mount('#app')

examples/inactive-links/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<link rel="stylesheet" href="/global.css">
3+
<style>
4+
a.router-link-active, li.router-link-active a {
5+
color: #f66;
6+
}
7+
a.router-link-inactive, li.router-link-inactive a {
8+
color: #66f;
9+
}
10+
</style>
11+
<a href="/">&larr; Examples index</a>
12+
<div id="app"></div>
13+
<script src="/__build__/shared.chunk.js"></script>
14+
<script src="/__build__/inactive-links.js"></script>

examples/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ <h1>Vue Router Examples</h1>
1414
<li><a href="named-views">Named Views</a></li>
1515
<li><a href="route-matching">Route Matching</a></li>
1616
<li><a href="active-links">Active Links</a></li>
17+
<li><a href="inactive-links">Inactive Links</a></li>
1718
<li><a href="redirect">Redirect</a></li>
1819
<li><a href="route-props">Route Props</a></li>
1920
<li><a href="route-alias">Route Alias</a></li>

flow/declarations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ declare type RouterOptions = {
3737
base?: string;
3838
linkActiveClass?: string;
3939
linkExactActiveClass?: string;
40+
linkInactiveClass?: string;
4041
parseQuery?: (query: string) => Object;
4142
stringifyQuery?: (query: Object) => string;
4243
scrollBehavior?: (

src/components/link.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default {
2626
append: Boolean,
2727
replace: Boolean,
2828
activeClass: String,
29+
inactiveClass: String,
2930
exactActiveClass: String,
3031
ariaCurrentValue: {
3132
type: String,
@@ -48,19 +49,25 @@ export default {
4849
const classes = {}
4950
const globalActiveClass = router.options.linkActiveClass
5051
const globalExactActiveClass = router.options.linkExactActiveClass
52+
const globalInactiveClass = router.options.linkInactiveClass
53+
5154
// Support global empty active class
5255
const activeClassFallback =
5356
globalActiveClass == null ? 'router-link-active' : globalActiveClass
5457
const exactActiveClassFallback =
5558
globalExactActiveClass == null
5659
? 'router-link-exact-active'
5760
: globalExactActiveClass
61+
const inactiveClassFallback =
62+
globalInactiveClass == null ? 'router-link-inactive' : globalInactiveClass
5863
const activeClass =
5964
this.activeClass == null ? activeClassFallback : this.activeClass
6065
const exactActiveClass =
6166
this.exactActiveClass == null
6267
? exactActiveClassFallback
6368
: this.exactActiveClass
69+
const inactiveClass =
70+
this.inactiveClass == null ? inactiveClassFallback : this.inactiveClass
6471

6572
const compareTarget = route.redirectedFrom
6673
? createRoute(null, normalizeLocation(route.redirectedFrom), null, router)
@@ -70,6 +77,7 @@ export default {
7077
classes[activeClass] = this.exact
7178
? classes[exactActiveClass]
7279
: isIncludedRoute(current, compareTarget)
80+
classes[inactiveClass] = !classes[activeClass]
7381

7482
const ariaCurrentValue = classes[exactActiveClass] ? this.ariaCurrentValue : null
7583

test/e2e/specs/inactive-links.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const bsStatus = require('../browserstack-send-status')
2+
3+
module.exports = {
4+
...bsStatus(),
5+
6+
'@tags': ['history', 'inactive', 'router-link'],
7+
8+
'inactive links': function (browser) {
9+
browser
10+
.url('http://localhost:8080/inactive-links/')
11+
.waitForElementVisible('#app', 1000)
12+
.assert.count('li a', 2)
13+
// assert correct href with base
14+
.assert.attributeContains('li:nth-child(1) a', 'href', '/inactive-links/')
15+
.assert.attributeContains('li:nth-child(2) a', 'href', '/inactive-links/about')
16+
.assert.containsText('.view', 'Home')
17+
18+
assertInactiveLinks(1, 2)
19+
assertInactiveLinks(2, 1)
20+
21+
browser.end()
22+
23+
function assertInactiveLinks (n, inactive) {
24+
browser.click(`li:nth-child(${n}) a`)
25+
browser.assert.cssClassPresent(`li:nth-child(${inactive}) a`, 'router-link-inactive')
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)