#sx( theme.colorScheme ) wrong
13 messages · Page 1 of 1 (latest)
Reproduce in a codesandbox please.
there is something wrong with my setup for sure, because it works on a fresh codesandbox.io project
<Text
sx={(theme) => ({
// subscribe to color scheme changes
backgroundColor: theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.colors.blue[0],
})}
>
My custom text
</Text>
pass your styles in parentheses. do not return the object
the notation () => ({}) is equivalent to () => {return {}}
it works by the way, the thing that is not working is that t.colorScheme is always returning light
although the outer colorscheme hook correctly switches between light and dark
oh I did not know that.thanks by the way
if you were only to use () => {}, it would open a new scope and return nothing, the parens "( )" are saying, "hey this is not a new scope by the way, just returning this object"
@tall flame I tested it on sandbox and the older version worked properly with dark mode switch but the new one doesn't seem to be working correctly, here you can test it yourself https://codesandbox.io/s/mantine-xxb1nv?file=/src/App.tsx
A couple of things:
- You might be confusing
useColorSchemewhich uses a media query to get the OS theme (https://mantine.dev/hooks/use-color-scheme/) withuseMantineColorScheme(https://mantine.dev/guides/dark-theme/#colorschemeprovider) - You cannot use
useMantineColorSchemein the same component that has the<ColorSchemeProvider>. You can only use it in nested components. This is standard for all components that rely on React Contexts. I think this provides the behavior that you are looking for? https://codesandbox.io/s/mantine-forked-4627gz?file=/src/App.tsx
@tall flame I think I understand what's the issue, I just copied the example from the documentation and didn't realize that preferredColorScheme is only the initial value, but not the driver.
once you change it to theme={{ colorScheme: preferredColorScheme }} , it works correctly