السهم الأيسر ينتقل إلى التبويب التالي.

Focus a tab and press the arrow keys: Left and Right swap with the direction.

Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/direction-provider.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { DirectionProvider } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What the `view` closure of the kit's `DirectionProvider` receives: `{ direction }`. */
    export type DirectionProviderParts = Component.Headless.Parts<typeof DirectionProvider>
    
    /**
     * The element a direction applies to. The kit's provider tells the
     * components inside which way the arrow keys go, but writes no attribute:
     * this region sets `dir`, so the browser lays the text out the same way.
     *
     * `DirectionProvider(h, { props: { direction: "rtl" }, view: (directionProvider) => DirectionRegion(h, directionProvider).children(...) })`
     */
    export const DirectionRegion = <Msg>(
      h: Html.H<Msg>,
      directionProvider: DirectionProviderParts,
      options: { readonly class?: string | undefined } = {}
    ) => h.div.class(cn("text-start", options.class)).attr("dir", directionProvider.direction)
    
  3. Import it

    import { DirectionRegion } from "#ui/direction-provider"
    

Usage

Switching direction.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DirectionProvider, DirectionProviderDirection, Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { DirectionRegion } from "#ui/direction-provider"

const DirectionProviderTabsMessage = UiSchema.TaggedEnum({
  PressedDirection: { direction: DirectionProviderDirection },
  SelectedTab: { value: Schema.String }
})

const labels = {
  ltr: { account: "Account", password: "Password", billing: "Billing" },
  rtl: { account: "الحساب", password: "كلمة المرور", billing: "الفوترة" }
} as const

const sections = ["account", "password", "billing"] as const

const tabsList = "flex gap-1 rounded-md bg-muted p-1"
const tab =
  "flex-1 rounded-sm px-3 py-1.5 text-[13px] font-medium text-muted-foreground transition-colors duration-150 " +
  "hover:text-foreground data-[active]:bg-background data-[active]:text-foreground data-[active]:shadow-sm"
const panel = "px-1 pt-4 text-[13px] text-muted-foreground"

export const DirectionProviderTabs = Html.component({
  name: "DirectionProviderTabs",
  model: Schema.Struct({ direction: DirectionProviderDirection, tab: Schema.String }),
  message: DirectionProviderTabsMessage
}).make({
  init: () => ({ direction: "rtl", tab: "account" }),
  update: {
    PressedDirection: ($, { direction }) => $.evolve({ direction }),
    SelectedTab: ($, { value }) => $.evolve({ tab: value })
  },
  view: (h, model) =>
    h.div.class("flex w-full max-w-sm flex-col gap-4").children(
      h.div.class("flex items-center gap-1").attrs({ role: "group", "aria-label": "Direction" }).children(
        DirectionProviderDirection.literals.map((direction) =>
          Button(h, {
            variant: direction === model.direction ? "secondary" : "ghost",
            size: "sm",
            onPress: DirectionProviderTabsMessage.PressedDirection({ direction })
          })
            .attr("aria-pressed", String(direction === model.direction))
            .children(direction === "ltr" ? "Left to right" : "Right to left")
        )
      ),
      // The provider writes no attribute. The region sets `dir` for the
      // layout, and the provider tells the tabs which way the arrow keys go.
      DirectionProvider(h, {
        props: { direction: model.direction },
        view: (directionProvider) =>
          DirectionRegion(h, directionProvider, { class: "rounded-lg border p-3" }).children(
            Tabs(h, {
              props: { value: model.tab, tabs: sections.map((value) => ({ value })) },
              onEmit: { Selected: ({ value }) => DirectionProviderTabsMessage.SelectedTab({ value }) },
              view: (tabs) =>
                tabs.root(h.div).children(
                  tabs.list(h.div.class(tabsList)).children(
                    sections.map((value) =>
                      tabs
                        .tab({ element: h.button.class(tab), value })
                        .children(labels[directionProvider.direction][value])
                    )
                  ),
                  sections.map((value) =>
                    tabs
                      .panel({ element: h.div.class(panel), value })
                      ?.children(
                        directionProvider.direction === "rtl"
                          ? "السهم الأيسر ينتقل إلى التبويب التالي."
                          : "Arrow Right moves to the next tab."
                      )
                  )
                )
            })
          )
      }),
      h.p.class("text-xs text-muted-foreground").children("Focus a tab and press the arrow keys: Left and Right swap with the direction.")
    )
})

Examples

Side by side

The same toggle group under two providers. Focus an item in each and press Arrow Right.

Left to right

Arrow Right moves to the next item

من اليمين إلى اليسار

Arrow Right moves to the previous item

Slider

A right-to-left slider has its minimum on the right, and Arrow Left adds a step.

مستوى الصوت30

Nested providers

A component follows the nearest provider: a left-to-right code block inside a right-to-left page.

الشيفرة تُكتب من اليسار إلى اليمين:

pnpm add @effect/ui-kit

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
direction"ltr" or "rtl": the provider's direction, for markup that depends on it, such as a dir attribute.

API reference

Props

PropTypeDescription
direction"ltr" | "rtl"The reading direction the components inside follow. "ltr" by default.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.