2022-04-24 00:19:38 +10:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTheme } from "next-themes";
|
|
|
|
import { Switch } from "@headlessui/react";
|
|
|
|
|
|
|
|
export const ThemeSwitch = () => {
|
|
|
|
const { theme, setTheme } = useTheme();
|
|
|
|
const [enabled, setEnabled] = useState(true);
|
|
|
|
|
|
|
|
// Use theme requires its own hook which uses Strings. Using an enabled, disabled for a bool hook and following translates that to a string.
|
|
|
|
if (enabled) {
|
|
|
|
setTheme("dark");
|
|
|
|
} else {
|
|
|
|
setTheme("light");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-05-14 16:48:48 +10:00
|
|
|
<div className="flex justify-start md:justify-end items-center space-x-2 mx-auto relative">
|
2022-04-24 00:19:38 +10:00
|
|
|
<Switch.Group>
|
|
|
|
<div className="flex items-center">
|
2022-05-14 16:48:48 +10:00
|
|
|
<Switch.Label className="mr-3 dark:text-white text-black">Dark Mode</Switch.Label>
|
2022-04-24 00:19:38 +10:00
|
|
|
<Switch
|
|
|
|
checked={enabled}
|
|
|
|
onChange={setEnabled}
|
2022-05-14 16:48:48 +10:00
|
|
|
className={`${enabled ? 'bg-gray-400' : 'bg-gray-200'} relative inline-flex items-center h-6 rounded-full w-11 transition-colors focus:outline-none focus:ring-1 focus:ring-offset-2 dark:ring-white ring-black dark:focus:bg-gray-400 focus:bg-gray-400`}
|
2022-04-24 00:19:38 +10:00
|
|
|
>
|
|
|
|
<span
|
2022-05-14 16:48:48 +10:00
|
|
|
className={`${enabled ? 'translate-x-6' : 'translate-x-1'} inline-block w-4 h-4 transform bg-black dark:bg-white rounded-full transition-transform`} />
|
2022-04-24 00:19:38 +10:00
|
|
|
</Switch>
|
|
|
|
</div>
|
|
|
|
</Switch.Group>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|