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.

1.7 KB
Raw Blame
import { useId } from 'react'
import { calcGridCellDimensions } from 'react-grid-layout/core'

interface DashboardGridBackgroundProps {
    width: number
    cols: number
    rowHeight: number
    margin: [number, number]
    /** Draws whole rows up to this height. Without it, the background fills its positioned parent as the grid grows. */
    height?: number
}

export function DashboardGridBackground({
    width,
    cols,
    rowHeight,
    margin,
    height,
}: DashboardGridBackgroundProps): JSX.Element {
    const patternId = useId()
    const { cellWidth, cellHeight, gapX, gapY } = calcGridCellDimensions({
        width,
        cols,
        rowHeight,
        margin,
        containerPadding: [0, 0],
    })
    const rowCount = height ? Math.ceil((height + gapY) / (cellHeight + gapY)) : 10
    const totalHeight = height === undefined ? '100%' : rowCount * (cellHeight + gapY) - gapY

    return (
        <svg
            className="absolute top-0 left-0 pointer-events-none"
            width={width}
            height={totalHeight}
            aria-hidden="true"
        >
            <defs>
                <pattern
                    id={patternId}
                    width={cellWidth + gapX}
                    height={cellHeight + gapY}
                    patternUnits="userSpaceOnUse"
                >
                    <rect
                        width={Math.max(0, cellWidth)}
                        height={cellHeight}
                        rx={4}
                        fill="var(--color-bg-surface-secondary)"
                    />
                </pattern>
            </defs>
            <rect width={width} height={totalHeight} fill={`url(#${patternId})`} />
        </svg>
    )
}