# Separator

A horizontal or vertical divider between sections of content.

- Page: https://www.effectcn.com/components/separator
- Kind: View function
- File: `src/ui/separator.ts`
- Built on: `Separator` from `@effect/ui-kit`

## Installation

1. Set up the project once: the packages, Tailwind CSS, the theme tokens and the `cn` helper. See [Installation](https://www.effectcn.com/docs/installation.md).
2. Copy the file below to `src/ui/separator.ts`. The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.
3. Import it.

`src/ui/separator.ts`

```ts
import { Separator as KitSeparator, type SeparatorElement, type SeparatorProps } from "@effect/ui-kit"
import { cn } from "#lib/cn"

const base = "shrink-0 data-[orientation=horizontal]:w-full data-[orientation=vertical]:self-stretch"

const rule = "data-[orientation=horizontal]:h-px data-[orientation=vertical]:min-h-[1em] data-[orientation=vertical]:w-px"

const variants = {
  default: cn(rule, "bg-border"),
  strong: cn(rule, "bg-foreground"),
  brand: cn(rule, "bg-brand"),
  dashed: "border-dashed border-input data-[orientation=horizontal]:border-t data-[orientation=vertical]:border-l",
  label: "flex items-center gap-3 text-xs leading-none whitespace-nowrap text-muted-foreground " +
    "before:h-px before:flex-1 before:bg-border after:h-px after:flex-1 after:bg-border"
} as const

export type SeparatorVariant = keyof typeof variants

/** The separator's classes, for another element that should look like one. */
export const separatorVariants = (
  options: { readonly variant?: SeparatorVariant | undefined; readonly class?: string | undefined } = {}
): string => cn(base, variants[options.variant ?? "default"], options.class)

/**
 * The kit's Separator with a look. A vertical one takes the height of the row
 * it sits in. With `variant: "label"` its children sit between two rules:
 * `Separator(h, { variant: "label" }).children("or")`.
 */
export const Separator = <Msg, R = never>(
  h: { readonly div: SeparatorElement<Msg> },
  options: {
    readonly variant?: SeparatorVariant | undefined
    readonly class?: string | undefined
    readonly element?: SeparatorElement<Msg, R> | undefined
    readonly props?: SeparatorProps | undefined
  } = {}
): SeparatorElement<Msg, R> =>
  KitSeparator<Msg, R>(h, { element: options.element, props: options.props }).class(separatorVariants(options))
```

```ts
import { separatorVariants, Separator } from "#ui/separator"
```

## Usage

A rule under a heading, and vertical rules between the links of a row.

```ts
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Separator } from "#ui/separator"

export const SeparatorBasic = Html.component({
  name: "SeparatorBasic",
  model: Schema.Struct({}),
  message: Schema.Never
}).make({
  init: () => ({}),
  update: ($) => $.model,
  view: (h) =>
    h.div.class("flex w-full max-w-xs flex-col gap-4").children(
      h.div.children(
        h.h4.class("m-0 text-sm font-medium").children("Effect UI Kit"),
        h.p.class("m-0 text-[13px] text-muted-foreground").children("Headless components for Effect UI.")
      ),
      Separator(h),
      h.div.class("flex items-center gap-4 text-[13px] [&_a]:no-underline [&_a]:underline-offset-4 [&_a]:hover:underline")
        .children(
          h.a.attr("href", "#").children("Blog"),
          Separator(h, { props: { orientation: "vertical" } }),
          h.a.attr("href", "#").children("Docs"),
          Separator(h, { props: { orientation: "vertical" } }),
          h.a.attr("href", "#").children("Source")
        )
    )
})
```

## Examples

### In a toolbar

Vertical separators between groups of buttons take the row's height.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { Separator } from "#ui/separator"

const SeparatorVerticalMessage = UiSchema.TaggedEnum({ PressedTool: { name: Schema.String } })

const groups = [["Undo", "Redo"], ["Bold", "Italic", "Underline"], ["Link"]]

export const SeparatorVertical = Html.component({
  name: "SeparatorVertical",
  model: Schema.Struct({ last: Schema.NullOr(Schema.String) }),
  message: SeparatorVerticalMessage
}).make({
  init: () => ({ last: null }),
  update: {
    PressedTool: ($, { name }) => $.evolve({ last: name })
  },
  view: (h, model) =>
    h.div.class("flex flex-col items-center gap-4").children(
      h.div.class("flex items-center gap-1 rounded-md border p-1").attrs({ role: "toolbar", "aria-label": "Formatting" })
        .children(
          groups.flatMap((group, index) => [
            index > 0 ? Separator(h, { props: { orientation: "vertical" }, class: "mx-1 my-1.5" }) : undefined,
            ...group.map((name) =>
              Button(h, { variant: "ghost", size: "sm", onPress: SeparatorVerticalMessage.PressedTool({ name }) })
                .children(name)
            )
          ])
        ),
      h.p.class("m-0 text-xs text-muted-foreground").children(
        model.last === null ? "Pick a tool" : `Last used: ${model.last}`
      )
    )
})
```

### With a label

With the label variant, the separator's own children sit between two rules.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { Separator } from "#ui/separator"

const SeparatorLabelMessage = UiSchema.TaggedEnum({ PressedEmail: {}, PressedGitHub: {} })

export const SeparatorLabel = Html.component({
  name: "SeparatorLabel",
  model: Schema.Struct({ provider: Schema.NullOr(Schema.String) }),
  message: SeparatorLabelMessage
}).make({
  init: () => ({ provider: null }),
  update: {
    PressedEmail: ($) => $.evolve({ provider: "email" }),
    PressedGitHub: ($) => $.evolve({ provider: "GitHub" })
  },
  view: (h, model) =>
    h.div.class("flex w-full max-w-xs flex-col gap-4").children(
      Button(h, { onPress: SeparatorLabelMessage.PressedEmail() }).children("Continue with email"),
      Separator(h, { variant: "label" }).children("or"),
      Button(h, { variant: "outline", onPress: SeparatorLabelMessage.PressedGitHub() }).children("Continue with GitHub"),
      h.p.class("m-0 text-center text-xs text-muted-foreground").children(
        model.provider === null ? "Not signed in" : `Signing in with ${model.provider}`
      )
    )
})
```

### In a menu

Full-width rules between the groups of a menu-like list.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Separator } from "#ui/separator"

