-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(website): revise environment doc
- Loading branch information
1 parent
986582c
commit 28b2b06
Showing
19 changed files
with
121 additions
and
66 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/react/src/providers/environment/examples/setup.tsx
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 @@ | ||
import Frame from 'react-frame-component' | ||
import { Environment } from '../' | ||
|
||
export const App = () => { | ||
return ( | ||
<Frame title="IFrame Context"> | ||
<Environment>{/* Your App */}</Environment> | ||
</Frame> | ||
) | ||
} |
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 @@ | ||
import { useEnvironmentContext } from '../use-environment-context' | ||
|
||
export const Usage = () => { | ||
const { getRootNode } = useEnvironmentContext() | ||
|
||
return <pre>{JSON.stringify(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 @@ | ||
import { Environment } from '../..' | ||
|
||
export const App = () => { | ||
return ( | ||
<iframe title="IFrame Context"> | ||
<Environment>{/* Your App */}</Environment> | ||
</iframe> | ||
) | ||
} |
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 @@ | ||
import { useEnvironmentContext } from '../use-environment-context' | ||
|
||
export const Usage = () => { | ||
const environment = useEnvironmentContext() | ||
|
||
return <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
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,10 +1,10 @@ | ||
<script setup lang="ts"> | ||
import Environment from '../environment.vue' | ||
import PrintEnvironment from './print-environment.vue' | ||
import Usage from './usage.vue' | ||
</script> | ||
|
||
<template> | ||
<Environment> | ||
<PrintEnvironment /> | ||
<Usage /> | ||
</Environment> | ||
</template> |
9 changes: 0 additions & 9 deletions
9
packages/vue/src/providers/environment/examples/print-environment.vue
This file was deleted.
Oops, something went wrong.
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 @@ | ||
<script setup lang="ts"> | ||
import { Environment } from '../' | ||
</script> | ||
|
||
<template> | ||
<iframe title="IFrame Context"> | ||
<Environment><!-- Your App --></Environment> | ||
</iframe> | ||
</template> |
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 @@ | ||
<script setup lang="ts"> | ||
import { useEnvironmentContext } from '../' | ||
const environment = useEnvironmentContext() | ||
</script> | ||
|
||
<template> | ||
<pre>{{ JSON.stringify(environment?.getRootNode(), null, 2) }}</pre> | ||
</template> |
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
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 |
---|---|---|
|
@@ -19,4 +19,3 @@ Building a company-wide design system or a component library for a client has ne | |
## FAQ | ||
|
||
<Faq /> | ||
|
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,50 +1,45 @@ | ||
--- | ||
id: environment | ||
title: Environment | ||
description: Setup components for a custom environment like IFrames, Shadow DOM or an | ||
Electron context. | ||
description: Setup components for custom environments like IFrames, Shadow DOM, or Electron contexts. | ||
--- | ||
|
||
## Explanation | ||
## Motivation | ||
|
||
Internally, we use | ||
[Zag.js](https://zagjs.com/overview/composition#custom-window-environment), | ||
which uses DOM query methods like `document.querySelectorAll` and | ||
`document.getElementById` to locate elements within the machine. | ||
We use [Zag.js](https://zagjs.com/overview/composition#custom-window-environment) internally, | ||
which relies on DOM query methods like `document.querySelectorAll` and `document.getElementById` to locate elements. | ||
In custom environments such as iframes, Shadow DOM, or Electron, these methods might not work as expected because `document` may not be available. | ||
|
||
In custom environments like iframe, Shadow DOM, Electron, etc., the machine | ||
might not work as expected because `document` may not be available. | ||
To handle this, you can set a default using the `Environment` component. This ensures the correct root node or document is referenced. | ||
|
||
To provide the correct reference to root node or document, you can pass | ||
`getRootNode` function either to all Ark UI root components, or provide a | ||
default with this Environment component. | ||
## Setup | ||
|
||
> In shadow DOM, the root node can be derived from calling | ||
> `element.getRootNode()` method on any element. | ||
To target custom environments like an iframe, Shadow DOM, or Electron, render the `Environment` component to provide the environment context to all Ark UI components. | ||
|
||
## Examples | ||
<Example id="setup" /> | ||
|
||
When targeting a custom environment like an iframe, Shadow DOM or Electron, you | ||
can render the Environment component to provide the environment context to all | ||
Ark UI child components. | ||
The `Environment` component will automatically detect the current environment and set the correct environment context. | ||
However, you can also manually set the `Document` like shown in this React example below: | ||
|
||
```tsx | ||
import { Environment, useEnvironmentContext } from '@ark-ui/react' | ||
import { useCallback, type ReactNode } from 'react' | ||
```jsx | ||
import Frame, { FrameContextConsumer } from 'react-frame-component' | ||
import { Environment } from '@ark-ui/react' | ||
|
||
export const App = (props: { children: ReactNode }) => { | ||
const getRootNode = useCallback(() => document, []) | ||
return <Environment value={getRootNode}>{props.children}</Environment> | ||
} | ||
export const App = () => ( | ||
<Frame title="IFrame Context"> | ||
<FrameContextConsumer> | ||
{({ document }) => <Environment value={document}>{/* Your App */}</Environment>} | ||
</FrameContextConsumer> | ||
</Frame> | ||
) | ||
``` | ||
|
||
### Consuming the environment context | ||
## Usage | ||
|
||
You can use the `useEnvironmentContext` hook to consume the environment context. | ||
Use the `useEnvironmentContext` hook to access the `RootNode`, `Document`, and `Window`. | ||
|
||
It will return `undefined` by default, if the Environment component is not | ||
rendered as a parent. | ||
<Example id="usage" /> | ||
|
||
```tsx | ||
const { getRootNode, getWindow, getDocument } = useEnvironmentContext() | ||
``` | ||
## API Reference | ||
|
||
<ComponentTypes id="environment" /> |