Skip to content

Commit 9ff343f

Browse files
committed
Improve PageLayout performance
1 parent c9659fd commit 9ff343f

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
@@ -123,19 +123,21 @@ const Root: React.FC<React.PropsWithChildren<PageLayoutProps>> = ({
123123
sx: {display: 'flex', flex: '1 1 100%', flexWrap: 'wrap', maxWidth: '100%'},
124124
}
125125

126+
const memoizedContextValue = React.useMemo(() => {
127+
return {
128+
padding,
129+
rowGap,
130+
columnGap,
131+
enableStickyPane,
132+
disableStickyPane,
133+
contentTopRef,
134+
contentBottomRef,
135+
paneRef,
136+
}
137+
}, [padding, rowGap, columnGap, enableStickyPane, disableStickyPane, contentTopRef, contentBottomRef, paneRef])
138+
126139
return (
127-
<PageLayoutContext.Provider
128-
value={{
129-
padding,
130-
rowGap,
131-
columnGap,
132-
enableStickyPane,
133-
disableStickyPane,
134-
contentTopRef,
135-
contentBottomRef,
136-
paneRef,
137-
}}
138-
>
140+
<PageLayoutContext.Provider value={memoizedContextValue}>
139141
<Box
140142
ref={rootRef}
141143
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)