const SeparatorMenuMessage = UiSchema.TaggedEnum({ ClickedAction: { name: Schema.String } })

const groups = [
  [{ name: "Rename", keys: "F2" }, { name: "Duplicate", keys: "⌘D" }],
  [{ name: "Share", keys: "" }, { name: "Move to", keys: "" }],
  [{ name: "Delete", keys: "⌫" }]
]

export const SeparatorMenu = Html.component({
  name: "SeparatorMenu",
  model: Schema.Struct({ last: Schema.NullOr(Schema.String) }),
  message: SeparatorMenuMessage
}).make({
  init: () => ({ last: null }),
  update: {
    ClickedAction: ($, { name }) => $.evolve({ last: name })
  },
  view: (h, model) =>
    h.div.class("flex flex-col items-center gap-4").children(
      h.div.class("grid w-56 rounded-lg border bg-popover p-1 shadow-float").children(
        groups.flatMap((group, index) => [
          index > 0 ? Separator(h, { class: "-mx-1 my-1 data-[orientation=horizontal]:w-auto" }) : undefined,
          ...group.map((action) =>
            h.button
              .class(
                "flex items-center justify-between gap-3 rounded-sm px-2 py-1.5 text-left text-[13px] hover:bg-accent " +
                  (action.name === "Delete" ? "text-destructive" : "text-foreground")
              )
              .on("click", SeparatorMenuMessage.ClickedAction({ name: action.name }))
              .children(
                action.name,
                action.keys === ""
                  ? undefined
                  : h.kbd.class("font-mono text-[11px] text-muted-foreground").children(action.keys)
              )
          )
        ])
      ),
      h.p.class("m-0 text-xs text-muted-foreground").children(
        model.last === null ? "Pick an action" : `Picked: ${model.last}`
      )
    )
})
```

### Variants

Four looks from the variant option, and a decorative rule that keeps the caller's role="none".

```ts
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Separator } from "#ui/separator"

const variants = ["default", "strong", "dashed", "brand"] as const

export const SeparatorVariants = Html.component({
  name: "SeparatorVariants",
  model: Schema.Struct({}),
  message: Schema.Never
}).make({
  init: () => ({}),
  update: ($) => $.model,
  view: (h) =>
    h.div.class("flex w-full max-w-xs flex-col gap-5").children(
      variants.map((variant) =>
        h.div.class("flex flex-col gap-2").children(
          h.span.class("text-xs text-muted-foreground").children(variant),
          Separator(h, { variant })
        )
      ),
      // role="none" keeps a purely decorative rule out of the accessibility tree.
      h.div.class("flex flex-col gap-2").children(
        h.span.class("text-xs text-muted-foreground").children("decorative, role=\"none\""),
        Separator(h, { element: h.div.attr("role", "none") })
      )
    )
})
```

## Anatomy

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

| Part | Description |
| --- | --- |
| `Separator(h, options)` | Returns one <div> builder: chain classes, attributes and children on it. |
| `options.element` | The caller's h.div, to share the divider with another part. h.div when left out. |

## API reference

### Props

| Prop | Type | Description |
| --- | --- | --- |
| `orientation` | `"horizontal" \| "vertical"` | The direction of the rule. "horizontal" by default. |

### Data attributes

| Attribute | Description |
| --- | --- |
| `data-orientation` | "horizontal" or "vertical". |
| `role, aria-orientation` | role="separator" with its orientation, unless the caller's element has role="none" or "presentation". |

## Related components

- [Button](https://www.effectcn.com/components/button.md): A native button that can stay focusable while disabled.
