1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-24 09:13:24 +01:00

Aligning DOM checking condition to what facebook/react does (#4114)

* feat: add Deno SSR support

* fix: check if DOM available as React does

Co-authored-by: oyzhen <oyzhen@vip.qq.com>
This commit is contained in:
oyzhen 2021-04-01 03:59:17 +08:00 committed by GitHub
parent 4dd8b98f7f
commit efce5373fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -1,7 +1,9 @@
import { useLayoutEffect, useEffect } from 'react'
import { CAN_USE_DOM } from '../utils/environment'
/**
* Prevent warning on SSR by falling back to useEffect when window is not defined
* Prevent warning on SSR by falling back to useEffect when DOM isn't available
*/
export const useIsomorphicLayoutEffect =
typeof window !== 'undefined' ? useLayoutEffect : useEffect
export const useIsomorphicLayoutEffect = CAN_USE_DOM
? useLayoutEffect
: useEffect

View File

@ -27,3 +27,13 @@ export const IS_CHROME =
export const IS_CHROME_LEGACY =
typeof navigator !== 'undefined' &&
/Chrome?\/(?:[0-7][0-5]|[0-6][0-9])/i.test(navigator.userAgent)
/**
* Check if DOM is available as React does internally.
* @see https://github.com/facebook/react/blob/master/packages/shared/ExecutionEnvironment.js
*/
export const CAN_USE_DOM = !!(
typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof window.document.createElement !== 'undefined'
)