index.jsx 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { styled, Switch } from "@mui/material";
  2. const StyledSwitch = styled((props) => (
  3. <Switch focusVisibleClassName=".Mui-focusVisible" disableRipple {...props} />
  4. ))(({ theme }) => ({
  5. position: "absolute",
  6. top: 10,
  7. right: 10,
  8. width: 62,
  9. height: 34,
  10. padding: 0,
  11. zIndex: 10,
  12. "& .MuiSwitch-switchBase": {
  13. padding: 0,
  14. margin: 2,
  15. transitionDuration: "300ms",
  16. "&.Mui-checked": {
  17. transform: "translateX(28px)",
  18. color: "#fff",
  19. "& + .MuiSwitch-track": {
  20. backgroundColor: theme.palette.base.dark,
  21. opacity: 1,
  22. border: 0,
  23. },
  24. "& .MuiSwitch-thumb:before": {
  25. backgroundImage: `url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px"><path fill="${encodeURIComponent(
  26. `${theme.palette.primary.main}`
  27. )}" d="M480-120q-151 0-255.5-104.5T120-480q0-138 90-239.5T440-838q13-2 23 3.5t16 14.5q6 9 6.5 21t-7.5 23q-17 26-25.5 55t-8.5 61q0 90 63 153t153 63q31 0 61.5-9t54.5-25q11-7 22.5-6.5T819-479q10 5 15.5 15t3.5 24q-14 138-117.5 229T480-120Z"/></svg>')`,
  28. },
  29. },
  30. },
  31. "& .MuiSwitch-thumb": {
  32. width: 30,
  33. height: 30,
  34. "&::before": {
  35. content: "''",
  36. position: "absolute",
  37. width: "100%",
  38. height: "100%",
  39. left: 0,
  40. top: 0,
  41. backgroundRepeat: "no-repeat",
  42. backgroundPosition: "center",
  43. backgroundImage: `url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px"><path fill="${encodeURIComponent(
  44. `${theme.palette.primary.main}`
  45. )}" d="M480-280q-83 0-141.5-58.5T280-480q0-83 58.5-141.5T480-680q83 0 141.5 58.5T680-480q0 83-58.5 141.5T480-280ZM80-440q-17 0-28.5-11.5T40-480q0-17 11.5-28.5T80-520h80q17 0 28.5 11.5T200-480q0 17-11.5 28.5T160-440H80Zm720 0q-17 0-28.5-11.5T760-480q0-17 11.5-28.5T800-520h80q17 0 28.5 11.5T920-480q0 17-11.5 28.5T880-440h-80ZM480-760q-17 0-28.5-11.5T440-800v-80q0-17 11.5-28.5T480-920q17 0 28.5 11.5T520-880v80q0 17-11.5 28.5T480-760Zm0 720q-17 0-28.5-11.5T440-80v-80q0-17 11.5-28.5T480-200q17 0 28.5 11.5T520-160v80q0 17-11.5 28.5T480-40ZM226-678l-43-42q-12-11-11.5-28t11.5-29q12-12 29-12t28 12l42 43q11 12 11 28t-11 28q-11 12-27.5 11.5T226-678Zm494 495-42-43q-11-12-11-28.5t11-27.5q11-12 27.5-11.5T734-282l43 42q12 11 11.5 28T777-183q-12 12-29 12t-28-12Zm-42-495q-12-11-11.5-27.5T678-734l42-43q11-12 28-11.5t29 11.5q12 12 12 29t-12 28l-43 42q-12 11-28 11t-28-11ZM183-183q-12-12-12-29t12-28l43-42q12-11 28.5-11t27.5 11q12 11 11.5 27.5T282-226l-42 43q-11 12-28 11.5T183-183Z"/></svg>')`,
  46. transition: `${theme.transitions.create(["background-image"], {
  47. duration: "300ms",
  48. easing: theme.transitions.easing.easeInOut,
  49. })}`,
  50. },
  51. },
  52. "& .MuiSwitch-track": {
  53. borderRadius: 17,
  54. backgroundColor: theme.palette.base.dark,
  55. opacity: 1,
  56. },
  57. [theme.breakpoints.down("sm")]: {
  58. scale: 0.6,
  59. top: 5,
  60. right: 0,
  61. },
  62. }));
  63. export const ColorSchemeSwitch = ({ colorScheme, onSwitch }) => {
  64. const handleSwitch = (event) => {
  65. onSwitch(event.target.checked ? "dark" : "light");
  66. };
  67. return <StyledSwitch checked={colorScheme === "dark" ? true : false} onChange={handleSwitch} />;
  68. };