Docs
useTextDirection

Detect and manage text direction (LTR or RTL) in React components with the useTextDirection hook, providing a way to adapt UI based on language directionality.

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-text-direction

Usage

"use client"
 
import { useTextDirection } from "@/hooks/use-text-direction"
 
export function Component() {
  const direction = useTextDirection({ initialDirection: "auto" })
 
  return (
    <div
      style={{
        padding: "24px",
        maxWidth: 500,
        margin: "0 auto",
        border: "1px solid #eee",
        borderRadius: 8,
        direction,
      }}
    >
      <h2>
        Current Direction: <code>{direction}</code>
      </h2>
      <p>
        This container's <b>CSS direction</b> is set to <code>{direction}</code>
        . Try changing your browser or document direction to see it update.
      </p>
      <input
        type="text"
        placeholder="Type here..."
        style={{ width: "100%", marginTop: 16, direction }}
      />
    </div>
  )
}

API Reference

Parameters

NameTypeDescriptionDefault ValueOptional
initialDirection"ltr" | "rtl" | "auto"Initial text direction to set. Defaults to auto.autoYes
watchbooleanWhether to watch for changes in text direction. Defaults to true.trueYes
targetElementHTMLElement | nullElement to observe for text direction changes. Defaults to document.documentElement.document.documentElementYes

Return Values

NameTypeDescription
direction"ltr" | "rtl"Current text direction. Can be either ltr or rtl.