Skip to content

Commit

Permalink
stop flicker
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgill committed Apr 1, 2024
1 parent ee0a8cc commit 3a27a44
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@radix-ui/react-tabs": "^1.0.4",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"astro": "^4.5.5",
"astro": "^4.5.12",
"astro-icon": "^1.1.0",
"balance-text": "^3.3.1",
"class-variance-authority": "^0.7.0",
Expand Down
82 changes: 54 additions & 28 deletions apps/docs/src/components/layout/BaseHead.astro
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,60 @@ const {
} = Astro.props;
---

<!-- inspiration https://github.com/withastro/astro/issues/8711#issue-1919938037 -->
<script is:inline>
// Dark mode with prevent flickering flash between navigations
const disableAnimation = () => {
const css = document.createElement("style");
css.appendChild(
document.createTextNode(
`*{
-webkit-transition:none!important;
-moz-transition:none!important;
-o-transition:none!important;
-ms-transition:none!important;
transition:none!important
}`,
),
);
document.head.appendChild(css);

return () => {
// Force restyle
(() => window.getComputedStyle(document.body))();

// Wait for next tick before removing
setTimeout(() => {
document.head.removeChild(css);
}, 1);
};
};

const setDarkMode = () => {
const theme = (() => {
if (
typeof localStorage !== "undefined" &&
localStorage.getItem("theme")
) {
return localStorage.getItem("theme");
}
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return "dark";
}
return "light";
})();
if (theme === "light") {
document.documentElement.classList.remove("dark");
} else {
document.documentElement.classList.add("dark");
}
disableAnimation();
window.localStorage.setItem("theme", theme);
};
setDarkMode();
document.addEventListener("astro:after-swap", setDarkMode);
</script>

<!-- Global Metadata -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
Expand Down Expand Up @@ -55,32 +109,4 @@ const {
<meta property="twitter:description" content={description} />
<meta property="twitter:image" content={new URL(image, Astro.url)} />

<!-- Dark mode with prevent flickering flash between navigations -->
<script is:inline>
const setDarkMode = () => {
const theme = (() => {
if (
typeof localStorage !== "undefined" &&
localStorage.getItem("theme")
) {
return localStorage.getItem("theme");
}
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return "dark";
}
return "light";
})();

if (theme === "light") {
document.documentElement.classList.remove("dark");
} else {
document.documentElement.classList.add("dark");
}
window.localStorage.setItem("theme", theme);
};

setDarkMode();
document.addEventListener("astro:after-swap", setDarkMode);
</script>

<ViewTransitions />
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3a27a44

Please sign in to comment.