Skip to content

Commit d064738

Browse files
P4sca1RobinMalfait
andauthored
use native useId from Vue.js 3.5 when available (#3458)
#3457 Vue 3.5 added a native SSR-safe `useId` function. This PR updates the code to use that function by default if no custom function has been provided. Theoretically this is a breaking change, as versions prior to Vue 3.5 are no longer supported. However, package managers should emit a warning or an error when the peer dependency range is no longer satisfied. --------- Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
1 parent 7004a25 commit d064738

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

packages/@headlessui-vue/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- use native `useId` from Vue.js 3.5 when available ([#3458](https://github.com/tailwindlabs/headlessui/pull/3458))
1113

1214
## [1.7.22] - 2024-05-08
1315

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import { inject, InjectionKey, provide } from 'vue'
2-
3-
let GENERATE_ID: InjectionKey<() => string> = Symbol('headlessui.useid')
1+
import * as Vue from 'vue'
42

3+
let GENERATE_ID: Vue.InjectionKey<() => string> = Symbol('headlessui.useid')
54
let globalId = 0
65

7-
export function useId() {
8-
let generateId = inject(GENERATE_ID, () => {
9-
return `${++globalId}`
10-
})
11-
12-
return generateId()
13-
}
6+
export const useId =
7+
// Prefer Vue's `useId` if it's available.
8+
// @ts-expect-error - `useId` doesn't exist in Vue < 3.5.
9+
Vue.useId ??
10+
function useId() {
11+
let generateId = Vue.inject(GENERATE_ID, () => {
12+
return `${++globalId}`
13+
})
1414

15+
return generateId()
16+
}
1517
/**
16-
* This function allows users to provide a custom ID generator
17-
* as a workaround for the lack of stable SSR IDs in Vue 3.x.
18-
*
19-
* This Nuxt users use the Nuxt provided `useId` function
20-
* which is stable across SSR and client.
18+
* This function allows users to provide a custom ID generator.
2119
*/
2220
export function provideUseId(fn: () => string) {
23-
provide(GENERATE_ID, fn)
21+
Vue.provide(GENERATE_ID, fn)
2422
}

0 commit comments

Comments
 (0)