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

Should this be a good way for multiple theming? #4

Open
Saad-Bashar opened this issue Oct 4, 2021 · 2 comments
Open

Should this be a good way for multiple theming? #4

Saad-Bashar opened this issue Oct 4, 2021 · 2 comments
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@Saad-Bashar
Copy link

Need some review on using multiple themes. So I have followed the doc and added light, dark themes separately. Let’s say I have to configure some other values like fontFamily and spacing, in that case, I have to duplicate them both inside light and dark. So I did create 3 themes like so

export const light = createTheme({
    colors: {
      // Define your theme colors for light mode
      primary: "#7C5DFA",
      primaryLight: "#9277FF",
      paper: "#F8F8F8",

    }
  })
  
export const dark = createTheme({
    colors: {
      // Define your theme colors for dark mode
      primary: '#000'
    }
})


const t = createTheme({
    fontFamilies: {
        'sans': 'Spartan-Medium' ,
        'sansBold': 'Spartan-Bold',
    },
})

export default t

So I am using the default theme everywhere and for colors, I am using the light or dark based on the theme appearance which is being passed to the app through a context. Even though I am using the color theme just for colors, they do have all other properties just like a normal theme object. Do you think its okay to do so or any other approach I should follow?

@arabold
Copy link
Owner

arabold commented Oct 6, 2021

I think this is an okay approach. Keep in mind that all the properties you pass to the createTheme are simple objects. So, there's also no reason you couldn't do something like this:

const commonStyles = {
    fontFamilies: {
        'sans': 'Spartan-Medium' ,
        'sansBold': 'Spartan-Bold',
    }
}

export const light = createTheme({
    ...commonStyles,
    colors: {
      // Define your theme colors for light mode
      primary: "#7C5DFA",
      primaryLight: "#9277FF",
      paper: "#F8F8F8",
    }
  })
  
export const dark = createTheme({
    ...commonStyles,
    colors: {
      // Define your theme colors for dark mode
      primary: '#000'
    }
})

const t = { light, dark }
export default t

And then access it like t.light.primary, t.dark.fontSans. Not sure if that would easier or not... 🤔 You can also try to use a hook to select the right theme ad-hoc:

/**
 * Chooses light or dark theme depending on the user's color scheme
 */
export default function useTheme() {
  const colorScheme = useColorScheme()
  return colorScheme === 'dark' ? dark : light
}

@arabold arabold added documentation Improvements or additions to documentation question Further information is requested labels Oct 6, 2021
@Saad-Bashar
Copy link
Author

Thank you so much for your elaborate answer. Your pattern does look cleaner :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants