File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
packages/docusaurus-plugin-ideal-image/src/theme/IdealImageLegacy/components/IdealImage Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff 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+
2133type Position = 'above' | 'inside' | 'below' | 'invisible' ;
2234
2335type 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 ( ) {
You can’t perform that action at this time.
0 commit comments