-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): new composable - useId() #16792
- Loading branch information
1 parent
05ec5b6
commit 072482b
Showing
10 changed files
with
136 additions
and
46 deletions.
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
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,63 @@ | ||
--- | ||
title: useId composable | ||
desc: What is useId() composable and how you can use it | ||
keys: useId | ||
badge: Quasar v2.15+ | ||
--- | ||
|
||
The `useId()` composable returns an "id" which is a ref() that can be used as a unique identifier to apply to a DOM node attribute. | ||
|
||
Should you supply a function (`getValue` from the typing below) to get the value that the id might have, it will make sure to keep it updated. | ||
|
||
On SSR, it takes into account the process of hydration so that your component won't generate any such errors. | ||
|
||
## Syntax | ||
|
||
```js | ||
import { useId } from 'quasar' | ||
|
||
setup () { | ||
const { id } = useId() | ||
// ... | ||
} | ||
``` | ||
|
||
```js | ||
function useId( | ||
opts?: { | ||
getValue?: () => string | null | undefined, | ||
required?: boolean // default: true | ||
} | ||
): { | ||
id: Ref<string>; | ||
}; | ||
``` | ||
|
||
## Example | ||
|
||
```html | ||
<template> | ||
<div :id="id"> | ||
Some component | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { useId } from 'quasar' | ||
export default { | ||
props: { | ||
for: String | ||
}, | ||
setup () { | ||
const { id } = useId({ | ||
getValue: () => props.for, | ||
required: true | ||
}) | ||
return { id } | ||
} | ||
} | ||
</script> | ||
``` |
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
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
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 was deleted.
Oops, something went wrong.
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,50 @@ | ||
import { ref, computed, watch, onMounted } from 'vue' | ||
|
||
import uid from '../utils/uid.js' | ||
|
||
import { isRuntimeSsrPreHydration } from '../plugins/Platform.js' | ||
|
||
function parseValue (val) { | ||
return val === void 0 || val === null | ||
? null | ||
: val | ||
} | ||
|
||
function getId (val, required) { | ||
return val === void 0 || val === null | ||
? (required === true ? `f_${ uid() }` : null) | ||
: val | ||
} | ||
|
||
/** | ||
* Returns an "id" which is a ref() that can be used as | ||
* a unique identifier to apply to a DOM node attribute. | ||
* | ||
* On SSR, it takes care of generating the id on the client side (only) to | ||
* avoid hydration errors. | ||
*/ | ||
export default function ({ getValue, required = true } = {}) { | ||
if (isRuntimeSsrPreHydration.value === true) { | ||
const id = getValue !== void 0 | ||
? ref(parseValue(getValue())) | ||
: ref(null) | ||
|
||
if (required === true && id.value === null) { | ||
onMounted(() => { | ||
id.value = `f_${ uid() }` // getId(null, true) | ||
}) | ||
} | ||
|
||
if (getValue !== void 0) { | ||
watch(getValue, newId => { | ||
id.value = getId(newId, required) | ||
}) | ||
} | ||
|
||
return id | ||
} | ||
|
||
return getValue !== void 0 | ||
? computed(() => getId(getValue(), required)) | ||
: ref(`f_${ uid() }`) // getId(null, true) | ||
} |
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