How to carry user choice for dark/light across all pages in an app #5394
-
First Check
Example CodeI am using the modularization example and added these 3 lines to the theme.py file:
dark = ui.dark_mode()
with ui.switch().bind_value(dark):
ui.tooltip('Toggle light and dark').classes('bg-green text-xl')
from contextlib import contextmanager
from menu import menu
from nicegui import ui
@contextmanager
def frame(navigation_title: str):
"""Custom page frame to share the same styling and behavior across all pages"""
ui.colors(primary='#6E93D6', secondary='#53B689', accent='#111B1E', positive='#53B689')
dark = ui.dark_mode()
with ui.header():
ui.label('Modularization Example').classes('font-bold')
ui.space()
ui.label(navigation_title)
ui.space()
with ui.switch().bind_value(dark):
ui.tooltip('Toggle light and dark').classes('bg-green text-xl')
with ui.row():
menu()
with ui.column().classes('absolute-center items-center'):
yieldDescriptionApp works as per the modularization example. NiceGUI Version3.2.0 Python Version3.12 BrowserFirefox Operating SystemWindows Additional ContextVSCode as my editor |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi @brunnenhof, I'd recommend binding both ui.dark_mode().bind_value(app.storage.user, 'dark_mode')
...
ui.switch('Dark Mode').bind_value(app.storage.user, 'dark_mode').props('color=white')Note that I chose "color=white". Otherwise the color turns "primary" when activating the switch, so the switch would be invisible on the "primary"-colored header. |
Beta Was this translation helpful? Give feedback.
-
|
Works perfect! Thanks - and thanks for the quick answer! |
Beta Was this translation helpful? Give feedback.
Hi @brunnenhof,
I'd recommend binding both
ui.dark_modeandui.switchagainstapp.storage.user:Note that I chose "color=white". Otherwise the color turns "primary" when activating the switch, so the switch would be invisible on the "primary"-colored header.