vuepress-theme-reco/components/SidebarLink.vue

116 lines
2.9 KiB
Vue
Raw Normal View History

2019-04-15 10:35:40 +08:00
<script>
import { isActive, hashRE, groupHeaders } from '@theme/helpers/utils'
2019-04-15 10:35:40 +08:00
export default {
functional: true,
props: ['item', 'sidebarDepth'],
render (h,
{
parent: {
$page,
$site,
$route,
$themeConfig,
$themeLocaleConfig
},
props: {
item,
sidebarDepth
}
}) {
// use custom active class matching logic
// due to edge case of paths ending with / + hash
const selfActive = isActive($route, item.path)
// for sidebar: auto pages, a hash link should be active if one of its child
// matches
const active = item.type === 'auto'
? selfActive || item.children.some(c => isActive($route, item.basePath + '#' + c.slug))
: selfActive
const link = renderLink(h, item.path, item.title || item.path, active)
2019-09-20 13:54:13 +08:00
const configDepth = $page.frontmatter.sidebarDepth ||
sidebarDepth ||
$themeLocaleConfig.sidebarDepth ||
$themeConfig.sidebarDepth
2019-04-15 10:35:40 +08:00
const maxDepth = configDepth == null ? 1 : configDepth
2019-09-20 13:54:13 +08:00
const displayAllHeaders = $themeLocaleConfig.displayAllHeaders ||
$themeConfig.displayAllHeaders
2019-04-15 10:35:40 +08:00
if (item.type === 'auto') {
return [link, renderChildren(h, item.children, item.basePath, $route, maxDepth)]
} else if ((active || displayAllHeaders) && item.headers && !hashRE.test(item.path)) {
const children = groupHeaders(item.headers)
return [link, renderChildren(h, children, item.path, $route, maxDepth)]
} else {
return link
}
}
}
function renderLink (h, to, text, active) {
return h('router-link', {
props: {
to,
activeClass: '',
exactActiveClass: ''
},
class: {
active,
'sidebar-link': true
}
}, text)
}
function renderChildren (h, children, path, route, maxDepth, depth = 1) {
if (!children || depth > maxDepth) return null
return h('ul', { class: 'sidebar-sub-headers' }, children.map(c => {
const active = isActive(route, path + '#' + c.slug)
return h('li', { class: 'sidebar-sub-header' }, [
renderLink(h, path + '#' + c.slug, c.title, active),
renderChildren(h, c.children, path, route, maxDepth, depth + 1)
])
}))
}
</script>
<style lang="stylus">
.sidebar .sidebar-sub-headers
2019-11-08 15:26:54 +08:00
padding-left 1.5rem
2019-04-15 10:35:40 +08:00
font-size 0.95em
2019-11-27 13:22:32 +08:00
.sidebar-sub-headers a.sidebar-link
margin 0 1rem 0 0.6rem
2019-04-15 10:35:40 +08:00
a.sidebar-link
font-size 1em
font-weight 400
2019-11-08 15:26:54 +08:00
display block!important
color var(--text-color)
2019-11-08 15:26:54 +08:00
padding 0.35rem 1rem 0.35rem .75rem
2019-04-15 10:35:40 +08:00
line-height 1.4
2019-11-08 15:26:54 +08:00
margin 0 1rem 0 1.5rem
2019-04-15 10:35:40 +08:00
box-sizing: border-box
2019-11-08 15:26:54 +08:00
border-radius .25rem
2019-04-15 10:35:40 +08:00
&:hover
color $accentColor
&.active
font-weight 600
2019-11-08 15:26:54 +08:00
color #fff
background $accentColor
// border-left-color $accentColor
2019-04-15 10:35:40 +08:00
.sidebar-group &
2019-11-08 15:26:54 +08:00
// padding-left 2rem
2019-04-15 10:35:40 +08:00
.sidebar-sub-headers &
padding-top 0.25rem
padding-bottom 0.25rem
border-left none
&.active
font-weight 500
2019-11-08 15:26:54 +08:00
background transparent
color $accentColor
2019-04-15 10:35:40 +08:00
</style>