Skip to content

Commit

Permalink
fix: do not use useLayoutEffect on SSR (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
theo-mesnil authored Jul 31, 2023
1 parent 47a49d7 commit eacb9a5
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/Field/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"@welcome-ui/field-group": "^5.0.0-alpha.41",
"@welcome-ui/hint": "^5.0.0-alpha.40",
"@welcome-ui/label": "^5.0.0-alpha.41",
"@welcome-ui/system": "^5.0.0-alpha.40"
"@welcome-ui/system": "^5.0.0-alpha.40",
"@welcome-ui/utils": "^5.0.0-alpha.37"
},
"peerDependencies": {
"@xstyled/styled-components": "^3.7.3",
Expand Down
5 changes: 3 additions & 2 deletions packages/Field/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { Fragment, useLayoutEffect } from 'react'
import React, { Fragment } from 'react'
import { Label } from '@welcome-ui/label'
import { Hint } from '@welcome-ui/hint'
import { CreateWuiProps, forwardRef } from '@welcome-ui/system'
import { useIsomorphicLayoutEffect } from '@welcome-ui/utils'

// Fields
import { RowContainer } from './layout'
Expand Down Expand Up @@ -66,7 +67,7 @@ export const Field = forwardRef<'div', FieldProps>(
...(isGroup ? { flexDirection: layout } : {}),
})

useLayoutEffect(() => {
useIsomorphicLayoutEffect(() => {
Object.keys(children.props).forEach(prop => {
if (forwardedProps.includes(prop)) {
const element = document.getElementById(htmlFor)
Expand Down
1 change: 1 addition & 0 deletions packages/Popover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@welcome-ui/button": "^5.0.0-alpha.40",
"@welcome-ui/icons": "^5.0.0-alpha.41",
"@welcome-ui/system": "^5.0.0-alpha.40",
"@welcome-ui/utils": "^5.0.0-alpha.37",
"reakit": "^1.3.11"
},
"peerDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/Popover/src/Trigger.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CreateWuiProps, forwardRef } from '@welcome-ui/system'
import React, { useLayoutEffect, useRef } from 'react'
import React, { useRef } from 'react'
import { useIsomorphicLayoutEffect } from '@welcome-ui/utils'

import { UsePopoverStateReturn } from './usePopoverState'
import * as S from './styles'
Expand Down Expand Up @@ -48,7 +49,7 @@ export const Trigger = forwardRef<'button', TriggerProps>(({ as, state, ...rest
}
}

useLayoutEffect(() => {
useIsomorphicLayoutEffect(() => {
const disclosure = disclosureRef.current
if (hoverable && disclosure) {
// add listeners on mount
Expand Down
6 changes: 4 additions & 2 deletions packages/Tabs/src/ActiveBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useLayoutEffect, useState } from 'react'
import React, { useState } from 'react'
import { useViewportSize } from '@welcome-ui/utils'
import { TabStateReturn } from 'reakit'
import { useIsomorphicLayoutEffect } from '@welcome-ui/utils'

import * as S from './styles'

Expand All @@ -17,7 +18,8 @@ function useActiveBarState(
): ActiveBarStateReturn {
const [state, setState] = useState({})
const { height: viewportHeight, width: viewportWidth } = useViewportSize()
useLayoutEffect(() => {

useIsomorphicLayoutEffect(() => {
const list = listRef.current
if (!list || !activeTab) return

Expand Down
1 change: 1 addition & 0 deletions packages/Tooltip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"dependencies": {
"@welcome-ui/box": "^5.0.0-alpha.40",
"@welcome-ui/system": "^5.0.0-alpha.40",
"@welcome-ui/utils": "^5.0.0-alpha.37",
"popper.js": "^1.16.1",
"reakit": "^1.3.11"
},
Expand Down
12 changes: 3 additions & 9 deletions packages/Tooltip/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import React, {
cloneElement,
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react'
import React, { cloneElement, useCallback, useEffect, useRef, useState } from 'react'
import Popper, { Placement } from 'popper.js'
import { TooltipReference, useTooltipState } from 'reakit'
import { useDialogState } from 'reakit'
import { CreateWuiProps, forwardRef } from '@welcome-ui/system'
import { Box } from '@welcome-ui/box'
import { useIsomorphicLayoutEffect } from '@welcome-ui/utils'

import * as S from './styles'

Expand Down Expand Up @@ -49,7 +43,7 @@ const useMouseTooltipState = ({
}
}, [originalPlacement, dialog.visible])

useLayoutEffect(() => {
useIsomorphicLayoutEffect(() => {
createPopper()
return () => {
if (popper.current) {
Expand Down
6 changes: 4 additions & 2 deletions packages/Utils/src/use-viewport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useLayoutEffect, useState } from 'react'
import { useState } from 'react'

import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect'

type Size = {
height: number
Expand All @@ -8,7 +10,7 @@ type Size = {
export function useViewportSize(): Size {
const [size, setSize] = useState<Size>({ height: undefined, width: undefined })

useLayoutEffect(() => {
useIsomorphicLayoutEffect(() => {
setSize({ width: window.innerWidth, height: window.innerHeight })

function handleResize() {
Expand Down

0 comments on commit eacb9a5

Please sign in to comment.