Skip to content

Commit

Permalink
Fix flashing white when dark theme is enabled (#1176)
Browse files Browse the repository at this point in the history
Load the theme code as fast as possible to prevent flashing the light theme
before fetching and applying the dark theme from the URL or localstorage.
  • Loading branch information
dheineman committed Aug 10, 2022
1 parent baa9912 commit c4a6a86
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
10 changes: 7 additions & 3 deletions gulpfile.js
Expand Up @@ -287,6 +287,10 @@ gulp.task('js-demo', () => {
return compileJs('demo')
})

gulp.task('js-demo-theme', () => {
return compileJs('demo-theme')
})

/**
* Compile JS module files to dist directory
*/
Expand Down Expand Up @@ -420,7 +424,7 @@ gulp.task('build-critical', (cb) => {
*/
gulp.task('watch', (cb) => {
gulp.watch('./src/scss/**/*.scss', gulp.series('sass'))
gulp.watch('./src/js/**/*.js', gulp.parallel('js', 'mjs', 'js-demo'))
gulp.watch('./src/js/**/*.js', gulp.parallel('js', 'mjs', gulp.parallel('js-demo', 'js-demo-theme')))
cb()
})

Expand Down Expand Up @@ -520,8 +524,8 @@ gulp.task('add-banner', () => {

gulp.task('clean', gulp.series('clean-dirs', 'clean-jekyll'))

gulp.task('start', gulp.series('clean', 'sass', 'js', 'js-demo', 'mjs', 'build-jekyll', gulp.parallel('watch-jekyll', 'watch', 'browser-sync')))
gulp.task('start', gulp.series('clean', 'sass', 'js', gulp.parallel('js-demo', 'js-demo-theme'), 'mjs', 'build-jekyll', gulp.parallel('watch-jekyll', 'watch', 'browser-sync')))

gulp.task('build-core', gulp.series('build-on', 'clean', 'sass', 'css-rtl', 'css-minify', 'js', 'js-demo', 'mjs', 'copy-images', 'copy-libs', 'add-banner'))
gulp.task('build-core', gulp.series('build-on', 'clean', 'sass', 'css-rtl', 'css-minify', 'js', gulp.parallel('js-demo', 'js-demo-theme'), 'mjs', 'copy-images', 'copy-libs', 'add-banner'))
gulp.task('build-demo', gulp.series('build-on', 'build-jekyll', 'copy-static', 'copy-dist', 'build-cleanup', 'build-purgecss'/*, 'build-critical'*/))
gulp.task('build', gulp.series('build-core', 'build-demo'))
25 changes: 25 additions & 0 deletions src/js/demo-theme.js
@@ -0,0 +1,25 @@
/**
* demo-theme is specifically loaded right after the body and not deferred
* to ensure we switch to the chosen dark/light theme as fast as possible.
* This will prevent any flashes of the light theme (default) before switching.
*/

const themeStorageKey = 'tablerTheme'
const defaultTheme = 'light'
let selectedTheme

// https://stackoverflow.com/a/901144
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
});

if (!!params.theme) {
localStorage.setItem(themeStorageKey, params.theme)
selectedTheme = params.theme
} else {
const storedTheme = localStorage.getItem(themeStorageKey)
selectedTheme = storedTheme ? storedTheme : defaultTheme
}

document.body.classList.remove('theme-dark', 'theme-light');
document.body.classList.add(`theme-${selectedTheme}`);
17 changes: 0 additions & 17 deletions src/js/demo.js
@@ -1,6 +1,5 @@
// Setting items
const items = {
'theme': { localStorage: 'tablerTheme', default: 'light' },
'menu-position': { localStorage: 'tablerMenuPosition', default: 'top' },
'menu-behavior': { localStorage: 'tablerMenuBehavior', default: 'sticky' },
'container-layout': { localStorage: 'tablerContainerLayout', default: 'boxed' }
Expand Down Expand Up @@ -44,16 +43,6 @@ const toggleFormControls = (form) => {
}
}

// Update body classes
const updateBodyClasses = () => {
document.body.classList.remove('theme-dark', 'theme-light');
document.body.classList.add(`theme-${config.theme}`);

// for (const [key, params] of Object.entries(items)) {
// document.body.setAttribute(`data-${key}`, config[key]);
// }
}

// Submit form
const submitForm = (form) => {
// Save data to localStorage
Expand All @@ -66,9 +55,6 @@ const submitForm = (form) => {
config[key] = value
}

// Update body classes
updateBodyClasses();

window.dispatchEvent(new Event('resize'));

(new bootstrap.Offcanvas(form)).hide()
Expand All @@ -78,9 +64,6 @@ const submitForm = (form) => {
// Parse url
parseUrl()

// Update body classes
updateBodyClasses();

// Elements
const form = document.querySelector('#offcanvasSettings')

Expand Down
1 change: 1 addition & 0 deletions src/pages/_layouts/base.html
Expand Up @@ -47,6 +47,7 @@

{% assign layout-dark = page.layout-dark | default: site.layout-dark %}
<body {% if layout.body-class or page.body-class %} class="{% if layout.body-class %} {{ layout.body-class }}{% endif %}{% if page.body-class %} {{ page.body-class }}{% endif %}"{% endif %}>
<script src="{{ site.base }}/dist/js/demo-theme{% if jekyll.environment != 'development' %}.min{% endif %}.js{% if jekyll.environment == 'preview' %}?{{ site.time | date: '%s' }}{% endif %}"></script>

{{ content }}

Expand Down

1 comment on commit c4a6a86

@vercel
Copy link

@vercel vercel bot commented on c4a6a86 Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tabler – ./

tabler-git-main-tabler-ui.vercel.app
tabler-tabler-ui.vercel.app
preview.tabler.io
demo.tabler.io

Please sign in to comment.