Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LocaleSwitcher Component #273

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

jcq
Copy link
Contributor

@jcq jcq commented Dec 22, 2023

Ticket

Resolves #237

Changes

  • Created LocaleSwitcher component and added it as the final item in the nav in Header
  • Currently this wraps the react-uswds LanguageSelector component
  • this implements some hacky styling workarounds due to react-uswds component erroneously applying .usa-language-container class to button as well as wrapping div
  • added tests, but they are failing a11y tests due to the react-uswds component erroneously applying a aria-controls prop even in dual-language mode when the corresponding element isn't present (it is in dropdown/multiple-language mode)

Context for reviewers

This can be seen in the nav when running the app in localhost. However, there are multiple issues with the implementation from react-uswds that make me think we may want to re-implement our own until such time as that version gets updated. I do also still have some UX concerns over both the basic USWDS implementation and the specific one from react-uswds.

Testing

- this wraps the `react-uswds` `LanguageSelector` component
- it is functional, but I have some UX concerns
- we will likely want to tweak styling
added test for LocaleSwitcher
this currently fails due to issue w/ `react-uswds` component
Comment on lines +25 to +29
// This won't actually work because the NextIntlProvider relies on middleware that isn't available in tests
// expect(
// await screen.findByRole("button", { name: "English" })
// ).toBeInTheDocument();
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left this in to note an a11y issue with the current version of the react-uswds component.

import { LanguageDefinition, LanguageSelector } from "@trussworks/react-uswds";

// Currently, the `react-uswds` component erroneously sets 'usa-language-container' class
// on both the container and the button, which causes incorrect positioning relative to nav items
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we open a PR to fix that for them? They're usually pretty open to contributions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file needed? I think the middleware handles prefixing and automatically redirecting to the expected locale prefix based on the preferred locale. The localePrefix setting in this file also looks different than what's set in the middleware.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is for re-exporting some enhanced versions of NextJS navigation functions. Its usage is documented here:
https://next-intl-docs.vercel.app/docs/routing/navigation#shared-pathnames

That said, I totally forgot to update to our actual locales and instead just copy-pasted what they had in the docs. 😓

I'm updating it to just pull locales from the existing export so they are only defined once.

I'm not entirely certain of their reasons for keeping this as its own file vs simply adding these exports to config.ts. My guess is that it is largely for slightly clearer code since it's specifically re-exporting stuff from next/navigation. I'm sort of ambivalent as to where we'd prefer to put it. 🤷 Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it accurate to say that these utilities are only needed for the locale switcher component?

It seems like the language is preserved fine when using next/navigation for routing elsewhere, so am wondering if these could be co-located in the component's file.

I was able to test usage of normal next/navigation routing by adding the following to the homepage and observing the URL and header's translated text. Seems to work:

// TestButton.tsx
"use client";

import { useRouter } from "next/navigation";

export default function TestButton() {
  const router = useRouter();

  return (
    <button onClick={() => router.push("/health")}>Go to health page</button>
  );
}
CleanShot.2024-01-11.at.10.17.45.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement the "Select a language" design pattern
2 participants