diff --git a/.babelrc b/.babelrc index 7bb5482ce..48f19b72b 100644 --- a/.babelrc +++ b/.babelrc @@ -4,8 +4,8 @@ [ "@babel/transform-react-jsx", { - "pragma": "Preact.h", - "pragmaFrag": "Preact.Fragment" + "runtime": "automatic", + "importSource": "preact" } ] ] diff --git a/cypress/e2e/surveys.cy.ts b/cypress/e2e/surveys.cy.ts index a086fd26b..b17ddd5bb 100644 --- a/cypress/e2e/surveys.cy.ts +++ b/cypress/e2e/surveys.cy.ts @@ -525,7 +525,7 @@ describe('Surveys', () => { type: 'popover', start_date: '2021-01-01T00:00:00Z', questions: [emojiRatingQuestion], - appearance: appearanceWithThanks, + appearance: { ...appearanceWithThanks, backgroundColor: 'black' }, }, ], }).as('surveys') diff --git a/src/__tests__/extensions/surveys.test.ts b/src/__tests__/extensions/surveys.test.ts index 9953f266f..d455bf588 100644 --- a/src/__tests__/extensions/surveys.test.ts +++ b/src/__tests__/extensions/surveys.test.ts @@ -1,5 +1,6 @@ import 'regenerator-runtime/runtime' -import { createShadow, generateSurveys } from '../../extensions/surveys' +import { generateSurveys } from '../../extensions/surveys' +import { createShadow } from '../../extensions/surveys/surveys-utils' import { SurveyType } from '../../posthog-surveys-types' describe('survey display logic', () => { diff --git a/src/extensions/surveys.tsx b/src/extensions/surveys.tsx index c44fb5f6a..b349548fe 100644 --- a/src/extensions/surveys.tsx +++ b/src/extensions/surveys.tsx @@ -12,30 +12,24 @@ import { } from '../posthog-surveys-types' import { window as _window, document as _document } from '../utils/globals' -import { style, getTextColor, defaultSurveyAppearance, sendSurveyEvent } from './surveys/surveys-utils' +import { style, defaultSurveyAppearance, sendSurveyEvent, createShadow } from './surveys/surveys-utils' +import { useContrastingTextColor } from './surveys/hooks/useContrastingTextColor' import * as Preact from 'preact' import { createWidgetShadow } from './surveys-widget' import { useState, useEffect, useRef } from 'preact/hooks' -import { _isArray, _isNull, _isNumber } from '../utils/type-utils' +import { _isNumber } from '../utils/type-utils' +import { ConfirmationMessage } from './surveys/components/ConfirmationMessage' +import { + OpenTextQuestion, + LinkQuestion, + RatingQuestion, + MultipleChoiceQuestion, +} from './surveys/components/QuestionTypes' // We cast the types here which is dangerous but protected by the top level generateSurveys call const window = _window as Window & typeof globalThis const document = _document as Document -export const createShadow = (styleSheet: string, surveyId: string) => { - const div = document.createElement('div') - div.className = `PostHogSurvey${surveyId}` - const shadow = div.attachShadow({ mode: 'open' }) - if (styleSheet) { - const styleElement = Object.assign(document.createElement('style'), { - innerText: styleSheet, - }) - shadow.appendChild(styleElement) - } - document.body.appendChild(div) - return shadow -} - const handleWidget = (posthog: PostHog, survey: Survey) => { const shadow = createWidgetShadow(survey) const surveyStyleSheet = style(survey.appearance) @@ -119,93 +113,6 @@ export function generateSurveys(posthog: PostHog) { }, 3000) } -const defaultBackgroundColor = '#eeeded' - -export function QuestionHeader({ - question, - description, - backgroundColor, -}: { - question: string - description?: string | null - backgroundColor?: string -}) { - return ( -
-
{question}
- {description &&
} -
- ) -} - -export function Cancel({ onClick }: { onClick: () => void }) { - return ( -
- -
- ) -} - -export function BottomSection({ - text, - submitDisabled, - appearance, - onSubmit, - link, -}: { - text: string - submitDisabled: boolean - appearance: SurveyAppearance - onSubmit: () => void - link?: string | null -}) { - const { textColor, ref } = useContrastingTextColor({ appearance }) - - return ( -
-
- -
- {!appearance.whiteLabel && ( - - )} -
- ) -} - -export function PostHogLogo({ backgroundColor }: { backgroundColor?: string }) { - const { textColor, ref } = useContrastingTextColor({ appearance: { backgroundColor } }) - - return ( - } - style={{ backgroundColor: backgroundColor, color: textColor }} - className="footer-branding" - > - Survey by {posthogLogo} - - ) -} - export function Surveys({ posthog, survey, style }: { posthog: PostHog; survey: Survey; style?: React.CSSProperties }) { const [displayState, setDisplayState] = useState<'survey' | 'confirmation' | 'closed'>('survey') @@ -253,6 +160,61 @@ export function Surveys({ posthog, survey, style }: { posthog: PostHog; survey: ) } +const questionTypeMap = ( + question: SurveyQuestion, + questionIndex: number, + appearance: SurveyAppearance, + onSubmit: (res: string | string[] | number | null) => void, + closeSurveyPopup: () => void +): JSX.Element => { + const mapping = { + [SurveyQuestionType.Open]: ( + + ), + [SurveyQuestionType.Link]: ( + + ), + [SurveyQuestionType.Rating]: ( + + ), + [SurveyQuestionType.SingleChoice]: ( + + ), + [SurveyQuestionType.MultipleChoice]: ( + + ), + } + return mapping[question.type] +} + export function Questions({ survey, posthog, @@ -329,347 +291,6 @@ const closeSurveyPopup = (posthog: PostHog, survey: Survey) => { window.dispatchEvent(new Event('PHSurveyClosed')) } -export function OpenTextQuestion({ - question, - appearance, - onSubmit, - closeSurveyPopup, -}: { - question: BasicSurveyQuestion - appearance: SurveyAppearance - onSubmit: (text: string) => void - closeSurveyPopup: () => void -}) { - const textRef = useRef(null) - const [text, setText] = useState('') - - return ( -
- closeSurveyPopup()} /> - -