Fragiola

Guides

Right-to-left

What changes in right to left, when the document's dir is enough, and when to add a DirectionProvider so the components know the direction first.

Every component that depends on the reading direction follows it: the arrow keys of Tabs, Slider, RadioGroup, ToggleGroup, Toolbar, Menu, Combobox's chips and the calendars swap, a horizontal Slider runs right to left, a Progress Indicator grows from the right, and every positioned popup resolves its logical sides (inline-start, inline-end) on the right side. What stays yours: setting the direction of your page, and the layout of your own CSS (use logical properties, margin-inline-start rather than margin-left, and it follows).

dir is usually enough

Without a provider, a component reads the computed direction of its element when it needs it: on a key press, on a placement. Set dir on the document, or on the element around a part of the page, and the components follow it.

<html dir="rtl" lang="ar">

This also works for CSS that sets direction, and for a part of the page whose dir changes: the direction is read again at the next key press or placement.

When to add a DirectionProvider

The DirectionProvider tells the components the direction without reading the DOM. Add one when your app knows the direction before the DOM does, or where the computed style is not the reading direction:

  • a page rendered on a server: the components' direction is known in the first render, not read after mount;
  • a subtree whose direction your app switches (a preview of an Arabic version), in the same render as the change;
  • components rendered where the computed direction differs from the direction you mean.

The provider sets no dir and no style. Keep dir too: it is what makes the layout flow right to left, and the Slider maps the pointer onto that layout.

import { DirectionProvider } from "@fragiola/primitives-react/direction";

<div dir={direction}>
    <DirectionProvider direction={direction}>
        <App />
    </DirectionProvider>
</div>;

A nested provider wins inside it. A component without a provider around it falls back to the computed direction, so the two can be mixed.

Reading the direction in your own code

useDirection() (React, Vue: a computed ref) and injectDirection() (Angular: a signal) read the nearest provider, or "ltr" without one: they never read the DOM. For an element of your own, the toolkit's getDirection(element, provided) gives the provider's direction when you pass it, else the element's computed one; the positioning entry's direction option does the same for a floating element (the toolkit).

What changes, component by component

componentin right to left
TabsArrowLeft moves to the next tab, ArrowRight to the previous (horizontal); data-activation-direction stays physical, so moving to the next tab gives left
AccordionArrowLeft moves to the next trigger, ArrowRight to the previous (horizontal)
Slidera horizontal slider has min at the right; the arrows swap
RadioGroupArrowLeft moves to the next radio (and checks it), ArrowRight to the previous; ArrowDown and ArrowUp do not change
ToggleGroup, Toolbarhorizontal: ArrowLeft moves to the next item, ArrowRight to the previous (a Toolbar.Input's "next" arrow is ArrowLeft); Home, End and a vertical one do not change
Progress, Meterthe Indicator grows from the inline start: from the right
Listboxhorizontal: ArrowLeft moves to the next option, ArrowRight to the previous; a vertical one does not change
OTPFieldthe first slot is the rightmost; ArrowLeft moves to the next slot, ArrowRight to the previous
DateField, TimeField, the range fields and the pickers' typed inputthe segments keep the order the locale's own text has (a he-IL date reads left to right, an ar-EG one right to left); ArrowLeft and ArrowRight move to the segment on that side of the screen
NumberFieldnothing: ArrowUp and ArrowDown are not mirrored, and the scrub's right is the screen's right
MenuArrowLeft opens a submenu, ArrowRight closes it; submenus open to the left
Menubarhorizontal: ArrowLeft moves to the next item (and, in a menu, to the next menu), ArrowRight to the previous; a vertical bar's menus open to the left
Comboboxthe arrows between chips swap
Calendar and the pickersArrowRight moves to the previous day, ArrowLeft to the next; weeks still start on the locale's first day, at the inline start
every positioned popupinline-start and inline-end resolve to the right and the left; left and right stay physical

The Tabs Indicator is measured in the List's own box, so it follows the layout whatever the provider says: its logical inline start is --active-tab-right in right to left.