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.1 KB
Raw Blame
import clsx from 'clsx'
import React from 'react'

import { IconCopy } from '@posthog/icons'

import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { copyToClipboard } from 'lib/utils/copyToClipboard'

interface InlinePropsBase {
    description?: string
    /** Makes text selectable instead of copying on click anywhere */
    selectable?: boolean
    /** adds ph-no-capture class to the element **/
    isValueSensitive?: boolean
    tooltipMessage?: React.ReactNode | null
    iconStyle?: Record<string, string | number>
    /** @default end */
    iconPosition?: 'end' | 'start'
    /** @default small */
    iconSize?: 'small' | 'xsmall'
    className?: string
    /** @deprecated */
    style?: React.CSSProperties
    /** @default true */
    iconMargin?: boolean
    'data-attr'?: string
}
interface InlinePropsWithStringInside extends InlinePropsBase {
    children: string
    explicitValue?: string
}
interface InlinePropsWithJSXInside extends InlinePropsBase {
    children?: JSX.Element
    explicitValue: string
}
export type InlineProps = InlinePropsWithStringInside | InlinePropsWithJSXInside

export const CopyToClipboardInline = React.forwardRef<HTMLSpanElement, InlineProps>(function CopyToClipboardInline(
    {
        children,
        explicitValue,
        description,
        selectable = false,
        isValueSensitive = false,
        tooltipMessage = null,
        iconStyle,
        iconPosition = 'end',
        iconSize = 'small',
        className,
        style,
        iconMargin = true,
        'data-attr': dataAttr,
        ...props
    },
    ref
) {
    const copy = (): void => void copyToClipboard((explicitValue ?? children) as string, description)

    let content = (
        <LemonButton
            size={iconSize}
            icon={<IconCopy style={{ ...iconStyle }} />}
            noPadding
            className={iconMargin ? 'ml-1' : undefined}
            data-attr={!children && dataAttr ? dataAttr : 'copy-icon'}
            onClick={selectable || !children ? copy : undefined}
        />
    )

    if (children) {
        content = (
            <span
                className={clsx(
                    'relative truncate inline-flex items-center flex-nowrap w-fit break-all',
                    selectable ? 'cursor-text' : 'cursor-pointer',
                    iconPosition === 'end' ? 'flex-row' : 'flex-row-reverse',
                    isValueSensitive && 'ph-no-capture',
                    className
                )}
                // eslint-disable-next-line react/forbid-dom-props
                style={style}
                onClick={!selectable ? copy : undefined}
                data-attr={dataAttr}
                {...props}
                ref={ref}
            >
                <span className={iconPosition === 'start' ? 'grow-1' : undefined}>{children}</span>
                {content}
            </span>
        )
    }
    return !selectable || tooltipMessage !== null ? (
        <Tooltip title={tooltipMessage || 'Click to copy'}>{content}</Tooltip>
    ) : (
        <>{content}</>
    )
})