DarkModeToggle.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import "./ToolIcon.scss";
  2. import React from "react";
  3. import { t } from "../i18n";
  4. export type Appearence = "light" | "dark";
  5. // We chose to use only explicit toggle and not a third option for system value,
  6. // but this could be added in the future.
  7. export const DarkModeToggle = (props: {
  8. value: Appearence;
  9. onChange: (value: Appearence) => void;
  10. title?: string;
  11. }) => {
  12. const title = props.title
  13. ? props.title
  14. : props.value === "dark"
  15. ? t("buttons.lightMode")
  16. : t("buttons.darkMode");
  17. return (
  18. <label
  19. className="ToolIcon ToolIcon_type_floating ToolIcon_size_M"
  20. data-testid="toggle-dark-mode"
  21. title={title}
  22. >
  23. <input
  24. className="ToolIcon_type_checkbox ToolIcon_toggle_opaque"
  25. type="checkbox"
  26. onChange={(event) =>
  27. props.onChange(event.target.checked ? "dark" : "light")
  28. }
  29. checked={props.value === "dark"}
  30. aria-label={title}
  31. />
  32. <div className="ToolIcon__icon">
  33. {props.value === "light" ? ICONS.MOON : ICONS.SUN}
  34. </div>
  35. </label>
  36. );
  37. };
  38. const ICONS = {
  39. SUN: (
  40. <svg width="512" height="512" className="rtl-mirror" viewBox="0 0 512 512">
  41. <path
  42. fill="currentColor"
  43. d="M256 160c-52.9 0-96 43.1-96 96s43.1 96 96 96 96-43.1 96-96-43.1-96-96-96zm246.4 80.5l-94.7-47.3 33.5-100.4c4.5-13.6-8.4-26.5-21.9-21.9l-100.4 33.5-47.4-94.8c-6.4-12.8-24.6-12.8-31 0l-47.3 94.7L92.7 70.8c-13.6-4.5-26.5 8.4-21.9 21.9l33.5 100.4-94.7 47.4c-12.8 6.4-12.8 24.6 0 31l94.7 47.3-33.5 100.5c-4.5 13.6 8.4 26.5 21.9 21.9l100.4-33.5 47.3 94.7c6.4 12.8 24.6 12.8 31 0l47.3-94.7 100.4 33.5c13.6 4.5 26.5-8.4 21.9-21.9l-33.5-100.4 94.7-47.3c13-6.5 13-24.7.2-31.1zm-155.9 106c-49.9 49.9-131.1 49.9-181 0-49.9-49.9-49.9-131.1 0-181 49.9-49.9 131.1-49.9 181 0 49.9 49.9 49.9 131.1 0 181z"
  44. ></path>
  45. </svg>
  46. ),
  47. MOON: (
  48. <svg width="512" height="512" className="rtl-mirror" viewBox="0 0 512 512">
  49. <path
  50. fill="currentColor"
  51. d="M283.211 512c78.962 0 151.079-35.925 198.857-94.792 7.068-8.708-.639-21.43-11.562-19.35-124.203 23.654-238.262-71.576-238.262-196.954 0-72.222 38.662-138.635 101.498-174.394 9.686-5.512 7.25-20.197-3.756-22.23A258.156 258.156 0 0 0 283.211 0c-141.309 0-256 114.511-256 256 0 141.309 114.511 256 256 256z"
  52. ></path>
  53. </svg>
  54. ),
  55. };