ZUI

Search documentation

Find a page or a heading.

Direction

Sets the direction text reads in, left to right or right to left, for the parts inside it.

Installation

bash

The CLI writes components/ui/direction.tsx. It needs no package.

Usage

tsx
tsx

Put the app's PortalHost inside the provider: overlays, such as a menu or a toast, draw there, and read the direction from the provider around the host.

tsx

API reference

DirectionProvider

It tells the parts inside it which way text reads. On the web it also sets dir on a box around its children that takes no room of its own (display: contents), so the page lays them out in that direction: rows start from the right, and ms-* and me-* margins swap sides.

useDirection

Returns 'ltr' or 'rtl': the nearest DirectionProvider's direction, or the system's outside one.

useDirectionSides

For a part that places or moves something itself. Returns:

  • rtl: whether it runs right to left, the provider's direction.
  • layout: the direction the platform lays out rows and start and end styles in around it. On the web that is the provider's, which sets dir; on a device it is the system's, whatever a provider says.
  • start and end: the style keys that put a child at the side it runs from and the side it runs to, 'start' and 'end' where layout runs the same way as rtl, swapped where it does not.
  • sidesSwapped: whether left and right styles are drawn swapped, true on a device the system lays out right to left unless the app called I18nManager.swapLeftAndRightInRTL(false), and false on the web.

useMirrored

Returns a style that mirrors an arrow drawn for text read left to right when the direction is 'rtl', and undefined when it is 'ltr'.

The items that read it

Breadcrumb, Pagination, Calendar, Carousel and the sub-menu triggers of Dropdown menu, Context menu and Menubar mirror their chevrons in text read right to left. Carousel, Slider and a horizontal Resizable group run from the right, Calendar and Slider swap their left and right arrow keys, as upstream's do, and Sheet and Drawer move toward the side a device draws them on. Carousel, Slider and Resizable place their own parts through useDirectionSides, so they run the provider's way on a device even where the system lays the app out the other way. The rows the platform lays out, such as a breadcrumb's or a calendar's, follow the system's direction there. Each item's page says how.

Right to left on a device

A device lays out the whole app in one direction, which I18nManager holds for the app's life: rows start from the right and start and end swap sides when the system language reads right to left, or after I18nManager.forceRTL(true) and a restart. A provider cannot change that for part of a screen, so on a device it draws nothing around its children and only tells them the direction it was given. A provider that disagrees with the system turns the items' arrows, keys and their own tracks its way while the rows around them run the system's way. Set it from the same place as the app's layout direction, such as expo-localization's getLocales()[0].textDirection, so that both agree.

Differences from upstream

  • direction defaults to the system's direction, where upstream's defaults to 'ltr': on a device the system sets the layout direction, and a provider that disagreed with it would make the parts reading it disagree with the layout.
  • Upstream's provider re-exports @base-ui/react's, which sets no dir itself and leaves it to the page's <html dir>. A React Native app on the web has no <html> of its own to set, so the provider here sets dir on a box around its children.
  • On a device the provider changes no layout, as above.
  • useDirectionSides and useMirrored are added. A device lays out in the system's direction whatever a provider says, and draws a sheet or a drawer named left on the right when that direction is right to left. Neither happens on the web, so upstream needs neither.