Skip to content

Commit e37ab00

Browse files
committed
Improve PageLayout performance (#5942)
1 parent 114c826 commit e37ab00

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

.changeset/clean-fried-eggs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Improve PageLayout performance

packages/react/src/PageLayout/PageLayout.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,21 @@ const Root: React.FC<React.PropsWithChildren<PageLayoutProps>> = ({
8888

8989
const [slots, rest] = useSlots(children, slotsConfig ?? {header: Header, footer: Footer})
9090

91+
const memoizedContextValue = React.useMemo(() => {
92+
return {
93+
padding,
94+
rowGap,
95+
columnGap,
96+
enableStickyPane,
97+
disableStickyPane,
98+
contentTopRef,
99+
contentBottomRef,
100+
paneRef,
101+
}
102+
}, [padding, rowGap, columnGap, enableStickyPane, disableStickyPane, contentTopRef, contentBottomRef, paneRef])
103+
91104
return (
92-
<PageLayoutContext.Provider
93-
value={{
94-
padding,
95-
rowGap,
96-
columnGap,
97-
enableStickyPane,
98-
disableStickyPane,
99-
contentTopRef,
100-
contentBottomRef,
101-
paneRef,
102-
}}
103-
>
105+
<PageLayoutContext.Provider value={memoizedContextValue}>
104106
<BoxWithFallback
105107
ref={rootRef}
106108
style={

packages/react/src/PageLayout/useStickyPaneHeight.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,33 @@ export function useStickyPaneHeight() {
8484
}
8585
}, [isEnabled, contentTopInView, contentBottomInView, calculateHeight])
8686

87-
function enableStickyPane(top: string | number) {
88-
setIsEnabled(true)
89-
setOffsetHeader(top)
90-
}
91-
92-
function disableStickyPane() {
93-
setIsEnabled(false)
94-
}
87+
const enableStickyPane = React.useCallback(
88+
function enableStickyPane(top: string | number) {
89+
setIsEnabled(true)
90+
setOffsetHeader(top)
91+
},
92+
[setIsEnabled, setOffsetHeader],
93+
)
94+
95+
const disableStickyPane = React.useCallback(
96+
function disableStickyPane() {
97+
setIsEnabled(false)
98+
},
99+
[setIsEnabled],
100+
)
101+
102+
const memoizedResult = React.useMemo(() => {
103+
return {
104+
rootRef,
105+
enableStickyPane,
106+
disableStickyPane,
107+
contentTopRef,
108+
contentBottomRef,
109+
stickyPaneHeight: height,
110+
}
111+
}, [rootRef, enableStickyPane, disableStickyPane, contentTopRef, contentBottomRef, height])
95112

96-
return {
97-
rootRef,
98-
enableStickyPane,
99-
disableStickyPane,
100-
contentTopRef,
101-
contentBottomRef,
102-
stickyPaneHeight: height,
103-
}
113+
return memoizedResult
104114
}
105115

106116
// TODO: there is currently an issue with dvh on Desktop Safari 15.6, 16.0. To

0 commit comments

Comments
 (0)