Color schemes
MantineProvider manages color scheme context in your application.
You can configure the default color scheme value with defaultColorScheme
prop, possible values are light
,
dark
and auto
(system color scheme is used). The default value is light
.
data-mantine-color-scheme attribute
When MantineProvider is mounted, it sets data-mantine-color-scheme
attribute on <html />
element to the value that the user has selected previously or to the value of defaultColorScheme
prop.
data-mantine-color-scheme
attribute is used in all components styles to determine which colors should component use.
use-mantine-color-scheme hook
useMantineColorScheme
hook can be used to get and set current color scheme value:
use-computed-color-scheme hook
useComputedColorScheme
returns a computed color scheme value, it returns either light
or dark
.
It can be used to implement color scheme toggle logic:
Color scheme value caveats
By default, the color scheme value is stored in local storage, and its value is saved in state before the component is mounted to avoid flash of inaccurate color scheme. This means that color scheme value can be different on client and server, as server does not have access to local storage and always uses the default value.
If you have server side rendering in your application (for example, if you use Next.js or Remix), then you cannot use colorScheme
value in your application to avoid hydration issues. Instead, you can use dark
and light
mixins from postcss-preset-mantine to generate styles that will
hide elements based on color scheme value:
colorScheme for client only applications
You can safely use
colorScheme
value in client only applications (for example, Vite or create-react-app applications). In this case, there is no hydration, and thus hydration error cannot occur.
ColorSchemeScript
ColorSchemeScript
component renders script tag that sets data-mantine-color-scheme
attribute
on <html />
element to user selected value or to defaultColorScheme
prop value before
hydration. It is used to avoid flash of inaccurate color scheme in server side rendered applications,
for example Next.js or Remix. Follows framework specific guides
to learn where to render ColorSchemeScript
component.
You can add any additional props to the <script />
tag generated by ColorSchemeScript
component,
for example, you can add nonce attribute:
Color scheme manager
By default, color scheme value is stored in local storage, but you can implement your own color scheme manager to store the value in any other external storage.
Color scheme manager must have the following methods:
Usually, it is better to wrap color scheme manager in a creator function to provide a way to configure it. Default local storage based color scheme manager example:
Then custom color scheme manager can be passed to MantineProvider:
Default color scheme
The default color scheme value is used when the user has not selected any color scheme yet.
It is required to be set both on MantineProvider and
ColorSchemeScript
. If defaultColorScheme
is not set, then light
is used –
your application color scheme will depend on the system color scheme.
Force color scheme
You can force the color scheme value to be either light
or dark
with forceColorScheme
prop.
It is required to be set both on MantineProvider and
ColorSchemeScript
. If forceColorScheme
is set, then defaultColorScheme
and colorSchemeManager
are ignored.
When forceColorScheme
is set, it is not possible to change color scheme value with setColorScheme
function.