Skip to content

Commit 388735b

Browse files
committed
feat(router): allow global router clases
1 parent 753ca96 commit 388735b

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

src/RouterLink.ts

+36-10
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,32 @@ export const RouterLink = (defineComponent({
9292
type: [String, Object] as PropType<RouteLocationRaw>,
9393
required: true,
9494
},
95-
activeClass: {
96-
type: String,
97-
default: 'router-link-active',
98-
},
99-
exactActiveClass: {
100-
type: String,
101-
default: 'router-link-exact-active',
102-
},
95+
activeClass: String,
96+
// inactiveClass: String,
97+
exactActiveClass: String,
10398
custom: Boolean,
10499
},
105100

106101
setup(props, { slots, attrs }) {
107102
const link = reactive(useLink(props))
103+
const { options } = inject(routerKey)!
108104

109105
const elClass = computed(() => ({
110-
[props.activeClass]: link.isActive,
111-
[props.exactActiveClass]: link.isExactActive,
106+
[getLinkClass(
107+
props.activeClass,
108+
options.linkActiveClass,
109+
'router-link-active'
110+
)]: link.isActive,
111+
// [getLinkClass(
112+
// props.inactiveClass,
113+
// options.linkInactiveClass,
114+
// 'router-link-inactive'
115+
// )]: !link.isExactActive,
116+
[getLinkClass(
117+
props.exactActiveClass,
118+
options.linkExactActiveClass,
119+
'router-link-exact-active'
120+
)]: link.isExactActive,
112121
}))
113122

114123
return () => {
@@ -179,3 +188,20 @@ function includesParams(
179188
function getOriginalPath(record: RouteRecord | undefined): string {
180189
return record ? (record.aliasOf ? record.aliasOf.path : record.path) : ''
181190
}
191+
192+
/**
193+
* Utility class to get the active class based on defaults.
194+
* @param propClass
195+
* @param globalClass
196+
* @param defaultClass
197+
*/
198+
let getLinkClass = (
199+
propClass: string | undefined,
200+
globalClass: string | undefined,
201+
defaultClass: string
202+
): string =>
203+
propClass != null
204+
? propClass
205+
: globalClass != null
206+
? globalClass
207+
: defaultClass

src/router.ts

+17
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,27 @@ export interface RouterOptions extends PathParserOptions {
109109
* {@link RouterOptions.parseQuery | `parseQuery`} counterpart to handle query parsing.
110110
*/
111111
stringifyQuery?: typeof originalStringifyQuery
112+
/**
113+
* Default class applied to active {@link RouterLink}. If none is provided,
114+
* `router-link-active` will be applied.
115+
*/
116+
linkActiveClass?: string
117+
/**
118+
* Default class applied to exact active {@link RouterLink}. If none is provided,
119+
* `router-link-exact-active` will be applied.
120+
*/
121+
linkExactActiveClass?: string
122+
/**
123+
* Default class applied to non active {@link RouterLink}. If none is provided,
124+
* `router-link-inactive` will be applied.
125+
*/
126+
// linkInactiveClass?: string
112127
}
113128

114129
export interface Router {
115130
readonly history: RouterHistory
116131
readonly currentRoute: Ref<RouteLocationNormalizedLoaded>
132+
readonly options: RouterOptions
117133

118134
addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void
119135
addRoute(route: RouteRecordRaw): () => void
@@ -714,6 +730,7 @@ export function createRouter(options: RouterOptions): Router {
714730
hasRoute,
715731
getRoutes,
716732
resolve,
733+
options,
717734

718735
push,
719736
replace,

test-dts/createRouter.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const component = defineComponent({})
55

66
const router = createRouter({
77
history: createWebHistory(),
8-
strict: true,
98
routes: [{ path: '/', component }],
109
parseQuery: search => ({}),
1110
stringifyQuery: query => '',
11+
strict: true,
1212
end: true,
1313
sensitive: true,
1414
scrollBehavior(to, from, savedPosition) {},

0 commit comments

Comments
 (0)