Skip to content

Commit

Permalink
feat: ajoute sélecteur de langue dans l'en-tête
Browse files Browse the repository at this point in the history
  • Loading branch information
ocruze committed Oct 23, 2023
1 parent 2748ad5 commit 1734ef0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions assets/components/Layout/AppHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { MainNavigationProps } from "@codegouvfr/react-dsfr/MainNavigation/MainN
import { FC, ReactNode } from "react";

import useUser from "../../hooks/useUser";
import { useLang } from "../../i18n";
import SymfonyRouting from "../../modules/Routing";
import { routes } from "../../router/router";
import LanguageSelector from "../Utils/LanguageSelector";

export type NavigationProps = MainNavigationProps.Item[] | ReactNode;

Expand All @@ -13,6 +15,7 @@ type AppHeaderProps = {
};
const AppHeader: FC<AppHeaderProps> = ({ navItems = [] }) => {
const { user } = useUser();
const { lang, setLang } = useLang();

const quickAccessItems: (HeaderProps.QuickAccessItem | ReactNode)[] = [];

Expand Down Expand Up @@ -55,6 +58,8 @@ const AppHeader: FC<AppHeaderProps> = ({ navItems = [] }) => {
});
}

quickAccessItems.push(<LanguageSelector lang={lang} setLang={setLang} />);

return (
<Header
brandTop={
Expand Down
63 changes: 63 additions & 0 deletions assets/components/Utils/LanguageSelector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { fr } from "@codegouvfr/react-dsfr";
import { memo, useId } from "react";
import { symToStr } from "tsafe/symToStr";

import { languagesDisplayNames, languages, type Language } from "../../i18n";

type Props = {
lang: Language;
setLang: (lang: Language) => void;
};

const LanguageSelector = memo((props: Props) => {
const { lang, setLang } = props;

const langSelectId = useId();

return (
<nav
role="navigation"
className={fr.cx("fr-translate", "fr-nav")}
style={{
display: "inline-flex",
}}
>
<div className="fr-nav__item">
<button
className="fr-translate__btn fr-btn fr-btn--tertiary"
aria-controls={langSelectId}
aria-expanded="false"
title="Sélectionner une langue"
>
{lang.toUpperCase()}
<span className="fr-hidden-lg">&nbsp;-&nbsp;{languagesDisplayNames[lang.toLowerCase()]}</span>
</button>
<div className={fr.cx("fr-collapse", "fr-translate__menu", "fr-menu")} id={langSelectId}>
<ul className={fr.cx("fr-menu__list")}>
{languages.map((lang_i) => (
<li key={lang_i}>
<a
className={fr.cx("fr-translate__language", "fr-nav__link")}
href="#"
hrefLang={lang_i}
lang={lang_i}
aria-current={lang_i === lang ? "true" : undefined}
onClick={(e) => {
e.preventDefault();
setLang(lang_i);
}}
>
{lang_i}&nbsp;-&nbsp;{languagesDisplayNames[lang_i]}
</a>
</li>
))}
</ul>
</div>
</div>
</nav>
);
});

LanguageSelector.displayName = symToStr({ LanguageSelector });

export default LanguageSelector;

0 comments on commit 1734ef0

Please sign in to comment.