-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow users customize ID generation #2959
Conversation
Vue does not currently have a native `useId` helper. However, Nuxt has created their own that they ensure works across the client/server boundary. Now a user can use `provide()` in their app to inject a custom useId generation function which, for Nuxt users, can defer to the one provided by Nuxt.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
What do you think about exposing this as a custom hook instead of a raw Rough example (would need to think through name): <template>
<div>
<Switch v-model="enabled">
Thing
</Switch>
</div>
</template>
<script setup>
import { Switch, provideUseId } from '@headlessui/vue'
provideUseId(() => useId())
let enabled = ref(false)
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like @adamwathan's suggestion so that we don't have to expose a "magic" string.
This is also implemented against the 1.x
branch. Should we also open a PR to do this against latest main
, or is the idea to wait for the useId
hook from Vue itself?
My idea was intended to not introduce an API that, if removed, would break builds once we require Vue v3.5. But we could just as easily keep it around and it do nothing once we've done that. As for Headless UI v2, either option seems fine to me. |
Updated implementation to use |
Thinking about the name here and wondering if there's something better than For example, what do you think of |
Is there any prior art we can reference here for how other popular Vue libraries name custom hooks that do similar things? Maybe @AlexVipond has an idea since he's pretty tuned in to the Vue composition API ecosystem. Another idea barring some established convention we can follow is something like |
TL;DR It's an uncommon enough pattern that there's no widely accepted naming convention, but I went with That's partly because of my own personal naming convention that reserves The Vue docs on this topic recommend |
I'd steer away from |
@AlexVipond Awesome thanks so much man, appreciate the sanity check on this one! |
Resolves #2913
We implement our own ID generation in
@headlessui/vue
because Vue has not historically offered a nativeuseId
helper. However, our implementation uses global state which means that long-running servers do not use fresh IDs for every request. Because of this users will encounter hydration mismatches on subsequent requests. These mismatches weren't warned about until Vue v3.4. Because of this, in the future, Vue v3.5 will be providing a nativeuseId
helper that lets us address this issue. In the meantime, Nuxt v3.10+ has implemented a customuseId
helper that is guaranteed to work across the server/client boundary to eliminate these hydration mismatches.This PR allows users to override our internal ID generation which can then use Nuxt's
useId
composable to ensure that IDs are always fresh, unique, and consistent across the server/client boundary and don't result in hydration mismatches.Warning
This API is a temporary measure and not guaranteed to work long-term. Once Vue v3.5 is out we'll switch to using the offical method for generating IDs.
This new API in Headless UI can be used by calling
provideUseId(…)
with a custom function. For Nuxt users, the function you pass should call theuseId
composable that it provides (no need to import, it's automatic) like so: