{"version":3,"file":"useIsScrolling.mjs","names":[],"sources":["../../../../../src/components/MessageList/hooks/useIsScrolling.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\n/**\n * Whether `scrollElement` has scrolled within the last `idleMs` milliseconds. Turns `true` on the\n * first `scroll` event and back to `false` once no further `scroll` event arrives for `idleMs`.\n */\nexport const useIsScrolling = (\n  scrollElement: HTMLElement | Window | null,\n  idleMs: number,\n) => {\n  // Holds the element that is scrolling, so a replaced `scrollElement` never reads as scrolling.\n  const [scrollingElement, setScrollingElement] = useState<HTMLElement | Window | null>(\n    null,\n  );\n\n  useEffect(() => {\n    if (!scrollElement) return;\n\n    let idleTimeout: ReturnType<typeof setTimeout> | undefined;\n    const handleScroll = () => {\n      setScrollingElement(scrollElement);\n      clearTimeout(idleTimeout);\n      idleTimeout = setTimeout(() => setScrollingElement(null), idleMs);\n    };\n\n    scrollElement.addEventListener('scroll', handleScroll, { passive: true });\n\n    return () => {\n      scrollElement.removeEventListener('scroll', handleScroll);\n      clearTimeout(idleTimeout);\n      // The idle timer that would end this scroll is gone, so the scroll ends here.\n      setScrollingElement((current) => (current === scrollElement ? null : current));\n    };\n  }, [idleMs, scrollElement]);\n\n  return scrollingElement !== null && scrollingElement === scrollElement;\n};\n"],"mappings":";;;;;;AAMA,IAAa,kBACX,eACA,WACG;CAEH,MAAM,CAAC,kBAAkB,uBAAuB,SAC9C,IACF;CAEA,gBAAgB;EACd,IAAI,CAAC,eAAe;EAEpB,IAAI;EACJ,MAAM,qBAAqB;GACzB,oBAAoB,aAAa;GACjC,aAAa,WAAW;GACxB,cAAc,iBAAiB,oBAAoB,IAAI,GAAG,MAAM;EAClE;EAEA,cAAc,iBAAiB,UAAU,cAAc,EAAE,SAAS,KAAK,CAAC;EAExE,aAAa;GACX,cAAc,oBAAoB,UAAU,YAAY;GACxD,aAAa,WAAW;GAExB,qBAAqB,YAAa,YAAY,gBAAgB,OAAO,OAAQ;EAC/E;CACF,GAAG,CAAC,QAAQ,aAAa,CAAC;CAE1B,OAAO,qBAAqB,QAAQ,qBAAqB;AAC3D"}