-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
127 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,16 @@ | ||
import { siteMetadata } from '~/data/siteMetadata' | ||
import { AreaChart } from 'lucide-react' | ||
|
||
export function AnalyticsLink() { | ||
return ( | ||
<button | ||
aria-label="Open analytics" | ||
type="button" | ||
className="ml-1 rounded p-2 hover:bg-gray-200 dark:hover:bg-gray-700 sm:ml-2" | ||
className="rounded p-1.5 hover:bg-gray-200 dark:hover:bg-gray-700" | ||
data-umami-event="nav-analytics" | ||
onClick={() => window.open(siteMetadata.analyticsURL, '_blank')} | ||
> | ||
<svg | ||
role="img" | ||
viewBox="0 0 24 24" | ||
xmlns="http://www.w3.org/2000/svg" | ||
fill="currentColor" | ||
className="h-5 w-5 text-gray-900 dark:text-gray-100" | ||
> | ||
<path d="M1.019 13.019h3.849V24h-3.85zm8.943-6.68h3.85V24h-3.85zM19.132 0h3.85v24h-3.85z" /> | ||
</svg> | ||
<AreaChart strokeWidth={1.5} size={20} /> | ||
</button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,66 @@ | ||
import { useEffect, useState } from 'react' | ||
import { clsx } from 'clsx' | ||
import { useRouter } from 'next/router' | ||
import { useEffect, useState } from 'react' | ||
import { LOCALES } from '~/constant' | ||
import { Image } from './Image' | ||
import { Popover, PopoverContent, PopoverTrigger } from './ui/Popover' | ||
|
||
const localeNames = { | ||
en: 'English', | ||
es: 'Español', | ||
ca: 'Català', | ||
// add other languages as needed | ||
} | ||
|
||
function getLocaleName(localeCode) { | ||
return localeNames[localeCode] || localeCode | ||
function getLocale(code: string) { | ||
return LOCALES.find((locale) => locale.code === code) | ||
} | ||
|
||
function capitalize(string) { | ||
return string.charAt(0).toUpperCase() + string.slice(1) | ||
} | ||
export function LanguageSwitcher() { | ||
const router = useRouter() | ||
const [value, setValue] = useState(router.locale) | ||
const [dropdownOpen, setDropdownOpen] = useState(false) | ||
let router = useRouter() | ||
let [localeCode, setLocaleCode] = useState(router.locale) | ||
let currentLocale = getLocale(localeCode) | ||
|
||
useEffect(() => { | ||
setValue(router.locale) | ||
setLocaleCode(router.locale) | ||
}, [router.locale]) | ||
|
||
function translate_to(locale) { | ||
router.push(router.pathname, router.asPath, { locale }) | ||
setDropdownOpen(false) // Tancar el menú desplegable un cop l'usuari hagi seleccionat una opció | ||
function handleChange(newLocale: string) { | ||
router.push(router.pathname, router.asPath, { locale: newLocale }) | ||
} | ||
|
||
return ( | ||
<div className="relative inline-block text-left"> | ||
<button | ||
id="dropdownDefaultButton" | ||
data-dropdown-toggle="dropdown" | ||
onClick={() => setDropdownOpen(!dropdownOpen)} | ||
className=" font-medium text-gray-700 hover:bg-gray-200 rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center | ||
dark:border-gray-600 dark:text-gray-200 dark:hover:bg-gray-700 dark:hover:text-white | ||
" | ||
type="button" | ||
> | ||
{capitalize(value)} | ||
<svg | ||
className="w-2.5 h-2.5 ml-2.5" | ||
aria-hidden="true" | ||
xmlns="http://www.w3.org/2000/svg" | ||
fill="none" | ||
viewBox="0 0 10 6" | ||
> | ||
<path | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth="2" | ||
d="m1 1 4 4 4-4" | ||
/> | ||
</svg> | ||
</button> | ||
|
||
{dropdownOpen && ( | ||
<div | ||
id="dropdown" | ||
className="origin-top-right absolute right-0 mt-2 w-44 rounded-lg shadow bg-white ring-1 ring-black ring-opacity-5 z-50 divide-y divide-gray-100 | ||
dark:divide-gray-600 dark:ring-gray-800 dark:ring-opacity-5 dark:ring-1 dark:bg-gray-700 dark:ring-offset-2 dark:ring-offset-gray-700" | ||
> | ||
<ul | ||
className="py-2 text-sm text-gray-700 dark:text-gray-200" | ||
aria-labelledby="dropdownDefaultButton" | ||
> | ||
{router.locales.map((locale) => { | ||
const label = getLocaleName(locale) | ||
return ( | ||
<li key={locale}> | ||
<button | ||
onClick={() => translate_to(locale)} | ||
className="block px-4 py-2 w-full text-left hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white" | ||
> | ||
{label} | ||
</button> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
</div> | ||
)} | ||
</div> | ||
<Popover> | ||
<PopoverTrigger className="p-1.5 hover:bg-gray-200 dark:hover:bg-gray-700 rounded"> | ||
<Image | ||
src={currentLocale.flag} | ||
alt={currentLocale.name} | ||
width={20} | ||
height={20} | ||
shouldOpenLightbox={false} | ||
/> | ||
</PopoverTrigger> | ||
<PopoverContent className="bg-white dark:bg-dark w-32"> | ||
<ul className="space-y-1"> | ||
{router.locales.map((code) => { | ||
let locale = getLocale(code) | ||
return ( | ||
<li key={code}> | ||
<button | ||
onClick={() => handleChange(code)} | ||
className={clsx( | ||
'inline-flex font-normal items-center gap-3 px-2 py-1 rounded w-full text-left hover:bg-gray-100 dark:hover:bg-gray-700', | ||
code === localeCode && 'text-primary-500 dark:text-primary-400' | ||
)} | ||
> | ||
<Image | ||
src={locale.flag} | ||
alt={locale.name} | ||
width={20} | ||
height={20} | ||
shouldOpenLightbox={false} | ||
className="shrink-0" | ||
/> | ||
<span>{locale.name}</span> | ||
</button> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
</PopoverContent> | ||
</Popover> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eda69a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
leo-huynh-dot-dev – ./
leo-huynh-dot-dev-git-main-hta218.vercel.app
www.leohuynh.dev
leo-huynh-dot-dev-hta218.vercel.app
leohuynh.dev