Feature: Instant Theme Change, without refresh [Booking Pages Only] (#1807)
* Avoid Theme Flicker. Render Server Side * Add back isReady implementation * Use shorter syntax for Tag * Listen to changes in pref-color-scheme and act * Uglify Theme Applier code * Resolve conflicts * Add comments * Appropriate function name * Move uglify-js to dependencies * Remove uglify-js * Fix commnt Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Omar López <zomars@me.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>pull/1874/head
parent
34c5360a4d
commit
4693cbba4f
|
@ -3,28 +3,42 @@ import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import { Maybe } from "@trpc/server";
|
import { Maybe } from "@trpc/server";
|
||||||
|
|
||||||
// makes sure the ui doesn't flash
|
// This method is stringified and executed only on client. So,
|
||||||
export default function useTheme(theme?: Maybe<string>) {
|
// - Pass all the params explicitly to this method. Don't use closure
|
||||||
const [isReady, setIsReady] = useState(false);
|
function applyThemeAndAddListener(theme: string) {
|
||||||
useEffect(() => {
|
const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)");
|
||||||
setIsReady(true);
|
const applyTheme = function (theme: string, darkMatch: boolean) {
|
||||||
}, []);
|
if (!theme) {
|
||||||
function Theme() {
|
if (darkMatch) {
|
||||||
const themeString = theme ? `"${theme}"` : null;
|
|
||||||
return (
|
|
||||||
<Head>
|
|
||||||
<script
|
|
||||||
dangerouslySetInnerHTML={{
|
|
||||||
__html: `(function (theme) {
|
|
||||||
if (!theme && window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
||||||
document.documentElement.classList.add("dark");
|
document.documentElement.classList.add("dark");
|
||||||
} else if (!theme) {
|
} else {
|
||||||
/** Uncovered case */
|
document.documentElement.classList.remove("dark");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
document.documentElement.classList.add(theme);
|
document.documentElement.classList.add(theme);
|
||||||
}
|
}
|
||||||
})(${themeString})`,
|
};
|
||||||
}}></script>
|
mediaQueryList.onchange = function (e) {
|
||||||
|
applyTheme(theme, e.matches);
|
||||||
|
};
|
||||||
|
applyTheme(theme, mediaQueryList.matches);
|
||||||
|
}
|
||||||
|
|
||||||
|
// makes sure the ui doesn't flash
|
||||||
|
export default function useTheme(theme?: Maybe<string>) {
|
||||||
|
const [isReady, setIsReady] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// TODO: isReady doesn't seem required now. This is also impacting PSI Score for pages which are using isReady.
|
||||||
|
setIsReady(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function Theme() {
|
||||||
|
const code = applyThemeAndAddListener.toString();
|
||||||
|
const themeStr = theme ? `"${theme}"` : null;
|
||||||
|
return (
|
||||||
|
<Head>
|
||||||
|
<script dangerouslySetInnerHTML={{ __html: `(${code})(${themeStr})` }}></script>
|
||||||
</Head>
|
</Head>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue