-
Notifications
You must be signed in to change notification settings - Fork 113
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 environment provider
- Loading branch information
1 parent
f6052e7
commit bb76fc4
Showing
13 changed files
with
142 additions
and
2 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
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
39 changes: 39 additions & 0 deletions
39
packages/svelte/src/lib/providers/environment/environment-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,39 @@ | ||
<script module lang="ts"> | ||
import { getDocument, getWindow } from '@zag-js/dom-query' | ||
import type { Snippet } from 'svelte' | ||
import type { RootNode } from './use-environment-context' | ||
export interface EnvironmentProviderProps { | ||
/** | ||
* The root node to use for the environment. | ||
*/ | ||
value?: RootNode | (() => RootNode) | ||
/** | ||
* The children to render. | ||
*/ | ||
children?: Snippet | ||
} | ||
</script> | ||
|
||
<script lang="ts"> | ||
import { EnvironmentContextProvider } from './use-environment-context' | ||
import { runIfFn } from '$lib/utils/run-if-fn' | ||
const { value, children }: EnvironmentProviderProps = $props() | ||
let spanRef: HTMLSpanElement | null = $state(null) | ||
const getRootNode = () => runIfFn(value) ?? spanRef?.ownerDocument ?? document | ||
const environment = $state({ | ||
getRootNode, | ||
getDocument: () => getDocument(getRootNode()), | ||
getWindow: () => getWindow(getRootNode()), | ||
}) | ||
EnvironmentContextProvider(environment) | ||
</script> | ||
|
||
{@render children?.()} | ||
{#if !value} | ||
<span bind:this={spanRef} hidden></span> | ||
{/if} |
14 changes: 14 additions & 0 deletions
14
packages/svelte/src/lib/providers/environment/environment.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,14 @@ | ||
import type { Meta } from '@storybook/svelte' | ||
import BasicExample from './examples/basic.svelte' | ||
|
||
const meta = { | ||
title: 'Providers / Environment', | ||
} as Meta | ||
|
||
export default meta | ||
|
||
export const Basic = { | ||
render: () => ({ | ||
Component: BasicExample, | ||
}), | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/svelte/src/lib/providers/environment/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,10 @@ | ||
<script lang="ts"> | ||
import { EnvironmentProvider } from '@ark-ui/svelte/environment' | ||
import Usage from './usage.svelte' | ||
</script> | ||
|
||
<ifrmae title="IFrame Context"> | ||
<EnvironmentProvider> | ||
<Usage /> | ||
</EnvironmentProvider> | ||
</ifrmae> |
7 changes: 7 additions & 0 deletions
7
packages/svelte/src/lib/providers/environment/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,7 @@ | ||
<script lang="ts"> | ||
import { EnvironmentProvider } from '@ark-ui/svelte/environment' | ||
</script> | ||
|
||
<ifrmae title="IFrame Context"> | ||
<EnvironmentProvider><!-- Your App --></EnvironmentProvider> | ||
</ifrmae> |
7 changes: 7 additions & 0 deletions
7
packages/svelte/src/lib/providers/environment/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 { useEnvironmentContext } from '@ark-ui/svelte/environment' | ||
const environment = useEnvironmentContext() | ||
</script> | ||
|
||
<pre>{JSON.stringify(environment?.getRootNode(), 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,9 @@ | ||
export { | ||
default as EnvironmentProvider, | ||
type EnvironmentProviderProps, | ||
} from './environment-provider.svelte' | ||
export { | ||
useEnvironmentContext, | ||
type EnvironmentContext, | ||
type RootNode, | ||
} from './use-environment-context' |
32 changes: 32 additions & 0 deletions
32
packages/svelte/src/lib/providers/environment/use-environment-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,32 @@ | ||
import { createContext } from '$lib/utils/create-context' | ||
|
||
export type RootNode = ShadowRoot | Document | Node | ||
|
||
export interface EnvironmentContext { | ||
/** | ||
* The root node of the application. | ||
* This is used to determine the window and document objects. | ||
* @default document | ||
*/ | ||
getRootNode(): RootNode | ||
/** | ||
* The document context for the root node. | ||
* @default document | ||
*/ | ||
getDocument(): Document | ||
/** | ||
* The window context for the root node. | ||
* @default window | ||
*/ | ||
getWindow(): Window & typeof globalThis | ||
} | ||
|
||
export const [EnvironmentContextProvider, useEnvironmentContext] = | ||
createContext<EnvironmentContext>({ | ||
key: 'EnvironmentContext', | ||
defaultValue: { | ||
getRootNode: () => document, | ||
getDocument: () => document, | ||
getWindow: () => window, | ||
}, | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type AnyFunction<Arg = unknown, ReturnValue = unknown> = (...args: Arg[]) => ReturnValue | ||
|
||
const isFunction = <T = AnyFunction>(value: unknown): value is T => typeof value === 'function' | ||
|
||
export const runIfFn = <MaybeReturnValue, FunctionArgs>( | ||
valueOrFn: MaybeReturnValue | ((...fnArgs: FunctionArgs[]) => MaybeReturnValue), | ||
...args: FunctionArgs[] | ||
) => | ||
isFunction<AnyFunction<FunctionArgs, MaybeReturnValue>>(valueOrFn) | ||
? valueOrFn(...args) | ||
: (valueOrFn as unknown as MaybeReturnValue) |