Skip to content

Commit 9d16750

Browse files
committed
add context component
1 parent 78e4a51 commit 9d16750

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { LocaleProviderRootContext } from '../modules/root-context.js';
2+
import { type ContextType, type ReactNode, use } from 'react';
3+
4+
export interface LocaleProviderRootContextProps {
5+
children: (locale: ContextType<typeof LocaleProviderRootContext>) => ReactNode;
6+
}
7+
8+
export default function RootContext(props: LocaleProviderRootContextProps) {
9+
const locale = use(LocaleProviderRootContext);
10+
11+
const { children } = props;
12+
13+
return children(locale);
14+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import Root from '../anatomy/root.jsx';
2+
import RootContext from '../anatomy/root-context.jsx';
23

3-
export const LocaleProvider = Root;
4+
export const LocaleProvider = Object.assign(Root, {
5+
Context: RootContext,
6+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script lang="ts" module>
2+
import type { Snippet } from 'svelte';
3+
4+
export interface LocaleProviderRootContextProps {
5+
children: Snippet<[ReturnType<typeof LocaleProviderRootContext.consume>]>;
6+
}
7+
</script>
8+
9+
<script lang="ts">
10+
import { LocaleProviderRootContext } from '../modules/root-context.js';
11+
12+
const props: LocaleProviderRootContextProps = $props();
13+
14+
const locale = LocaleProviderRootContext.consume();
15+
16+
const { children } = $derived(props);
17+
</script>
18+
19+
{@render children(locale)}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import Root from '../anatomy/root.svelte';
2+
import RootContext from '../anatomy/root-context.svelte';
23

3-
export const LocaleProvider = Root;
4+
export const LocaleProvider = Object.assign(Root, {
5+
Context: RootContext,
6+
});

0 commit comments

Comments
 (0)