Skip to content

Commit

Permalink
chore(language-switcher): use proper <a> tags
Browse files Browse the repository at this point in the history
https://github.com/orgs/mdn/discussions/365

removes another use of client-side navigation
  • Loading branch information
LeoMcA committed Aug 16, 2023
1 parent 9e29236 commit 2df370c
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions client/src/ui/organisms/article-actions/language-menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,14 @@ export function LanguageMenu({
}) {
const menuId = "language-menu";
const ga = useGA();
const { pathname } = useLocation();
const navigate = useNavigate();
const locale = useLocale();
const [isOpen, setIsOpen] = useState<boolean>(false);

function translateURL(destinationLocale: string) {
return pathname.replace(`/${locale}/`, `/${destinationLocale}/`);
}

function changeLocale(event) {
event.preventDefault();

const preferredLocale = event.currentTarget.name;
const changeLocale: React.MouseEventHandler<HTMLAnchorElement> = (event) => {
const preferredLocale = event.currentTarget.dataset.locale;
// The default is the current locale itself. If that's what's chosen,
// don't bother redirecting.
if (preferredLocale !== locale) {
const localeURL = translateURL(preferredLocale);
let cookieValueBefore = document.cookie
.split("; ")
.find((row) => row.startsWith(`${PREFERRED_LOCALE_COOKIE_NAME}=`));
Expand Down Expand Up @@ -71,16 +62,10 @@ export function LanguageMenu({
eventAction: `Change preferred language (cookie before: ${
cookieValueBefore || "none"
})`,
eventLabel: `${window.location.pathname} to ${localeURL}`,
eventLabel: `${window.location.pathname} to ${event.currentTarget.href}`,
});

navigate(localeURL);
window.scrollTo(0, 0);

setIsOpen(false);
onClose();
}
}
};

const menuEntry = {
label: "Languages",
Expand Down Expand Up @@ -122,16 +107,28 @@ export function LanguageMenu({
);
}

function LanguageMenuItem({ translation, changeLocale, native }) {
function LanguageMenuItem({
translation,
changeLocale,
native,
}: {
translation: Translation;
changeLocale: React.MouseEventHandler<HTMLAnchorElement>;
native: string;
}) {
const { pathname } = useLocation();
const locale = useLocale();

return (
<button
<a
aria-current={translation.native === native || undefined}
key={translation.locale}
name={translation.locale}
data-locale={translation.locale}
onClick={changeLocale}
href={pathname.replace(`/${locale}/`, `/${translation.locale}/`)}
className="button submenu-item"
>
<span>{translation.native}</span>
</button>
</a>
);
}

0 comments on commit 2df370c

Please sign in to comment.