📦 Install from npm
ngx-bidi is an Angular(^20.0.0) library for managing text direction (LTR / RTL) automatically based on selected language or manual control.
It supports:
- Automatic direction switching via directive
- Full programmatic control using NgxBidiService
- SCSS mixins for RTL/LTR styles
- Angular module and standalone usage
npm install ngx-bidiUse this if you want the directive available in a module:
import { NgxBidiModule } from 'ngx-bidi';
@NgModule({
imports: [NgxBidiModule]
})
export class AppModule {}If your component is standalone:
import { NgxBidiDirective } from 'ngx-bidi';
@Component({
standalone: true,
selector: 'app-example',
template: `<div dirAuto>...</div>`,
imports: [NgxBidiDirective],
})
export class ExampleComponent {}If you only need direction logic without binding to HTML:
import { NgxBidiService } from 'ngx-bidi';
constructor(private dir: NgxBidiService) {}
ngOnInit() {
this.dir.setLang('ar'); // rtl
this.dir.setDir('ltr'); // override
}Subscribe to changes:
this.dir.getDir$().subscribe(dir => console.log(dir));Opposite direction:
this.dir.getOppositeDir$().subscribe(dir => console.log(dir));Auto-detect direction:
<div dirAuto>Text</div>Force direction:
<div dirAuto="rtl">RTL block</div>
<div dirAuto="ltr">LTR block</div>Import SCSS helpers:
@use 'ngx-bidi/scss' as dir;All mixins support ViewEncapsulation via optional $use-host-context parameter (defaults to true for component styles).
dir-ltr($use-host-context: true)- Apply styles only for LTR directiondir-rtl($use-host-context: true)- Apply styles only for RTL directiondir($value, $use-host-context: true)- Apply styles for specific direction (ltr/rtl)
padding-start($value, $use-host-context: true)- Padding on start side (right in LTR, left in RTL)padding-end($value, $use-host-context: true)- Padding on end side (left in LTR, right in RTL)margin-start($value, $use-host-context: true)- Margin on start side (right in LTR, left in RTL)margin-end($value, $use-host-context: true)- Margin on end side (left in LTR, right in RTL)
float($pos, $use-host-context: true)- Float element to start or end (startorend)
left($value, $use-host-context: true)- Left position (right in RTL)right($value, $use-host-context: true)- Right position (left in RTL)
text-align-start($use-host-context: true)- Align text to start (left in LTR, right in RTL)text-align-end($use-host-context: true)- Align text to end (right in LTR, left in RTL)
transformTranslate($x, $y: 0, $use-host-context: true)- Translate with X-axis inversion for RTLtransformScale($x, $y: 1, $use-host-context: true)- Scale with X-axis mirroring for RTLmirror($use-host-context: true)- Full horizontal mirroring for RTL
start($property, $value, $use-host-context: true)- Apply any property to start sideend($property, $value, $use-host-context: true)- Apply any property to end side
If you're using mixins inside Angular components with ViewEncapsulation, the $use-host-context parameter defaults to true:
:host {
.button {
@include dir.padding-start(20px); // Uses :host-context by default
}
}For global styles, you can pass false:
.button {
@include dir.padding-start(20px, false); // Uses [dir] selector
}RTL is automatically enabled for:
['ar', 'he', 'fa', 'dv', 'ku', 'ur', 'ps']MIT