Direction
Tells the components the reading direction, so a right-to-left page gets the right keyboard behaviour.
Direction tells the components inside it whether text runs left to right or right to
left. The dir attribute on an element turns the layout around; the provider adds what CSS cannot:
which way the arrow keys move in tabs, sliders, menus and toggle groups. Use both together for a
right-to-left language such as Arabic or Hebrew. It is Base UI's direction provider, passed
through.
// apps/docs/examples/ui/direction.tsx'use client';
import {useState} from 'react';
import {DirectionProvider} from '@workspace/active-ui/components/primitives/direction';import {Slider} from '@workspace/active-ui/components/primitives/slider';import { Tabs, TabsList, TabsTrigger,} from '@workspace/active-ui/components/primitives/tabs';
/** * The same tabs and slider left to right and right to left. The `dir` * attribute turns the layout around; `DirectionProvider` tells the * components, so the arrow keys follow the reading direction. */export default function DirectionExample() { const [direction, setDirection] = useState<'ltr' | 'rtl'>('rtl'); return ( <div className="flex w-full max-w-sm flex-col gap-4"> <Tabs value={direction} onValueChange={(value) => setDirection(value as 'ltr' | 'rtl')} > <TabsList aria-label="Reading direction"> <TabsTrigger value="ltr">Left to right</TabsTrigger> <TabsTrigger value="rtl">Right to left</TabsTrigger> </TabsList> </Tabs> <DirectionProvider direction={direction}> <div dir={direction} className="flex flex-col gap-4 rounded-lg border p-4" > <Tabs defaultValue="one"> <TabsList aria-label="Steps"> <TabsTrigger value="one">One</TabsTrigger> <TabsTrigger value="two">Two</TabsTrigger> <TabsTrigger value="three">Three</TabsTrigger> </TabsList> </Tabs> <Slider defaultValue={30} aria-label="Share" /> </div> </DirectionProvider> </div> );}Usage#
import {DirectionProvider} from '@workspace/active-ui/components/primitives/direction';
<html dir="rtl"> <body> <DirectionProvider direction="rtl">{app}</DirectionProvider> </body></html>;What your application provides#
The direction of your reader's language, on the
dirattribute of<html>(or of the part of the page in that language) and on the provider.usereturns the current direction, for a component of your own that needs it.Direction()
Components that format numbers and dates take the language from you as well; see active-format.
API#
DirectionProvider
Functions, hooks and constants
- Type
() => TextDirection