-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: load theme earlier when using SSG (#557)
* fix: load theme earlier when using SSG * implement suggestions from code review * Apply suggestions from code review Co-authored-by: Maarten Breddels <[email protected]> --------- Co-authored-by: Maarten Breddels <[email protected]>
- Loading branch information
1 parent
61f6d64
commit 3aaa4b3
Showing
3 changed files
with
68 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,11 @@ | |
|
||
{% block header %} | ||
|
||
|
||
{{ pre_rendered_css | safe }} | ||
|
||
{% if vue3 == True %} | ||
<link href="{{cdn}}/@widgetti/[email protected]/dist/main{{ipywidget_major_version}}.css" rel="stylesheet"></link> | ||
<link href="{{cdn}}/@widgetti/[email protected]/dist/main{{ipywidget_major_version}}.css" rel="stylesheet" class="solara-template-css"></link> | ||
{% else %} | ||
<link href="{{cdn}}/@widgetti/[email protected]/dist/main{{ipywidget_major_version}}.css" rel="stylesheet"></link> | ||
<link href="{{cdn}}/@widgetti/[email protected]/dist/main{{ipywidget_major_version}}.css" rel="stylesheet" class="solara-template-css"></link> | ||
{% endif %} | ||
|
||
|
||
|
@@ -27,8 +25,7 @@ | |
{{ resources.include_css("/static/highlight.css") }} | ||
{{ resources.include_css("/static/highlight-dark.css") }} | ||
{{ resources.include_css("/static/assets/style.css") }} | ||
<style id="jupyter-theme-css"> | ||
{{ resources.include_css("/static/assets/theme-"~theme.variant~".css") }} | ||
<style id="jupyter-theme-css" class="solara-template-css"> | ||
</style> | ||
{{ resources.include_css("/static/assets/custom.css") }} | ||
|
||
|
@@ -38,9 +35,24 @@ | |
"kernelId": "1234" | ||
} | ||
</script> | ||
<style> | ||
<style class="solara-template-css"> | ||
{% include "loader-"~theme.loader~".css" %} | ||
</style> | ||
<!-- Include Vuetify background colours so static html from SSG renders the right general colour theme | ||
before first render. We remove these after Vue takes over rendering to avoid collisions --> | ||
<style id="pre-render-theme"> | ||
.theme--light .v-sheet { | ||
background-color: #fff; | ||
border-color: #fff; | ||
color: rgba(0,0,0,.87) | ||
} | ||
.theme--dark .v-sheet { | ||
background-color: #1e1e1e; | ||
border-color: #1e1e1e; | ||
color: #fff | ||
} | ||
</style> | ||
{% endblock header %} | ||
</head> | ||
{% raw -%} | ||
|
@@ -175,6 +187,38 @@ | |
{# next div is used in ssg code to see if vue took over rendering #} | ||
<div id="pre-rendered-html-present" style="display: none"></div> | ||
</div> | ||
<script> | ||
var theme = {{ theme | tojson | safe }} | ||
function getThemeVariant() { | ||
if (localStorage.getItem(':solara:theme.variant')) { | ||
return JSON.parse(localStorage.getItem(':solara:theme.variant')) | ||
} | ||
return theme.variant; | ||
} | ||
if (localStorage.getItem(':solara:theme.variant')) { | ||
theme.variant = JSON.parse(localStorage.getItem(':solara:theme.variant')) | ||
} | ||
function prefersDarkScheme() { | ||
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches | ||
} | ||
function inDarkMode() { | ||
if (getThemeVariant() == 'auto') { | ||
return prefersDarkScheme(); | ||
} | ||
else { | ||
return getThemeVariant() == 'dark'; | ||
} | ||
} | ||
// Init theme | ||
let appContainer = document.getElementById('app'); | ||
if (inDarkMode()) { | ||
appContainer.classList.remove('theme--light'); | ||
appContainer.classList.add('theme--dark'); | ||
} else { | ||
appContainer.classList.remove('theme--dark'); | ||
appContainer.classList.add('theme--light'); | ||
} | ||
</script> | ||
{% block after_pre_rendered_html %}{% endblock %} | ||
{% if vue3 == True %} | ||
<link href="{{cdn}}/@widgetti/[email protected]/dist/fonts.css" rel="stylesheet"></link> | ||
|
@@ -194,6 +238,12 @@ | |
<script> | ||
solara.rootPath = {{ root_path | tojson | safe}}; | ||
console.log("rootPath", solara.rootPath); | ||
async function changeThemeCSS(theme) { | ||
let css = await fetch(`${solara.rootPath}/static/assets/theme-${theme}.css`).then(r => r.text()); | ||
document.getElementById('jupyter-theme-css').innerHTML = css; | ||
} | ||
changeThemeCSS(inDarkMode() ? 'dark' : 'light'); | ||
</script> | ||
|
||
{{ resources.include_js("/static/assets/custom.js") }} | ||
|
@@ -208,27 +258,6 @@ | |
solara.production = {{ production | tojson | safe }}; | ||
const themeVariants = ['light', 'dark', 'auto'] | ||
solara.preRendered = {{ pre_rendered_html | safe | length | tojson }} > 0 | ||
var theme = {{ theme | tojson | safe }} | ||
function getThemeVariant() { | ||
if (localStorage.getItem(':solara:theme.variant')) { | ||
return JSON.parse(localStorage.getItem(':solara:theme.variant')) | ||
} | ||
return theme.variant; | ||
} | ||
if (localStorage.getItem(':solara:theme.variant')) { | ||
theme.variant = JSON.parse(localStorage.getItem(':solara:theme.variant')) | ||
} | ||
function prefersDarkScheme() { | ||
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches | ||
} | ||
function inDarkMode() { | ||
if (getThemeVariant() == 'auto') { | ||
return prefersDarkScheme(); | ||
} | ||
else { | ||
return getThemeVariant() == 'dark'; | ||
} | ||
} | ||
</script> | ||
|
||
<script> | ||
|
@@ -256,11 +285,7 @@ | |
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { | ||
this.$vuetify.theme.dark = inDarkMode(); | ||
}); | ||
if (this.$vuetify.theme.dark) { | ||
this.changeThemeCSS('dark'); | ||
} else { | ||
this.changeThemeCSS('light'); | ||
} | ||
document.getElementById('pre-render-theme').remove(); | ||
}, | ||
methods: { | ||
stateReset() { | ||
|
@@ -275,10 +300,6 @@ | |
reload() { | ||
location.reload(); | ||
}, | ||
async changeThemeCSS(theme) { | ||
let css = await fetch(`${solara.rootPath}/static/assets/theme-${theme}.css`).then(r => r.text()); | ||
document.getElementById('jupyter-theme-css').innerHTML = css; | ||
} | ||
}, | ||
watch: { | ||
kernelBusy: function (value) { | ||
|
@@ -325,15 +346,14 @@ | |
} | ||
}, | ||
'$vuetify.theme.dark': function (value) { | ||
let app = document.getElementById('app'); | ||
if ( value ) { | ||
this.changeThemeCSS('dark'); | ||
app.classList.remove('theme--light'); | ||
app.classList.add('theme--dark'); | ||
appContainer.classList.remove('theme--light'); | ||
appContainer.classList.add('theme--dark'); | ||
} else { | ||
this.changeThemeCSS('light'); | ||
app.classList.remove('theme--dark'); | ||
app.classList.add('theme--light'); | ||
appContainer.classList.remove('theme--dark'); | ||
appContainer.classList.add('theme--light'); | ||
} | ||
} | ||
}, | ||
|