组件

Direction

Tells every Radix part, every drawing and every logical property which way the page reads. One provider at the root of a right-to-left page (or on an island inside a left-to-right one) turns arrow keys, sub-menus, slider edges, tab shoulders and hand-drawn strokes round together.

安装

terminal
npx shadcn@latest add @wobko/direction

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Reading direction

One provider, one prop. Flip it and the Radix keys, the slider's start edge, the tab shoulders and the words all turn round together. Click a tab and press the arrow keys before and after.

Arabic: notebook

Tabs, a menu with a sub-menu, a slider and a table, in Arabic. Tab and arrow keys run right to left, the sub-menu opens with ArrowLeft, the slider starts at its right edge, the table columns read from the right.

Hebrew: pages

A breadcrumb, a carousel and a pager, in Hebrew. The separators and arrows are the same drawings as in English: direction glyphs flip themselves, so the first slide sits at the right and ArrowLeft moves on to the next.

Arabic: sidebar

The pad hangs on the right, where a right-to-left page keeps its margin: side="right" is a physical side, so you choose it. The clip, the highlighter, the bracket, the panel icon and the sheet on a phone all follow.

useDirection and islands

Your own widgets can read the direction too. This pager decides which arrow key goes forward from useDirection(), and its arrow drawings flip themselves. The English block is a left-to-right island inside the Hebrew page: it has its own provider, and its popover opens left to right.

API 参考

DirectionProvider

Sets the direction for Radix, the ink engine and the DOM. It renders a layout-neutral wrapper (`display: contents`) that carries `dir`; portalled content (popovers, dialogs, sheets, tooltips, menus) leaves the wrapper, so it is handed the direction through the ink scope and carries `dir` itself.

属性类型默认值说明
direction"ltr" | "rtl"-The reading direction. Leave it out to follow the page: the nearest `dir` above the provider, else `<html dir>`, read after mount and whenever it changes (the first render is `ltr` on the server and in hydration).
dir"ltr" | "rtl"-Upstream shadcn's prop name for `direction`. `direction` wins when both are given.
asChildbooleanfalsePut `dir` on your own single child (a `main`, a `section`) instead of the wrapper element.
classNamestring"contents"The wrapper is `contents`; give it a display utility (`grid`, `flex`) to make it a real box.

useDirection

`useDirection(localDir?)` returns the direction of the nearest provider, `"ltr"` without one. It is Radix's own hook, so it stays in step with the keys and thumbs of every Radix part. A `localDir` wins, the way a `dir` prop does on a Radix root.

属性类型默认值说明
localDir"ltr" | "rtl"-A direction that overrides the context, typically a prop of your own component.

无障碍

  • Radix reads its direction from this provider only, never from the `dir` attribute. Without it, Tabs, Menubar, Radio Group, Toggle Group, Navigation Menu and the menus keep their left-to-right arrow keys on a right-to-left page, a Scroll Area keeps its scrollbar on the wrong side and a Slider starts at the wrong edge. With it, ArrowLeft moves to the next tab, opens a sub-menu towards the left, and so on.
  • The wrapper carries `dir`, so the browser lays out bidirectional text, punctuation and inline order correctly; `lang` is yours to set on the wrapper or the page (`<DirectionProvider direction="rtl" lang="ar">`) so screen readers pick the right voice.
  • Direction glyphs (chevrons, arrows, panel, log-in and log-out, undo and redo, send, list and menu icons) flip themselves in a right-to-left context; icons that do not depend on reading order (check, plus, clock, media controls) stay as drawn.
  • The drawn layer is aria-hidden and decorative; mirroring it never changes what a screen reader announces.
  • Two engines behind Wobko parts take the direction as an option of their own and do not read the provider yet: give a Carousel `opts={{ direction: "rtl" }}` (Embla) and a Calendar `dir="rtl"` (react-day-picker); a Sonner Toaster follows `<html dir>` or takes its own `dir`. The Direction examples do it.
  • Kevin Sans covers Latin only. Arabic and Hebrew text falls back to the system font for each missing glyph, which is correct and readable but not hand-lettered; ship an Arabic or Hebrew face in `--font-hand` for a matching look.

源码

CLI 会复制进你项目的文件。

正在加载源码