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.

2.8 KB
Raw Blame
import { useValues } from 'kea'
import { useEffect } from 'react'

import { ScrollArea } from '@posthog/quill'

import { CommandKSearchFooter } from './CommandKSearchFooter'
import { COMMAND_K_LISTBOX_ID, CommandKSearchInput } from './CommandKSearchInput'
import { commandKSearchLogic } from './commandKSearchLogic'
import { rowDomId } from './CommandKSearchRow'
import { CommandKSearchSection } from './CommandKSearchSection'

export function CommandKSearchPalette(): JSX.Element {
    const { sections, highlightedRow } = useValues(commandKSearchLogic)
    const highlightedKey = highlightedRow?.key ?? null

    useEffect(() => {
        const row = highlightedKey ? document.getElementById(rowDomId(highlightedKey)) : null
        const viewport = row?.closest<HTMLElement>('[data-slot="scroll-area-viewport"]')
        if (!row || !viewport) {
            return
        }
        // scrollIntoView also scrolls the dialog, which clips the input, so scroll only the list.
        // The sticky section header covers the top of the viewport, so keep rows below it.
        const header = row.closest('[role="group"]')?.querySelector<HTMLElement>('[data-slot="menu-label"]')
        const headerHeight = header?.offsetHeight ?? 0
        const rowRect = row.getBoundingClientRect()
        const viewportRect = viewport.getBoundingClientRect()
        if (rowRect.top < viewportRect.top + headerHeight) {
            viewport.scrollTop -= viewportRect.top + headerHeight - rowRect.top
        } else if (rowRect.bottom > viewportRect.bottom) {
            viewport.scrollTop += rowRect.bottom - viewportRect.bottom
        }
    }, [highlightedKey])

    return (
        <div
            data-quill
            className="flex min-h-0 flex-1 flex-col bg-background text-foreground"
            data-attr="command-k-search"
        >
            <CommandKSearchInput />
            <ScrollArea
                // Sticky section headers sit at z-10, so lift the scrollbar above them.
                className="flex min-h-0 flex-1 flex-col border-t border-border [&>[data-slot=scroll-area-scrollbar]]:z-20"
                viewportClassName="h-auto min-h-0 flex-1"
            >
                <div
                    id={COMMAND_K_LISTBOX_ID}
                    role="listbox"
                    aria-label="Search results"
                    // The colorful-product-icons group turns on each product's brand color, as in the navbar.
                    className="group/colorful-product-icons colorful-product-icons-true bg-card pb-1"
                >
                    {sections.map((section) => (
                        <CommandKSearchSection key={section.key} section={section} highlightedKey={highlightedKey} />
                    ))}
                </div>
            </ScrollArea>
            <CommandKSearchFooter />
        </div>
    )
}