Skip to content

Commit

Permalink
refactor: env package
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Aug 13, 2022
1 parent fb65a7a commit 239a3cd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .changeset/many-rivers-listen.md
@@ -0,0 +1,6 @@
---
"@chakra-ui/react-env": patch
---

- Omit rendered env span when the environment prop is passed. - Add `id` and
`hidden` attributes to the rendered env span
3 changes: 0 additions & 3 deletions packages/env/package.json
Expand Up @@ -31,9 +31,6 @@
"bugs": {
"url": "https://github.com/chakra-ui/chakra-ui/issues"
},
"dependencies": {
"@chakra-ui/utils": "workspace:*"
},
"peerDependencies": {
"react": ">=18"
},
Expand Down
24 changes: 13 additions & 11 deletions packages/env/src/env.tsx
@@ -1,4 +1,3 @@
import { isBrowser, __DEV__ } from "@chakra-ui/utils"
import {
createContext,
startTransition,
Expand All @@ -20,13 +19,12 @@ const mockEnv = {
document: ssrDocument,
}

const defaultEnv: Environment = isBrowser ? { window, document } : mockEnv
const defaultEnv: Environment =
typeof window !== "undefined" ? { window, document } : mockEnv

const EnvironmentContext = createContext(defaultEnv)

if (__DEV__) {
EnvironmentContext.displayName = "EnvironmentContext"
}
EnvironmentContext.displayName = "EnvironmentContext"

export function useEnvironment() {
return useContext(EnvironmentContext)
Expand All @@ -45,18 +43,24 @@ export function EnvironmentProvider(props: EnvironmentProviderProps) {
useEffect(() => setMounted(true), [])

const context = useMemo(() => {
if (environmentProp) {
return environmentProp
}

const doc = node?.ownerDocument
const win = node?.ownerDocument.defaultView
const nodeEnv = doc ? { document: doc, window: win } : undefined
const env = environmentProp ?? nodeEnv ?? defaultEnv

const env = doc ? { document: doc, window: win } : defaultEnv
return env as Environment
}, [node, environmentProp])

return (
<EnvironmentContext.Provider value={context}>
{children}
{mounted && (
{!environmentProp && mounted && (
<span
id="__chakra_env"
hidden
ref={(el) => {
startTransition(() => {
if (el) setNode(el)
Expand All @@ -68,6 +72,4 @@ export function EnvironmentProvider(props: EnvironmentProviderProps) {
)
}

if (__DEV__) {
EnvironmentProvider.displayName = "EnvironmentProvider"
}
EnvironmentProvider.displayName = "EnvironmentProvider"
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 239a3cd

@vercel
Copy link

@vercel vercel bot commented on 239a3cd Aug 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.