fix: home action link not being reactive (#195) (#212)

Co-authored-by: mariotang <mariotang@tencent.com>
pull/233/head
旧巷馆子 · 沈青川 4 years ago committed by GitHub
parent 6bf0d14930
commit 5678dc3a25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,16 +9,16 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { defineProps } from 'vue' import { defineProps, toRefs } from 'vue'
import type { DefaultTheme } from '../config' import type { DefaultTheme } from '../config'
import { useNavLink } from '../composables/navLink' import { useNavLink } from '../composables/navLink'
import OutboundLink from './icons/OutboundLink.vue' import OutboundLink from './icons/OutboundLink.vue'
const { item } = defineProps<{ const props = defineProps<{
item: DefaultTheme.NavItemWithLink item: DefaultTheme.NavItemWithLink
}>() }>()
const propsRefs = toRefs(props);
const { props: linkProps, isExternal } = useNavLink(item) const { props: linkProps, isExternal } = useNavLink(propsRefs.item)
</script> </script>
<style scoped> <style scoped>

@ -7,16 +7,16 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { defineProps } from 'vue' import { defineProps, toRefs } from 'vue'
import type { DefaultTheme } from '../config' import type { DefaultTheme } from '../config'
import { useNavLink } from '../composables/navLink' import { useNavLink } from '../composables/navLink'
import OutboundLink from './icons/OutboundLink.vue' import OutboundLink from './icons/OutboundLink.vue'
const { item } = defineProps<{ const props = defineProps<{
item: DefaultTheme.NavItemWithLink item: DefaultTheme.NavItemWithLink,
}>() }>();
const propsRefs = toRefs(props);
const { props: linkProps, isExternal } = useNavLink(item) const { props: linkProps, isExternal } = useNavLink(propsRefs.item);
</script> </script>
<style scoped> <style scoped>

@ -1,23 +1,23 @@
import { computed } from 'vue' import { computed, Ref } from 'vue'
import { useRoute } from 'vitepress' import { useRoute } from 'vitepress'
import type { DefaultTheme } from '../config' import type { DefaultTheme } from '../config'
import { isExternal as isExternalCheck } from '../utils' import { isExternal as isExternalCheck } from '../utils'
import { useUrl } from '../composables/url' import { useUrl } from '../composables/url'
export function useNavLink(item: DefaultTheme.NavItemWithLink) { export function useNavLink(item: Ref<DefaultTheme.NavItemWithLink>) {
const route = useRoute() const route = useRoute()
const { withBase } = useUrl() const { withBase } = useUrl()
const isExternal = isExternalCheck(item.link) const isExternal = isExternalCheck(item.value.link)
const props = computed(() => { const props = computed(() => {
const routePath = normalizePath(route.path) const routePath = normalizePath(route.path)
let active = false let active = false
if (item.activeMatch) { if (item.value.activeMatch) {
active = new RegExp(item.activeMatch).test(routePath) active = new RegExp(item.value.activeMatch).test(routePath)
} else { } else {
const itemPath = normalizePath(withBase(item.link)) const itemPath = normalizePath(withBase(item.value.link))
active = active =
itemPath === '/' itemPath === '/'
? itemPath === routePath ? itemPath === routePath
@ -29,10 +29,10 @@ export function useNavLink(item: DefaultTheme.NavItemWithLink) {
active, active,
isExternal isExternal
}, },
href: isExternal ? item.link : withBase(item.link), href: isExternal ? item.value.link : withBase(item.value.link),
target: item.target || isExternal ? `_blank` : null, target: item.value.target || isExternal ? `_blank` : null,
rel: item.rel || isExternal ? `noopener noreferrer` : null, rel: item.value.rel || isExternal ? `noopener noreferrer` : null,
'aria-label': item.ariaLabel 'aria-label': item.value.ariaLabel
} }
}) })

Loading…
Cancel
Save