AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
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.

theme-switch.tsx 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import type { LucideIcon } from 'lucide-react';
  2. import { Moon, Sun } from 'lucide-react';
  3. import {
  4. SidebarMenu,
  5. SidebarMenuButton,
  6. SidebarMenuItem,
  7. } from '@/components/ui/sidebar';
  8. import type { ResolvedAppearance } from '@/hooks/use-appearance';
  9. import { useAppearance } from '@/hooks/use-appearance';
  10. import { cn } from '@/lib/utils';
  11. const options: {
  12. value: ResolvedAppearance;
  13. icon: LucideIcon;
  14. label: string;
  15. }[] = [
  16. { value: 'light', icon: Sun, label: 'Light' },
  17. { value: 'dark', icon: Moon, label: 'Dark' },
  18. ];
  19. /**
  20. * Light or dark, in the sidebar just above the user. One click each.
  21. * Someone who never chose follows their device, and the switch shows how the page looks now.
  22. * The choice is saved in this browser and a cookie, the same as Settings → Appearance.
  23. * With the sidebar folded to icons it becomes one button that flips between the two.
  24. */
  25. export function ThemeSwitch() {
  26. const { resolvedAppearance, updateAppearance } = useAppearance();
  27. const other = resolvedAppearance === 'dark' ? 'light' : 'dark';
  28. const OtherIcon = other === 'dark' ? Moon : Sun;
  29. return (
  30. <SidebarMenu>
  31. <SidebarMenuItem>
  32. <div
  33. role="radiogroup"
  34. aria-label="Theme"
  35. 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"
  36. data-test="theme-switch"
  37. >
  38. {options.map(({ value, icon: Icon, label }) => {
  39. const active = resolvedAppearance === value;
  40. return (
  41. <button
  42. key={value}
  43. type="button"
  44. role="radio"
  45. aria-checked={active}
  46. onClick={() => updateAppearance(value)}
  47. className={cn(
  48. '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',
  49. active
  50. ? 'bg-sidebar font-medium text-sidebar-foreground shadow-sm'
  51. : 'text-sidebar-foreground/60 hover:bg-sidebar/60 hover:text-sidebar-foreground',
  52. )}
  53. >
  54. <Icon className="size-4" aria-hidden="true" />
  55. {label}
  56. </button>
  57. );
  58. })}
  59. </div>
  60. <SidebarMenuButton
  61. className="hidden group-data-[collapsible=icon]:flex"
  62. onClick={() => updateAppearance(other)}
  63. tooltip={{
  64. children:
  65. other === 'dark'
  66. ? 'Switch to dark theme'
  67. : 'Switch to light theme',
  68. }}
  69. data-test="theme-toggle"
  70. >
  71. <OtherIcon />
  72. <span>
  73. {other === 'dark'
  74. ? 'Switch to dark theme'
  75. : 'Switch to light theme'}
  76. </span>
  77. </SidebarMenuButton>
  78. </SidebarMenuItem>
  79. </SidebarMenu>
  80. );
  81. }