Skip to content
GitHub11 stars

Right-to-left support for Blazix components.

Blazix components support right-to-left (RTL) layouts. Text alignment, positioning, keyboard navigation and directional styles follow the reading direction for languages like Arabic, Hebrew, and Persian.

Translated with AI for the demo, so it may have mistakes.

تسجيل الدخول إلى حسابك
أدخل بريدك الإلكتروني أدناه لتسجيل الدخول إلى حسابك
نسيت كلمة المرور؟
A card component in RTL mode.

When you add components to a project set up for RTL, the CLI turns physical positioning classes into logical ones, so the components work in both left-to-right and right-to-left layouts.

Get Started

Set up the project for RTL

For a new project, pass --rtl to init. It sets "rtl": true in blazix.json.

dnx Blazix.Cli init --rtl

For a project that already has components, migrate them. See Migrating existing components.

Set the direction on the page

Put dir and lang on <html> in App.razor. The layout, keyboard navigation and popup placement all read the direction from there.

Components/App.razor
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    ...
</head>
<body>
    <Routes />
    <script src="_framework/blazor.web.js"></script>
</body>
</html>

Add the direction provider

Some things are decided while rendering, where there's no CSS to read: which way a Tabs or Navigation Menu indicator moves, how a Slider thumb is centred, and which way the arrow keys go in a Calendar. Wrap the app in a DirectionProvider to tell them.

dnx Blazix.Cli add direction
Components/Routes.razor
<DirectionProvider Direction="TextDirection.Rtl">
    <Router AppAssembly="typeof(Program).Assembly">
        ...
    </Router>
</DirectionProvider>

How it works

With "rtl": true in blazix.json, add rewrites the classes of each component as it copies it, the way shadcn/ui's CLI does:

  • Physical classes become logical ones: ml-2 becomes ms-2, pr-4 becomes pe-4, left-0 becomes start-0, rounded-l-lg becomes rounded-s-lg, border-r becomes border-e and text-left becomes text-start.
  • Classes with no logical form get an rtl: twin: -translate-x-1/2 gets rtl:translate-x-1/2, and cursor-w-resize gets rtl:cursor-e-resize.
  • Popups on a logical side slide in from it: data-[side=inline-end]:slide-in-from-left-2 becomes slide-in-from-start-2.
  • Directional icons (chevrons, arrows) turn around under rtl:rotate-180.
  • Context Menu and Dropdown Menu submenus open on Side.InlineEnd instead of Side.Right.

Where the side is physical, classes stay physical: a sheet with Side="SheetSide.Left" keeps its border on its right edge, and a tooltip on Side.Left keeps its arrow on the right. Tailwind 4's space-x-* and divide-x-* already follow the direction, so they're left as they are.

Without "rtl": true, components are copied with physical classes, as before.

Sides

Popups take a logical side: Side.InlineStart and Side.InlineEnd follow the reading direction, and the popup's data-side keeps saying inline-start or inline-end. Side.Left and Side.Right stay where they are. See the Tooltip and Popover examples.

Sheet, Drawer and Sidebar sides are physical. Pick the side for the direction, as the Sheet and Sidebar examples do.

Icons

Icons that point along the line, like ArrowRight or ChevronLeft, need rtl:rotate-180 to point the right way. The components that ship one already have it. Add it to your own:

<Button Variant="ButtonVariant.Outline">
    Next
    <ArrowRight class="rtl:rotate-180" data-icon="inline-end" />
</Button>

Font Recommendations

Use a font that covers your language. Noto has Arabic, Hebrew and Persian families, and it pairs well with Inter and Geist.

Migrating existing components

Run the migrate command

dnx Blazix.Cli migrate rtl [path]

[path] is a file or a folder to migrate. Without one, it migrates the files in componentsDir. It also sets "rtl": true, so components you add later come migrated.

Run it once: a second run adds the rtl: classes again.

Check the components it lists

Sidebar.razor, Pagination.razor and Calendar.razor may need a look: pick the sidebar's side for the direction, and check that the pagination and calendar chevrons point the right way.

Turn your own icons around

Add rtl:rotate-180 to the directional icons in your own markup. See Icons.