2024-02-22 23:15:46 +08:00
|
|
|
/*
|
2024-09-06 16:22:13 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2024-02-22 23:15:46 +08:00
|
|
|
|
2024-09-06 16:22:13 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2024-02-22 23:15:46 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { useLayoutEffect, useRef } from "react";
|
|
|
|
|
|
|
|
import { useUrlParams } from "./UrlParams";
|
|
|
|
|
|
|
|
export const useTheme = (): void => {
|
|
|
|
const { theme: themeName } = useUrlParams();
|
|
|
|
const previousTheme = useRef<string | null>(document.body.classList.item(0));
|
|
|
|
useLayoutEffect(() => {
|
2024-03-13 21:58:21 +08:00
|
|
|
// If the url does not contain a theme props we default to "dark".
|
|
|
|
const theme = themeName?.includes("light") ? "light" : "dark";
|
|
|
|
const themeHighContrast = themeName?.includes("high-contrast") ? "-hc" : "";
|
2024-02-22 23:15:46 +08:00
|
|
|
const themeString = "cpd-theme-" + theme + themeHighContrast;
|
|
|
|
if (themeString !== previousTheme.current) {
|
|
|
|
document.body.classList.remove(
|
|
|
|
"cpd-theme-light",
|
|
|
|
"cpd-theme-dark",
|
|
|
|
"cpd-theme-light-hc",
|
|
|
|
"cpd-theme-dark-hc",
|
|
|
|
);
|
|
|
|
document.body.classList.add(themeString);
|
|
|
|
previousTheme.current = themeString;
|
|
|
|
}
|
2024-03-13 21:58:21 +08:00
|
|
|
document.body.classList.remove("no-theme");
|
2024-02-22 23:15:46 +08:00
|
|
|
}, [previousTheme, themeName]);
|
|
|
|
};
|