Skip to content

Commit dbcdcba

Browse files
committed
[WIP]Use Isomorphic effect (#1583)
* 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 <[email protected]>
1 parent 367a6b5 commit dbcdcba

File tree

7 files changed

+26
-8
lines changed

7 files changed

+26
-8
lines changed

.changeset/brown-squids-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/components": patch
3+
---
4+
5+
Add a utility to provide useIsoMorphicEffect function and use that instead of useLayoutEffect everywhere

src/Overlay.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import styled from 'styled-components'
2-
import React, {ReactElement, useEffect, useLayoutEffect, useRef} from 'react'
2+
import React, {ReactElement, useEffect, useRef} from 'react'
33
import {get, COMMON, SystemPositionProps, SystemCommonProps} from './constants'
44
import {ComponentProps} from './utils/types'
5+
import useLayoutEffect from './utils/useIsomorphicLayoutEffect'
56
import {useOverlay, TouchOrMouseEvent} from './hooks'
67
import Portal from './Portal'
78
import sx, {SxProp} from './sx'

src/Portal/Portal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import {createPortal} from 'react-dom'
3+
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'
34

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

72-
React.useLayoutEffect(() => {
73+
useLayoutEffect(() => {
7374
let containerName = _containerName
7475
if (containerName === undefined) {
7576
containerName = DEFAULT_PORTAL_CONTAINER_NAME

src/hooks/useAnchoredPosition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22
import {PositionSettings, getAnchoredPosition, AnchorPosition} from '../behaviors/anchoredPosition'
33
import {useProvidedRefOrCreate} from './useProvidedRefOrCreate'
44
import {useResizeObserver} from './useResizeObserver'
5+
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'
56

67
export interface AnchoredPositionHookSettings extends Partial<PositionSettings> {
78
floatingElementRef?: React.RefObject<Element>
@@ -41,7 +42,7 @@ export function useAnchoredPosition(
4142
[floatingElementRef, anchorElementRef, ...dependencies]
4243
)
4344

44-
React.useLayoutEffect(updatePosition, [updatePosition])
45+
useLayoutEffect(updatePosition, [updatePosition])
4546

4647
useResizeObserver(updatePosition)
4748

src/hooks/useCombinedRefs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, {ForwardedRef, useRef} from 'react'
1+
import {ForwardedRef, useRef} from 'react'
2+
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'
23

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

14-
React.useLayoutEffect(() => {
15+
useLayoutEffect(() => {
1516
function setRefs(current: T | null = null) {
1617
for (const ref of refs) {
1718
if (!ref) {
@@ -32,7 +33,6 @@ export function useCombinedRefs<T>(...refs: (ForwardedRef<T> | null | undefined)
3233
// eslint-disable-next-line react-hooks/exhaustive-deps
3334
setRefs(combinedRef.current)
3435
}
35-
3636
// eslint-disable-next-line react-hooks/exhaustive-deps
3737
}, [...refs, combinedRef.current])
3838

src/hooks/useResizeObserver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React from 'react'
1+
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'
22

33
export function useResizeObserver(callback: () => void) {
4-
React.useLayoutEffect(() => {
4+
useLayoutEffect(() => {
55
const observer = new window.ResizeObserver(() => callback())
66
observer.observe(document.documentElement)
77
return () => {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {useEffect, useLayoutEffect} from 'react'
2+
3+
const useIsomorphicLayoutEffect =
4+
typeof window !== 'undefined' &&
5+
typeof window.document !== 'undefined' &&
6+
typeof window.document.createElement !== 'undefined'
7+
? useLayoutEffect
8+
: useEffect
9+
10+
export default useIsomorphicLayoutEffect

0 commit comments

Comments
 (0)