Skip to content

Commit

Permalink
feat: auto collapse when click on another section (#527)
Browse files Browse the repository at this point in the history
* init pr

* ✨ (aside) restore collapse state on section toggle

Co-authored-by: Yaël GUILLOUX <[email protected]>
  • Loading branch information
bdrtsky and Tahul authored Jun 29, 2021
1 parent d6120be commit b571730
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
44 changes: 33 additions & 11 deletions src/defaultTheme/components/molecules/AsideNavigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
:title="link.title"
:docs="link.children"
:collapse="link.collapse === true"
@toggle="toggleLinks(link)"
/>
<AsideNavigationItem v-else :key="link.to" :docs="[link]" />
</template>
Expand All @@ -75,20 +76,41 @@
</template>

<script>
import { defineComponent } from '@nuxtjs/composition-api'
import { computed, defineComponent, ref, watch, useContext } from '@nuxtjs/composition-api'
export default defineComponent({
computed: {
links() {
const nav = this.$docus.currentNav.value
return nav.links
},
parent() {
return this.$docus.currentNav.value.parent
},
lastRelease() {
return this.$docus.lastRelease?.value
setup() {
const { $docus } = useContext()
const links = ref($docus.currentNav.value.links)
watch(
$docus.currentNav,
newVal => {
links.value = newVal.links
},
{ deep: true }
)
function toggleLinks(link) {
const newLinks = $docus.currentNav.value.links.map(l => {
l = { ...l }
if (link.slug === l.slug) {
l.collapse = !l.collapse
}
return l
})
links.value = newLinks
}
const parent = computed(() => $docus.currentNav.value.parent)
const lastRelease = computed(() => $docus.lastRelease?.value)
return { toggleLinks, links, parent, lastRelease }
}
})
</script>
13 changes: 4 additions & 9 deletions src/defaultTheme/components/molecules/AsideNavigationItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
{{ title }}
</h5>
<ul v-if="!isCollapsed || isActive" class="mb-2 ml-2">
<ul v-if="!collapse || isActive" class="mb-2 ml-2">
<li v-for="doc of docs" :key="doc.to">
<NuxtLink
:to="$contentLocalePath(doc.redirect || doc.to)"
Expand Down Expand Up @@ -67,19 +67,15 @@ export default defineComponent({
default: false
}
},
setup(props) {
setup(props, { emit }) {
const { $docus } = useContext()
const isCollapsed = ref(props.collapse)
const isActive = computed(() => props.docs.some(document => $docus.isLinkActive(document.to)))
const toggle = () => {
if (isActive.value) {
return
}
if (isActive.value) return
isCollapsed.value = !isCollapsed.value
emit('toggle', true)
}
const isDocumentNew = document => {
Expand All @@ -97,7 +93,6 @@ export default defineComponent({
return {
toggle,
isActive,
isCollapsed,
isDocumentNew
}
}
Expand Down

1 comment on commit b571730

@vercel
Copy link

@vercel vercel bot commented on b571730 Jun 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.