Skip to content

Commit 465fd83

Browse files
committed
feat(*): improve performance by reading layout in one badge
1 parent d0167f1 commit 465fd83

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

lib/Sticky.tsx

+16-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ interface IState {
2727
styles: React.CSSProperties;
2828
}
2929

30+
interface ILayoutSnapshot {
31+
stickyRect: IRect;
32+
containerRect: IRect;
33+
}
34+
3035
class Sticky extends React.PureComponent<IProps, IState> {
3136
private stickyRef: React.RefObject<any> = React.createRef();
3237
private placeholderRef: React.RefObject<any> = React.createRef();
@@ -125,10 +130,19 @@ class Sticky extends React.PureComponent<IProps, IState> {
125130
return styles;
126131
}
127132

128-
handleScrollUpdate = () => {
133+
recalculateLayoutBeforeUpdate = (): ILayoutSnapshot => {
129134
const stickyRect = this.stickyRef.current.getBoundingClientRect();
130135
const containerRect = this.container.current.getBoundingClientRect();
136+
return {
137+
stickyRect,
138+
containerRect,
139+
};
140+
};
131141

142+
handleScrollUpdate = (
143+
_: any,
144+
{ stickyRect, containerRect }: ILayoutSnapshot,
145+
) => {
132146
// in case children is not a function renderArgs will never be used
133147
const willRenderAsAFunction = typeof this.props.children === 'function';
134148

@@ -187,6 +201,7 @@ class Sticky extends React.PureComponent<IProps, IState> {
187201
<ObserveViewport
188202
disableDimensionsUpdates
189203
onUpdate={this.handleScrollUpdate}
204+
recalculateLayoutBeforeUpdate={this.recalculateLayoutBeforeUpdate}
190205
/>
191206
</>
192207
);

lib/StickyScrollUp.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,14 @@ class StickyScrollUp extends React.PureComponent<IProps, IState> {
113113
return styles;
114114
}
115115

116-
handleViewportUpdate = ({ scroll }: { scroll: IScroll }) => {
117-
const stickyRect = this.stickyRef.current.getBoundingClientRect();
116+
recalculateLayoutBeforeUpdate = (): IRect => {
117+
return this.stickyRef.current.getBoundingClientRect();
118+
};
119+
120+
handleViewportUpdate = (
121+
{ scroll }: { scroll: IScroll },
122+
stickyRect: IRect,
123+
) => {
118124
const nextOffset = Math.max(stickyRect.bottom, 0);
119125
if (
120126
this.props.updateStickyOffset &&
@@ -161,6 +167,7 @@ class StickyScrollUp extends React.PureComponent<IProps, IState> {
161167
<ObserveViewport
162168
disableDimensionsUpdates
163169
onUpdate={this.handleViewportUpdate}
170+
recalculateLayoutBeforeUpdate={this.recalculateLayoutBeforeUpdate}
164171
/>
165172
</>
166173
);

package-lock.json

+24-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"typescript": "^3.0.1"
4949
},
5050
"dependencies": {
51-
"react-viewport-utils": "^1.0.0",
51+
"react-viewport-utils": "^1.1.0",
5252
"recompose": "^0.30.0"
5353
},
5454
"peerDependencies": {

0 commit comments

Comments
 (0)