Skip to content

Commit

Permalink
feat(router): allow global router clases
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Apr 30, 2020
1 parent 753ca96 commit 388735b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
46 changes: 36 additions & 10 deletions src/RouterLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,32 @@ export const RouterLink = (defineComponent({
type: [String, Object] as PropType<RouteLocationRaw>,
required: true,
},
activeClass: {
type: String,
default: 'router-link-active',
},
exactActiveClass: {
type: String,
default: 'router-link-exact-active',
},
activeClass: String,
// inactiveClass: String,
exactActiveClass: String,
custom: Boolean,
},

setup(props, { slots, attrs }) {
const link = reactive(useLink(props))
const { options } = inject(routerKey)!

const elClass = computed(() => ({
[props.activeClass]: link.isActive,
[props.exactActiveClass]: link.isExactActive,
[getLinkClass(
props.activeClass,
options.linkActiveClass,
'router-link-active'
)]: link.isActive,
// [getLinkClass(
// props.inactiveClass,
// options.linkInactiveClass,
// 'router-link-inactive'
// )]: !link.isExactActive,
[getLinkClass(
props.exactActiveClass,
options.linkExactActiveClass,
'router-link-exact-active'
)]: link.isExactActive,
}))

return () => {
Expand Down Expand Up @@ -179,3 +188,20 @@ function includesParams(
function getOriginalPath(record: RouteRecord | undefined): string {
return record ? (record.aliasOf ? record.aliasOf.path : record.path) : ''
}

/**
* Utility class to get the active class based on defaults.
* @param propClass
* @param globalClass
* @param defaultClass
*/
let getLinkClass = (
propClass: string | undefined,
globalClass: string | undefined,
defaultClass: string
): string =>
propClass != null
? propClass
: globalClass != null
? globalClass
: defaultClass
17 changes: 17 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,27 @@ export interface RouterOptions extends PathParserOptions {
* {@link RouterOptions.parseQuery | `parseQuery`} counterpart to handle query parsing.
*/
stringifyQuery?: typeof originalStringifyQuery
/**
* Default class applied to active {@link RouterLink}. If none is provided,
* `router-link-active` will be applied.
*/
linkActiveClass?: string
/**
* Default class applied to exact active {@link RouterLink}. If none is provided,
* `router-link-exact-active` will be applied.
*/
linkExactActiveClass?: string
/**
* Default class applied to non active {@link RouterLink}. If none is provided,
* `router-link-inactive` will be applied.
*/
// linkInactiveClass?: string
}

export interface Router {
readonly history: RouterHistory
readonly currentRoute: Ref<RouteLocationNormalizedLoaded>
readonly options: RouterOptions

addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void
addRoute(route: RouteRecordRaw): () => void
Expand Down Expand Up @@ -714,6 +730,7 @@ export function createRouter(options: RouterOptions): Router {
hasRoute,
getRoutes,
resolve,
options,

push,
replace,
Expand Down
2 changes: 1 addition & 1 deletion test-dts/createRouter.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const component = defineComponent({})

const router = createRouter({
history: createWebHistory(),
strict: true,
routes: [{ path: '/', component }],
parseQuery: search => ({}),
stringifyQuery: query => '',
strict: true,
end: true,
sensitive: true,
scrollBehavior(to, from, savedPosition) {},
Expand Down

0 comments on commit 388735b

Please sign in to comment.