-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace button theme switcher with select
- Loading branch information
Showing
5 changed files
with
66 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,46 @@ | ||
import { useState } from 'react' | ||
import { Theme, useTheme } from '#/context/providers/theme-provider' | ||
import { clx } from '#/utils/ui-helper' | ||
|
||
const themes = [ | ||
{ id: Theme.LIGHT, name: 'Light' }, | ||
{ id: Theme.DARK, name: 'Dark' }, | ||
{ id: Theme.SYSTEM, name: 'System' }, | ||
] as const | ||
|
||
type ThemeOption = (typeof themes)[number] | ||
|
||
export default function ThemeSwitcher() { | ||
const [, setTheme] = useTheme() | ||
const [theme, setTheme] = useTheme() | ||
const [selectedTheme, setSelectedTheme] = useState<ThemeOption>( | ||
themes.find((t) => t.id === theme) || themes[0] | ||
) | ||
|
||
const handleThemeChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
const newTheme = themes.find((theme) => theme.id === (event.target.value as Theme)) | ||
if (newTheme) { | ||
setSelectedTheme(newTheme) | ||
setTheme(newTheme.id) | ||
} | ||
} | ||
|
||
return ( | ||
<div className="flex flex-wrap justify-center gap-4 rounded-2xl p-1"> | ||
<button | ||
type="button" | ||
className="rounded-xl border-2 border-gray-900 bg-white px-4 py-1 text-gray-900 text-sm" | ||
onClick={() => setTheme(Theme.LIGHT)} | ||
> | ||
Light | ||
</button> | ||
<button | ||
type="button" | ||
className="rounded-xl border-2 border-gray-50 bg-gray-900 px-4 py-1 text-gray-50 text-sm" | ||
onClick={() => setTheme(Theme.DARK)} | ||
<div className="relative"> | ||
<select | ||
value={selectedTheme.id} | ||
onChange={handleThemeChange} | ||
className={clx( | ||
'w-full appearance-none rounded-lg border px-3 py-1.5 pr-8 text-sm focus:outline-none', | ||
'border-gray-300 bg-white text-gray-900 focus:border-primary-500', | ||
'dark:border-gray-600 dark:bg-gray-800 dark:text-white dark:focus:border-primary-400' | ||
)} | ||
> | ||
Dark | ||
</button> | ||
{themes.map((theme) => ( | ||
<option key={theme.id} value={theme.id}> | ||
{theme.name} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters