-
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.
fix: load theme earlier when using SSG
- Loading branch information
1 parent
e3ea88a
commit a7ab230
Showing
3 changed files
with
66 additions
and
48 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,7 +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"> | ||
<style id="jupyter-theme-css" class="solara-template-css"> | ||
{{ resources.include_css("/static/assets/theme-"~theme.variant~".css") }} | ||
</style> | ||
{{ resources.include_css("/static/assets/custom.css") }} | ||
|
@@ -38,9 +36,22 @@ | |
"kernelId": "1234" | ||
} | ||
</script> | ||
<style> | ||
<style class="solara-template-css"> | ||
{% include "loader-"~theme.loader~".css" %} | ||
</style> | ||
<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 +186,46 @@ | |
{# 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> | ||
solara.rootPath = {{ root_path | tojson | safe}}; | ||
console.log("rootPath", solara.rootPath); | ||
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 app_container = document.getElementById('app'); | ||
if (inDarkMode()) { | ||
app_container.classList.remove('theme--light'); | ||
app_container.classList.add('theme--dark'); | ||
} else { | ||
app_container.classList.remove('theme--dark'); | ||
app_container.classList.add('theme--light'); | ||
} | ||
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> | ||
{% block after_pre_rendered_html %}{% endblock %} | ||
{% if vue3 == True %} | ||
<link href="{{cdn}}/@widgetti/[email protected]/dist/fonts.css" rel="stylesheet"></link> | ||
|
@@ -191,10 +242,6 @@ | |
<script src="{{cdn}}/@widgetti/[email protected]/dist/solara-vuetify-app{{ipywidget_major_version}}.js"></script> | ||
{% endif %} | ||
{% endif %} | ||
<script> | ||
solara.rootPath = {{ root_path | tojson | safe}}; | ||
console.log("rootPath", solara.rootPath); | ||
</script> | ||
|
||
{{ resources.include_js("/static/assets/custom.js") }} | ||
{{ resources.include_js("/static/assets/theme.js") }} | ||
|
@@ -208,27 +255,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 +282,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 +297,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 +343,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'); | ||
app_container.classList.remove('theme--light'); | ||
app_container.classList.add('theme--dark'); | ||
} else { | ||
this.changeThemeCSS('light'); | ||
app.classList.remove('theme--dark'); | ||
app.classList.add('theme--light'); | ||
app_container.classList.remove('theme--dark'); | ||
app_container.classList.add('theme--light'); | ||
} | ||
} | ||
}, | ||
|