2022-09-20 11:14:09 +02:00
|
|
|
import { Fragment } from 'react'
|
2022-01-27 18:07:37 +01:00
|
|
|
import LocaleIcon from 'shared/components/icons/i18n.js'
|
2022-02-06 19:16:49 +01:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-09-20 11:14:09 +02:00
|
|
|
import { Popover, Transition } from '@headlessui/react'
|
2022-10-05 23:01:04 +02:00
|
|
|
import DownIcon from 'shared/components/icons/down'
|
2022-09-20 11:14:09 +02:00
|
|
|
import Link from 'next/link'
|
2022-02-06 13:17:42 +01:00
|
|
|
|
2023-01-28 18:10:29 +01:00
|
|
|
export const ns = ['locales']
|
|
|
|
|
2023-01-09 21:02:08 +01:00
|
|
|
const LocalePicker = ({ iconOnly = false, bottom = false }) => {
|
2023-01-28 18:10:29 +01:00
|
|
|
const { t } = useTranslation(ns)
|
2022-02-06 19:16:49 +01:00
|
|
|
const router = useRouter()
|
2022-02-07 20:02:28 +01:00
|
|
|
|
2022-02-06 19:16:49 +01:00
|
|
|
return (
|
2022-09-20 11:14:09 +02:00
|
|
|
<Popover className="relative">
|
|
|
|
{() => (
|
|
|
|
<>
|
|
|
|
<Popover.Button
|
2022-12-26 18:54:28 +01:00
|
|
|
className={`h-12 group border-0 inline-flex items-center px-3 text-base text-neural-content hover:bg-neutral-focus`}
|
2022-09-20 11:14:09 +02:00
|
|
|
>
|
|
|
|
<LocaleIcon />
|
2023-01-28 18:10:29 +01:00
|
|
|
{!iconOnly && <span className="ml-4 font-medium capitalize">{t(router.locale)}</span>}
|
2022-11-23 21:42:22 +01:00
|
|
|
<DownIcon className={`ml-2 h-5 w-5 ${bottom ? 'rotate-180' : ''}`} aria-hidden="true" />
|
2022-09-20 11:14:09 +02:00
|
|
|
</Popover.Button>
|
|
|
|
<Transition
|
|
|
|
as={Fragment}
|
|
|
|
enter="transition ease-out duration-200"
|
|
|
|
enterFrom="opacity-0 translate-y-1"
|
|
|
|
enterTo="opacity-100 translate-y-0"
|
|
|
|
leave="transition ease-in duration-150"
|
|
|
|
leaveFrom="opacity-100 translate-y-0"
|
|
|
|
leaveTo="opacity-0 translate-y-1"
|
|
|
|
>
|
2022-11-23 21:42:22 +01:00
|
|
|
<Popover.Panel
|
|
|
|
className={`absolute z-10 mb-3 w-64 transform px-4 sm:px-0 lg:max-w-xl right-0 ${
|
|
|
|
iconOnly ? 'translate-x-4' : ''
|
|
|
|
} ${bottom ? 'bottom-10' : 'top-12'}`}
|
|
|
|
>
|
2022-09-20 11:14:09 +02:00
|
|
|
<div className="overflow-hidden rounded-lg shadow-lg">
|
2022-11-23 21:42:22 +01:00
|
|
|
<div className="relative grid gap-2 bg-base-100 p-4 grid-cols-1">
|
2022-10-05 23:01:04 +02:00
|
|
|
{router.locales.map((locale) => (
|
2022-12-04 15:04:56 +01:00
|
|
|
<Link
|
2022-12-26 18:54:28 +01:00
|
|
|
href={
|
|
|
|
locale === router.defaultLocale
|
|
|
|
? `/${router.asPath}`
|
|
|
|
: `/${locale}/${router.asPath}`
|
|
|
|
}
|
2022-12-04 15:04:56 +01:00
|
|
|
key={locale}
|
2022-12-26 18:54:28 +01:00
|
|
|
locale={locale}
|
|
|
|
className="btn btn-neutral"
|
2022-12-04 15:04:56 +01:00
|
|
|
>
|
|
|
|
{t(locale)}
|
2022-09-20 11:14:09 +02:00
|
|
|
</Link>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Popover.Panel>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2022-02-06 19:16:49 +01:00
|
|
|
)
|
|
|
|
}
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-01-27 18:07:37 +01:00
|
|
|
export default LocalePicker
|