Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 1.17 KB

useDetectSticky.md

File metadata and controls

37 lines (27 loc) · 1.17 KB

useDetectSticky

React lifecycle hook that detects if an element is sticky based on IntersectionObserver API.

Parameters

  • ref: React.MutableRefObject<unknown>: MutableRefObject for the element to observe.
  • observerSettings?: ObserverSettings: Optional settings for IntersectionObserver. Default value is { threshold: [1] }.

Return Value

A tuple containing:

  1. isSticky: boolean: Represents whether the observed element is currently sticky or not.
  2. ref: React.MutableRefObject<unknown>: MutableRefObject for the observed element.
  3. setIsSticky: React.Dispatch<React.SetStateAction<boolean>>: Function to update the isSticky state.

Usage

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>
  );
};