Skip to content

Commit 7196e10

Browse files
committed
fiw waypoint initial scroll bug
1 parent 43fdb82 commit 7196e10

File tree

1 file changed

+17
-2
lines changed
  • packages/docusaurus-plugin-ideal-image/src/theme/IdealImageLegacy/components/IdealImage

1 file changed

+17
-2
lines changed

packages/docusaurus-plugin-ideal-image/src/theme/IdealImageLegacy/components/IdealImage/waypoint.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ function addEventListener(
1818
return () => element.removeEventListener(type, listener, options);
1919
}
2020

21+
// Because waypoint may fire before the setState() updates due to batching
22+
// queueMicrotask is a better option than setTimeout() or React.flushSync()
23+
// See https://github.com/facebook/docusaurus/issues/11018
24+
// See https://github.com/civiccc/react-waypoint/blob/0905ac5a073131147c96dd0694bd6f1b6ee8bc97/src/onNextTick.js
25+
function subscribeMicrotask(callback: () => void) {
26+
let subscribed = true;
27+
queueMicrotask(() => {
28+
if (subscribed) callback();
29+
});
30+
return () => (subscribed = false);
31+
}
32+
2133
type Position = 'above' | 'inside' | 'below' | 'invisible';
2234

2335
type Props = {
@@ -70,12 +82,15 @@ class WaypointClient extends React.Component<Props> {
7082
{passive: true},
7183
);
7284

85+
const unsubscribeInitialScroll = subscribeMicrotask(() => {
86+
this._handleScroll();
87+
});
88+
7389
this.unsubscribe = () => {
7490
unsubscribeScroll();
7591
unsubscribeResize();
92+
unsubscribeInitialScroll();
7693
};
77-
78-
this._handleScroll();
7994
}
8095

8196
override componentDidUpdate() {

0 commit comments

Comments
 (0)