Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use native useId from Vue.js 3.5 when available #3458

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/@headlessui-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- use native `useId` from Vue.js 3.5 when available ([#3458](https://github.com/tailwindlabs/headlessui/pull/3458))

## [1.7.22] - 2024-05-08

Expand Down
30 changes: 14 additions & 16 deletions packages/@headlessui-vue/src/hooks/use-id.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { inject, InjectionKey, provide } from 'vue'

let GENERATE_ID: InjectionKey<() => string> = Symbol('headlessui.useid')
import * as Vue from 'vue'

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

export function useId() {
let generateId = inject(GENERATE_ID, () => {
return `${++globalId}`
})

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

return generateId()
}
/**
* This function allows users to provide a custom ID generator
* as a workaround for the lack of stable SSR IDs in Vue 3.x.
*
* This Nuxt users use the Nuxt provided `useId` function
* which is stable across SSR and client.
* This function allows users to provide a custom ID generator.
*/
export function provideUseId(fn: () => string) {
provide(GENERATE_ID, fn)
Vue.provide(GENERATE_ID, fn)
}
Loading