Skip to main content

React Native - Global StyleSheet and Dark/Light Theme design approach

We use StyleSheet in almost all components. Generally there are three different approaches to changing component layout with React Native:

  1. Direct layout change in the component (e.g. text field)
  2. Create a StyleSheet section in each page
  3. Create a Global StyleSheet and reference in each component
Personally speaking, I do not recommend the first 2 options due to:
  1. maintenance and inconsistency approach in design between different components
  2. harder to transfer to a new project.
  3. with GlobalStyles, you can put on a separate windows for easy view and update as you work on different component
  4. apply the dark/light theme to the component directly through GlobalStyles without having to customize it for each page

Theme - Dark and Light theme with React Native with iOS, Android, Web, etc.

Warning: if you use Expo Debugging, the system will default to Light theme (at least on iOS). Remove Remote Debugging if you want the Dark Theme to show up.

In your Navigagor, apply the theme:
  • const scheme = useColorScheme();
  • <NavigationContainer theme={scheme === 'dark' ? DarkTheme : DefaultTheme}>

In the import section, import the useTheme:

import { useTheme } from '@react-navigation/native';

In the function section, you can use the colors from the theme similar to below:

  • const { colors } = useTheme();

To use the theme, you can access using the command colors.background (for background color), colors.text (for text color), etc.

Apply this concept suing the GlobalStyles stylesheet and you can use it on the stylesheet directly for all your projects - see the GlobalStyles section below.

Customized layout


Just a note, the first option could be used if you require flexibility in your component (where a conditional (if/else) needs to be used in your code or customized style needs to be applied to the component directly

  • <Icon style={{...GlobalStyles.icon, color: iconColor, fontSize: iconSize }} />
<- Customized is on the red-text

GlobalStyles StyleSheet and reference Dark/Light Theme

Credit: the idea on using GlobalStyle/props/and hook to support Dark and Light Theme comes from this page: link.

For global stylesheet, create a stylesheet on a specific folder: 

E.g. Add the file to the root folder: globalStyles.js

Add the code similar to be low in the file:

import { StyleSheet } from 'react-native';
import { useTheme } from '@react-navigation/native';
import React from 'react';

const getGlobalStyles = (props) => StyleSheet.create({
    container: {
        backgroundColor: props.colors.background,
    },
}

function useGlobalStyles() {
    const { colors } = useTheme();
    const styles = React.useMemo(() => getGlobalStyles({ colors }), [colors]);
    return styles;
}

export default useGlobalStyles;

In the component that you want to use the Global Style, import the StyleSheet:
import useGlobalStyles from '../globalStyles.js';

export default function Feed({ content, onProfileRequested, navigation }) {

Now to use the GlobalStyles in the component:

const globalStyles = useGlobalStyles();
<TouchableOpacity style={globalStyles.flatListRow} ....

That is about it... easy to use and edit!





 

Comments

Popular posts from this blog

Checked Task Pro Instruction (BlackBerry 10)

  Checked Instruction I just started working on this page, please send me comments and suggestions that could improve this site. Please note that if you use the Android app, the instruction is similar to the BlackBerry app below. I will create a separate instruction page for Android app soon. Contents 1  Initial Set-up after Installation 2  SMART task system that combine the best of GTD (Getting Things Done) and MYN  3  Smart Add - natural language date parser 4  ***NEW*** Shortcuts 5  Back-up and Restore - How to Transfer data from one device to another 6  BES - Work Space - Add a public app from the BlackBerry World storefront 7  Settings options that will definitely increase your productivity 8  Automatically add email as task 9  Tips on improving performance 10  Add a category 11  Delete a category 12  Keep categories permanently 13  Add sub-task 14  Automatic Synchronize from remember 15  Sort opti...

Checked! Task Pro - Blackberry Instruction

  Checked Instruction I just started working on this page, please send me comments and suggestions that could improve this site. Please note that if you use the Android app, the instruction is similar to the BlackBerry app below. I will create a separate instruction page for Android app soon. Contents 1  Initial Set-up after Installation 2  SMART task system that combine the best of GTD (Getting Things Done) and MYN  3  Smart Add - natural language date parser 4  ***NEW*** Shortcuts 5  Back-up and Restore - How to Transfer data from one device to another 6  BES - Work Space - Add a public app from the BlackBerry World storefront 7  Settings options that will definitely increase your productivity 8  Automatically add email as task 9  Tips on improving performance 10  Add a category 11  Delete a category 12  Keep categories permanently 13  Add sub-task 14  Automatic Synchronize from remember 15  Sort opti...

Setting up Mac for Windows users (users that need to be use both Mac + Windows)

My personal usage... please make sure the software you use is safe prior to downloading: AltTab -> Switching windows Change keyboard short to use Control vs Command Mouse setting: disable natural scrolling (make it similar to windows scrolling) Safari View -> Show Favorites Bar App Store: download ad blocker.. I use AdGuard but people said good things about Ghostery BetterTouchControl ( https://boastr.net/ ) Setup back and forward Logitech button (or any other mouse). Normal Mice Tab: Add Key and select the key to replaceBack and Forward button control Terminal: setup Control + C to break the current command (instead of Command + C)