Skip to content

Commit

Permalink
fix: add default listFormat locale where LocaleProvider may not be av…
Browse files Browse the repository at this point in the history
…ailable (#6147)

* fix: do not use useListFormat hook in <Translate />

* fix: put list format in try/catch block

* fix: use cache
  • Loading branch information
cngonzalez committed Mar 28, 2024
1 parent 27643c8 commit 3c4d4cf
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/sanity/src/core/hooks/useListFormat.ts
Expand Up @@ -34,6 +34,15 @@ export interface UseListFormatOptions {
* @public
*/
export function useListFormat(options: UseListFormatOptions = {}): Intl.ListFormat {
const currentLocale = useCurrentLocale().id
return intlCache.listFormat(currentLocale, options)
/*
* Certain components using this hook (such as the <Translate/> in toasts)
* may not have access to the LocaleProvider that lets us use useCurrentLocale.
* In that case, we fall back to a default, unobstrusive list format.
*/
try {
const currentLocale = useCurrentLocale().id
return intlCache.listFormat(currentLocale, options)
} catch {
return intlCache.listFormat('en-US', {...options, style: 'narrow'})
}
}

0 comments on commit 3c4d4cf

Please sign in to comment.