Added languageswitcher, alternatelinkscontext components support
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
|
||||
import type { I18nConfig } from './types.js'
|
||||
|
||||
import { useAlternateLinks } from './AlternateLinksContext.js'
|
||||
|
||||
export type LanguageSwitcherClassNames = {
|
||||
item?: string
|
||||
itemActive?: string
|
||||
menu?: string
|
||||
root?: string
|
||||
trigger?: string
|
||||
}
|
||||
|
||||
type LocaleItem = { code: string; label: string }
|
||||
|
||||
/**
|
||||
* Language switcher that links to the CORRECT localized URL per language,
|
||||
* reading the current page's alternates from AlternateLinksProvider — so it
|
||||
* never 404s on differing slugs (/pl/uslugi vs /en/services).
|
||||
*
|
||||
* Unstyled by default (classNames, like CookieBanner) — NOT a fixed
|
||||
* light/dark variant, so it matches any project's design. A locale with no
|
||||
* known alternate falls back to its root (/{code}).
|
||||
*
|
||||
* // layout/header (client), inside AlternateLinksProvider
|
||||
* <LanguageSwitcher
|
||||
* config={i18nConfig}
|
||||
* currentLocale={locale}
|
||||
* classNames={{ root: '...', trigger: '...', menu: '...', item: '...' }}
|
||||
* />
|
||||
*/
|
||||
export function LanguageSwitcher({
|
||||
classNames,
|
||||
config,
|
||||
currentLocale,
|
||||
renderLabel,
|
||||
}: {
|
||||
classNames?: LanguageSwitcherClassNames
|
||||
config: I18nConfig
|
||||
currentLocale: string
|
||||
/** Custom label per locale (e.g. flag + code). Defaults to the config label. */
|
||||
renderLabel?: (locale: LocaleItem) => React.ReactNode
|
||||
}) {
|
||||
const alternates = useAlternateLinks()
|
||||
const [open, setOpen] = useState(false)
|
||||
const cn = classNames ?? {}
|
||||
|
||||
const locales: LocaleItem[] = config.locales.map((l) => ({
|
||||
code: l.code,
|
||||
label: l.label ?? l.code.toUpperCase(),
|
||||
}))
|
||||
|
||||
const hrefFor = (code: string): string => alternates[code] ?? `/${code}`
|
||||
|
||||
const current = locales.find((l) => l.code === currentLocale)
|
||||
|
||||
return (
|
||||
<div className={cn.root} data-ipal-langswitcher>
|
||||
<button
|
||||
aria-expanded={open}
|
||||
aria-haspopup="true"
|
||||
className={cn.trigger}
|
||||
onClick={() => setOpen((o) => !o)}
|
||||
type="button"
|
||||
>
|
||||
{current ? (renderLabel ? renderLabel(current) : current.label) : currentLocale}
|
||||
</button>
|
||||
{open && (
|
||||
<ul className={cn.menu} role="menu">
|
||||
{locales.map((loc) => {
|
||||
const active = loc.code === currentLocale
|
||||
return (
|
||||
<li key={loc.code} role="none">
|
||||
<a
|
||||
aria-current={active ? 'true' : undefined}
|
||||
className={`${cn.item ?? ''} ${active ? (cn.itemActive ?? '') : ''}`.trim()}
|
||||
href={hrefFor(loc.code)}
|
||||
hrefLang={loc.code}
|
||||
onClick={() => setOpen(false)}
|
||||
role="menuitem"
|
||||
>
|
||||
{renderLabel ? renderLabel(loc) : loc.label}
|
||||
</a>
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user