Skip to content
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

feat(svelte): add locale provider #3086

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
7 changes: 7 additions & 0 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"types": "./src/lib/index.ts",
"svelte": "./src/lib/index.ts"
},
"./locale": {
"types": "./src/lib/providers/locale/index.ts",
"svelte": "./src/lib/providers/locale/index.ts"
},
"./*": {
"types": "./src/lib/components/*/index.ts",
"svelte": "./src/lib/components/*/index.ts"
Expand All @@ -42,6 +46,8 @@
"replace": {
"exports.\\..svelte": "./dist/index.js",
"exports.\\..types": "./dist/index.d.ts",
"exports.\\./locale.svelte": "./dist/providers/locale/index.js",
"exports.\\./locale.types": "./dist/providers/locale/index.d.ts",
"exports.\\./*.svelte": "./dist/components/*/index.js",
"exports.\\./*.types": "./dist/components/*/index.d.ts"
}
Expand All @@ -53,6 +59,7 @@
"dependencies": {
"@zag-js/avatar": "0.77.1",
"@zag-js/core": "0.77.1",
"@zag-js/i18n-utils": "0.77.1",
"@zag-js/svelte": "0.77.1",
"nanoid": "5.0.9"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<script lang="ts">
import { Avatar } from '@ark-ui/svelte/avatar'
import { LocaleProvider } from '$lib/providers'
import { Avatar } from '@ark-ui/svelte/avatar'
</script>

<LocaleProvider locale="de-DE">
<Avatar.Root>
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/3000" alt="avatar" />
</Avatar.Root>
</LocaleProvider>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import { createContext } from '$lib/utils/create-context'
import type { UseAvatarReturn } from './use-avatar.svelte'

export interface UseAvatarContext extends UseAvatarReturn {}
export const [AvatarProvider, useAvatarContext] = createContext<UseAvatarContext>('AvatarContext')
export const [AvatarProvider, useAvatarContext] = createContext<UseAvatarContext>({
key: 'AvatarContext',
})
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEFAULT_LOCALE, useLocaleContext } from '$lib/providers/locale'
import type { Accessor, Optional } from '$lib/types'
import { createId } from '$lib/utils/create-id'
import * as avatar from '@zag-js/avatar'
Expand All @@ -8,8 +9,10 @@ export interface UseAvatarProps
export interface UseAvatarReturn extends Accessor<avatar.Api<PropTypes>> {}

export const useAvatar = (props: UseAvatarProps = {}) => {
const { dir } = useLocaleContext(DEFAULT_LOCALE)
const context = $derived({
id: createId(),
dir,
...props,
})

Expand Down
2 changes: 2 additions & 0 deletions packages/svelte/src/lib/index.ts
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'
1 change: 1 addition & 0 deletions packages/svelte/src/lib/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './locale'
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>
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>
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>
2 changes: 2 additions & 0 deletions packages/svelte/src/lib/providers/locale/index.ts
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 { DEFAULT_LOCALE, useLocaleContext, type UseLocaleContext } from './use-locale-context'
32 changes: 32 additions & 0 deletions packages/svelte/src/lib/providers/locale/locale-provider.svelte
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 packages/svelte/src/lib/providers/locale/locale.stories.ts
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,
}),
}
13 changes: 13 additions & 0 deletions packages/svelte/src/lib/providers/locale/use-locale-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createContext } from '$lib/utils/create-context'
import type { Locale } from '@zag-js/i18n-utils'

export const DEFAULT_LOCALE: Locale = {
dir: 'ltr',
locale: 'en-US',
}

export interface UseLocaleContext extends Locale {}

export const [LocaleContextProvider, useLocaleContext] = createContext<UseLocaleContext>({
key: 'LocaleContext',
})
11 changes: 8 additions & 3 deletions packages/svelte/src/lib/utils/create-context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { getContext, setContext } from 'svelte'

interface CreateContextOptions<T> {
key?: string
}

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 = () => getContext<T>(contextId)

return [provider, consumer, contextId] as CreateContextReturn<T>
}
4 changes: 2 additions & 2 deletions templates/svelte/svelte-kit/src/lib/avatar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface AvatarProps extends AvatarRootProps {
</script>

<script lang="ts">
let { src, name }: AvatarProps = $props()
let { src, name, ...rootProps }: AvatarProps = $props()
const getInitials = $derived(() =>
name
Expand All @@ -21,7 +21,7 @@ export interface AvatarProps extends AvatarRootProps {
)
</script>

<Avatar.Root>
<Avatar.Root {...rootProps}>
<Avatar.Fallback>{getInitials()}</Avatar.Fallback>
<Avatar.Image {src} alt={name} />
</Avatar.Root>
Loading