: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import clsx from 'clsx'
import { toHtml } from 'hast-util-to-html'
import xml from 'highlight.js/lib/languages/xml'
import { useValues } from 'kea'
import { common, createLowlight } from 'lowlight'
import { useMemo, useRef } from 'react'
import { LemonBanner, LemonTabs, LemonTextArea } from '@posthog/lemon-ui'
import { themeLogic } from '~/layout/navigation-3000/themeLogic'
import { SurveyQuestionDescriptionContentType } from '~/types'
const lowlight = createLowlight(common)
lowlight.register({ xml })
const CODE_FONT_FAMILY = 'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace'
function HighlightedTextArea({
value,
onChange,
placeholder,
}: {
value?: string
onChange: (value: string) => void
placeholder?: string
}): JSX.Element {
const { isDarkModeOn } = useValues(themeLogic)
const textareaRef = useRef<HTMLTextAreaElement>(null)
const preRef = useRef<HTMLPreElement>(null)
const handleScroll = (): void => {
if (textareaRef.current && preRef.current) {
preRef.current.scrollTop = textareaRef.current.scrollTop
preRef.current.scrollLeft = textareaRef.current.scrollLeft
}
}
const displayValue = value || ''
const showPlaceholder = !displayValue && placeholder
const highlighted = useMemo(
() =>
lowlight.registered('xml') ? lowlight.highlight('xml', displayValue) : lowlight.highlightAuto(displayValue),
[displayValue]
)
return (
<div
className={clsx(
'relative font-mono text-[13px] border rounded overflow-hidden resize-y',
isDarkModeOn ? 'bg-[#1e1e1e]' : 'bg-[#f5f5f5]'
)}
style={{ minHeight: '150px', height: '150px' }}
>
{showPlaceholder ? (
<div
className="absolute inset-0 p-[10px_12px] text-muted pointer-events-none leading-6"
style={{ fontFamily: CODE_FONT_FAMILY }}
>
{placeholder}
</div>
) : (
<pre
ref={preRef}
className={clsx(
'm-0 overflow-auto pointer-events-none bg-transparent h-full whitespace-pre-wrap',
'border-none leading-6'
)}
style={{
padding: '10px 12px',
wordWrap: 'break-word',
fontFamily: 'inherit',
fontSize: 'inherit',
}}
>
<code
className={clsx('hljs leading-6', isDarkModeOn && 'hljs-dark')}
style={{
fontFamily: CODE_FONT_FAMILY,
fontSize: 'inherit',
}}
dangerouslySetInnerHTML={{ __html: toHtml(highlighted) }}
/>
</pre>
)}
<textarea
ref={textareaRef}
value={value}
onChange={(e) => onChange(e.target.value)}
onScroll={handleScroll}
aria-label="HTML code editor"
spellCheck={false}
autoCorrect="off"
autoCapitalize="off"
className={clsx(
'absolute inset-0 w-full h-full resize-none bg-transparent leading-6',
'p-[10px_12px] text-transparent selection:bg-primary-highlight',
'focus:outline-none focus:ring-1 focus:ring-primary',
isDarkModeOn ? 'caret-white' : 'caret-black'
)}
style={{
fontFamily: CODE_FONT_FAMILY,
fontSize: 'inherit',
}}
/>
</div>
)
}
export function PresentationTypeCard({
title,
description,
children,
onClick,
value,
active,
disabled,
}: {
title: string
description?: string
children?: React.ReactNode
onClick: () => void
value: any
active: boolean
disabled?: boolean
}): JSX.Element {
return (
<div
className={clsx(
'border rounded relative px-4 py-2 overflow-hidden h-[180px] w-full',
active ? 'border-accent' : 'border-primary',
disabled && 'opacity-50'
)}
>
<p className="font-semibold m-0">{title}</p>
{description && <p className="m-0 text-xs">{description}</p>}
<div className="relative mt-2 presentation-preview">{children}</div>
<input
onClick={onClick}
className="opacity-0 absolute inset-0 h-full w-full cursor-pointer"
name="type"
value={value}
type="radio"
disabled={disabled}
/>
</div>
)
}
export function HTMLEditor({
value,
onChange,
onTabChange,
activeTab,
textPlaceholder,
textMinRows = 2,
disableTabSwitching = false,
className,
}: {
value?: string
onChange: (value: any) => void
onTabChange: (key: SurveyQuestionDescriptionContentType) => void
activeTab: SurveyQuestionDescriptionContentType
textPlaceholder?: string
textMinRows?: number
disableTabSwitching?: boolean
className?: string
}): JSX.Element {
return (
<>
<LemonTabs
activeKey={activeTab}
onChange={disableTabSwitching ? undefined : onTabChange}
tabs={[
{
key: 'text',
label: <span className="text-sm">Text</span>,
content: (
<LemonTextArea
minRows={textMinRows}
value={value}
onChange={(v) => onChange(v)}
placeholder={textPlaceholder}
className={className}
/>
),
},
{
key: 'html',
label: <span className="text-sm">HTML</span>,
content: (
<HighlightedTextArea value={value} onChange={onChange} placeholder={textPlaceholder} />
),
},
]}
/>
{value && value?.toLowerCase().includes('<script') && (
<LemonBanner type="warning">
Scripts won't run in the survey popover and we'll remove these on save. Use the API question mode to
run your own scripts in surveys.
</LemonBanner>
)}
</>
)
}