Skip to content
WebJs
Menu

Right-to-left support for WebJs UI components.

WebJs UI components have first-class support for right-to-left (RTL) layouts. Text alignment, positioning, and directional styles automatically adapt for languages like Arabic, Hebrew, and Persian.

تسجيل الدخول إلى حسابك
أدخل بريدك الإلكتروني أدناه لتسجيل الدخول إلى حسابك
نسيت كلمة المرور؟

The components are written with logical properties, so they install ready for both directions. Set dir="rtl" on an element and every component inside it lays itself out from the right.

Get Started

Set the direction

Add the dir="rtl" and lang="ar" attributes to the html tag. Update lang="ar" to your target language. The root layout is the one layout that may write the document shell, so it is where the attributes go.

app/layout.ts
import { html } from '@webjsdev/core';
import type { LayoutProps } from '@webjsdev/core';

export default function RootLayout({ children }: LayoutProps) {
  return html`<!doctype html>
<html lang="ar" dir="rtl">
  <head></head>
  <body>${children}</body>
</html>`;
}

The dir attribute is the direction provider, and it works on any element, so one region of a left-to-right page can be right-to-left: <section dir="rtl" lang="ar">. Set lang beside it, since a screen reader picks its voice from lang while the layout comes from dir.

Add Font

For the best RTL experience, we recommend using fonts that have proper support for your target language. Noto is a great font family for this and it pairs well with Inter and Geist.

Self-host the font file under public/fonts/, declare it in the Tailwind entry your app compiles, and make it the sans family:

public/input.css
@font-face {
  font-family: "Noto Sans Arabic";
  src: url("/fonts/noto-sans-arabic.woff2") format("woff2");
  font-weight: 100 900;
  font-display: swap;
}

@theme {
  --font-sans: "Noto Sans Arabic", ui-sans-serif, system-ui, sans-serif;
}

For other languages, eg. Hebrew, you can use Noto Sans Hebrew.

Add Components

You are now ready to add components to your project. They need no RTL step of their own.

pnpm dlx @webjsdev/ui add item

How it works

Every component styles itself with logical utilities, which the browser resolves from the nearest dir:

  • Padding and margin use ps-*, pe-*, ms-* and me-* instead of pl-*, pr-*, ml-* and mr-*.
  • Positioning uses start-* and end-* instead of left-* and right-*.
  • Text alignment and corners use text-start, text-end, rounded-s-* and rounded-e-*.
  • Keyboard handling follows the visual direction. A slider's Left and Right keys swap under RTL, for example.

For your own code that has to branch on the direction, such as an arrow-key handler, the Direction component exports readDirection(element), which reads the nearest dir attribute. It reads attributes only, so it also works while the page renders on the server.

pnpm dlx @webjsdev/ui add direction

Supported Styles

Every style supports RTL. The default style, new-york, is written with logical properties throughout, as are the components' own classes.

The eight styles (vega, nova, maia, lyra, mira, luma, sera and rhea) ship with a right-to-left conversion table already applied. So their physical utilities (pl-2, ml-auto, rounded-l-md) arrive as logical ones, and the same files render a left-to-right page exactly as before and a right-to-left one correctly.

Font Recommendations

For the best RTL experience, we recommend using fonts that have proper support for your target language. Noto is a great font family for this and it pairs well with Inter and Geist.

See Add Font above for how to install one in a WebJs app.

Animations

A dropdown, a popover or a tooltip slides in from the side it opened on. The positioning reports that side on the panel as data-side, the side it was actually placed on, and the animation keys on it, so it is right in either direction.

Overlays render in the top layer through the native <dialog> element and the Popover API, but they stay where you wrote them in the document rather than moving to a portal. So they inherit dir from their ancestors like everything else.

<div dir="rtl">
  <ui-tooltip>
    <ui-tooltip-trigger>...</ui-tooltip-trigger>
    <ui-tooltip-content>...</ui-tooltip-content>
  </ui-tooltip>
</div>

Icons

An icon that points a direction, like an arrow or a chevron, does not flip on its own. Add Tailwind's rtl:rotate-180 to it:

<svg class="rtl:rotate-180" ...>...</svg>