From f8c095827b6983fb07f2b721df38561240991ac4 Mon Sep 17 00:00:00 2001 From: Jonas Kuske <30421456+jonaskuske@users.noreply.github.com> Date: Sat, 30 May 2020 03:00:15 +0200 Subject: [PATCH] fix: always update product hunt on theme change If the selected (forced) theme was different from the system preference and then switches back to auto, need to reset the product hunt theme too --- docs/script.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/script.js b/docs/script.js index a7502ff..02d72d0 100644 --- a/docs/script.js +++ b/docs/script.js @@ -22,7 +22,10 @@ const table = { browserSupport: document.getElementById('table-browser-support') } -const updateProductHunt = (theme) => { +const prefersColorScheme = window.matchMedia('(prefers-color-scheme: light)') + +const updateProductHunt = () => { + const theme = prefersColorScheme.matches ? 'light' : 'dark' productHunt.src = `https://api.producthunt.com/widgets/embed-image/v1/top-post-badge.svg?post_id=150490&theme=${theme}&period=daily` } @@ -39,6 +42,8 @@ const updateTheme = () => { table.fileName.innerText = fileName table.fileSize.innerText = `${fileSizes[theme].toFixed(2)} kb` + updateProductHunt() + if (theme === 'auto') { table.theme.innerHTML = ` Defaults to light, but respects user-defined theme settings.
@@ -51,17 +56,15 @@ const updateTheme = () => { } else { table.theme.innerText = `Theme is forced to ${theme}.` table.browserSupport.innerText = 'All browsers (including Internet Explorer)' - updateProductHunt(theme) } } themeForm.addEventListener('change', updateTheme) -const prefersColorScheme = window.matchMedia('(prefers-color-scheme: light)') -updateProductHunt(prefersColorScheme.matches ? 'light' : 'dark') +updateProductHunt() prefersColorScheme.addListener(() => { if (themeForm.theme.value !== 'auto') return - updateProductHunt(prefersColorScheme.matches ? 'light' : 'dark') + updateProductHunt() }) updateTheme()