Skip to content

Commit

Permalink
chore: update files
Browse files Browse the repository at this point in the history
  • Loading branch information
haru52 committed May 25, 2024
1 parent cf36e4e commit 0e51835
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/app/_components/theme-controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ export function ThemeController({
}: {
placeContentStart?: boolean;
}) {
// https://next-themes-example.vercel.app/
// https://github.com/pacocoursey/next-themes/blob/bf0c5a45eaf6fb2b336a6b93840e4ec572bc08c8/examples/example/pages/index.js
const [mounted, setMounted] = useState(false);
const [isSystemDarkMode, setIsSystemDarkMode] = useState(false);
const { theme, setTheme } = useTheme();
useEffect(() => setMounted(true), []);
const [systemIsDarkMode, setSystemIsDarkMode] = useState(false);
useEffect(() => {
setSystemIsDarkMode(
setMounted(true);
setIsSystemDarkMode(
window.matchMedia("(prefers-color-scheme: dark)").matches,
);
}, []);

return mounted ? (
// https://daisyui.com/components/theme-controller/#theme-controller-using-a-swap
<label
className={clsx("swap swap-rotate", {
"place-content-start": placeContentStart,
Expand All @@ -29,13 +32,7 @@ export function ThemeController({
<input
type="checkbox"
value="dark"
checked={
!mounted
? false
: theme === "system"
? systemIsDarkMode
: theme === "dark"
}
checked={theme === "system" ? isSystemDarkMode : theme === "dark"}
onChange={(e) => setTheme(e.target.checked ? "dark" : "light")}
/>

Expand All @@ -58,6 +55,6 @@ export function ThemeController({
</svg>
</label>
) : (
<div className="h-10 w-10"></div>
<div className="h-10 w-10"></div> // マウント前後でチェックボックスの位置が変わらないようにする
);
}

0 comments on commit 0e51835

Please sign in to comment.