Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/wide-dolls-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'posthog-react-native': minor
'@posthog/core': minor
---

fix survey text color on react native
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export type SurveyAppearance = {
submitButtonTextColor?: string
ratingButtonColor?: string
ratingButtonActiveColor?: string
inputBackground?: string
autoDisappear?: boolean
displayThankYouMessage?: boolean
thankYouMessageHeader?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from 'react'
import { StyleSheet, Text, View, ViewStyle } from 'react-native'

import { getContrastingTextColor, shouldRenderDescription, SurveyAppearanceTheme } from '../surveys-utils'
import {
defaultDescriptionOpacity,
getContrastingTextColor,
shouldRenderDescription,
SurveyAppearanceTheme,
} from '../surveys-utils'
import { SurveyQuestionDescriptionContentType } from '@posthog/core'
import { BottomSection } from './BottomSection'

Expand All @@ -28,7 +33,9 @@ export function ConfirmationMessage({
<View style={styleOverrides}>
<View style={styles.thankYouMessageContainer}>
<Text style={[styles.thankYouMessageHeader, { color: textColor }]}>{header}</Text>
{shouldRenderDescription(description, contentType) && <Text>{description}</Text>}
{shouldRenderDescription(description, contentType) && (
<Text style={{ color: textColor, opacity: defaultDescriptionOpacity }}>{description}</Text>
)}
</View>
{isModal && (
<BottomSection
Expand Down
17 changes: 14 additions & 3 deletions packages/react-native/src/surveys/components/QuestionHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@ import React from 'react'
import { StyleSheet, Text, View } from 'react-native'

import { SurveyQuestionDescriptionContentType } from '@posthog/core'
import { shouldRenderDescription } from '../surveys-utils'
import {
defaultDescriptionOpacity,
getContrastingTextColor,
shouldRenderDescription,
SurveyAppearanceTheme,
} from '../surveys-utils'

export function QuestionHeader({
question,
description,
descriptionContentType,
appearance,
}: {
question: string
description?: string | null
descriptionContentType?: SurveyQuestionDescriptionContentType
appearance: SurveyAppearanceTheme
}): JSX.Element {
const textColor = getContrastingTextColor(appearance.backgroundColor)

return (
<View style={styles.container}>
<Text style={styles.question}>{question}</Text>
<Text style={[styles.question, { color: textColor }]}>{question}</Text>
{shouldRenderDescription(description, descriptionContentType) && (
<Text style={styles.description}>{description}</Text>
<Text style={[styles.description, { color: textColor, opacity: defaultDescriptionOpacity }]}>
{description}
</Text>
)}
</View>
)
Expand Down
48 changes: 40 additions & 8 deletions packages/react-native/src/surveys/components/QuestionTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
VeryDissatisfiedEmoji,
VerySatisfiedEmoji,
} from '../icons'
import { getContrastingTextColor, getDisplayOrderChoices, SurveyAppearanceTheme } from '../surveys-utils'
import {
defaultRatingLabelOpacity,
getContrastingTextColor,
getDisplayOrderChoices,
SurveyAppearanceTheme,
} from '../surveys-utils'
import {
SurveyQuestion,
SurveyRatingDisplay,
Expand Down Expand Up @@ -41,13 +46,25 @@ export function OpenTextQuestion({
question={question.question}
description={question.description}
descriptionContentType={question.descriptionContentType}
appearance={appearance}
/>
<View style={styles.textInputContainer}>
<TextInput
style={styles.textInput}
style={[
styles.textInput,
{
backgroundColor: appearance.inputBackground,
color: getContrastingTextColor(appearance.inputBackground),
},
]}
multiline
numberOfLines={4}
placeholder={appearance.placeholder}
placeholderTextColor={
getContrastingTextColor(appearance.inputBackground) === 'black'
? 'rgba(0, 0, 0, 0.5)'
: 'rgba(255, 255, 255, 0.5)'
}
onChangeText={setText}
value={text}
/>
Expand Down Expand Up @@ -77,6 +94,7 @@ export function LinkQuestion({
question={question.question}
description={question.description}
descriptionContentType={question.descriptionContentType}
appearance={appearance}
/>
<BottomSection
text={question.buttonText ?? appearance.submitButtonText ?? 'Submit'}
Expand Down Expand Up @@ -106,6 +124,7 @@ export function RatingQuestion({
question={question.question}
description={question.description}
descriptionContentType={question.descriptionContentType}
appearance={appearance}
/>
<View style={styles.ratingSection}>
<View style={styles.ratingOptions}>
Expand Down Expand Up @@ -140,8 +159,16 @@ export function RatingQuestion({
)}
</View>
<View style={styles.ratingText}>
<Text>{question.lowerBoundLabel}</Text>
<Text>{question.upperBoundLabel}</Text>
<Text
style={{ color: getContrastingTextColor(appearance.backgroundColor), opacity: defaultRatingLabelOpacity }}
>
{question.lowerBoundLabel}
</Text>
<Text
style={{ color: getContrastingTextColor(appearance.backgroundColor), opacity: defaultRatingLabelOpacity }}
>
{question.upperBoundLabel}
</Text>
</View>
</View>
<BottomSection
Expand Down Expand Up @@ -204,16 +231,23 @@ export function MultipleChoiceQuestion({
question={question.question}
description={question.description}
descriptionContentType={question.descriptionContentType}
appearance={appearance}
/>
<View style={styles.multipleChoiceOptions}>
{choices.map((choice: string, idx: number) => {
const isOpenChoice = choice === openChoice
const isSelected = selectedChoices.includes(choice)

const inputTextColor = getContrastingTextColor(appearance.inputBackground)

return (
<Pressable
key={idx}
style={[styles.choiceOption, isSelected ? { borderColor: 'black' } : {}]}
style={[
styles.choiceOption,
{ backgroundColor: appearance.inputBackground },
isSelected ? { borderColor: getContrastingTextColor(appearance.backgroundColor) } : {},
]}
onPress={() => {
if (allowMultiple) {
setSelectedChoices(
Expand All @@ -225,7 +259,7 @@ export function MultipleChoiceQuestion({
}}
>
<View style={styles.choiceText}>
<Text style={{ flexGrow: 1 }}>
<Text style={{ flexGrow: 1, color: inputTextColor }}>
{choice}
{isOpenChoice ? ':' : ''}
</Text>
Expand Down Expand Up @@ -294,7 +328,6 @@ const styles = StyleSheet.create({
borderWidth: 1,
padding: 10,
marginVertical: 10,
backgroundColor: 'white',
fontSize: 16,
},
ratingSection: {
Expand Down Expand Up @@ -343,7 +376,6 @@ const styles = StyleSheet.create({
borderColor: 'grey',
borderRadius: 5,
padding: 10,
backgroundColor: 'white',
},
choiceText: {
flexDirection: 'row',
Expand Down
9 changes: 8 additions & 1 deletion packages/react-native/src/surveys/surveys-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export function shouldRenderDescription(
}

export const defaultBackgroundColor = '#eeeded' as const
export const defaultDescriptionOpacity = 0.8
export const defaultRatingLabelOpacity = 0.7

export type SurveyAppearanceTheme = Omit<
Required<SurveyAppearance>,
Expand All @@ -48,6 +50,7 @@ export const defaultSurveyAppearance: SurveyAppearanceTheme = {
submitButtonTextColor: 'white',
ratingButtonColor: 'white',
ratingButtonActiveColor: 'black',
inputBackground: 'white',
borderColor: '#c9c6c6',
placeholder: 'Start typing...',
displayThankYouMessage: true,
Expand Down Expand Up @@ -290,7 +293,11 @@ export function getContrastingTextColor(color: string): 'black' | 'white' {

function hex2rgb(c: string): string {
if (c.startsWith('#')) {
const hexColor = c.replace(/^#/, '')
let hexColor = c.replace(/^#/, '')
// Handle 3-character shorthand (e.g., #111 -> #111111, #abc -> #aabbcc)
if (/^[0-9A-Fa-f]{3}$/.test(hexColor)) {
hexColor = hexColor[0] + hexColor[0] + hexColor[1] + hexColor[1] + hexColor[2] + hexColor[2]
}
if (!/^[0-9A-Fa-f]{6}$/.test(hexColor)) {
return 'rgb(255, 255, 255)'
}
Expand Down
Loading