useDirection
The one thing that writes dir and lang on <html>. Direction follows the language, until you pin it.
Import
ts
import { useDirection } from '@core/i18n/useDirection'Signature
ts
function useDirection(): {
setDirection: (mode: DirectionMode) => void
currentDirection: Readonly<Ref<Direction>>
}Direction is 'ltr' | 'rtl'. DirectionMode is Direction | 'auto'.
Returns
| Member | Type | Description |
|---|---|---|
setDirection | (mode: DirectionMode) => void | 'auto' re-derives from the locale. 'ltr' or 'rtl' pins until the next call. |
currentDirection | Readonly<Ref<Direction>> | What dir currently is. Never 'auto' — that is a mode, not a direction. |
Example
vue
<script setup lang="ts">
import { useDirection } from '@core/i18n/useDirection'
const MODES = ['auto', 'ltr', 'rtl'] as const
const { setDirection, currentDirection } = useDirection()
</script>
<template>
<div class="flex flex-wrap items-center gap-2">
<button
v-for="mode in MODES"
:key="mode"
type="button"
@click="setDirection(mode)"
>
{{ mode }}
</button>
<span>Rendering {{ currentDirection }}</span>
</div>
</template>Notes
- Two paths reach
dir. A watcher on the active locale re-derives it while the mode is'auto', andsetDirection('ltr' | 'rtl')pins it. The right-to-left locales arear,he,faandur. - A pin is released by a language switch, because
setLocalecallssetDirection('auto'). That is what makes "preview the RTL layout" a preview: pick Arabic, or pinrtl, and the next language change puts it back. langis not pinned. The watcher mirrors the active locale onto<html lang>unconditionally, so an override changes the direction only, never the document's language. Screen readers and hyphenation keep reading the right one.- The watcher is
immediate, sodirandlangare correct on the first paint rather than one tick later. - Both writers no-op without a
document.
Related
useLocale— the switch that resets this toauto.- Internationalization — what RTL changes across the app.
useConfig—config.ui.directionis the stored mode.