diff --git a/plugins/markdown/plugin-markdown-tab/src/client/components/CodeTabs.ts b/plugins/markdown/plugin-markdown-tab/src/client/components/CodeTabs.ts index 2b86ff7bac..806af95231 100644 --- a/plugins/markdown/plugin-markdown-tab/src/client/components/CodeTabs.ts +++ b/plugins/markdown/plugin-markdown-tab/src/client/components/CodeTabs.ts @@ -1,7 +1,15 @@ import type { RequiredSlot } from '@vuepress/helper/client' import { useStorage } from '@vueuse/core' import type { PropType, SlotsType, VNode } from 'vue' -import { defineComponent, h, onMounted, ref, shallowRef, watch } from 'vue' +import { + defineComponent, + h, + onMounted, + ref, + shallowRef, + useId, + watch, +} from 'vue' import type { TabProps } from './Tabs.js' @@ -35,16 +43,6 @@ export const CodeTabs = defineComponent({ required: true, }, - /** - * Code tab id - * - * 代码标签页 id - */ - id: { - type: String, - required: true, - }, - /** * Tab id * @@ -65,6 +63,8 @@ export const CodeTabs = defineComponent({ }>, setup(props, { slots }) { + let ids = props.data.map(() => useId()) + // Index of current active item const activeIndex = ref(props.active) @@ -118,6 +118,16 @@ export const CodeTabs = defineComponent({ return props.active } + // only update ids in dev mode + if (__VUEPRESS_DEV__) { + watch( + () => props.data.length, + () => { + ids = props.data.map(() => useId()) + }, + ) + } + onMounted(() => { activeIndex.value = getInitialIndex() @@ -152,7 +162,7 @@ export const CodeTabs = defineComponent({ }, 'class': ['vp-code-tab-nav', { active: isActive }], 'role': 'tab', - 'aria-controls': `codetab-${props.id}-${index}`, + 'aria-controls': ids[index], 'aria-selected': isActive, 'onClick': () => { activeIndex.value = index @@ -173,7 +183,7 @@ export const CodeTabs = defineComponent({ 'div', { 'class': ['vp-code-tab', { active: isActive }], - 'id': `codetab-${props.id}-${index}`, + 'id': ids[index], 'role': 'tabpanel', 'aria-expanded': isActive, }, diff --git a/plugins/markdown/plugin-markdown-tab/src/client/components/Tabs.ts b/plugins/markdown/plugin-markdown-tab/src/client/components/Tabs.ts index 8e1f0d4aef..7537e2daea 100644 --- a/plugins/markdown/plugin-markdown-tab/src/client/components/Tabs.ts +++ b/plugins/markdown/plugin-markdown-tab/src/client/components/Tabs.ts @@ -1,7 +1,15 @@ import type { RequiredSlot } from '@vuepress/helper/client' import { useStorage } from '@vueuse/core' import type { PropType, SlotsType, VNode } from 'vue' -import { defineComponent, h, onMounted, ref, shallowRef, watch } from 'vue' +import { + defineComponent, + h, + onMounted, + ref, + shallowRef, + useId, + watch, +} from 'vue' import '../styles/tabs.css' @@ -37,16 +45,6 @@ export const Tabs = defineComponent({ required: true, }, - /** - * Tab id - * - * 标签页 id - */ - id: { - type: String, - required: true, - }, - /** * Tab id * @@ -67,6 +65,7 @@ export const Tabs = defineComponent({ }>, setup(props, { slots }) { + let ids = props.data.map(() => useId()) // Index of current active item const activeIndex = ref(props.active) @@ -119,6 +118,16 @@ export const Tabs = defineComponent({ return props.active } + // only update ids in dev mode + if (__VUEPRESS_DEV__) { + watch( + () => props.data.length, + () => { + ids = props.data.map(() => useId()) + }, + ) + } + onMounted(() => { activeIndex.value = getInitialIndex() @@ -153,7 +162,7 @@ export const Tabs = defineComponent({ }, 'class': ['vp-tab-nav', { active: isActive }], 'role': 'tab', - 'aria-controls': `tab-${props.id}-${index}`, + 'aria-controls': ids[index], 'aria-selected': isActive, 'onClick': () => { activeIndex.value = index @@ -174,7 +183,7 @@ export const Tabs = defineComponent({ 'div', { 'class': ['vp-tab', { active: isActive }], - 'id': `tab-${props.id}-${index}`, + 'id': ids[index], 'role': 'tabpanel', 'aria-expanded': isActive, }, diff --git a/plugins/markdown/plugin-markdown-tab/src/node/codeTabs.ts b/plugins/markdown/plugin-markdown-tab/src/node/codeTabs.ts index 8d085b3c2b..c9f3342f6f 100644 --- a/plugins/markdown/plugin-markdown-tab/src/node/codeTabs.ts +++ b/plugins/markdown/plugin-markdown-tab/src/node/codeTabs.ts @@ -22,7 +22,7 @@ export const codeTabs: PluginSimple = (md) => { return { id } }) - return ` { }) return `\ - Should render multiple block 1`] = ` -" +"