Skip to content

Commit

Permalink
fix(PageLayout): update useLayoutEffect to isomorphic effect (#2432)
Browse files Browse the repository at this point in the history
* fix(PageLayout): update useLayoutEffect to isomorphic effect

* refactor(useResponsiveValue): use fallback value during SSR

* chore: add changeset
  • Loading branch information
joshblack committed Oct 17, 2022
1 parent a077d9c commit 99309e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-icons-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Update useStickyPaneHeight and useMedia to not warn during SSR
3 changes: 2 additions & 1 deletion src/PageLayout/useStickyPaneHeight.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import {useInView} from 'react-intersection-observer'
import {canUseDOM} from '../utils/environment'
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'

/**
* Calculates the height of the sticky pane such that it always
Expand Down Expand Up @@ -49,7 +50,7 @@ export function useStickyPaneHeight() {
// if the pane is sticky.
const [isEnabled, setIsEnabled] = React.useState(false)

React.useLayoutEffect(() => {
useLayoutEffect(() => {
const scrollContainer = getScrollContainer(rootRef.current)

if (isEnabled && (contentTopInView || contentBottomInView)) {
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useResponsiveValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export function isResponsiveValue(value: any): value is ResponsiveValue<any> {
export function useResponsiveValue<T, F>(value: T, fallback: F): FlattenResponsiveValue<T> | F {
// Check viewport size
// TODO: What is the performance cost of creating media query listeners in this hook?
const isNarrowViewport = useMedia(viewportRanges.narrow)
const isRegularViewport = useMedia(viewportRanges.regular)
const isWideViewport = useMedia(viewportRanges.wide)
const isNarrowViewport = useMedia(viewportRanges.narrow, false)
const isRegularViewport = useMedia(viewportRanges.regular, false)
const isWideViewport = useMedia(viewportRanges.wide, false)

if (isResponsiveValue(value)) {
// If we've reached this line, we know that value is a responsive value
Expand Down

0 comments on commit 99309e0

Please sign in to comment.