-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vue,nuxt): Add function to dynamically update Clerk options (#5235)
- Loading branch information
1 parent
382c302
commit 54a3b5b
Showing
10 changed files
with
127 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
"@clerk/nuxt": minor | ||
"@clerk/vue": minor | ||
--- | ||
|
||
Introduce `updateClerkOptions()` utility function to update Clerk options on the fly. | ||
|
||
Usage: | ||
|
||
```vue | ||
<script setup> | ||
import { updateClerkOptions } from '@clerk/vue' | ||
import { dark } from '@clerk/themes' | ||
import { frFR } from '@clerk/localizations' | ||
function enableDarkTheme() { | ||
updateClerkOptions({ | ||
appearance: { | ||
baseTheme: dark | ||
} | ||
}) | ||
} | ||
function changeToFrench() { | ||
updateClerkOptions({ | ||
localization: frFR | ||
}) | ||
} | ||
</script> | ||
<template> | ||
<button @click="enableDarkTheme">Enable Dark Theme</button> | ||
<button @click="changeToFrench">Change to French</button> | ||
</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
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
25 changes: 25 additions & 0 deletions
25
integration/templates/vue-vite/src/components/LanguagePicker.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,25 @@ | ||
<script setup lang="ts"> | ||
import { updateClerkOptions } from '@clerk/vue'; | ||
const onChange = async (event: Event) => { | ||
const value = (event.target as HTMLSelectElement).value; | ||
let localization: Record<string, any> | undefined; | ||
if (value === 'fr') { | ||
localization = (await import('@clerk/localizations/fr-FR')).frFR; | ||
} else { | ||
localization = undefined; | ||
} | ||
updateClerkOptions({ | ||
localization, | ||
}); | ||
}; | ||
</script> | ||
|
||
<template> | ||
<select @change="onChange"> | ||
<option value="en">English</option> | ||
<option value="fr">French</option> | ||
</select> | ||
</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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { createRouteMatcher } from './routeMatcher'; | ||
export { updateClerkOptions } from '@clerk/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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './childrenUtils'; | ||
export * from './toComputedRefs'; | ||
export * from './useClerkLoaded'; | ||
export * from './updateClerkOptions'; |
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,31 @@ | ||
import type { ClerkOptions } from '@clerk/types'; | ||
|
||
type ClerkUpdateOptions = Pick<ClerkOptions, 'appearance' | 'localization'>; | ||
|
||
/** | ||
* Updates Clerk's options at runtime. | ||
* | ||
* @param options - The Clerk options to update | ||
* | ||
* @example | ||
* import { frFR } from '@clerk/localizations'; | ||
* import { dark } from '@clerk/themes'; | ||
* | ||
* updateClerkOptions({ | ||
* appearance: { baseTheme: dark }, | ||
* localization: frFR | ||
* }); | ||
*/ | ||
export function updateClerkOptions(options: ClerkUpdateOptions) { | ||
if (!window.Clerk) { | ||
throw new Error('Missing Clerk instance'); | ||
} | ||
|
||
// @ts-expect-error - `__unstable__updateProps` is not exposed as public API from `@clerk/types` | ||
void window.Clerk.__unstable__updateProps({ | ||
options: { | ||
localization: options.localization, | ||
}, | ||
appearance: options.appearance, | ||
}); | ||
} |