diff --git a/src/components/App.tsx b/src/components/App.tsx index 9a220db..1d63070 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -10,10 +10,12 @@ import { DarkTheme, } from '@react-navigation/native'; import { useColorScheme } from 'react-native'; +import { ColorSchemeContext, themes } from './Colors'; export default function App(): JSX.Element { const colorScheme = useColorScheme(); - // const colorScheme = 'dark'; + const theme = themes[colorScheme || 'light']; + // const theme = 'dark'; useEffect(() => { async function setupTrackPlayer() { @@ -35,9 +37,11 @@ export default function App(): JSX.Element { return ( - - - + + + + + ); diff --git a/src/components/Button.tsx b/src/components/Button.tsx index a1be682..f1b7cdc 100644 --- a/src/components/Button.tsx +++ b/src/components/Button.tsx @@ -1,7 +1,7 @@ import React, { useCallback, useState } from 'react'; import { SvgProps } from 'react-native-svg'; import { - PressableProps, + PressableProps, ViewProps, } from 'react-native'; import { THEME_COLOR } from 'CONSTANTS'; import styled, { css } from 'styled-components/native'; @@ -10,6 +10,7 @@ import useDefaultStyles from './Colors'; interface ButtonProps extends PressableProps { icon?: React.FC; title: string; + style?: ViewProps['style']; } interface PressableStyleProps { diff --git a/src/components/Colors.ts b/src/components/Colors.ts index 5219d80..0b77b60 100644 --- a/src/components/Colors.ts +++ b/src/components/Colors.ts @@ -1,10 +1,13 @@ import { THEME_COLOR } from 'CONSTANTS'; -import { StyleSheet, useColorScheme } from 'react-native'; - -export default function useDefaultStyles() { - const scheme = useColorScheme(); - // const scheme = 'dark'; +import React from 'react'; +import { useContext } from 'react'; +import { ColorSchemeName, StyleSheet } from 'react-native'; +/** + * Function for generating both the dark and light stylesheets, so that they + * don't have to be generate on every individual component render + */ +function generateStyles(scheme: ColorSchemeName) { return StyleSheet.create({ text: { color: scheme === 'dark' ? '#fff' : '#000', @@ -44,10 +47,29 @@ export default function useDefaultStyles() { }); } +// Prerender both stylesheets +export const themes: Record<'dark' | 'light', ReturnType> = { + 'dark': generateStyles('dark'), + 'light': generateStyles('light'), +}; + +// Create context for supplying the theming information +export const ColorSchemeContext = React.createContext(themes.dark); + +/** + * Retrieves the default styles object in hook form + */ +export default function useDefaultStyles() { + return useContext(ColorSchemeContext); +} + interface DefaultStylesProviderProps { children: (defaultStyles: ReturnType) => JSX.Element; } +/** + * A render props component to supply the defaultStyles object. + */ export function DefaultStylesProvider(props: DefaultStylesProviderProps) { const defaultStyles = useDefaultStyles();