question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set color mode colors via `theme.colors.modes`

See original GitHub issue

Is it possible to use color mode like in theme-ui:

colors: {
    ...chakraTheme.colors,
    accent1: '#FAFAFA',
    accent2: '#EAEAEA',
    accent3: '#999',
    accent4: '#888',
    modes: {
      dark: {
        accent1: '#111',
        accent2: '#333',
        accent3: '#999',
        accent4: '#FAFAFA',
      },
    },
  },

_Originally posted by @arnaudjnn in https://github.com/chakra-ui/chakra-ui/issues/1147#issuecomment-656531188_

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

15reactions
jesstelfordcommented, Aug 28, 2020

As a workaround for this kind of thing, I’ve created a Provider which sets the right colours directly onto the theme object by making a new .mode property available based on your theme config having a .modes.<light|dark> key:

// dynamic-color-mode.js
import React from 'react';
import { useTheme, useColorMode, ThemeProvider } from '@chakra-ui/core';

export const DynamicColorMode = (props) => {
  const { colorMode } = useColorMode();
  const theme = useTheme();

  return (
    <ThemeProvider
      {...props}
      theme={{
        // Chakra does a deep-merge of the theme objects
        colors: {
          mode: {
            // Retain access to all the default colours specified
            ...theme.colors,
            // Overwrite with mode-specific colors
            ...theme.colors.modes?.[colorMode],
          },
        },
      }}
    />
  );
};

Then use it like so:

import defaultTheme from '@chakra-ui/theme';
import { ChakraProvider, InitializeColorMode, Box } from '@chakra-ui/core';
import { DynamicColorMode } from './dynamic-color-mode';

const theme = {
  ...defaultTheme,
  colors: {
    ...defaultTheme.colors,
    border: '#888',
    modes: {
      dark: {
        bg: '#333'
      },
      light: {
        bg: '#ddd'
      }
    }
  },
}

export default function App() {
  return (
    <ChakraProvider theme={theme}>
      <InitializeColorMode />
      <DynamicColorMode>
        <Box bg="mode.bg" border="1px solid" borderColor="mode.border" />
      </DynamicColorMode>
    </ChakraProvider>
  );
}
11reactions
arnaudjnncommented, Jul 10, 2020

So we have to use a condition each time we use color?

<Text bg="accent2"/> is better than <Text bg={colorMode === "dark" ? "green.300" : "green.500"} />

Read more comments on GitHub >

github_iconTop Results From Across the Web

Color Modes
Color Modes. Color modes can be used to create a user-configurable dark mode or any number of other color modes. Defining colors. In...
Read more >
Colors and styling | Visualize and present data
Under Features, select Colors & Themes. Click on the Report Themes tab. Click on the New Theme button. Enter a name for your...
Read more >
prefers-color-scheme - CSS: Cascading Style Sheets | MDN
The prefers-color-scheme CSS media feature is used to detect if a user has requested light or dark color themes.
Read more >
Dark theme - Material Design
A dark theme displays dark surfaces across the majority of a UI. It's designed to be a supplemental mode to a default (or...
Read more >
Color modes
For example, to change the color mode of a dropdown menu, add data-bs-theme="light" or data-bs-theme="dark" to the parent .dropdown . Now, no matter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found