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 1 commit
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);
12 changes: 11 additions & 1 deletion main/theme/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import React, {useEffect, useState} from 'react';

import {ColorSchemeName} from 'react-native';
import {DoobooTheme} from '.';
import createCtx from './createCtx';
import useColorScheme from './useColorScheme';
import {useMediaQuery} from 'react-responsive';
Expand Down Expand Up @@ -89,4 +90,13 @@ function ThemeProvider({
);
}

export {useCtx as useTheme, ThemeProvider, withTheme};
const useTheme = (): Context | {theme: DoobooTheme} => {
const currentTheme = useCtx();
const defaultTheme = light;

if (!currentTheme) return {theme: defaultTheme}!;

return currentTheme;
};

export {useTheme, ThemeProvider, withTheme};
10 changes: 2 additions & 8 deletions main/theme/createCtx.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

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

Expand All @@ -10,13 +10,7 @@ type CreateCtx<A> = readonly [
function createCtx<A>(): CreateCtx<A> {
const ctx = React.createContext<A | undefined>(undefined);

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 | undefined => React.useContext(ctx);

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