Skip to content

Commit cf5a529

Browse files
committed
feat: implement useStableId composable and update ExtensionCardPopover to use it
1 parent 780d521 commit cf5a529

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

packages/components/src/extension-manager/components/ExtensionCardPopover.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import type { ComponentPublicInstance, VNode } from 'vue'
3-
import { computed, ref, toRef, useId } from 'vue'
3+
import { computed, ref, toRef } from 'vue'
4+
import { useStableId } from '../../shared/composables'
45
import { useAsChild, useExtensionCardPopoverPosition } from '../composables'
56
import type { ExtensionCardPopoverPlacement } from '../internal.type'
67
@@ -19,7 +20,7 @@ const props = withDefaults(
1920
},
2021
)
2122
22-
const popoverId = `tr-extension-card-popover-${useId()}`
23+
const popoverId = `tr-extension-card-popover-${useStableId()}`
2324
const triggerRef = ref<HTMLElement | null>(null)
2425
const popoverRef = ref<HTMLElement | null>(null)
2526
const open = ref(false)

packages/components/src/shared/composables/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export * from './useAutoScroll'
33
export * from './useControllableState'
44
export * from './useSenderContentRegistration'
55
export * from './useSlotRefs'
6+
export * from './useStableId'
67
export * from './useTeleportTarget'
78
export * from './useTouchDevice'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as Vue from 'vue'
2+
3+
type VueWithUseId = typeof Vue & {
4+
useId?: () => string
5+
}
6+
7+
const appIdCounters = new WeakMap<object, number>()
8+
9+
export function useStableId() {
10+
const nativeUseId = (Vue as VueWithUseId).useId
11+
if (nativeUseId) {
12+
return nativeUseId()
13+
}
14+
15+
const instance = Vue.getCurrentInstance()
16+
if (!instance) {
17+
return ''
18+
}
19+
20+
const appContext = instance.appContext
21+
const id = appIdCounters.get(appContext) ?? 0
22+
appIdCounters.set(appContext, id + 1)
23+
24+
return `tr-${id}`
25+
}

0 commit comments

Comments
 (0)