Skip to content
WebJs
Menu

Reads the text direction an element renders in.

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

Installation

pnpm dlx @webjsdev/ui add direction

Usage

Every kit component styles itself with logical properties (ps-*, ms-*, start-*, text-start), which the browser resolves from the nearest dir attribute, so setting dir="rtl" on an ancestor is the whole of it. Set lang on the same element, since a screen reader picks its voice from lang.

<html lang="ar" dir="rtl">
  <body>
    <!-- Your app content -->
  </body>
</html>

One region of a page can carry its own direction the same way:

<section lang="ar" dir="rtl">
  <button type="button" data-slot="button" class=${buttonClass()}>حفظ</button>
</section>

useDirection

The readDirection() helper returns the direction in effect for an element: the nearest ancestor's dir, else ltr. It reads attributes only, so it also runs during server rendering. Reach for it in code that must branch on the direction itself, such as a keyboard handler mapping ArrowLeft to next in RTL.

import { readDirection } from '#components/ui/direction.ts';

class MyComponent extends WebComponent {
  render() {
    const direction = readDirection(this);
    return html`<div>Current direction: ${direction}</div>`;
  }
}