-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support dark, light and auto theming (#494)
Requires ipyvuetify 1.9.0 for proper detection of dark_effective Co-authored-by: Maarten Breddels <[email protected]>
- Loading branch information
1 parent
3a6caf3
commit 238e09a
Showing
13 changed files
with
282 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from typing import Callable, cast | ||
|
||
import ipyvuetify.Themes | ||
from ipyvuetify.Themes import Theme | ||
|
||
import solara | ||
from solara.components.component_vue import component_vue | ||
from solara.tasks import Proxy | ||
|
||
theme = Proxy(Theme) | ||
ipyvuetify.Themes.theme = cast(ipyvuetify.Themes.Theme, theme) | ||
|
||
|
||
@component_vue("theming.vue") | ||
def _ThemeToggle( | ||
theme_dark: str, | ||
event_sync_themes: Callable[[str], None], | ||
enable_auto: bool, | ||
on_icon: str, | ||
off_icon: str, | ||
auto_icon: str, | ||
clicks: int = 1, | ||
): | ||
pass | ||
|
||
|
||
@solara.component | ||
def ThemeToggle( | ||
on_icon: str = "mdi-weather-night", | ||
off_icon: str = "mdi-weather-sunny", | ||
auto_icon: str = "mdi-brightness-auto", | ||
enable_auto: bool = True, | ||
): | ||
""" | ||
Insert a toggle switch for user to switch between light and dark themes. | ||
```solara | ||
import solara.lab | ||
@solara.component | ||
def Page(): | ||
solara.lab.ThemeToggle() | ||
``` | ||
## Arguments | ||
- `on_icon`: The icon to display when the dark theme is enabled. | ||
- `off_icon`: The icon to display when the dark theme is disabled. | ||
- `auto_icon`: The icon to display when the theme is set to auto. Only visible if `enable_auto` is `True`. | ||
- `enable_auto`: Whether to enable the auto detection of dark mode. | ||
""" | ||
|
||
def sync_themes(selected_theme: str): | ||
theme.dark = selected_theme | ||
|
||
return _ThemeToggle( | ||
theme_dark=theme.dark, | ||
event_sync_themes=sync_themes, | ||
enable_auto=enable_auto, | ||
on_icon="mdi-weather-night", | ||
off_icon="mdi-weather-sunny", | ||
auto_icon="mdi-brightness-auto", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<template> | ||
<v-btn | ||
icon | ||
@click="countClicks" | ||
> | ||
<v-icon | ||
:color="theme_dark ? 'primary' : null"> | ||
{{ this.clicks === 1 ? this.on_icon : this.clicks === 2 ? this.off_icon : this.auto_icon }} | ||
</v-icon> | ||
</v-btn> | ||
</template> | ||
<script> | ||
module.exports = { | ||
mounted() { | ||
if (window.solara) { | ||
if (localStorage.getItem(':solara:theme.variant')) { | ||
this.theme_dark = this.initTheme(); | ||
} | ||
} | ||
if ( this.theme_dark === false ) { | ||
this.clicks = 2; | ||
} else if ( this.theme_dark === null ) { | ||
this.clicks = 3; | ||
} | ||
this.lim = this.enable_auto ? 3 : 2; | ||
}, | ||
methods: { | ||
countClicks() { | ||
if ( this.clicks < this.lim ) { | ||
this.clicks++; | ||
} else { | ||
this.clicks = 1; | ||
} | ||
this.theme_dark = this.get_theme_bool( this.clicks ); | ||
}, | ||
get_theme_bool( clicks ) { | ||
if ( clicks === 3 ) { | ||
return null; | ||
} else if ( clicks === 2 ) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
}, | ||
stringifyTheme() { | ||
return this.theme_dark === true ? 'dark' : this.theme_dark === false ? 'light' : 'auto'; | ||
}, | ||
initTheme() { | ||
storedTheme = JSON.parse(localStorage.getItem(':solara:theme.variant')); | ||
return storedTheme === 'dark' ? true : storedTheme === 'light' ? false : null; | ||
}, | ||
setTheme() { | ||
if ( window.solara && this.theme_dark === null ) { | ||
this.$vuetify.theme.dark = this.prefersDarkScheme(); | ||
return; | ||
} | ||
this.$vuetify.theme.dark = this.theme_dark; | ||
}, | ||
prefersDarkScheme() { | ||
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches | ||
}, | ||
}, | ||
watch: { | ||
clicks (val) { | ||
if ( window.solara ) {theme.variant = this.stringifyTheme();} | ||
this.setTheme(); | ||
if ( window.solara ) {localStorage.setItem(':solara:theme.variant', JSON.stringify(theme.variant));} | ||
this.sync_themes(this.theme_dark); | ||
}, | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.