Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Redesgin theme selector to use generic Dropdown component

Blue Mousefe49e373c16c
diff --git a/web/app/components/Header/index.tsx b/web/app/components/Header/index.tsx
index aac5ac87d..d867934be 100644
--- a/web/app/components/Header/index.tsx
+++ b/web/app/components/Header/index.tsx
@@ -17,7 +17,7 @@ import { ArrowRightIcon } from '@heroicons/react/20/solid'
import { Bars3Icon, XMarkIcon, ChevronDownIcon } from '@heroicons/react/24/outline'
import { MoonIcon, SunIcon } from '@heroicons/react/24/solid'
import { SiYoutube } from '@icons-pack/react-simple-icons'
-import cx, { clsx } from 'clsx'
+import cx from 'clsx'
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
import utc from 'dayjs/plugin/utc'
@@ -108,7 +108,7 @@ const SolutionsMenu = () => {
<PopoverButton className='inline-flex items-center gap-x-1 text-base leading-6 font-semibold text-slate-800 hover:text-slate-700 dark:text-slate-200 dark:hover:text-white'>
<span>{t('header.solutions.title')}</span>
<ChevronDownIcon
- className={clsx('h-3 w-3 stroke-2 transition-all', {
+ className={cx('h-3 w-3 stroke-2 transition-all', {
'rotate-180': open,
})}
aria-hidden='true'
@@ -211,62 +211,46 @@ const ThemeMenu = ({ className }: { className?: string }) => {
const { t } = useTranslation('common')
return (
- <Menu as='div' className={cx('relative', className)}>
- <div>
- <MenuButton className='flex items-center justify-center text-base leading-6 font-semibold text-slate-800 hover:text-slate-700 dark:text-slate-200 dark:hover:text-white'>
+ <Dropdown
+ title={
+ <span className='flex items-center justify-center'>
<span className='sr-only'>{t('header.switchTheme')}</span>
{theme === 'dark' ? (
<SunIcon className='h-6 w-6 cursor-pointer text-gray-200 hover:text-gray-300' aria-hidden='true' />
) : (
<MoonIcon className='h-6 w-6 cursor-pointer text-slate-700 hover:text-slate-600' aria-hidden='true' />
)}
- </MenuButton>
- </div>
- <Transition
- as={Fragment}
- enter='transition ease-out duration-100'
- enterFrom='transform opacity-0 scale-95'
- enterTo='transform opacity-100 scale-100'
- leave='transition ease-in duration-75'
- leaveFrom='transform opacity-100 scale-100'
- leaveTo='transform opacity-0 scale-95'
- >
- <MenuItems className='absolute right-0 z-30 mt-2 w-36 min-w-max origin-top-right rounded-md bg-white py-1 ring-1 ring-slate-200 focus:outline-hidden dark:bg-slate-900 dark:ring-slate-800'>
- <MenuItem>
- {({ active }) => (
- <div
- className={cx(
- 'flex w-full cursor-pointer px-4 py-2 text-sm font-semibold text-indigo-600 hover:bg-gray-100 dark:text-gray-50 hover:dark:bg-slate-800',
- {
- 'bg-gray-100 dark:bg-slate-800': active,
- },
- )}
- onClick={() => setTheme('light')}
- >
- <SunIcon className='mr-2 h-5 w-5 text-indigo-600 dark:text-gray-200' aria-hidden='true' />
- {t('header.light')}
- </div>
- )}
- </MenuItem>
- <MenuItem>
- {({ active }) => (
- <div
- className={cx(
- 'flex w-full cursor-pointer px-4 py-2 text-sm font-semibold text-gray-700 hover:bg-gray-100 dark:text-indigo-400 hover:dark:bg-slate-800',
- {
- 'bg-gray-100 dark:bg-slate-800': active,
- },
- )}
- onClick={() => setTheme('dark')}
- >
- <MoonIcon className='mr-2 h-5 w-5 text-gray-200 dark:text-indigo-400' aria-hidden='true' />
- {t('header.dark')}
- </div>
- )}
- </MenuItem>
- </MenuItems>
- </Transition>
- </Menu>
+ </span>
+ }
+ items={[
+ { key: 'light', label: t('header.light'), icon: SunIcon },
+ { key: 'dark', label: t('header.dark'), icon: MoonIcon },
+ ]}
+ keyExtractor={(item) => item.key}
+ labelExtractor={(item) => (
+ <div
+ className={cx('flex w-full items-center', {
+ 'light:text-indigo-600': item.key === 'light',
+ 'dark:text-indigo-400': item.key === 'dark',
+ })}
+ >
+ <item.icon
+ className={cx('mr-2 h-5 w-5', {
+ 'dark:text-gray-300': item.key === 'light',
+ 'light:text-gray-400': item.key === 'dark',
+ })}
+ aria-hidden='true'
+ />
+ {item.label}
+ </div>
+ )}
+ onSelect={(item) => setTheme(item.key as 'light' | 'dark')}
+ className={cx('flex', className)}
+ chevron={null}
+ headless
+ buttonClassName='p-0 md:p-0'
+ selectItemClassName='font-semibold'
+ />
)
}
diff --git a/web/app/styles/tailwind.css b/web/app/styles/tailwind.css
index f427cb9de..ea6b21e2f 100644
--- a/web/app/styles/tailwind.css
+++ b/web/app/styles/tailwind.css
@@ -43,6 +43,7 @@
}
@custom-variant dark (&:where(.dark, .dark *));
+@custom-variant light (&:where(.light, .light *));
@keyframes move-x {
0% {
diff --git a/web/app/ui/Dropdown.tsx b/web/app/ui/Dropdown.tsx
index 1e8832223..fb8c2de65 100644
--- a/web/app/ui/Dropdown.tsx
+++ b/web/app/ui/Dropdown.tsx
@@ -1,7 +1,6 @@
import { Menu, MenuButton, MenuItems, MenuItem, Transition } from '@headlessui/react'
import { ChevronDownIcon as ChevronDownIconMini } from '@heroicons/react/20/solid'
import { ChevronDownIcon } from '@heroicons/react/24/solid'
-import cx from 'clsx'
import _isEmpty from 'lodash/isEmpty'
import _map from 'lodash/map'
import React, { memo, Fragment, Key } from 'react'
@@ -23,7 +22,7 @@ interface DropdownProps<T> {
selectItemClassName?: string
menuItemsClassName?: string
header?: React.ReactNode
- chevron?: 'regular' | 'mini'
+ chevron?: 'regular' | 'mini' | null
headless?: boolean
disabled?: boolean
onClick?: React.MouseEventHandler<HTMLButtonElement>
@@ -50,7 +49,7 @@ function Dropdown<T>({
loading,
}: DropdownProps<T>) {
return (
- <Menu as='div' className={cx('relative inline-block text-left', className)}>
+ <Menu as='div' className={cn('relative inline-block text-left', className)}>
{({ open, close }) => (
<>
{!_isEmpty(desc) ? <p className='mb-2 text-sm text-gray-900'>{desc}</p> : null}
@@ -73,7 +72,7 @@ function Dropdown<T>({
<span>{title}</span>
{chevron === 'regular' ? (
<ChevronDownIcon
- className={cx('-mr-1 ml-2 h-5 w-5 transform-gpu transition-transform', {
+ className={cn('-mr-1 ml-2 h-5 w-5 transform-gpu transition-transform', {
'group-hover:text-slate-500 dark:group-hover:text-slate-400': headless,
'rotate-180': open,
})}
@@ -82,7 +81,7 @@ function Dropdown<T>({
) : null}
{chevron === 'mini' ? (
<ChevronDownIconMini
- className={cx('-mr-1 ml-1 h-5 w-5 transform-gpu transition-transform', {
+ className={cn('-mr-1 ml-1 h-5 w-5 transform-gpu transition-transform', {
'group-hover:text-slate-500 dark:group-hover:text-slate-400': headless,
'rotate-180': open,
})}
@@ -103,7 +102,7 @@ function Dropdown<T>({
>
<MenuItems
static
- className={cx(
+ className={cn(
'absolute right-0 z-50 mt-2 w-40 min-w-max origin-top-right rounded-md bg-gray-50 p-1 ring-1 ring-black/10 focus:outline-hidden dark:bg-slate-800',
menuItemsClassName,
)}