-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vue): useId() collisions (#12112)
- Loading branch information
1 parent
34d7952
commit f9dd942
Showing
7 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/vue': patch | ||
--- | ||
|
||
Fixes a case where IDs generated by `useId()` (introduced in Vue 3.5) would not be unique between islands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const contexts = new WeakMap(); | ||
|
||
const ID_PREFIX = 'v'; | ||
|
||
function getContext(rendererContextResult) { | ||
if (contexts.has(rendererContextResult)) { | ||
return contexts.get(rendererContextResult); | ||
} | ||
const ctx = { | ||
currentIndex: 0, | ||
get id() { | ||
return ID_PREFIX + this.currentIndex.toString(); | ||
}, | ||
}; | ||
contexts.set(rendererContextResult, ctx); | ||
return ctx; | ||
} | ||
|
||
export function incrementId(rendererContextResult) { | ||
const ctx = getContext(rendererContextResult); | ||
const id = ctx.id; | ||
ctx.currentIndex++; | ||
return id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/integrations/vue/test/fixtures/basics/src/components/WithId.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script setup> | ||
import { useId } from "vue" | ||
const id = useId() | ||
</script> | ||
|
||
<template> | ||
<p class="vue-use-id" :id="id">{{ id }}</p> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters