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.

Semantic tokens don't work with DarkMode and LightMode component

See original GitHub issue

Description

Semantic tokens don’t work with DarkMode component.

Link to Reproduction

https://codesandbox.io/s/intelligent-thompson-563p3?file=/src/index.tsx

Steps to reproduce

This code is not working:

const App = () => (
  <ChakraProvider
    theme={extendTheme({
      semanticTokens: {
        colors: {
          text: {
            default: "red.700",
            _dark: "red.200"
          }
        }
      }
    })}
  >
    <DarkMode>
      <GlobalStyle />
      <Heading color="text">Welcome to Chakra + TS</Heading>
    </DarkMode>
  </ChakraProvider>
);
  1. Go to ‘https://codesandbox.io/s/intelligent-thompson-563p3?file=/src/index.tsx
  2. Heading should be in light red.

Chakra UI Version

1.8.1

Browser

No response

Operating System

  • macOS
  • Windows
  • Linux

Additional Information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
TimKolbergercommented, Feb 5, 2022

This is currently a documented edge case.

The CSS variables are attached to the host element (CSS selector :host, :root) and cannot react to the components <LightMode /> and <DarkMode />, data-theme attributes nor body class .chakra-ui-dark. It listens to e.g. html[data-theme=“dark”]. - Docs

We are working on a feature to re-attach the css vars to an element to create a new token layer - this would enable the semantic tokens to react to data-theme attributes.

2reactions
TimKolbergercommented, Apr 8, 2022

It was released with a slight twist.

The css variables need a DOM Element to attach them again. The LightMode and DarkMode components do not render an Element, they render only a React Context Provider. Rendering the Element inside of DarkMode would be a breaking change and would introduce a circular dependency.

This code example shows how to use the _dark values of your Semantic Tokens.

<DarkMode>
  <div data-theme="dark">
    <chakra.div color="your-semantic-token">
      This uses always the _dark value of your semantic token
    </chakra.div>
  </div>
</DarkMode>

We know that this API is not the most intuitive, and we will revisit this API in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dark mode with design tokens - UX Collective
Dark & light mode with semantic tokens​​ Choose token names that don't reference a mode (e.g. color-text-primary ) and avoid words like light...
Read more >
Dark Mode with Style Dictionary | dbanksdesign - Danny Banks
Using CSS custom properties and having a layer of semantic tokens allows you to not have to re-style every component for light and...
Read more >
Color Mode - Chakra UI
Color Mode. Chakra UI comes with built-in support for managing color mode in your apps. By default, most of Chakra's components are dark...
Read more >
The hardest part about building dark mode is that people think ...
Join Jacob and Ryhan to understand how Figma shipped one of their most requested features, what barriers you'll face implementing your own, ...
Read more >
Color - Reshaped design system
By assigning semantic roles to colors, we achieve predictable theming and dark mode support. We use generic names for the color tokens, which...
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