| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import type { LucideIcon } from 'lucide-react';
- import { Moon, Sun } from 'lucide-react';
- import {
- SidebarMenu,
- SidebarMenuButton,
- SidebarMenuItem,
- } from '@/components/ui/sidebar';
- import type { ResolvedAppearance } from '@/hooks/use-appearance';
- import { useAppearance } from '@/hooks/use-appearance';
- import { cn } from '@/lib/utils';
-
- const options: {
- value: ResolvedAppearance;
- icon: LucideIcon;
- label: string;
- }[] = [
- { value: 'light', icon: Sun, label: 'Light' },
- { value: 'dark', icon: Moon, label: 'Dark' },
- ];
-
- /**
- * Light or dark, in the sidebar just above the user. One click each.
- * Someone who never chose follows their device, and the switch shows how the page looks now.
- * The choice is saved in this browser and a cookie, the same as Settings → Appearance.
- * With the sidebar folded to icons it becomes one button that flips between the two.
- */
- export function ThemeSwitch() {
- const { resolvedAppearance, updateAppearance } = useAppearance();
- const other = resolvedAppearance === 'dark' ? 'light' : 'dark';
- const OtherIcon = other === 'dark' ? Moon : Sun;
-
- return (
- <SidebarMenu>
- <SidebarMenuItem>
- <div
- role="radiogroup"
- aria-label="Theme"
- className="grid grid-cols-2 gap-0.5 rounded-lg border border-sidebar-border bg-sidebar-accent/50 p-0.5 group-data-[collapsible=icon]:hidden"
- data-test="theme-switch"
- >
- {options.map(({ value, icon: Icon, label }) => {
- const active = resolvedAppearance === value;
-
- return (
- <button
- key={value}
- type="button"
- role="radio"
- aria-checked={active}
- onClick={() => updateAppearance(value)}
- className={cn(
- 'inline-flex h-8 items-center justify-center gap-1.5 rounded-md text-sm transition-colors focus-visible:ring-2 focus-visible:ring-sidebar-ring focus-visible:outline-none',
- active
- ? 'bg-sidebar font-medium text-sidebar-foreground shadow-sm'
- : 'text-sidebar-foreground/60 hover:bg-sidebar/60 hover:text-sidebar-foreground',
- )}
- >
- <Icon className="size-4" aria-hidden="true" />
- {label}
- </button>
- );
- })}
- </div>
-
- <SidebarMenuButton
- className="hidden group-data-[collapsible=icon]:flex"
- onClick={() => updateAppearance(other)}
- tooltip={{
- children:
- other === 'dark'
- ? 'Switch to dark theme'
- : 'Switch to light theme',
- }}
- data-test="theme-toggle"
- >
- <OtherIcon />
- <span>
- {other === 'dark'
- ? 'Switch to dark theme'
- : 'Switch to light theme'}
- </span>
- </SidebarMenuButton>
- </SidebarMenuItem>
- </SidebarMenu>
- );
- }
|