Created
June 9, 2023 11:58
-
-
Save martinratinaud/8c708c5c42e69d46652403795df94490 to your computer and use it in GitHub Desktop.
Floating CTA using `react-use` `useIntersection`
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
import React from 'react'; | |
import { useIntersection } from 'react-use'; | |
const App = () => { | |
const intersectionRef = React.useRef(null); | |
const intersection = useIntersection(intersectionRef, { | |
root: null, | |
rootMargin: '0px', | |
threshold: 1, | |
}); | |
return ( | |
<div> | |
<div className="px-4 py-8 sm:px-10" ref={intersectionRef}> | |
In the viewport | |
</div> | |
{Array.from({ length: 100 }, (_, i) => ( | |
<> | |
{i} | |
<br key={i} /> | |
</> | |
))} | |
<div className="fixed bottom-0 bg-white w-full" style={intersection && intersection.intersectionRatio < 1 ? {} : { display: 'none' }}> | |
Floating CTA | |
</div> | |
</div> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment