suiss/ui
  • Docs
  • Components
  • Patterns
  • Interactions
  • Charts
  • Blocks
  • AI
GitHubCreate
DocsComponentsPatternsInteractionsChartsBlocksAICreate
Components84
  • Overview
  • Accordion
  • Alert
  • Alert Dialog
  • Alert Sheet
  • Amount Input
  • Aspect Ratio
  • Attachment
  • Avatar
  • Badge
  • Bottom Bar
  • Breadcrumb
  • Bubble
  • Button
  • Button Group
  • Calendar
  • Card
  • Carousel
  • Chart
  • Checkbox
  • Chip
  • Code Block
  • Collapsible
  • Combobox
  • Command
  • Context Menu
  • Dialog
  • Direction
  • Drawer
  • Dropdown Menu
  • Empty
  • FAB
  • Field
  • Fullscreen Menu
  • Haptics
  • Header
  • Hover Card
  • Icon
  • Input
  • Input Accessory Bar
  • Input Group
  • Input OTP
  • Item
  • Kbd
  • Keyboard Scroll View
  • Keypad
  • Label
  • Marker
  • Menubar
  • Message
  • Message Scroller
  • Native Select
  • Navigation Menu
  • Page Indicator
  • Pagination
  • Pin Pad
  • Popover
  • Progress
  • QR Scanner
  • Questionnaire
  • Radio Group
  • Resizable
  • Screen
  • Scroll Area
  • Select
  • Separator
  • Sheet
  • Sidebar
  • Signature Pad
  • Skeleton
  • Slider
  • Spinner
  • Switch
  • Tab Bar
  • Table
  • Tabs
  • Text
  • Textarea
  • Toast
  • Toggle
  • Toggle Group
  • Tooltip
  • Trace
  • Wheel Picker
  • Widget

suiss/ui · Component

Direction

A provider that sets the reading direction, left-to-right or right-to-left, for the components inside it.

Direction is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.

import { ArrowRightIcon } from "@phosphor-icons/react"
import { Button } from "@/components/ui/button"
import {
  Card,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from "@/components/ui/card"
import { DirectionProvider, useDirection } from "@/components/ui/direction"

function DirectionPreview() {
  const direction = useDirection()

  return (

The direction module provides DirectionProvider and useDirection from the chosen base. DirectionProvider takes a direction of ltr or rtl and makes it available to the primitives below it, so behaviors that depend on direction, such as arrow-key navigation and popup placement, follow the reading direction. useDirection returns the current direction for your own components.

The provider does not set the dir attribute on the page. Set dir on the html element or on a container yourself so that the browser and logical CSS properties flip the layout.

Installation

$ pnpm dlx shadcn@latest add @esuiss/direction

Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:

$ pnpm dlx @esuiss/ui@latest add direction
  1. Install the following dependencies:
$ pnpm add @base-ui/react
  1. Copy and paste the following code into your project.
components/ui/direction.tsx
"use client"

export {
  DirectionProvider,
  useDirection,
} from "@base-ui/react/direction-provider"
  1. Update the import paths to match your project setup.

Usage

import { DirectionProvider, useDirection } from "@/components/ui/direction"
<div dir="rtl">
  <DirectionProvider direction="rtl">
    <Card />
  </DirectionProvider>
</div>
function Card() {
  const direction = useDirection()

  return <section dir={direction}>The direction is {direction}.</section>
}

Examples

Right to left

Set direction to rtl on the provider. The card reads it with useDirection and passes it to its dir attribute, so its footer buttons appear in reversed order.

import { ArrowRightIcon } from "@phosphor-icons/react"
import { Button } from "@/components/ui/button"
import {
  Card,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from "@/components/ui/card"
import { DirectionProvider, useDirection } from "@/components/ui/direction"

function DirectionPreview() {
  const direction = useDirection()

  return (

API Reference

DirectionProvider

Makes the direction available to the components below it.

PropTypeDefault
direction"ltr" | "rtl""ltr"
PropTypeDefault
direction"ltr" | "rtl""ltr"
dir"ltr" | "rtl"-

dir is the Radix name for the same prop. direction wins when both are set.

PropTypeDefault
direction"ltr" | "rtl""ltr"
localestring-

locale is passed to the I18nProvider of React Aria. When it is unset, the current locale is used with an Arabic script for rtl and a Latin script for ltr, so React Aria components follow direction.

Wrap your app, or a subtree, in DirectionProvider with the same direction you set in the dir attribute. Many components in the library use logical properties such as ps-, pe-, ms- and me-, which flip automatically under dir="rtl".

useDirection

Returns the current direction, "ltr" or "rtl", from the nearest DirectionProvider.

Read the value with useDirection when a component needs to adapt, for example to pass it to a dir attribute or to rotate a directional icon. For icons you can also use the rtl: Tailwind variant, for example rtl:rotate-180, so arrows point the right way.

NextDrawerPreviousDialog

On this page

  1. 01Installation
  2. 02Usage
  3. 03Examples
    • Right to left
  4. 04API Reference
    • DirectionProvider
    • useDirection