shadcn.io is not affiliated with official shadcn/ui
Language Switch Top Bar Banner Block for React
A 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.
Installation
Overview
A top bar for React with Base UI, a banner that shows what it announces by saying it in three languages. Heathstead's strip sits between the page's rails with a language icon at its start, one centred sentence ("The briefing comes in three languages.") and an underlined button, "Switch to Español". Pressing it rewrites the banner itself in Spanish, whose button then offers Arabic, whose button offers English again. Each version sits in a span with its own lang and dir, so the Arabic sentence runs right to left while the strip, its close button and its rules stay where they are. The three versions share one grid cell, so the strip stays 44 pixels on a desktop and two lines at most on a phone. A ghost close folds the strip away. It follows your theme and is built from Guides and Button. It switches the banner's own text, not the page's language: for that, see the language selector pill.
FAQ
How do I add a language or change the wording?
Edit VERSIONS at the top of the file: for each language the BCP 47 code, the direction dir, the line, the lead words in front of the next language's name and the language's own name. The button cycles through the list in order and wraps round. Keep each sentence and its button under about 55 characters so they fit two lines at 320 pixels.
Why doesn't the bar move when the language changes?
All the versions are rendered in the same grid cell: the visible one, plus a hidden copy of each (invisible, aria-hidden and inert), so the cell is always as tall as the tallest version. Switching only changes which text shows. The button itself is the same element in every version, so keyboard focus stays on it.
How does the Arabic version work?
Its span carries lang="ar" and dir="rtl", which sets the sentence's reading order, puts the button at the sentence's end and isolates it from the English page around it. The name of the next language in each button is its own lang and dir span too, so "العربية" inside a Spanish sentence and "English" inside an Arabic one read correctly. If the page itself is already right to left, the strip's own layout (the icon, the close button) mirrors with it.
What does a screen reader say when I switch?
A polite status line, present from load and empty until the first switch, then holds the new sentence, with the new language's lang and dir so it is read in that voice. The visible sentence is not itself a live region, so nothing is announced twice. The button keeps its place, and its name changes with the sentence.
What does the close button do?
It folds the strip's height away with a CSS transition (at once under reduced motion), makes the bar inert, moves focus to the first thing after it on the page and says "Announcement dismissed." The section's padding is inside the fold, so the block ends up 0 pixels tall.
Related
- Pro
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.
- Pro
Top Bar Banner
Top barsA slim announcement strip for a writing app's release: one centred line with the version, the news and an underlined link, and a ghost button that folds the strip away.
- Pro
Dismissible Top Bar Banner
Top barsA top bar for a developer tool whose long message stops at an ellipsis, with a More button that opens the whole text and a ghost close that folds the strip away.
- Pro
Workspace Top Bar Banner
Utility barsA top bar for a team workspace manager that says which workspace you are in, with your role as a badge and a Switch menu of workspaces behind a divider.
- Pro
Dropdown Multi-Action Banner
Choices and promptsA region prompt for a data platform: a Select of four regions with their latency beside a Shine Button Continue and a ghost Skip, with a line that says why Continue waits.
- Pro
A launch band for a design tool: a tinted strip with a centred line, a Launch week chip whose outline a light sweeps three times, and a link to the keynote.