Direction
Tells every component inside which way text runs, so menus, sliders and tabs move the right way round in right-to-left languages.
The slider fills from the start of the line, and the arrow keys follow the way it runs.
"use client";
import { useState } from "react";
import { DirectionProvider, type TextDirection } from "@/components/ui/direction";
import { Slider } from "@/components/ui/slider";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
export default function DirectionDemo() {
const [dir, setDir] = useState<TextDirection>("rtl");
return (
<div className="flex w-full max-w-sm flex-col gap-6">
<ToggleGroup value={[dir]} onValueChange={(next) => next[0] && setDir(next[0] as TextDirection)} variant="outline" aria-label="Direction">
<ToggleGroupItem value="ltr" seed="dir-ltr">
Left to right
</ToggleGroupItem>
<ToggleGroupItem value="rtl" seed="dir-rtl">
Right to left
</ToggleGroupItem>
</ToggleGroup>
<div dir={dir}>
<DirectionProvider direction={dir}>
<Tabs defaultValue="one">
<TabsList>
<TabsTrigger value="one">One</TabsTrigger>
<TabsTrigger value="two">Two</TabsTrigger>
</TabsList>
<TabsContent value="one" className="flex flex-col gap-4 pt-2">
<Slider defaultValue={[40]} aria-label="Amount" />
<p className="text-ink-2">The slider fills from the start of the line, and the arrow keys follow the way it runs.</p>
</TabsContent>
<TabsContent value="two">
<p className="text-ink-2">Tabs, menus and sliders all follow `dir`.</p>
</TabsContent>
</Tabs>
</DirectionProvider>
</div>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ direction"use client";
import { DirectionProvider as DirectionProviderPrimitive, useDirection, type TextDirection } from "@base-ui/react/direction-provider";
import type { ReactNode } from "react";
/**
* Tells everything inside which way text runs, so menus, sliders, tabs and
* the rest open and move the right way round in right-to-left languages.
* Set `dir` on the page as well: this only informs the components.
*/
function DirectionProvider({ dir, direction, children }: { dir?: TextDirection; direction?: TextDirection; children: ReactNode }) {
return <DirectionProviderPrimitive direction={direction ?? dir ?? "ltr"}>{children}</DirectionProviderPrimitive>;
}
export { DirectionProvider, useDirection, type TextDirection };Usage
import { DirectionProvider } from "@/components/ui/direction"
<html dir="rtl">
<body>
<DirectionProvider direction="rtl">{children}</DirectionProvider>
</body>
</html>API reference
- direction (or dir)default
"ltr" "ltr" | "rtl"Which way text runs. Set `dir` on the page too: the provider only tells the components.
- useDirection()
() => { direction }Read it from any component inside.
Every other prop goes to Base UI's Direction Provider.