You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use client'
  2. import { useState } from 'react'
  3. import {
  4. RiCheckLine,
  5. RiComputerLine,
  6. RiMoonLine,
  7. RiSunLine,
  8. } from '@remixicon/react'
  9. import { useTranslation } from 'react-i18next'
  10. import { useTheme } from 'next-themes'
  11. import ActionButton from '@/app/components/base/action-button'
  12. import {
  13. PortalToFollowElem,
  14. PortalToFollowElemContent,
  15. PortalToFollowElemTrigger,
  16. } from '@/app/components/base/portal-to-follow-elem'
  17. export type Theme = 'light' | 'dark' | 'system'
  18. export default function ThemeSelector() {
  19. const { t } = useTranslation()
  20. const { theme, setTheme } = useTheme()
  21. const [open, setOpen] = useState(false)
  22. const handleThemeChange = (newTheme: Theme) => {
  23. setTheme(newTheme)
  24. setOpen(false)
  25. }
  26. const getCurrentIcon = () => {
  27. switch (theme) {
  28. case 'light': return <RiSunLine className='h-4 w-4 text-text-tertiary' />
  29. case 'dark': return <RiMoonLine className='h-4 w-4 text-text-tertiary' />
  30. default: return <RiComputerLine className='h-4 w-4 text-text-tertiary' />
  31. }
  32. }
  33. return (
  34. <PortalToFollowElem
  35. open={open}
  36. onOpenChange={setOpen}
  37. placement='bottom-end'
  38. offset={{ mainAxis: 6 }}
  39. >
  40. <PortalToFollowElemTrigger
  41. onClick={() => setOpen(!open)}
  42. >
  43. <ActionButton
  44. className={`h-8 w-8 p-[6px] ${open && 'bg-state-base-hover'}`}
  45. >
  46. {getCurrentIcon()}
  47. </ActionButton>
  48. </PortalToFollowElemTrigger>
  49. <PortalToFollowElemContent className='z-[1000]'>
  50. <div className='flex w-[144px] flex-col items-start rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur p-1 shadow-lg'>
  51. <button
  52. className='flex w-full items-center gap-1 rounded-lg px-2 py-1.5 text-text-secondary hover:bg-state-base-hover'
  53. onClick={() => handleThemeChange('light')}
  54. >
  55. <RiSunLine className='h-4 w-4 text-text-tertiary' />
  56. <div className='flex grow items-center justify-start px-1'>
  57. <span className='system-md-regular'>{t('common.theme.light')}</span>
  58. </div>
  59. {theme === 'light' && <div className='flex h-4 w-4 shrink-0 items-center justify-center'>
  60. <RiCheckLine className='h-4 w-4 text-text-accent' />
  61. </div>}
  62. </button>
  63. <button
  64. className='flex w-full items-center gap-1 rounded-lg px-2 py-1.5 text-text-secondary hover:bg-state-base-hover'
  65. onClick={() => handleThemeChange('dark')}
  66. >
  67. <RiMoonLine className='h-4 w-4 text-text-tertiary' />
  68. <div className='flex grow items-center justify-start px-1'>
  69. <span className='system-md-regular'>{t('common.theme.dark')}</span>
  70. </div>
  71. {theme === 'dark' && <div className='flex h-4 w-4 shrink-0 items-center justify-center'>
  72. <RiCheckLine className='h-4 w-4 text-text-accent' />
  73. </div>}
  74. </button>
  75. <button
  76. className='flex w-full items-center gap-1 rounded-lg px-2 py-1.5 text-text-secondary hover:bg-state-base-hover'
  77. onClick={() => handleThemeChange('system')}
  78. >
  79. <RiComputerLine className='h-4 w-4 text-text-tertiary' />
  80. <div className='flex grow items-center justify-start px-1'>
  81. <span className='system-md-regular'>{t('common.theme.auto')}</span>
  82. </div>
  83. {theme === 'system' && <div className='flex h-4 w-4 shrink-0 items-center justify-center'>
  84. <RiCheckLine className='h-4 w-4 text-text-accent' />
  85. </div>}
  86. </button>
  87. </div>
  88. </PortalToFollowElemContent>
  89. </PortalToFollowElem>
  90. )
  91. }