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.

13.0 KB
Raw Blame
import { useValues } from 'kea'
import { DeepPartialMap, ValidationErrorType } from 'kea-forms'

import { IconCheck } from '@posthog/icons'
import { LemonButton, LemonInput, LemonSelect } from '@posthog/lemon-ui'

import { LemonField } from 'lib/lemon-ui/LemonField'
import { WEB_SAFE_FONTS } from 'scenes/surveys/constants'
import { surveysLogic } from 'scenes/surveys/surveysLogic'
import { ColorInput } from 'scenes/surveys/wizard/ColorInput'

import { SurveyAppearance, SurveyPosition, SurveyType, SurveyWidgetType } from '~/types'

import { SurveyPositionSelector } from './SurveyAppearancePositionSelector'

interface CommonProps {
    appearance: SurveyAppearance
    onAppearanceChange: (appearance: SurveyAppearance) => void
    validationErrors?: DeepPartialMap<SurveyAppearance, ValidationErrorType> | null
    surveyType?: SurveyType
    disabledReason?: string
}

const gridPositions: SurveyPosition[] = [
    SurveyPosition.TopLeft,
    SurveyPosition.TopCenter,
    SurveyPosition.TopRight,
    SurveyPosition.MiddleLeft,
    SurveyPosition.MiddleCenter,
    SurveyPosition.MiddleRight,
    SurveyPosition.Left,
    SurveyPosition.Center,
    SurveyPosition.Right,
    // Bottom positions are not in the current enum, will add if they become available
    // SurveyPosition.BottomLeft, SurveyPosition.BottomCenter, SurveyPosition.BottomRight
]

const positionDisplayNames: Record<SurveyPosition, string> = {
    [SurveyPosition.TopLeft]: 'Top Left',
    [SurveyPosition.TopCenter]: 'Top Center',
    [SurveyPosition.TopRight]: 'Top Right',
    [SurveyPosition.MiddleLeft]: 'Middle Left',
    [SurveyPosition.MiddleCenter]: 'Middle Center',
    [SurveyPosition.MiddleRight]: 'Middle Right',
    [SurveyPosition.Left]: 'Bottom Left',
    [SurveyPosition.Center]: 'Bottom Center',
    [SurveyPosition.Right]: 'Bottom Right',
    [SurveyPosition.NextToTrigger]: 'Next to feedback button',
}

const IGNORE_ERROR_BORDER_CLASS = 'ignore-error-border'

interface SurveyAppearanceInputProps {
    value?: string
    onChange: (value: string) => void
    error?: string
    label: string
    info?: string
    placeholder?: string
    disabledReason?: string | null
    inputType?: 'text' | 'color'
}

function SurveyAppearanceInput({
    value,
    onChange,
    error,
    label,
    info,
    placeholder,
    disabledReason,
    inputType = 'text',
}: SurveyAppearanceInputProps): JSX.Element {
    const { surveysStylingAvailable } = useValues(surveysLogic)
    const disabled = !surveysStylingAvailable || !!disabledReason

    return (
        <LemonField.Pure label={label} className="flex-1 gap-1" info={info}>
            {inputType === 'color' ? (
                <ColorInput
                    value={value}
                    onChange={onChange}
                    disabled={disabled}
                    disabledReason={disabledReason || undefined}
                />
            ) : (
                <LemonInput
                    value={value}
                    onChange={onChange}
                    disabled={disabled}
                    className={IGNORE_ERROR_BORDER_CLASS}
                    placeholder={placeholder}
                    disabledReason={disabledReason || undefined}
                />
            )}
            {error && <LemonField.Error error={error} />}
        </LemonField.Pure>
    )
}

