From 6d64dbc57430f97c0853777bf9cdaff2df693b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schr=C3=B6ter?= Date: Wed, 27 Nov 2024 21:07:54 +0100 Subject: [PATCH] wip --- bun.lockb | Bin 908148 -> 908180 bytes packages/svelte/package.json | 7 ++++ .../components/avatar/examples/basic.svelte | 5 ++- .../components/avatar/use-avatar-context.ts | 4 ++- .../components/avatar/use-avatar.svelte.ts | 3 ++ packages/svelte/src/lib/index.ts | 2 ++ packages/svelte/src/lib/providers/index.ts | 1 + .../providers/locale/examples/basic.svelte | 8 +++++ .../providers/locale/examples/setup.svelte | 5 +++ .../providers/locale/examples/usage.svelte | 7 ++++ .../svelte/src/lib/providers/locale/index.ts | 2 ++ .../providers/locale/locale-provider.svelte | 32 ++++++++++++++++++ .../lib/providers/locale/locale.stories.ts | 21 ++++++++++++ .../providers/locale/use-locale-context.ts | 13 +++++++ .../svelte/src/lib/utils/create-context.ts | 11 ++++-- .../svelte/svelte-kit/src/lib/avatar.svelte | 4 +-- 16 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 packages/svelte/src/lib/providers/index.ts create mode 100644 packages/svelte/src/lib/providers/locale/examples/basic.svelte create mode 100644 packages/svelte/src/lib/providers/locale/examples/setup.svelte create mode 100644 packages/svelte/src/lib/providers/locale/examples/usage.svelte create mode 100644 packages/svelte/src/lib/providers/locale/index.ts create mode 100644 packages/svelte/src/lib/providers/locale/locale-provider.svelte create mode 100644 packages/svelte/src/lib/providers/locale/locale.stories.ts create mode 100644 packages/svelte/src/lib/providers/locale/use-locale-context.ts diff --git a/bun.lockb b/bun.lockb index 5ea8f7a74059c1d8af1f62ac5d26faf66d1be06b..6de47d2642f83db0f6af9f4e794eaf23d05fa196 100755 GIT binary patch delta 5386 zcmeI0e{7Z28OQH!Z|S+c+|hFBz%CUKt7%ILgD|3|o74iUtx(W1r!q8iLnKmAoRq3} z_^SwlMX`q#`B4UO4u>$Q@4zi63Uw973R}|K#$>*eTTV6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6R@~Pc_x%%NSNc`G V@ddH>%sr3$^Z_@a`D}dKzW@r4YAXN$ diff --git a/packages/svelte/package.json b/packages/svelte/package.json index 2806e5180e..331b738059 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -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" @@ -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" } @@ -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" }, diff --git a/packages/svelte/src/lib/components/avatar/examples/basic.svelte b/packages/svelte/src/lib/components/avatar/examples/basic.svelte index b4b5c32a6b..de0faba0e7 100644 --- a/packages/svelte/src/lib/components/avatar/examples/basic.svelte +++ b/packages/svelte/src/lib/components/avatar/examples/basic.svelte @@ -1,8 +1,11 @@ + PA + diff --git a/packages/svelte/src/lib/components/avatar/use-avatar-context.ts b/packages/svelte/src/lib/components/avatar/use-avatar-context.ts index aebd6fd0e1..6de9c72969 100644 --- a/packages/svelte/src/lib/components/avatar/use-avatar-context.ts +++ b/packages/svelte/src/lib/components/avatar/use-avatar-context.ts @@ -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('AvatarContext') +export const [AvatarProvider, useAvatarContext] = createContext({ + key: 'AvatarContext', +}) diff --git a/packages/svelte/src/lib/components/avatar/use-avatar.svelte.ts b/packages/svelte/src/lib/components/avatar/use-avatar.svelte.ts index 45fa66f7ca..a717cf26b5 100644 --- a/packages/svelte/src/lib/components/avatar/use-avatar.svelte.ts +++ b/packages/svelte/src/lib/components/avatar/use-avatar.svelte.ts @@ -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' @@ -8,8 +9,10 @@ export interface UseAvatarProps export interface UseAvatarReturn extends Accessor> {} export const useAvatar = (props: UseAvatarProps = {}) => { + const { dir } = useLocaleContext(DEFAULT_LOCALE) const context = $derived({ id: createId(), + dir, ...props, }) diff --git a/packages/svelte/src/lib/index.ts b/packages/svelte/src/lib/index.ts index cb64ac1b52..6d63b651ae 100644 --- a/packages/svelte/src/lib/index.ts +++ b/packages/svelte/src/lib/index.ts @@ -1 +1,3 @@ export * from './components' +export * from './providers' +export type { Assign, Optional } from './types' diff --git a/packages/svelte/src/lib/providers/index.ts b/packages/svelte/src/lib/providers/index.ts new file mode 100644 index 0000000000..1e26053c0c --- /dev/null +++ b/packages/svelte/src/lib/providers/index.ts @@ -0,0 +1 @@ +export * from './locale' diff --git a/packages/svelte/src/lib/providers/locale/examples/basic.svelte b/packages/svelte/src/lib/providers/locale/examples/basic.svelte new file mode 100644 index 0000000000..8f545feb39 --- /dev/null +++ b/packages/svelte/src/lib/providers/locale/examples/basic.svelte @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/packages/svelte/src/lib/providers/locale/examples/setup.svelte b/packages/svelte/src/lib/providers/locale/examples/setup.svelte new file mode 100644 index 0000000000..b6730b02b8 --- /dev/null +++ b/packages/svelte/src/lib/providers/locale/examples/setup.svelte @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/packages/svelte/src/lib/providers/locale/examples/usage.svelte b/packages/svelte/src/lib/providers/locale/examples/usage.svelte new file mode 100644 index 0000000000..834289c050 --- /dev/null +++ b/packages/svelte/src/lib/providers/locale/examples/usage.svelte @@ -0,0 +1,7 @@ + + +
{JSON.stringify(locale, null, 2)}
\ No newline at end of file diff --git a/packages/svelte/src/lib/providers/locale/index.ts b/packages/svelte/src/lib/providers/locale/index.ts new file mode 100644 index 0000000000..074a1a1c15 --- /dev/null +++ b/packages/svelte/src/lib/providers/locale/index.ts @@ -0,0 +1,2 @@ +export { default as LocaleProvider, type LocaleProviderProps } from './locale-provider.svelte' +export { DEFAULT_LOCALE, useLocaleContext, type UseLocaleContext } from './use-locale-context' diff --git a/packages/svelte/src/lib/providers/locale/locale-provider.svelte b/packages/svelte/src/lib/providers/locale/locale-provider.svelte new file mode 100644 index 0000000000..461a915a29 --- /dev/null +++ b/packages/svelte/src/lib/providers/locale/locale-provider.svelte @@ -0,0 +1,32 @@ + + + + +{@render children()} \ No newline at end of file diff --git a/packages/svelte/src/lib/providers/locale/locale.stories.ts b/packages/svelte/src/lib/providers/locale/locale.stories.ts new file mode 100644 index 0000000000..d982e4472a --- /dev/null +++ b/packages/svelte/src/lib/providers/locale/locale.stories.ts @@ -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, + }), +} diff --git a/packages/svelte/src/lib/providers/locale/use-locale-context.ts b/packages/svelte/src/lib/providers/locale/use-locale-context.ts new file mode 100644 index 0000000000..2adb69a7a5 --- /dev/null +++ b/packages/svelte/src/lib/providers/locale/use-locale-context.ts @@ -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({ + key: 'LocaleContext', +}) diff --git a/packages/svelte/src/lib/utils/create-context.ts b/packages/svelte/src/lib/utils/create-context.ts index b22a8928bf..78f0ba0817 100644 --- a/packages/svelte/src/lib/utils/create-context.ts +++ b/packages/svelte/src/lib/utils/create-context.ts @@ -1,11 +1,16 @@ import { getContext, setContext } from 'svelte' +interface CreateContextOptions { + key?: string +} + type CreateContextReturn = [(opts: T) => void, (fallback?: T) => T, symbol] -export const createContext = (id: string) => { - const contextId = Symbol(id) +export const createContext = (options: CreateContextOptions) => { + const { key } = options + const contextId = Symbol(key) const provider = (value: T) => setContext(contextId, value) - const consumer = () => getContext(contextId) + const consumer = () => getContext(contextId) return [provider, consumer, contextId] as CreateContextReturn } diff --git a/templates/svelte/svelte-kit/src/lib/avatar.svelte b/templates/svelte/svelte-kit/src/lib/avatar.svelte index f51ddc91bc..088a9c3ec1 100644 --- a/templates/svelte/svelte-kit/src/lib/avatar.svelte +++ b/templates/svelte/svelte-kit/src/lib/avatar.svelte @@ -9,7 +9,7 @@ export interface AvatarProps extends AvatarRootProps { - + {getInitials()}