Browse Source

add a label to theme icon

pull/520/head
bkimmel 1 year ago
parent
commit
f82bc660b0
  1. 20
      src/components/ThemeSwitcher.tsx

20
src/components/ThemeSwitcher.tsx

@ -1,3 +1,4 @@
import { useState } from "react";
import { useTheme } from "../core/hooks/useTheme.ts"; import { useTheme } from "../core/hooks/useTheme.ts";
import { cn } from "../core/utils/cn.ts"; import { cn } from "../core/utils/cn.ts";
import { Monitor, Moon, Sun } from "lucide-react"; import { Monitor, Moon, Sun } from "lucide-react";
@ -10,6 +11,7 @@ export default function ThemeSwitcher({
className?: string; className?: string;
}) { }) {
const { theme, preference, setPreference } = useTheme(); const { theme, preference, setPreference } = useTheme();
const [isFocused, setIsFocused] = useState(false);
const themeIcons = { const themeIcons = {
light: <Sun className="size-5" />, light: <Sun className="size-5" />,
@ -24,6 +26,17 @@ export default function ThemeSwitcher({
setPreference(nextPreference); setPreference(nextPreference);
}; };
const labelStyle = {
display: "block",
margin: "0 auto",
fontSize: ".65rem",
width: "100%",
position: "absolute",
top: isFocused ? "-2em" : "2em",
left: "0",
opacity: isFocused ? "100" : "0"
};
return ( return (
<button <button
type="button" type="button"
@ -32,10 +45,11 @@ export default function ThemeSwitcher({
className, className,
)} )}
onClick={toggleTheme} onClick={toggleTheme}
aria-label={preference === "system" onFocus={() => setIsFocused(true)}
? `System theme (currently ${theme}). Click to change theme.` onBlur={() => setIsFocused(false)}
: `Current theme: ${theme}. Click to change theme.`} aria-description={'Current theme'}
> >
<span style={labelStyle}>{preference}</span>
{themeIcons[preference]} {themeIcons[preference]}
</button> </button>
); );

Loading…
Cancel
Save