Skip to content

Commit

Permalink
feat: update theme-color tag on theme switch
Browse files Browse the repository at this point in the history
  • Loading branch information
satnaing committed Sep 25, 2023
1 parent 87b3e5b commit f253776
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions public/toggle-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ function reflectPreference() {
document.firstElementChild.setAttribute("data-theme", themeValue);

document.querySelector("#theme-btn")?.setAttribute("aria-label", themeValue);

// Get a reference to the body element
const body = document.body;

// Check if the body element exists before using getComputedStyle
if (body) {
// Get the computed styles for the body element
const computedStyles = window.getComputedStyle(body);

// Get the background color property
const bgColor = computedStyles.backgroundColor;

// Set the background color in <meta theme-color ... />
document
.querySelector("meta[name='theme-color']")
?.setAttribute("content", bgColor);
}
}

// set early so no page flashes / CSS is made aware
Expand Down
2 changes: 2 additions & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const socialImageURL = new URL(
rel="stylesheet"
/>

<meta name="theme-color" content="" />

{
// If PUBLIC_GOOGLE_SITE_VERIFICATION is set in the environment variable,
// include google-site-verification tag in the heading
Expand Down

0 comments on commit f253776

Please sign in to comment.