Added languageswitcher, alternatelinkscontext components support
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
'use client'
|
||||
|
||||
import { createContext, use, useCallback, useState } from 'react'
|
||||
|
||||
/** Map of locale code → absolute-or-relative URL for the current page. */
|
||||
export type AlternateLinks = Record<string, string>
|
||||
|
||||
type AlternateLinksContextValue = {
|
||||
links: AlternateLinks
|
||||
setLinks: (links: AlternateLinks) => void
|
||||
}
|
||||
|
||||
const AlternateLinksContext = createContext<AlternateLinksContextValue | null>(null)
|
||||
|
||||
/**
|
||||
* Holds the current page's per-locale URLs so a LanguageSwitcher links to the
|
||||
* CORRECT localized slug instead of naively swapping the locale prefix
|
||||
* (/pl/uslugi → /en/uslugi, which 404s when the English slug is /en/services).
|
||||
*
|
||||
* Wrap the app once; each page reports its own alternates via
|
||||
* <AlternateLinksUpdater> (or useSetAlternateLinks). The switcher reads them
|
||||
* with useAlternateLinks().
|
||||
*
|
||||
* // layout.tsx
|
||||
* import { AlternateLinksProvider } from '@intecion/ipal-kit/client'
|
||||
* <AlternateLinksProvider>{children}</AlternateLinksProvider>
|
||||
*/
|
||||
export function AlternateLinksProvider({ children }: { children: React.ReactNode }) {
|
||||
const [links, setLinksState] = useState<AlternateLinks>({})
|
||||
const setLinks = useCallback((next: AlternateLinks) => setLinksState(next), [])
|
||||
return (
|
||||
<AlternateLinksContext value={{ links, setLinks }}>
|
||||
{children}
|
||||
</AlternateLinksContext>
|
||||
)
|
||||
}
|
||||
|
||||
/** Read the current page's per-locale URLs (for a language switcher). */
|
||||
export function useAlternateLinks(): AlternateLinks {
|
||||
const ctx = use(AlternateLinksContext)
|
||||
return ctx?.links ?? {}
|
||||
}
|
||||
|
||||
/** Setter for the alternates (used by AlternateLinksUpdater). */
|
||||
export function useSetAlternateLinks(): (links: AlternateLinks) => void {
|
||||
const ctx = use(AlternateLinksContext)
|
||||
return ctx?.setLinks ?? (() => {})
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect } from 'react'
|
||||
|
||||
import { type AlternateLinks, useSetAlternateLinks } from './AlternateLinksContext.js'
|
||||
|
||||
/**
|
||||
* Reports the current page's per-locale URLs into AlternateLinksProvider. Render
|
||||
* it on each page (a Server Component computes the links via resolveRoute /
|
||||
* getLocalizedSlugs and passes them down). The switcher then links correctly.
|
||||
*
|
||||
* // page.tsx (RSC) — compute alternates from the resolved route
|
||||
* import { AlternateLinksUpdater } from '@intecion/ipal-kit/client'
|
||||
* const alternates = { pl: '/pl/uslugi/karne', en: '/en/services/criminal' }
|
||||
* return <>
|
||||
* <AlternateLinksUpdater links={alternates} />
|
||||
* {/* page content *\/}
|
||||
* </>
|
||||
*
|
||||
* Renders nothing; it only pushes the links into context on mount/change.
|
||||
*/
|
||||
export function AlternateLinksUpdater({ links }: { links: AlternateLinks }) {
|
||||
const setLinks = useSetAlternateLinks()
|
||||
const key = JSON.stringify(links)
|
||||
useEffect(() => {
|
||||
setLinks(links)
|
||||
// key covers link changes; setLinks is stable (useCallback).
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [key, setLinks])
|
||||
return null
|
||||
}
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,15 @@
|
||||
export {
|
||||
AlternateLinksProvider,
|
||||
useAlternateLinks,
|
||||
useSetAlternateLinks,
|
||||
} from './AlternateLinksContext.js'
|
||||
export type { AlternateLinks } from './AlternateLinksContext.js'
|
||||
export { AlternateLinksUpdater } from './AlternateLinksUpdater.js'
|
||||
export { createProxy, DEFAULT_PROXY_MATCHER } from './createProxy.js'
|
||||
export { getLocalizedSlugs } from './getLocalizedSlugs.js'
|
||||
export { getDefaultLocale, getLocaleCodes, getLocaleDefinition, isValidLocale } from './helpers.js'
|
||||
export { LanguageSwitcher } from './LanguageSwitcher.js'
|
||||
export type { LanguageSwitcherClassNames } from './LanguageSwitcher.js'
|
||||
export { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './localeMiddleware.js'
|
||||
export type { LocaleMiddlewareResult } from './localeMiddleware.js'
|
||||
export { buildLocalizationConfig } from './localizationConfig.js'
|
||||
|
||||
Reference in New Issue
Block a user