DirectionProvider
Tells the components inside it which way the text runs, so the keys that follow the reading direction go the right way.
Focus a tab and press the arrow keys: Left and Right swap with the direction.
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.")
)
})
Installation
Set up the project
Once per project: the packages, Tailwind CSS, the theme tokens and the
cnhelper. Follow Installation.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)Import it
import { DirectionRegion } from "#ui/direction-provider"
Usage
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.
Arrow Right moves to the next item
Arrow Right moves to the previous item
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DirectionProvider, DirectionProviderDirection, ToggleGroup } from "@effect/ui-kit"
import { Schema } from "effect"
import { DirectionRegion } from "#ui/direction-provider"
const DirectionProviderToggleGroupMessage = UiSchema.TaggedEnum({
ToggledAlignment: { direction: DirectionProviderDirection, value: Schema.Array(Schema.String) }
})
const alignments = ["start", "center", "end", "justify"]
const directions: ReadonlyArray<DirectionProviderDirection> = ["ltr", "rtl"]
export const DirectionProviderToggleGroup = Html.component({
name: "DirectionProviderToggleGroup",
model: Schema.Struct({ ltr: Schema.Array(Schema.String), rtl: Schema.Array(Schema.String) }),
message: DirectionProviderToggleGroupMessage
}).make({
init: () => ({ ltr: ["start"], rtl: ["start"] }),
update: {
ToggledAlignment: ($, { direction, value }) => $.evolve({ [direction]: value })
},
view: (h, model) =>
h.div.class("grid grid-cols-2 gap-8").children(
directions.map((direction) =>
// Two providers in one view, so each has a key, and so does the group inside.
DirectionProvider(h, {
key: direction,
props: { direction },
view: (directionProvider) =>
DirectionRegion(h, directionProvider, { class: "flex flex-col items-start gap-3" }).children(
h.span.class("text-[13px] font-medium").children(direction === "ltr" ? "Left to right" : "من اليمين إلى اليسار"),
ToggleGroup(h, {
key: direction,
props: { value: model[direction], items: alignments.map((value) => ({ value })) },
onEmit: ({ value }) => DirectionProviderToggleGroupMessage.ToggledAlignment({ direction, value }),
view: (toggleGroup) =>
toggleGroup
.root(h.div.class("inline-flex gap-0.5 rounded-md border p-0.5").attr("aria-label", "Text alignment"))
.children(
alignments.map((value) =>
toggleGroup
.item({
element: h.button
.class(
"rounded-sm px-2.5 py-1 text-xs font-medium text-muted-foreground transition-colors " +
"duration-150 hover:bg-accent data-[pressed]:bg-brand data-[pressed]:text-brand-foreground"
),
value
})
.children(value)
)
)
}),
h.p.class("text-xs text-muted-foreground").children(`Arrow Right moves to the ${direction === "ltr" ? "next" : "previous"} item`)
)
})
)
)
})
Slider
A right-to-left slider has its minimum on the right, and Arrow Left adds a step.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DirectionProvider, DirectionProviderDirection, Slider } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { DirectionRegion } from "#ui/direction-provider"
const DirectionProviderSliderMessage = UiSchema.TaggedEnum({
PressedSwitchDirection: {},
MovedVolume: { value: Schema.Number }
})
export const DirectionProviderSlider = Html.component({
name: "DirectionProviderSlider",
model: Schema.Struct({ direction: DirectionProviderDirection, volume: Schema.Number }),
message: DirectionProviderSliderMessage
}).make({
init: () => ({ direction: "rtl", volume: 30 }),
update: {
PressedSwitchDirection: ($) => $.evolve({ direction: (direction) => (direction === "ltr" ? "rtl" : "ltr") }),
MovedVolume: ($, { value }) => $.evolve({ volume: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-xs flex-col items-start gap-4").children(
DirectionProvider(h, {
props: { direction: model.direction },
view: (directionProvider) =>
DirectionRegion(h, directionProvider, { class: "grid w-full gap-3" }).children(
h.div.class("flex items-center justify-between").children(
h.span.class("text-[13px] font-medium").children(directionProvider.direction === "rtl" ? "مستوى الصوت" : "Volume"),
h.span.class("text-xs text-muted-foreground tabular-nums").children(String(model.volume))
),
// In a right-to-left slider `min` is on the right, and Arrow Left adds a step.
Slider(h, {
props: { value: model.volume },
onEmit: {
Moved: ({ value }) =>
typeof value === "number" ? DirectionProviderSliderMessage.MovedVolume({ value }) : undefined,
Committed: () => undefined
},
view: (slider) =>
slider.root(h.div).children(
slider.control(h.div.class("flex h-5 touch-none items-center")).children(
slider.track(h.div.class("relative h-1.5 w-full rounded-full bg-muted")).children(
slider.indicator(h.div.class(
"absolute inset-y-0 left-[var(--slider-indicator-start)] w-[var(--slider-indicator-size)] rounded-full bg-brand"
)),
slider.thumb({
element: h.div.class(
"absolute top-1/2 left-[var(--slider-thumb-position)] size-4 -translate-1/2 rounded-full border " +
"border-brand bg-background shadow-sm has-focus-visible:ring-2 has-focus-visible:ring-ring"
),
input: h.input.attr("aria-label", "Volume")
})
)
)
)
})
)
}),
Button(h, { variant: "outline", size: "sm", onPress: DirectionProviderSliderMessage.PressedSwitchDirection() })
.children(model.direction === "rtl" ? "Switch to left to right" : "Switch to right to left")
)
})
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
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DirectionProvider, Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { DirectionRegion } from "#ui/direction-provider"
const DirectionProviderNestedMessage = UiSchema.TaggedEnum({
SelectedSection: { value: Schema.String },
SelectedLanguage: { value: Schema.String }
})
const sections = [
{ value: "overview", label: "نظرة عامة" },
{ value: "install", label: "التثبيت" },
{ value: "usage", label: "الاستخدام" }
]
const languages = [
{ value: "pnpm", code: "pnpm add @effect/ui-kit" },
{ value: "npm", code: "npm install @effect/ui-kit" },
{ value: "bun", code: "bun add @effect/ui-kit" }
]
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 DirectionProviderNested = Html.component({
name: "DirectionProviderNested",
model: Schema.Struct({ section: Schema.String, language: Schema.String }),
message: DirectionProviderNestedMessage
}).make({
init: () => ({ section: "install", language: "pnpm" }),
update: {
SelectedSection: ($, { value }) => $.evolve({ section: value }),
SelectedLanguage: ($, { value }) => $.evolve({ language: value })
},
view: (h, model) =>
DirectionProvider(h, {
key: "page",
props: { direction: "rtl" },
view: (pageDirection) =>
DirectionRegion(h, pageDirection, { class: "w-full max-w-sm rounded-lg border p-3" }).children(
Tabs(h, {
key: "sections",
props: { value: model.section, tabs: sections.map(({ value }) => ({ value })) },
onEmit: { Selected: ({ value }) => DirectionProviderNestedMessage.SelectedSection({ value }) },
view: (tabs) =>
tabs.root(h.div).children(
tabs.list(h.div.class(tabsList)).children(
sections.map(({ label, value }) =>
tabs.tab({ element: h.button.class(tab), value }).children(label)
)
),
tabs
.panel({ element: h.div.class(panel), value: "overview" })
?.children("مكتبة مكوّنات بلا تنسيق."),
tabs.panel({ element: h.div.class(panel), value: "install" })?.children(
h.p.children("الشيفرة تُكتب من اليسار إلى اليمين:"),
// A component reads the nearest provider, so this region is
// left-to-right inside the right-to-left page.
DirectionProvider(h, {
key: "code",
props: { direction: "ltr" },
view: (codeDirection) =>
DirectionRegion(h, codeDirection, { class: "mt-3 rounded-md border bg-background p-2" }).children(
Tabs(h, {
key: "languages",
props: { value: model.language, tabs: languages.map(({ value }) => ({ value })) },
onEmit: {
Selected: ({ value }) => DirectionProviderNestedMessage.SelectedLanguage({ value })
},
view: (codeTabs) =>
codeTabs.root(h.div).children(
codeTabs.list(h.div.class(tabsList)).children(
languages.map(({ value }) =>
codeTabs
.tab({ element: h.button.class(tab), value })
.children(value)
)
),
languages.map(({ code, value }) =>
codeTabs
.panel({ element: h.pre.class("px-1 pt-3 pb-1 font-mono text-xs text-foreground"), value })
?.children(code)
)
)
})
)
})
),
tabs
.panel({ element: h.div.class(panel), value: "usage" })
?.children("استورد المكوّن وارسم أجزاءه.")
)
})
)
})
})
Anatomy
The parts the view closure receives. Apply each one to an element of your own.
| Part | Description |
|---|---|
direction | "ltr" or "rtl": the provider's direction, for markup that depends on it, such as a dir attribute. |
API reference
Props
| Prop | Type | Description |
|---|---|---|
direction | "ltr" | "rtl" | The reading direction the components inside follow. "ltr" by default. |
Related components
The components this page's file and examples import.
- Button: A native button that can stay focusable while disabled.