shadcn.io is not affiliated with official shadcn/ui
Language Selector Pill Banner Block for React
A language pill for a travel-guide site: a globe, "Guides in" and a button naming the current language, opening a popover of six languages written in their own scripts.
Installation
Overview
A language pill for React with Base UI, a quiet switcher that stays out of the way until it's needed. An end-aligned capsule sits in a muted shell between the page's rails: a globe, the words "Guides in" and an outline button that shows the current language in its own script. The button opens a popover list of six languages (English, Español, Français, Deutsch, 日本語 and 中文), each in its own script with its lang attribute, its English name beside it and a check on the current one. Choosing one updates the pill, closes the popover, returns focus to the button and says so in a polite line. The pill stays one row at every width. It follows your theme and the popover uses its own surface tokens. It's built from Guides, Popover and Button.
FAQ
How do I connect it to my router or i18n library?
Everything that happens on a choice is in one function, choose: it stores the code, closes the popover and speaks the result. Replace its first line with your navigation (swap the locale segment of the URL, or call your i18n library's setLocale) and let the page reload in that language. Write the code to a cookie too, so the next visit starts in the same language.
Why are the names written in their own script?
Someone who reads only Japanese can't find "Japanese" in a list. Each language's name is in its own script with a lang attribute, so screen readers switch voice for it, and the English name sits beside it as a quiet hint. The button's visually hidden prefix, "Language:", names the control for assistive technology.
How do I add a right-to-left language?
Add an entry such as { code: "ar", english: "Arabic", native: "العربية" }. The pill and the list already use logical spacing, so they mirror with the page. In choose, set dir="rtl" on the document's html element when the code is a right-to-left one, and keep numbers and code in dir="ltr" spans.
What does the keyboard do?
Enter or Space on the button opens the list, Tab and Shift+Tab move through the languages, Enter chooses one, and Escape closes the list without changing anything. After either, focus is back on the button. The current language carries aria-current, so screen readers say which one it is.
How do I pin it to the bottom of the screen?
The pill renders in flow so the preview and a stack of blocks can see it. To float it, add fixed inset-x-4 bottom-4 z-50 mx-auto to the pill (the section named "Guide language") and remove the block's wrapper padding if the pill lives on its own. The popover flips upward on its own when there is no room below. Keep room for the home indicator with bottom-[max(1rem,env(safe-area-inset-bottom))].
Can it start in the visitor's browser language?
Yes, but decide on the server: read the Accept-Language header and pass the best match as the first code, so the pill's first paint is already right. Reading navigator.language in an effect works too, at the cost of the button's label changing a moment after load.
Related
- Pro
Language Switch Top Bar Banner
Top barsA top bar for a news site that translates itself: one line in English, Spanish or Arabic, with an underlined button that switches the line to the next language and flips it to right to left for Arabic.
- Pro
A theme pill for a writing app: a three-way toggle group (Light, System, Dark) seeded from the page's theme, with a label that says which one is on.
- Pro
Reading Preferences Multi-Action Banner
Choices and promptsA reading-preferences prompt for a reading app: three toggles with a live preview that changes size, weight and border, a polite count, and Save, Reset and Not now.
- Pro
A dock pill for a workspace app: a toolbar of four icon buttons (New page, Search, Share, Notifications), each with a tooltip showing its name and its key.
- Pro
Theme Picker Multi-Action Banner
Choices and promptsA corner-radius picker card for a design-token editor: four chips drawn with their own corners, a greyscale preview that re-rounds as you choose, and Apply and Reset actions.
- Pro
An install pill for a plant-care app: a greyscale app tile, the name, a row of stars with its score, a small Get link and a dismiss button that folds the block away.