fixed: hydration error in Time and Background
This commit is contained in:
parent
7d412ad439
commit
67b5f47440
@ -3,21 +3,18 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useRecoilState } from "recoil";
|
import { useRecoilState } from "recoil";
|
||||||
import { bgFocusState } from "./state/background";
|
import { bgFocusState } from "./state/background";
|
||||||
|
import BackgroundContainer from "./backgroundContainer";
|
||||||
import dynamic from "next/dynamic";
|
|
||||||
|
|
||||||
const Background = dynamic(() => import("./backgroundContainer"), { ssr: false });
|
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
const [isFocus, setFocus] = useRecoilState(bgFocusState);
|
const [isFocus, setFocus] = useRecoilState(bgFocusState);
|
||||||
const [colorScheme, setColorScheme] = useState("light");
|
const [darkMode, setDarkMode] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const colorSchemeQueryList = window.matchMedia("(prefers-color-scheme: dark)");
|
const colorSchemeQueryList = window.matchMedia("(prefers-color-scheme: dark)");
|
||||||
setColorScheme(colorSchemeQueryList.matches ? "dark" : "light");
|
setDarkMode(colorSchemeQueryList.matches ? true : false);
|
||||||
|
|
||||||
const handleChange = () => {
|
const handleChange = () => {
|
||||||
setColorScheme(colorSchemeQueryList.matches ? "dark" : "light");
|
setDarkMode(colorSchemeQueryList.matches ? true : false);
|
||||||
};
|
};
|
||||||
|
|
||||||
colorSchemeQueryList.addEventListener("change", handleChange);
|
colorSchemeQueryList.addEventListener("change", handleChange);
|
||||||
@ -26,22 +23,12 @@ export default function () {
|
|||||||
colorSchemeQueryList.removeEventListener("change", handleChange);
|
colorSchemeQueryList.removeEventListener("change", handleChange);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
const [isClient, setIsClient] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setIsClient(true);
|
|
||||||
}, []);
|
|
||||||
return (
|
return (
|
||||||
<div suppressHydrationWarning>
|
<div suppressHydrationWarning>
|
||||||
{isClient && (
|
{darkMode ? (
|
||||||
<div>
|
<BackgroundContainer src="rgb(23,25,29)" isFocus={isFocus} onClick={() => setFocus(false)} darkMode={darkMode}/>
|
||||||
{colorScheme === "dark" && (
|
) : (
|
||||||
<Background src="rgb(23,25,29)" isFocus={isFocus} onClick={() => setFocus(false)} />
|
<BackgroundContainer src="white" isFocus={isFocus} onClick={() => setFocus(false)} darkMode={darkMode}/>
|
||||||
)}
|
|
||||||
{colorScheme === "light" && (
|
|
||||||
<Background src="white" isFocus={isFocus} onClick={() => setFocus(false)} />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -4,11 +4,7 @@ import { settingsState } from "./state/settings";
|
|||||||
import validUrl from "valid-url";
|
import validUrl from "valid-url";
|
||||||
import validateColor from "validate-color";
|
import validateColor from "validate-color";
|
||||||
|
|
||||||
export default function(props: {
|
export default function (props: { isFocus: boolean; src: string; darkMode: boolean; onClick: () => void }) {
|
||||||
isFocus: boolean;
|
|
||||||
src: string;
|
|
||||||
onClick: () => void;
|
|
||||||
}) {
|
|
||||||
const settings = useRecoilValue(settingsState);
|
const settings = useRecoilValue(settingsState);
|
||||||
if (validateColor(props.src)) {
|
if (validateColor(props.src)) {
|
||||||
return (
|
return (
|
||||||
@ -24,16 +20,28 @@ export default function(props: {
|
|||||||
src={props.src}
|
src={props.src}
|
||||||
className={
|
className={
|
||||||
"w-full h-full fixed object-cover inset-0 duration-200 z-0 " +
|
"w-full h-full fixed object-cover inset-0 duration-200 z-0 " +
|
||||||
(props.isFocus
|
(props.isFocus ? (settings.bgBlur ? "blur-lg scale-110" : "brightness-50 scale-105") : "")
|
||||||
? settings.bgBlur
|
|
||||||
? "blur-lg scale-110"
|
|
||||||
: "brightness-50 scale-105"
|
|
||||||
: "")
|
|
||||||
}
|
}
|
||||||
alt="background"
|
alt="background"
|
||||||
onClick={props.onClick}
|
onClick={props.onClick}
|
||||||
fill={true}
|
fill={true}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
if (props.darkMode) {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="w-full h-full fixed object-cover inset-0 duration-200 z-0 bg-[rgb(23,25,29)]"
|
||||||
|
onClick={props.onClick}
|
||||||
|
></div>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="w-full h-full fixed object-cover inset-0 duration-200 z-0 bg-white"
|
||||||
|
onClick={props.onClick}
|
||||||
|
></div>
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,17 @@ import { useRecoilState, useSetRecoilState } from "recoil";
|
|||||||
import { settingsState } from "./state/settings";
|
import { settingsState } from "./state/settings";
|
||||||
import Search from "./search/search";
|
import Search from "./search/search";
|
||||||
import { bgFocusState } from "./state/background";
|
import { bgFocusState } from "./state/background";
|
||||||
import Time from "./time";
|
|
||||||
import loadSettings from "@/lib/loadSettings";
|
import loadSettings from "@/lib/loadSettings";
|
||||||
import EngineSelector from "./search/engineSelector";
|
import EngineSelector from "./search/engineSelector";
|
||||||
import Onesearch from "./search/onesearch/onesearch";
|
import Onesearch from "./search/onesearch/onesearch";
|
||||||
import Background from "./background";
|
import dynamic from "next/dynamic";
|
||||||
|
|
||||||
|
const Background = dynamic(() => import("./background"), {
|
||||||
|
ssr: false
|
||||||
|
});
|
||||||
|
const Time = dynamic(() => import("./time"), {
|
||||||
|
ssr: false
|
||||||
|
});
|
||||||
|
|
||||||
export default function Homepage() {
|
export default function Homepage() {
|
||||||
const [settings, setSettings] = useRecoilState(settingsState);
|
const [settings, setSettings] = useRecoilState(settingsState);
|
||||||
@ -20,17 +25,17 @@ export default function Homepage() {
|
|||||||
loadSettings(setSettings);
|
loadSettings(setSettings);
|
||||||
}, [setSettings]);
|
}, [setSettings]);
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full fixed overflow-hidden w-full bg-black">
|
<div className="h-full fixed overflow-hidden w-full bg-black">
|
||||||
<Time showSecond={settings.timeShowSecond} />
|
<Time showSecond={settings.timeShowSecond} />
|
||||||
<EngineSelector className="absolute top-20 lg:top-44 short:top-0 translate-x-[-50%] translate-y-[-0.2rem]
|
<EngineSelector
|
||||||
|
className="absolute top-20 lg:top-44 short:top-0 translate-x-[-50%] translate-y-[-0.2rem]
|
||||||
left-1/2 w-11/12 sm:w-[700px] text:black text-right
|
left-1/2 w-11/12 sm:w-[700px] text:black text-right
|
||||||
dark:text-white text-3xl text-shadow-lg z-10"/>
|
dark:text-white text-3xl text-shadow-lg z-10"
|
||||||
|
/>
|
||||||
<Background />
|
<Background />
|
||||||
<Search onFocus={() => setFocus(true)} />
|
<Search onFocus={() => setFocus(true)} />
|
||||||
<Onesearch />
|
<Onesearch />
|
||||||
<p className="z-20 relative text-white text-2xl">FUCK YOU SSR</p>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user