ThemeToggle.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useMemo } from 'react';
  16. import { Button, Dropdown } from '@douyinfe/semi-ui';
  17. import { Sun, Moon, Monitor } from 'lucide-react';
  18. import { useActualTheme } from '../../../context/Theme';
  19. const ThemeToggle = ({ theme, onThemeToggle, t }) => {
  20. const actualTheme = useActualTheme();
  21. const themeOptions = useMemo(
  22. () => [
  23. {
  24. key: 'light',
  25. icon: <Sun size={18} />,
  26. buttonIcon: <Sun size={18} />,
  27. label: t('浅色模式'),
  28. description: t('始终使用浅色主题'),
  29. },
  30. {
  31. key: 'dark',
  32. icon: <Moon size={18} />,
  33. buttonIcon: <Moon size={18} />,
  34. label: t('深色模式'),
  35. description: t('始终使用深色主题'),
  36. },
  37. {
  38. key: 'auto',
  39. icon: <Monitor size={18} />,
  40. buttonIcon: <Monitor size={18} />,
  41. label: t('自动模式'),
  42. description: t('跟随系统主题设置'),
  43. },
  44. ],
  45. [t],
  46. );
  47. const getItemClassName = (isSelected) =>
  48. isSelected
  49. ? '!bg-semi-color-primary-light-default !font-semibold'
  50. : 'hover:!bg-semi-color-fill-1';
  51. const currentButtonIcon = useMemo(() => {
  52. const currentOption = themeOptions.find((option) => option.key === theme);
  53. return currentOption?.buttonIcon || themeOptions[2].buttonIcon;
  54. }, [theme, themeOptions]);
  55. return (
  56. <Dropdown
  57. position='bottomRight'
  58. render={
  59. <Dropdown.Menu>
  60. {themeOptions.map((option) => (
  61. <Dropdown.Item
  62. key={option.key}
  63. icon={option.icon}
  64. onClick={() => onThemeToggle(option.key)}
  65. className={getItemClassName(theme === option.key)}
  66. >
  67. <div className='flex flex-col'>
  68. <span>{option.label}</span>
  69. <span className='text-xs text-semi-color-text-2'>
  70. {option.description}
  71. </span>
  72. </div>
  73. </Dropdown.Item>
  74. ))}
  75. {theme === 'auto' && (
  76. <>
  77. <Dropdown.Divider />
  78. <div className='px-3 py-2 text-xs text-semi-color-text-2'>
  79. {t('当前跟随系统')}:
  80. {actualTheme === 'dark' ? t('深色') : t('浅色')}
  81. </div>
  82. </>
  83. )}
  84. </Dropdown.Menu>
  85. }
  86. >
  87. <Button
  88. icon={currentButtonIcon}
  89. aria-label={t('切换主题')}
  90. theme='borderless'
  91. type='tertiary'
  92. className='!p-1.5 !text-current focus:!bg-semi-color-fill-1 !rounded-full !bg-semi-color-fill-0 hover:!bg-semi-color-fill-1'
  93. />
  94. </Dropdown>
  95. );
  96. };
  97. export default ThemeToggle;