Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
fix: avoid SSR warning on Text
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Nov 23, 2019
1 parent c003e6b commit 38af635
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/shared/core/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ function ellipsis({ lines = Infinity }) {
`
}

const isServer = typeof window === 'undefined'

function useUniversalLayoutEffect(callback, deps) {
return isServer
? React.useEffect(callback, deps)
: React.useLayoutEffect(callback, deps)
}

export const TEXT_VARIANTS = [
'h1',
'h2',
Expand All @@ -40,7 +48,7 @@ export const Text = createComponent({
const As = asProp || (variantConfig && variantConfig.defaultAs) || 'span'
const localRef = React.useRef()
const [height, setHeight] = React.useState(null)
React.useLayoutEffect(() => {
useUniversalLayoutEffect(() => {
if (!window.getComputedStyle || lines === Infinity) return
const style = window.getComputedStyle(localRef.current)
const lineHeight = parseInt(style.lineHeight, 10)
Expand Down

0 comments on commit 38af635

Please sign in to comment.