Skip to content

Commit 4c9d1fc

Browse files
committed
fix(navbar): mirror layout for RTL locales
1 parent 46a94a5 commit 4c9d1fc

2 files changed

Lines changed: 67 additions & 7 deletions

File tree

‎src/registry/components/navbar.tsx‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { cn } from "@/lib/utils";
55
import { useControllableState } from "@/lib/use-controllable-state";
66
import { useListboxNavigation } from "@/lib/use-listbox-navigation";
77
import type { Locale } from "./types";
8-
import { getFlag, getLanguageName } from "./locale-data";
8+
import { getFlag, getLanguageName, isRtl } from "./locale-data";
99

1010
export type { Locale };
1111

@@ -74,6 +74,7 @@ export const NavBar = React.forwardRef<HTMLElement, NavBarProps>(function NavBar
7474
githubUrl = "https://github.com/babelize/babelize-elements",
7575
sticky = true,
7676
className,
77+
dir,
7778
showFlags = true,
7879
maxHeight = "15rem",
7980
linkComponent: Link = "a",
@@ -98,10 +99,9 @@ export const NavBar = React.forwardRef<HTMLElement, NavBarProps>(function NavBar
9899
const mobileTriggerRef = React.useRef<HTMLButtonElement>(null);
99100
const menuId = React.useId();
100101

101-
const currentLocaleInfo = getLocaleInfo(
102-
locale,
103-
locales.find((l) => l.code === locale),
104-
);
102+
const activeLocale = locales.find((l) => l.code === locale);
103+
const direction = dir ?? ((activeLocale?.rtl ?? isRtl(locale)) ? "rtl" : "ltr");
104+
const currentLocaleInfo = getLocaleInfo(locale, activeLocale);
105105

106106
/**
107107
* The trigger that owns the interaction: whichever bar is open, or — while the
@@ -197,7 +197,7 @@ export const NavBar = React.forwardRef<HTMLElement, NavBarProps>(function NavBar
197197
);
198198

199199
const langDropdown = (
200-
<div className="absolute right-0 top-full z-50 mt-1.5 min-w-[170px] rounded-xl border border-zinc-200 bg-white py-1.5 shadow-xl dark:border-zinc-800 dark:bg-zinc-950">
200+
<div className="absolute end-0 top-full z-50 mt-1.5 min-w-[170px] rounded-xl border border-zinc-200 bg-white py-1.5 shadow-xl dark:border-zinc-800 dark:bg-zinc-950">
201201
{/* The listbox owns the options only, and scrolls on its own: a long list
202202
never drags the page along with it. */}
203203
<div
@@ -216,11 +216,12 @@ export const NavBar = React.forwardRef<HTMLElement, NavBarProps>(function NavBar
216216
key={item.code}
217217
type="button"
218218
role="option"
219+
dir={(item.rtl ?? isRtl(item.code)) ? "rtl" : "ltr"}
219220
aria-selected={isCurrent}
220221
onClick={() => nav.select(index)}
221222
{...nav.optionProps(index)}
222223
className={cn(
223-
"mx-1.5 flex w-[calc(100%-12px)] items-center gap-2.5 rounded-lg px-3 py-2 text-sm transition-colors",
224+
"mx-1.5 flex w-[calc(100%-12px)] items-center gap-2.5 rounded-lg px-3 py-2 text-start text-sm transition-colors",
224225
isCurrent
225226
? "bg-emerald-500/10 text-emerald-600 font-medium dark:text-emerald-400"
226227
: isActive
@@ -252,6 +253,7 @@ export const NavBar = React.forwardRef<HTMLElement, NavBarProps>(function NavBar
252253
<nav
253254
ref={forwardedRef}
254255
{...rest}
256+
dir={direction}
255257
onKeyDown={(event) => {
256258
nav.onKeyDown(event);
257259
onKeyDown?.(event);

‎test/navbar.test.tsx‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,64 @@ describe("NavBar", () => {
162162
});
163163
});
164164

165+
describe("NavBar direction", () => {
166+
it("mirrors the bar when selecting an RTL locale and switches back for LTR", async () => {
167+
render(<NavBar locales={[{ code: "en" }, { code: "ar" }]} />);
168+
const bar = screen.getByRole("navigation");
169+
const trigger = () => screen.getAllByRole("button", { name: /Current language/ })[0];
170+
expect(bar).toHaveAttribute("dir", "ltr");
171+
await userEvent.click(trigger());
172+
await userEvent.click(screen.getByRole("option", { name: /العربية/ }));
173+
expect(bar).toHaveAttribute("dir", "rtl");
174+
await userEvent.click(trigger());
175+
await userEvent.click(screen.getByRole("option", { name: /English/ }));
176+
expect(bar).toHaveAttribute("dir", "ltr");
177+
});
178+
179+
it.each([
180+
{ code: "en", rtl: true, expected: "rtl" },
181+
{ code: "ar", rtl: false, expected: "ltr" },
182+
{ code: "ar-EG", rtl: undefined, expected: "rtl" },
183+
])("resolves $code with rtl=$rtl to $expected", ({ code, rtl, expected }) => {
184+
render(<NavBar locales={[{ code, rtl }]} />);
185+
expect(screen.getByRole("navigation")).toHaveAttribute("dir", expected);
186+
});
187+
188+
it.each(["ltr", "rtl", "auto"])("honors an explicit dir=%s", (dir) => {
189+
render(<NavBar locales={[{ code: "ar" }]} dir={dir} />);
190+
expect(screen.getByRole("navigation")).toHaveAttribute("dir", dir);
191+
});
192+
193+
it("follows controlled locale updates", () => {
194+
const { rerender } = render(<NavBar locales={[{ code: "en" }, { code: "ar" }]} value="en" />);
195+
rerender(<NavBar locales={[{ code: "en" }, { code: "ar" }]} value="ar" />);
196+
expect(screen.getByRole("navigation")).toHaveAttribute("dir", "rtl");
197+
});
198+
199+
it.each([0, 1])("keeps each option's direction in menu %i", async (triggerIndex) => {
200+
render(
201+
<NavBar
202+
locales={[
203+
{ code: "en" },
204+
{ code: "ar" },
205+
{ code: "fr", rtl: true },
206+
{ code: "he", rtl: false },
207+
]}
208+
/>,
209+
);
210+
await userEvent.click(
211+
screen.getAllByRole("button", { name: /Current language/ })[triggerIndex],
212+
);
213+
const options = screen.getAllByRole("option");
214+
expect(options.map((option) => option.getAttribute("dir"))).toEqual([
215+
"ltr",
216+
"rtl",
217+
"rtl",
218+
"ltr",
219+
]);
220+
});
221+
});
222+
165223
describe("NavBar keyboard navigation", () => {
166224
const triggers = () => screen.getAllByRole("button", { name: /Current language/ });
167225
// Index 0 is the desktop bar, index 1 the mobile one — both live in the DOM.

0 commit comments

Comments
 (0)