React lifecycle hook that detects if an element is sticky based on IntersectionObserver API.
ref: React.MutableRefObject<unknown>
: MutableRefObject for the element to observe.observerSettings?: ObserverSettings
: Optional settings for IntersectionObserver. Default value is{ threshold: [1] }
.
A tuple containing:
isSticky: boolean
: Represents whether the observed element is currently sticky or not.ref: React.MutableRefObject<unknown>
: MutableRefObject for the observed element.setIsSticky: React.Dispatch<React.SetStateAction<boolean>>
: Function to update theisSticky
state.
import React, { useRef } from 'react';
import useDetectSticky from './useDetectSticky'; // Assuming useDetectSticky is in a separate file
const ExampleComponent = () => {
const ref = useRef(null);
const [isSticky, , setIsSticky] = useDetectSticky(ref);
return (
<div ref={ref} style={{ height: '200px', overflow: 'auto' }}>
<div style={{ paddingTop: '100px', backgroundColor: isSticky ? 'red' : 'transparent' }}>
Sticky Header
</div>
{/* Other content */}
</div>
);
};