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.4 KB
Raw Blame
import './LemonBanner.scss'

import clsx from 'clsx'
import { useActions, useValues } from 'kea'

import { IconInfo, IconSparkles, IconWarning, IconX } from '@posthog/icons'

import { LemonButton, SideAction } from 'lib/lemon-ui/LemonButton'
import { LemonButtonPropsBase } from 'lib/lemon-ui/LemonButton'

import { lemonBannerLogic } from './lemonBannerLogic'

export type LemonBannerAction = SideAction & Pick<LemonButtonPropsBase, 'children'>

export interface LemonBannerProps {
    type: 'info' | 'warning' | 'error' | 'success' | 'ai'
    /** If onClose is provided, a close button will be shown and this callback will be fired when it's clicked. */
    onClose?: () => void
    children: React.ReactNode
    action?: LemonBannerAction
    className?: string
    /** If provided, the banner will be dismissed and hidden when the key is set in localStorage. */
    dismissKey?: string
    /**
     * If left unset, the type-specific icon will show up above a certain width of the banner.
     * If set to a boolean, the icon will either always be hidden or always shown.
     */
    hideIcon?: boolean
    square?: boolean
    icon?: React.ReactNode
    /** Keep 'center' for one-line banners: the row fills the 3rem min-height, and centering holds the text off the top edge. */
    alignItems?: 'center' | 'start'
}

/** Generic alert message. */
export function LemonBanner({
    type,
    onClose,
    children,
    action,
    className,
    dismissKey = '',
    hideIcon,
    square = false,
    icon,
    alignItems = 'center',
}: LemonBannerProps): JSX.Element | null {
    const logic = lemonBannerLogic({ dismissKey })
    const { isDismissed } = useValues(logic)
    const { dismiss } = useActions(logic)
    const showCloseButton = dismissKey || onClose

    const _onClose = (): void => {
        if (dismissKey) {
            dismiss()
        }
        onClose?.()
    }

    if (isDismissed) {
        return null
    }

    return (
        <div
            className={clsx(
                'LemonBanner @container',
                `LemonBanner--${type}`,
                className,
                square && 'LemonBanner--square'
            )}
        >
            <div className={clsx('flex gap-2 grow @md:!px-1', alignItems === 'start' ? 'items-start' : 'items-center')}>
                {!hideIcon &&
                    (icon ? (
                        icon
                    ) : type === 'warning' || type === 'error' ? (
                        <IconWarning className={clsx('LemonBanner__icon', hideIcon !== false && 'hidden @md:!block')} />
                    ) : type === 'ai' ? (
                        <IconSparkles
                            className={clsx('LemonBanner__icon', hideIcon !== false && 'hidden @md:!block')}
                        />
                    ) : (
                        <IconInfo className={clsx('LemonBanner__icon', hideIcon !== false && 'hidden @md:!block')} />
                    ))}
                <div className="grow overflow-hidden">{children}</div>
                {action && <LemonButton className="!hidden @md:!flex" type="secondary" {...action} />}
                {showCloseButton && (
                    <LemonButton size="xsmall" icon={<IconX />} onClick={_onClose} aria-label="close" />
                )}
            </div>
            {action && <LemonButton className="@md:!hidden" type="secondary" fullWidth {...action} />}
        </div>
    )
}