-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(svelte): add locale provider (#3086)
- Loading branch information
1 parent
afaecc8
commit 8f786c1
Showing
17 changed files
with
123 additions
and
19 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
2 changes: 1 addition & 1 deletion
2
packages/svelte/src/lib/components/avatar/examples/basic.svelte
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './components' | ||
export * from './providers' | ||
export type { Assign, Optional } from './types' |
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 @@ | ||
export * from './locale' |
8 changes: 8 additions & 0 deletions
8
packages/svelte/src/lib/providers/locale/examples/basic.svelte
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,8 @@ | ||
<script lang="ts"> | ||
import { LocaleProvider } from '@ark-ui/svelte/locale' | ||
import Usage from './usage.svelte' | ||
</script> | ||
|
||
<LocaleProvider locale="ar-BH"> | ||
<Usage /> | ||
</LocaleProvider> |
5 changes: 5 additions & 0 deletions
5
packages/svelte/src/lib/providers/locale/examples/setup.svelte
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,5 @@ | ||
<script lang="ts"> | ||
import { LocaleProvider } from '@ark-ui/svelte/locale' | ||
</script> | ||
|
||
<LocaleProvider locale="de-DE"><!-- Your App --></LocaleProvider> |
7 changes: 7 additions & 0 deletions
7
packages/svelte/src/lib/providers/locale/examples/usage.svelte
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,7 @@ | ||
<script lang="ts"> | ||
import { useLocaleContext } from '@ark-ui/svelte/locale' | ||
const locale = useLocaleContext() | ||
</script> | ||
|
||
<pre>{JSON.stringify(locale, null, 2)}</pre> |
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,2 @@ | ||
export { default as LocaleProvider, type LocaleProviderProps } from './locale-provider.svelte' | ||
export { useLocaleContext, type UseLocaleContext } from './use-locale-context' |
32 changes: 32 additions & 0 deletions
32
packages/svelte/src/lib/providers/locale/locale-provider.svelte
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,32 @@ | ||
<script module lang="ts"> | ||
import type { Locale } from '@zag-js/i18n-utils' | ||
import type { Snippet } from 'svelte' | ||
export interface LocaleProviderProps { | ||
/** | ||
* The locale to use for the application. | ||
* @default 'en-US' | ||
*/ | ||
locale: string | ||
/** | ||
* The children to render. | ||
*/ | ||
children?: Snippet | ||
} | ||
</script> | ||
|
||
<script lang="ts"> | ||
import { isRTL } from '@zag-js/i18n-utils' | ||
import { LocaleContextProvider } from './use-locale-context' | ||
let { locale, children }: LocaleProviderProps = $props() | ||
const context = $state<Locale>({ | ||
locale, | ||
dir: isRTL(locale) ? 'rtl' : 'ltr', | ||
}) | ||
LocaleContextProvider(context) | ||
</script> | ||
|
||
{@render children?.()} |
21 changes: 21 additions & 0 deletions
21
packages/svelte/src/lib/providers/locale/locale.stories.ts
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,21 @@ | ||
import type { Meta } from '@storybook/svelte' | ||
import BasicExample from './examples/basic.svelte' | ||
import SetupExample from './examples/setup.svelte' | ||
|
||
const meta = { | ||
title: 'Providers / Locale', | ||
} as Meta | ||
|
||
export default meta | ||
|
||
export const Basic = { | ||
render: () => ({ | ||
Component: BasicExample, | ||
}), | ||
} | ||
|
||
export const Setup = { | ||
render: () => ({ | ||
Component: SetupExample, | ||
}), | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/svelte/src/lib/providers/locale/use-locale-context.ts
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,9 @@ | ||
import { createContext } from '$lib/utils/create-context' | ||
import type { Locale } from '@zag-js/i18n-utils' | ||
|
||
export interface UseLocaleContext extends Locale {} | ||
|
||
export const [LocaleContextProvider, useLocaleContext] = createContext<UseLocaleContext>({ | ||
key: 'LocaleContext', | ||
defaultValue: { dir: 'ltr', locale: 'en-US' }, | ||
}) |
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,11 +1,17 @@ | ||
import { getContext, setContext } from 'svelte' | ||
import { getContext, hasContext, setContext } from 'svelte' | ||
|
||
interface CreateContextOptions<T> { | ||
key?: string | ||
defaultValue?: T | ||
} | ||
|
||
type CreateContextReturn<T> = [(opts: T) => void, (fallback?: T) => T, symbol] | ||
|
||
export const createContext = <T>(id: string) => { | ||
const contextId = Symbol(id) | ||
export const createContext = <T>(options: CreateContextOptions<T>) => { | ||
const { key } = options | ||
const contextId = Symbol(key) | ||
const provider = (value: T) => setContext(contextId, value) | ||
const consumer = () => getContext(contextId) | ||
const consumer = () => (hasContext(contextId) ? getContext(contextId) : options.defaultValue) | ||
|
||
return [provider, consumer, contextId] as CreateContextReturn<T> | ||
} |
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,12 +1,11 @@ | ||
<script> | ||
import Avatar from '$lib/avatar.svelte' | ||
import Avatar from '$lib/avatar.svelte' | ||
</script> | ||
|
||
<main> | ||
<h1>Welcome to Ark UI</h1> | ||
<Avatar | ||
name="Christian Schröter" | ||
src="https://avatars.githubusercontent.com/u/1846056?s=400&u=bc2821d6154517e6f62795b11ffe0e8e001764a5&v=4" | ||
/> | ||
|
||
name="Christian Schröter" | ||
src="https://avatars.githubusercontent.com/u/1846056?s=400&u=bc2821d6154517e6f62795b11ffe0e8e001764a5&v=4" | ||
/> | ||
</main> |