Skip to content

Commit

Permalink
Merge pull request #265 from iteratehq/sam/appearance-options
Browse files Browse the repository at this point in the history
Add support for new appearance options (light, dark, or auto)
  • Loading branch information
sambrown3 authored Jun 11, 2024
2 parents cf8b0da + 28e985a commit ab790dd
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 21 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -682,4 +682,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 329483eb6daf495a1eab8db2c188f13aaa25dcf9

COCOAPODS: 1.12.1
COCOAPODS: 1.15.2
2 changes: 0 additions & 2 deletions example/ios/example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
Expand Down
23 changes: 19 additions & 4 deletions src/components/Prompt/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,40 @@ import {
} from 'react-native';
import { Colors, Themes } from '../../constants';
import Iterate from '../../iterate';
import type { Survey } from '../../types';

interface Props {
color: string;
colorDark?: string;
onPress: () => void;
text: string;
survey?: Survey;
}

const PromptButton: (Props: Props) => JSX.Element = ({
color,
colorDark,
onPress,
text,
survey,
}) => {
const theme = Appearance.getColorScheme();
let backgroundColor;
let textColor;

const backgroundColor =
theme === Themes.Dark && colorDark != null ? colorDark : color;
const textColor = theme === Themes.Dark ? Colors.Black : Colors.White;
switch (survey?.appearance) {
case Themes.Dark:
backgroundColor = colorDark || color;
textColor = Colors.Black;
break;
case Themes.Light:
backgroundColor = color;
textColor = Colors.White;
break;
default:
Appearance.getColorScheme() === Themes.Dark
? ((backgroundColor = colorDark || color), (textColor = Colors.Black))
: ((backgroundColor = color), (textColor = Colors.White));
}

return (
<View>
Expand Down
57 changes: 47 additions & 10 deletions src/components/Prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,30 @@ const Prompt: (Props: Props) => JSX.Element = ({
}, ANIMATION_DURATION);
}, [dispatchShowSurvey, promptAnimation, survey]);

const theme = Appearance.getColorScheme();
const promptBackgroundColor =
theme === Themes.Dark ? Colors.LightBlack : Colors.White;
const promptTextColor = theme === Themes.Dark ? Colors.White : Colors.Black;
const shadowOpacity = theme === Themes.Dark ? 0.8 : 0.4;
let promptBackgroundColor;
let promptTextColor;
let shadowOpacity;

switch (survey?.appearance) {
case Themes.Dark:
promptBackgroundColor = Colors.LightBlack;
promptTextColor = Colors.White;
shadowOpacity = 0.8;
break;
case Themes.Light:
promptBackgroundColor = Colors.White;
promptTextColor = Colors.Black;
shadowOpacity = 0.4;
break;
default:
Appearance.getColorScheme() === Themes.Dark
? ((promptBackgroundColor = Colors.LightBlack),
(promptTextColor = Colors.White),
(shadowOpacity = 0.8))
: ((promptBackgroundColor = Colors.White),
(promptTextColor = Colors.Black),
(shadowOpacity = 0.4));
}

const paddingBottom = safeAreaInsets.bottom > 0 ? safeAreaInsets.bottom : 20;

Expand Down Expand Up @@ -150,7 +169,7 @@ const Prompt: (Props: Props) => JSX.Element = ({
]}
>
<View style={{ paddingBottom }}>
<CloseButton onPress={onDismissAnimated} />
<CloseButton onPress={onDismissAnimated} survey={survey} />
{markdown.Render(survey?.prompt?.message ?? '', {
body: [
promptTextStyle,
Expand All @@ -175,6 +194,7 @@ const Prompt: (Props: Props) => JSX.Element = ({
color={`${survey?.color || '#7457be'}`}
colorDark={survey?.color_dark}
onPress={showSurveyButtonClicked}
survey={survey}
/>
</View>
</View>
Expand Down Expand Up @@ -215,10 +235,27 @@ const styles = StyleSheet.create({
},
});

const CloseButton = ({ onPress }: { onPress: () => void }) => {
const theme = Appearance.getColorScheme();
const backgroundColor =
theme === Themes.Dark ? Colors.LightBlack : Colors.Grey;
const CloseButton = ({
onPress,
survey,
}: {
onPress: () => void;
survey?: Survey;
}) => {
let backgroundColor;

switch (survey?.appearance) {
case Themes.Dark:
backgroundColor = Colors.LightBlack;
break;
case Themes.Light:
backgroundColor = Colors.Grey;
break;
default:
Appearance.getColorScheme() === Themes.Dark
? (backgroundColor = Colors.LightBlack)
: (backgroundColor = Colors.Grey);
}

return (
<TouchableHighlight
Expand Down
23 changes: 19 additions & 4 deletions src/components/Survey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ const SurveyView: (Props: Props) => JSX.Element = ({

// Add theme
params.push(
`theme=${useColorScheme() === Themes.Dark ? Themes.Dark : Themes.Light}`
`theme=${
useColorScheme() === Themes.Dark || survey?.appearance === Themes.Dark
? Themes.Dark
: Themes.Light
}`
);

params.push('absoluteURLs=true');
Expand Down Expand Up @@ -181,9 +185,20 @@ const SurveyView: (Props: Props) => JSX.Element = ({
[dismiss, survey]
);

const theme = Appearance.getColorScheme();
const backgroundColor =
theme === Themes.Dark ? Colors.LightBlack : Colors.White;
let backgroundColor;

switch (survey?.appearance) {
case Themes.Dark:
backgroundColor = Colors.LightBlack;
break;
case Themes.Light:
backgroundColor = Colors.Grey;
break;
default:
Appearance.getColorScheme() === Themes.Dark
? (backgroundColor = Colors.LightBlack)
: (backgroundColor = Colors.Grey);
}

// Only do this if we haven't already
const addQueryParamScript = `if (!window.location.search) {
Expand Down
1 change: 1 addition & 0 deletions src/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const Colors = {
};

export const Themes = {
Auto: 'auto',
Light: 'light',
Dark: 'dark',
};
Expand Down
1 change: 1 addition & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export type PresentationStyle =
| 'overFullScreen';

export type Survey = {
appearance?: string;
color?: string;
color_dark?: string;
company_id: string;
Expand Down

0 comments on commit ab790dd

Please sign in to comment.