Skip to content

add ColorSchemeToggle#693

Merged
AnnMarieW merged 9 commits intosnehilvj:masterfrom
AnnMarieW:add-theme-switch-component
Feb 14, 2026
Merged

add ColorSchemeToggle#693
AnnMarieW merged 9 commits intosnehilvj:masterfrom
AnnMarieW:add-theme-switch-component

Conversation

@AnnMarieW
Copy link
Copy Markdown
Collaborator

@AnnMarieW AnnMarieW commented Jan 26, 2026

closes #680

This ColorSchemeToggle uses the use-computed-color-scheme as recomended by Mantine. It sets the color scheme value in local storage so it's persistent.

It also has a read-only prop computedColorScheme that can be used to trigger a dash callback.

Here is a sample app:

import dash_mantine_components as dmc
from dash import Dash, Input, Output
from dash_iconify import DashIconify

app = Dash()

light=dmc.Tooltip(
    DashIconify(icon="radix-icons:sun", width=20,),
    label="Light Mode"
)

dark = dmc.Tooltip(
    DashIconify(icon="radix-icons:moon", width=20),
    label="Dark Mode"
)


component = dmc.ColorSchemeToggle(
    lightIcon=light,
    darkIcon=dark,
    variant="filled",
    color="yellow",
    size="lg",
    m="xl",
    id="color-scheme-toggle",
)

app.layout = dmc.MantineProvider(
    [component, dmc.Text(id="color-scheme-output")],
    defaultColorScheme="dark"
)


@app.callback(
    Output("color-scheme-output", "children"),
    Input("color-scheme-toggle", "computedColorScheme"),
)
def update(scheme):
    return f"Current color scheme: {scheme}"

if __name__ == "__main__":
    app.run(debug=True)

@AnnMarieW
Copy link
Copy Markdown
Collaborator Author

AnnMarieW commented Jan 27, 2026

Added a helper function to prevent the flash of the wrong color scheme when the app starts and/or when refreshing a page.

Uses Dash index hook based on #680

In the app above, you just need to add 1 line of code

dmc.initalize_color_scheme()

So cool! 😎

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 27, 2026

Test Environment for snehilvj/dash-mantine-components-693
Updated on: 2026-01-27 19:12:04 UTC

@AnnMarieW
Copy link
Copy Markdown
Collaborator Author

Here's a sample app in this environment
https://py.cafe/amward/dash-color-scheme-toggle

@AnnMarieW
Copy link
Copy Markdown
Collaborator Author

Updated the docs to use this switch and added a page for ColorSchemeToggle component snehilvj/dmc-docs#283

Copy link
Copy Markdown
Collaborator

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool use of the index hook!

@AnnMarieW AnnMarieW merged commit 5b34443 into snehilvj:master Feb 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting correct light/dark theme

2 participants