Skip to content

Instantly share code, notes, and snippets.

@martinratinaud
Created June 9, 2023 11:58
Show Gist options
  • Save martinratinaud/8c708c5c42e69d46652403795df94490 to your computer and use it in GitHub Desktop.
Save martinratinaud/8c708c5c42e69d46652403795df94490 to your computer and use it in GitHub Desktop.
Floating CTA using `react-use` `useIntersection`
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