Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance useTheme to provide default theme without ThemeProvider #105

Merged
merged 4 commits into from
Sep 10, 2021
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
16 changes: 5 additions & 11 deletions main/SwitchToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {Animated, StyleProp, TouchableOpacity, ViewStyle} from 'react-native';
import {DoobooTheme, light} from './theme';
import React, {ReactElement, useEffect, useState} from 'react';

import {isEmptyObject} from './utils';
import styled from '@emotion/native';
import {withTheme} from '@emotion/react';
import {useTheme} from './theme/ThemeProvider';

interface Styles {
containerStyle?: ViewStyle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may remove prefix Style when it is grouped under Styles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll remove it in other PR. (because it is not related to this PR.)

Expand All @@ -21,7 +19,6 @@ interface Styles {
interface Props {
testID?: string;
isOn: boolean;
theme?: DoobooTheme;
style?: StyleProp<ViewStyle>;
styles?: Styles;
duration?: number;
Expand Down Expand Up @@ -50,7 +47,7 @@ const defaultCircleStyle: ViewStyle = {
borderRadius: 16,
};

function Component(props: Props): React.ReactElement {
export function SwitchToggle(props: Props): ReactElement {
const {
testID,
isOn,
Expand All @@ -62,10 +59,9 @@ function Component(props: Props): React.ReactElement {
onPress,
} = props;

const theme =
!props.theme || isEmptyObject(props.theme) ? light : props.theme;

const {primary, disabled, textContrast, placeholder} = theme;
const {
theme: {primary, disabled, textContrast, placeholder},
} = useTheme();

const {
backgroundColorOn = primary,
Expand Down Expand Up @@ -195,5 +191,3 @@ function Component(props: Props): React.ReactElement {
</TouchableOpacity>
);
}

export const SwitchToggle = withTheme(Component);
20 changes: 10 additions & 10 deletions main/theme/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Context {
colors: Colors;
}

const [useCtx, Provider] = createCtx<Context>();
const [useCtx, Provider] = createCtx<Context>({theme: light} as Context);

interface Props {
children?: React.ReactElement;
Expand All @@ -45,7 +45,7 @@ function ThemeProvider({

const colorScheme = useColorScheme();

const [themeType, setThemeType] = useState(initialThemeType || colorScheme);
const [themeType, setThemeType] = useState(initialThemeType ?? colorScheme);

useEffect(() => {
if (!initialThemeType) setThemeType(colorScheme);
Expand All @@ -61,10 +61,10 @@ function ThemeProvider({
setThemeType(themeTypeProp);
};

const defaultTheme =
themeType === 'dark'
? {...dark, ...customTheme?.dark}
: {...light, ...customTheme?.light};
const theme = {
light: {...light, ...customTheme?.light},
dark: {...dark, ...customTheme?.dark},
}[themeType ?? 'light'];

const media = {
isPortrait,
Expand All @@ -73,18 +73,18 @@ function ThemeProvider({
isDesktop,
};

const theme: DefaultTheme = {...defaultTheme, ...media};

return (
<Provider
value={{
media,
themeType,
changeThemeType,
theme: defaultTheme,
theme,
colors,
}}>
<OriginalThemeProvider theme={theme}>{children}</OriginalThemeProvider>
<OriginalThemeProvider theme={{...theme, ...media}}>
{children}
</OriginalThemeProvider>
</Provider>
);
}
Expand Down
14 changes: 4 additions & 10 deletions main/theme/createCtx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ import React from 'react';

type CreateCtx<A> = readonly [
() => A,
React.ProviderExoticComponent<React.ProviderProps<A | undefined>>,
React.ProviderExoticComponent<React.ProviderProps<A>>,
];

// create context with no upfront defaultValue
// without having to do undefined check all the time
function createCtx<A>(): CreateCtx<A> {
const ctx = React.createContext<A | undefined>(undefined);
function createCtx<A>(defaultContext: A): CreateCtx<A> {
const ctx = React.createContext<A>(defaultContext);

function useCtx(): A {
const c = React.useContext(ctx);

if (!c) throw new Error('useCtx must be inside a Provider with a value');

return c;
}
const useCtx = (): A => React.useContext(ctx);
Comment on lines +10 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very clever~! However, I am worried that many devs will be curious that useTheme works without a Provider because usually context wasn't provided like that 🤔

I hope there is more thing related to this to read about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyochan Ok. I will write about it soon.


// make TypeScript infer a tuple, not an array of union types
return [useCtx, ctx.Provider] as const;
Expand Down