Skip to content

Commit

Permalink
[WIP]Use Isomorphic effect (#1583)
Browse files Browse the repository at this point in the history
* Use Isomorphic effect

* Create brown-squids-unite.md

* Fix eslint rules

* Make it typescript

* Add back the eslint disables

* Fix up the tests and make the method more robust

* Fix lint

Co-authored-by: Pavithra Kodmad <pksjce@github.com>
  • Loading branch information
pksjce authored Nov 10, 2021
1 parent 94c6122 commit 24b1ebb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 8 deletions.
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

0 comments on commit 24b1ebb

Please sign in to comment.