Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Isomorphic effect #1583

Merged
merged 7 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-squids-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Add a utility to provide useIsoMorphicEffect function and use that instead of useLayoutEffect everywhere
3 changes: 2 additions & 1 deletion src/Overlay.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import styled from 'styled-components'
import React, {ReactElement, useEffect, useLayoutEffect, useRef} from 'react'
import React, {ReactElement, useEffect, useRef} from 'react'
import {get, COMMON, SystemPositionProps, SystemCommonProps} from './constants'
import {ComponentProps} from './utils/types'
import useLayoutEffect from './utils/useIsomorphicLayoutEffect'
import {useOverlay, TouchOrMouseEvent} from './hooks'
import Portal from './Portal'
import sx, {SxProp} from './sx'
Expand Down
3 changes: 2 additions & 1 deletion src/Portal/Portal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import {createPortal} from 'react-dom'
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'

const PRIMER_PORTAL_ROOT_ID = '__primerPortalRoot__'
const DEFAULT_PORTAL_CONTAINER_NAME = '__default__'
Expand Down Expand Up @@ -69,7 +70,7 @@ export const Portal: React.FC<PortalProps> = ({children, onMount, containerName:
hostElement.style.zIndex = '1'
const elementRef = React.useRef(hostElement)

React.useLayoutEffect(() => {
useLayoutEffect(() => {
let containerName = _containerName
if (containerName === undefined) {
containerName = DEFAULT_PORTAL_CONTAINER_NAME
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useAnchoredPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import {PositionSettings, getAnchoredPosition, AnchorPosition} from '../behaviors/anchoredPosition'
import {useProvidedRefOrCreate} from './useProvidedRefOrCreate'
import {useResizeObserver} from './useResizeObserver'
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'

export interface AnchoredPositionHookSettings extends Partial<PositionSettings> {
floatingElementRef?: React.RefObject<Element>
Expand Down Expand Up @@ -41,7 +42,7 @@ export function useAnchoredPosition(
[floatingElementRef, anchorElementRef, ...dependencies]
)

React.useLayoutEffect(updatePosition, [updatePosition])
useLayoutEffect(updatePosition, [updatePosition])

useResizeObserver(updatePosition)

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useCombinedRefs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {ForwardedRef, useRef} from 'react'
import {ForwardedRef, useRef} from 'react'
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'

/**
* Creates a ref by combining multiple constituent refs. The ref returned by this hook
Expand All @@ -11,7 +12,7 @@ import React, {ForwardedRef, useRef} from 'react'
export function useCombinedRefs<T>(...refs: (ForwardedRef<T> | null | undefined)[]) {
const combinedRef = useRef<T | null>(null)

React.useLayoutEffect(() => {
useLayoutEffect(() => {
function setRefs(current: T | null = null) {
for (const ref of refs) {
if (!ref) {
Expand All @@ -32,7 +33,6 @@ export function useCombinedRefs<T>(...refs: (ForwardedRef<T> | null | undefined)
// eslint-disable-next-line react-hooks/exhaustive-deps
setRefs(combinedRef.current)
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [...refs, combinedRef.current])

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useResizeObserver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'

export function useResizeObserver(callback: () => void) {
React.useLayoutEffect(() => {
useLayoutEffect(() => {
const observer = new window.ResizeObserver(() => callback())
observer.observe(document.documentElement)
return () => {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/useIsomorphicLayoutEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {useEffect, useLayoutEffect} from 'react'

const useIsomorphicLayoutEffect =
typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof window.document.createElement !== 'undefined'
? useLayoutEffect
: useEffect

export default useIsomorphicLayoutEffect