:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useActions } from 'kea'
import { useEffect, useRef } from 'react'
import { visibilitySensorLogic } from './visibilitySensorLogic'
interface VisibilityProps {
id: string // Must be unique for each component
offset?: number
children: React.ReactNode | null
}
export function VisibilitySensor({ id, offset, children }: VisibilityProps): JSX.Element {
const { scrolling } = useActions(visibilitySensorLogic({ id, offset }))
const ref = useRef<HTMLDivElement | null>(null)
useEffect(() => {
const element = ref.current
const handler = (): void => {
if (element) {
scrolling(element)
}
}
document.addEventListener('scroll', handler, { passive: true })
return () => document.removeEventListener('scroll', handler)
}, [ref.current]) // oxlint-disable-line react-hooks/exhaustive-deps
return <div ref={ref}>{children}</div>
}