TextDirection

Reactive read/write wrapper around an element's dir attribute for detecting and controlling text directionality.

Loading demo...

Usage

angular-ts
import { Component } from '@angular/core';
import { textDirection } from '@signality/core';

@Component({
  template: `
    <p>Current direction: {{ dir() }}</p>
    <button (click)="dir.set('rtl')">RTL</button>
    <button (click)="dir.set('ltr')">LTR</button>
  `,
})
export class TextDirectionDemo {
  readonly dir = textDirection(); 
}

Use InjectionToken for Singleton

For global state tracking like textDirection, consider using the provided TEXT_DIRECTION token instead of calling the function directly. This provides a singleton instance that can be shared across your entire application, reducing memory usage and event listener overhead:

typescript
import { inject } from '@angular/core';
import { TEXT_DIRECTION } from '@signality/core';

const dir = textDirection(); 
const dir = inject(TEXT_DIRECTION); 

Learn more about Token-based utilities.

Parameters

ParameterTypeDescription
optionsTextDirectionOptionsOptional configuration (see Options below)

Options

The TextDirectionOptions extends CreateSignalOptions<TextDirection> and WithInjector:

OptionTypeDefaultDescription
targetMaybeElementSignal<HTMLElement>document.documentElementElement to observe
initialValueTextDirection'ltr'Initial direction value used before the DOM is read
equal(a: TextDirection, b: TextDirection) => boolean-Custom equality function for the signal
injectorInjector-Optional injector for DI context

Return Value

Returns a WritableSignal<TextDirection>. Possible values:

  • 'ltr' — Left-to-right text direction
  • 'rtl' — Right-to-left text direction
  • 'auto' — Direction determined by the browser based on content

Calling .set(dir) updates both the signal and the element's dir attribute. External attribute changes are automatically tracked via MutationObserver.

Examples

RTL support toggle

angular-ts
import { Component, computed } from '@angular/core';
import { textDirection } from '@signality/core';

@Component({
  template: `
    <button (click)="toggle()">
      Switch to {{ isRtl() ? 'LTR' : 'RTL' }}
    </button>
  `,
})
export class RtlToggle {
  readonly dir = textDirection();
  readonly isRtl = computed(() => this.dir() === 'rtl');

  toggle() {
    this.dir.set(this.isRtl() ? 'ltr' : 'rtl');
  }
}

Observe specific element

angular-ts
import { Component, viewChild, ElementRef } from '@angular/core';
import { textDirection } from '@signality/core';

@Component({
  template: `
    <div #content dir="auto">
      <p>Direction: {{ dir() }}</p>
    </div>
  `,
})
export class ElementDirectionDemo {
  readonly content = viewChild<ElementRef>('content');
  readonly dir = textDirection({ target: this.content }); 
}

SSR Compatibility

On the server, returns a regular WritableSignal initialized with 'ltr' (or the provided initialValue).

Type Definitions

typescript
type TextDirection = 'ltr' | 'rtl' | 'auto';

interface TextDirectionOptions extends CreateSignalOptions<TextDirection>, WithInjector {
  readonly target?: MaybeElementSignal<HTMLElement>;
  readonly initialValue?: TextDirection;
}

function textDirection(options?: TextDirectionOptions): WritableSignal<TextDirection>;
Edit this page on GitHub Last updated: Mar 19, 2026, 23:28:23