# RTL

Starwind components support RTL through the native HTML `dir` attribute.

<FrameworkCodeSwitcher>
  <div slot="astro">
```astro
---
import { Input } from "@/components/starwind/input";
import { Switch } from "@/components/starwind/switch";
---

<fieldset dir="rtl">
  <legend>Delivery settings</legend>
  <Switch id="updates" label="Email updates" defaultChecked />
  <label>Tracking link <Input type="url" dir="ltr" /></label>
</fieldset>
```
  </div>
  <div slot="react">
```tsx
import { Input } from "@/components/starwind/input";
import { Switch } from "@/components/starwind/switch";

export function Example() {
  return (
    <fieldset dir="rtl">
      <legend>Delivery settings</legend>
      <Switch id="updates" label="Email updates" defaultChecked />
      <label>Tracking link <Input type="url" dir="ltr" /></label>
    </fieldset>
  );
}
```
  </div>
  <div slot="vue">
```vue
<script setup lang="ts">
import { Input } from "@/components/starwind/input";
import { Switch } from "@/components/starwind/switch";
</script>

<template>
  <fieldset dir="rtl">
    <legend>Delivery settings</legend>
    <Switch id="updates" label="Email updates" default-checked />
    <label>Tracking link <Input type="url" dir="ltr" /></label>
  </fieldset>
</template>
```
  </div>
  <div slot="svelte">
```svelte
<script lang="ts">
  import { Input } from "@/components/starwind/input";
  import { Switch } from "@/components/starwind/switch";
</script>

<fieldset dir="rtl">
  <legend>Delivery settings</legend>
  <Switch id="updates" label="Email updates" defaultChecked />
  <label>Tracking link <Input type="url" dir="ltr" /></label>
</fieldset>
```
  </div>
</FrameworkCodeSwitcher>

## Set the direction

Add `dir="rtl"` to `<html>` for a whole page, or to any container for one section. Children inherit it. Add `dir="ltr"` to keep an English passage or a URL input left-to-right.

### Change the direction after the page loads

If your app changes `dir` after the page loads, for example with a language switch, send the `starwind:direction-change` event after the change:

```js
document.documentElement.dir = "rtl";
document.dispatchEvent(new Event("starwind:direction-change"));
```

Starwind then updates the components on the page, including open popups. Without the event, each component uses the new direction at its next interaction.

## Set up your stylesheet

`starwind init` adds these lines to new Tailwind stylesheets:

```css
@custom-variant rtl (&:where([dir="rtl"], [dir="rtl"] *:not([dir="rtl"] [dir="ltr"], [dir="rtl"] [dir="ltr"] *)));
@custom-variant ltr (&:where(:not([dir="rtl"], [dir="rtl"] *:not([dir="rtl"] [dir="ltr"], [dir="rtl"] [dir="ltr"] *))));
```

They replace the Tailwind `rtl` and `ltr` variants. They also change your own `rtl:` and `ltr:` classes. Review any existing `rtl` or `ltr` definitions before you replace them.

The lines need no change to your Vite config. A production build gives the same result as dev.

### Add the lines to an existing stylesheet

If init kept your existing stylesheet, add the lines when your app puts LTR content inside an RTL page. Then rebuild. An app that uses one direction for the whole page works without them. An app with RTL sections inside an LTR page also works without them, because stock Tailwind `rtl:` covers it.

Your own `ltr:` classes can need the lines even when Starwind components do not. With stock Tailwind, `ltr:` also matches inside an RTL section when the page has `dir="ltr"`. In a Vite 8 production build, it also matches on a `lang="en" dir="rtl"` page. Add the lines if your app uses `ltr:` in one of these cases.

### What the variants support

For an RTL site, set `dir="rtl"` on `<html>` or `<body>`. A page with no `dir` stays LTR.

One level of mixed direction works. You can put an LTR area inside an RTL page, or an RTL area inside an LTR page. A popup that opens from a section with its own `dir` keeps that direction.

### Limits

The variants read the `dir` attribute. They do not read the CSS `direction` property. For an element with `dir="auto"`, they use the nearest `dir="rtl"` or `dir="ltr"` attribute above it. When the text direction of a control is different from the page, set `dir="rtl"` or `dir="ltr"` on the control or on its container. The RTL styles in Color Picker and Prose follow the same rules.

A third level of mixed direction is not supported. Content in an RTL area inside an LTR area inside an RTL page gets LTR styles.

## Update an existing project

Update the Runtime and the framework package you use. Update copied components with `starwind update`, and vendored Primitive files with `starwind primitives update --all`. Add the two stylesheet lines if your app needs them. See [Add the lines to an existing stylesheet](#add-the-lines-to-an-existing-stylesheet).

While updating, change custom padding meant for the reading edge to logical utilities, such as `pl-6` to `ps-6`. Remove old workarounds that the built-in RTL behavior now replaces. Keep code that must stay left-to-right, such as code samples and video timelines.