Skip to content

: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.

3.0 KB
Raw Blame
import { MakeLogicType, actions, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { windowValues } from 'kea-window-values'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visibilitySensorLogicValues {
    checkIsVisible: (element: HTMLElement) => boolean
    innerHeight: number
    visible: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visibilitySensorLogicActions {
    scrolling: (element: HTMLElement) => {
        element: HTMLElement
    }
    setVisible: (visible: boolean) => {
        visible: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visibilitySensorLogicProps {
    id: string
    offset?: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface visibilitySensorLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        checkIsVisible: (innerHeight: number, arg: any) => (element: HTMLElement) => boolean
    }
}

export type visibilitySensorLogicType = MakeLogicType<
    visibilitySensorLogicValues,
    visibilitySensorLogicActions,
    visibilitySensorLogicProps,
    visibilitySensorLogicMeta
>

export const visibilitySensorLogic = kea<visibilitySensorLogicType>([
    props(
        {} as {
            id: string
            offset?: number
        }
    ),
    key((props) => props.id || 'undefined'),
    path((key) => ['lib', 'components', 'VisibilitySensor', 'visibilitySensorLogic', key]),
    actions(() => ({
        setVisible: (visible: boolean) => ({ visible }),
        scrolling: (element: HTMLElement) => ({ element }),
    })),
    windowValues({
        innerHeight: (window: Window) => window.innerHeight,
    }),
    reducers(() => ({
        visible: [
            false,
            {
                setVisible: (_, { visible }) => visible,
            },
        ],
    })),
    selectors(() => ({
        checkIsVisible: [
            (selectors) => [selectors.innerHeight, (_, props) => props.offset || 0],
            (windowHeight: number, offset) => (element: HTMLElement) => {
                if (!element) {
                    return false
                }
                const { top, bottom, left, right } = element.getBoundingClientRect()
                // happens when switching tabs, element is gone, but sensorLogic is still mounted
                if (top === 0 && bottom === 0 && left === 0 && right === 0) {
                    return false
                }
                return top + offset >= 0 && top + offset <= windowHeight
            },
        ],
    })),
    listeners(({ actions, values }) => ({
        scrolling: async ({ element }, breakpoint) => {
            await breakpoint(500)

            if (values.checkIsVisible(element) && !values.visible) {
                actions.setVisible(true)
            } else if (!values.checkIsVisible(element) && values.visible) {
                actions.setVisible(false)
            }
        },
    })),
])