export function SurveyContainerAppearance({
    appearance,
    onAppearanceChange,
    validationErrors,
    surveyType,
    disabledReason,
}: CommonProps): JSX.Element | null {
    const { surveysStylingAvailable } = useValues(surveysLogic)

    const disabled = !surveysStylingAvailable || !!disabledReason

    return (
        <div className="space-y-4">
            {/* CSS value inputs — consistent 2-col grid */}
            <div className="grid grid-cols-2 gap-x-4 gap-y-2">
                <SurveyAppearanceInput
                    value={appearance.maxWidth}
                    onChange={(maxWidth) => onAppearanceChange({ maxWidth })}
                    error={validationErrors?.maxWidth}
                    disabledReason={disabledReason}
                    label="Survey width"
                    info="Min-width is always set to 300px"
                />
                <SurveyAppearanceInput
                    value={appearance.boxPadding}
                    onChange={(boxPadding) => onAppearanceChange({ boxPadding })}
                    error={validationErrors?.boxPadding}
                    disabledReason={disabledReason}
                    label="Box padding"
                />
                <SurveyAppearanceInput
                    value={appearance.borderRadius}
                    onChange={(borderRadius) => onAppearanceChange({ borderRadius })}
                    error={validationErrors?.borderRadius}
                    disabledReason={disabledReason}
                    label="Border radius"
                />
                <SurveyAppearanceInput
                    value={appearance.boxShadow}
                    onChange={(boxShadow) => onAppearanceChange({ boxShadow })}
                    error={validationErrors?.boxShadow}
                    disabledReason={disabledReason}
                    label="Box shadow"
                />
                <LemonField.Pure
                    label="Font family"
                    info="Custom font selection requires at least version 1.223.4 of posthog-js"
                    className="gap-1"
                >
                    <LemonSelect
                        value={appearance?.fontFamily}
                        onChange={(fontFamily) => onAppearanceChange({ fontFamily })}
                        options={WEB_SAFE_FONTS.map((font) => ({
                            label: <span className={font.value.toLowerCase().replace(/\s/g, '-')}>{font.label}</span>,
                            value: font.value,
                        }))}
                        className="ignore-error-border"
                        disabled={disabled}
                        disabledReason={disabledReason || undefined}
                    />
                </LemonField.Pure>
                <SurveyAppearanceInput
                    value={appearance.zIndex}
                    onChange={(zIndex) => onAppearanceChange({ zIndex })}
                    error={validationErrors?.zIndex}
                    disabledReason={disabledReason}
                    label="z-index"
                    info="If the survey popup is hidden, set this value higher than the overlapping element's zIndex."
                />
            </div>

            {/* Position selector — own row since it's visually different */}
            <LemonField.Pure
                label="Position"
                info={
                    surveyType === SurveyType.Widget && appearance.widgetType === SurveyWidgetType.Selector
                        ? 'The "next to feedback button" option requires posthog.js version 1.235.2 or higher.'
                        : undefined
                }
                className="gap-1"
            >
                <div className="flex items-center gap-2">
                    <SurveyPositionSelector
                        currentPosition={appearance.position}
                        onAppearanceChange={onAppearanceChange}
                        disabled={disabled}
                    />
                    <LemonSelect
                        value={appearance.position}
                        onChange={(position) => onAppearanceChange({ position })}
                        options={gridPositions.map((position) => ({
                            label: positionDisplayNames[position],
                            value: position,
                        }))}
                        disabled={disabled}
                        disabledReason={disabledReason || undefined}
                    />
                </div>
                {surveyType === SurveyType.Widget && appearance.widgetType === SurveyWidgetType.Selector && (
                    <div className="flex flex-col gap-1 items-start w-60">
                        <LemonButton
                            key={SurveyPosition.NextToTrigger}
                            type="tertiary"
                            size="small"
                            fullWidth
                            onClick={() =>
                                onAppearanceChange({ ...appearance, position: SurveyPosition.NextToTrigger })
                            }
                            active={appearance.position === SurveyPosition.NextToTrigger}
                            disabled={disabled}
                            disabledReason={disabledReason || undefined}
                        >
                            {positionDisplayNames[SurveyPosition.NextToTrigger]}
                            {appearance.position === SurveyPosition.NextToTrigger && (
                                <IconCheck className="ml-2 size-4" />
                            )}
                        </LemonButton>
                    </div>
                )}
            </LemonField.Pure>
        </div>
    )
}

export function SurveyColorsAppearance({
    appearance,
    onAppearanceChange,
    validationErrors,
    customizeRatingButtons,
    customizePlaceholderText,
    disabledReason,
}: CommonProps & {
    customizeRatingButtons: boolean
    customizePlaceholderText: boolean
}): JSX.Element {
    return (
        <div className="grid grid-cols-2 gap-x-4 gap-y-2 sm:grid-cols-3">
            <SurveyAppearanceInput
                value={appearance.backgroundColor}
                onChange={(backgroundColor) => onAppearanceChange({ backgroundColor })}
                error={validationErrors?.backgroundColor}
                disabledReason={disabledReason}
                label="Survey background"
                inputType="color"
            />
            <SurveyAppearanceInput
                value={appearance.textColor}
                onChange={(textColor) => onAppearanceChange({ textColor })}
                error={validationErrors?.textColor}
                disabledReason={disabledReason}
                label="Question text"
                placeholder="Leave empty for auto-contrast"
                inputType="color"
            />
            <SurveyAppearanceInput
                value={appearance.borderColor}
                onChange={(borderColor) => onAppearanceChange({ borderColor })}
                error={validationErrors?.borderColor}
                disabledReason={disabledReason}
                label="Border"
                inputType="color"
            />
            <SurveyAppearanceInput
                value={appearance.inputBackground}
                onChange={(inputBackground) =>
                    onAppearanceChange({ inputBackground, ratingButtonColor: inputBackground })
                }
                error={validationErrors?.inputBackground || validationErrors?.ratingButtonColor}
                disabledReason={disabledReason}
                label="Input background"
                inputType="color"
            />
            <SurveyAppearanceInput
                value={appearance.inputTextColor}
                onChange={(inputTextColor) => onAppearanceChange({ inputTextColor })}
                error={validationErrors?.inputTextColor}
                disabledReason={disabledReason}
                label="Input text"
                placeholder="Leave empty for auto-contrast"
                inputType="color"
            />
            {customizeRatingButtons && (
                <SurveyAppearanceInput
                    value={appearance.ratingButtonActiveColor}
                    onChange={(ratingButtonActiveColor) => onAppearanceChange({ ratingButtonActiveColor })}
                    error={validationErrors?.ratingButtonActiveColor}
                    disabledReason={disabledReason}
                    label="Selected rating"
                    inputType="color"
                />
            )}
            <SurveyAppearanceInput
                value={appearance.submitButtonColor}
                onChange={(submitButtonColor) => onAppearanceChange({ submitButtonColor })}
                error={validationErrors?.submitButtonColor}
                disabledReason={disabledReason}
                label="Button background"
                inputType="color"
            />
            <SurveyAppearanceInput
                value={appearance.submitButtonTextColor}
                onChange={(submitButtonTextColor) => onAppearanceChange({ submitButtonTextColor })}
                error={validationErrors?.submitButtonTextColor}
                disabledReason={disabledReason}
                label="Button text"
                placeholder="Leave empty for auto-contrast"
                inputType="color"
            />
            {customizePlaceholderText && (
                <SurveyAppearanceInput
                    value={appearance.placeholder}
                    onChange={(placeholder) => onAppearanceChange({ placeholder })}
                    error={validationErrors?.placeholder}
                    disabledReason={disabledReason}
                    label="Placeholder text"
                />
            )}
        </div>
    )
}