Add Dark Mode Option for Documentation Page #771
Replies: 5 comments 8 replies
-
Good idea, @Slanderkin! But we should probably store the user preference in some session data, so that the dark mode is not always reset when the user visits the site again. |
Beta Was this translation helpful? Give feedback.
-
@Slanderkin After thinking about it once again: Why should we use a toggle instead of simply setting dark mode to "auto", i.e. automatically setting the theme based on the user's system settings? |
Beta Was this translation helpful? Give feedback.
-
I've come up with this so far. Which kinda works..? from functools import partial
from nicegui import ui, app
async def manage_darkmode(current_theme, dark):
if current_theme['theme'] == 'OS-default':
dark.auto()
elif current_theme['theme'] == 'Dark':
dark.enable()
else:
dark.disable()
await ui.run_javascript(f'localStorage.setItem("theme", "{current_theme["theme"]}")', respond=False)
@ui.page("/")
async def index():
current_theme = {'theme': None}
dark = ui.dark_mode()
ui.select(
['OS-default', 'Dark', 'Light'], on_change=partial(manage_darkmode, current_theme, dark)).bind_value(current_theme, 'theme')
app.on_connect(partial(get_local_storage, current_theme, dark))
async def get_local_storage(current_theme, dark):
current_theme['theme'] = await ui.run_javascript(f'localStorage.getItem("theme");')
await manage_darkmode(current_theme, dark)
ui.run() The code can probably be simplified and improved more as well, but I'm still not familiar enough with NiceGUI and Python to know how |
Beta Was this translation helpful? Give feedback.
-
With the new storage API (planned for 1.2.17) we can simplify this code quite a bit: from nicegui import Client, app, ui
@ui.page('/')
async def page(client: Client):
await client.connected()
app.storage.user['dark_mode'] = app.storage.user.get('dark_mode', False)
ui.dark_mode().bind_value(app.storage.user, 'dark_mode')
ui.toggle({False: 'Light', True: 'Dark', None: 'Auto'}).bind_value(app.storage.user, 'dark_mode')
ui.run(storage_secret='secret') And we're planning to automatically initialize a dictionary value for missing keys when binding to UI, so the line Unfortunately there's a short flash of white background before switching to dark mode, because the user storage is only available after the client is connected. We should be able to replace Apart from the theme selection UI, the main challenge is to update the website to support both light and dark mode. If you run main.py with |
Beta Was this translation helpful? Give feedback.
-
I just started to implement this feature in PR #1164. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think it would be nice to be able to have a toggle on the documentation page so that you can view it in dark mode, or just have a dark version of the documentation pages.
Beta Was this translation helpful? Give feedback.
All reactions