-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from thomasKn/thomas/fv-267-shoppay-lazyload
Lazyload shoppay
- Loading branch information
Showing
6 changed files
with
164 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {createContext, useCallback, useEffect, useState} from 'react'; | ||
import {useFirstMountState} from 'react-use'; | ||
|
||
type LoadOnInteractionContext = { | ||
ready: boolean; | ||
}; | ||
|
||
export const LoadOnInteractionContext = | ||
createContext<LoadOnInteractionContext | null>(null); | ||
|
||
export function LoadOnInteractionProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const [ready, setReady] = useState(false); | ||
const isFirstRender = useFirstMountState(); | ||
|
||
const handleReady = useCallback(() => { | ||
if (!ready) { | ||
setReady(true); | ||
console.log('ready'); | ||
|
||
window.removeEventListener('scroll', handleReady); | ||
window.removeEventListener('mousemove', handleReady); | ||
window.removeEventListener('touchstart', handleReady); | ||
window.removeEventListener('keypress', handleReady); | ||
} | ||
}, [ready]); | ||
|
||
useEffect(() => { | ||
console.log(isFirstRender); | ||
|
||
if (isFirstRender) { | ||
window.addEventListener('scroll', handleReady); | ||
window.addEventListener('mousemove', handleReady); | ||
window.addEventListener('touchstart', handleReady); | ||
window.addEventListener('keypress', handleReady); | ||
} | ||
|
||
return () => { | ||
window.removeEventListener('scroll', handleReady); | ||
window.removeEventListener('mousemove', handleReady); | ||
window.removeEventListener('touchstart', handleReady); | ||
window.removeEventListener('keypress', handleReady); | ||
}; | ||
}, [handleReady, isFirstRender]); | ||
|
||
return ( | ||
<LoadOnInteractionContext.Provider value={{ready}}> | ||
{children} | ||
</LoadOnInteractionContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters