# Installation

Start an Effect UI app with Tailwind CSS and add your first component by hand.

## 1. Create the app

Effect UI is in public preview and isn't on npm yet, so the packages install from snapshot builds.

```sh
pnpm create vite my-app --template vanilla-ts --no-interactive
cd my-app
pnpm add effect tailwind-merge \
  @effect/ui@https://pkg.pr.new/Effect-TS/ui/@effect/ui@main \
  @effect/ui-kit@https://pkg.pr.new/Effect-TS/ui/@effect/ui-kit@main
pnpm add -D tailwindcss @tailwindcss/vite
```

## 2. Turn on the compiler options

Add both to `tsconfig.json`. Without `exactOptionalPropertyTypes`, mounting some components fails to type-check.

`tsconfig.json`

```json
{
  "compilerOptions": {
    "strict": true,
    "exactOptionalPropertyTypes": true
  }
}
```

## 3. Add Tailwind CSS

The components are styled with Tailwind classes. Add its Vite plugin.

`vite.config.ts`

```ts
import tailwindcss from "@tailwindcss/vite"
import { defineConfig } from "vite"

export default defineConfig({ plugins: [tailwindcss()] })
```

## 4. Add the theme

Two files: the tokens, and the stylesheet that imports Tailwind and maps the tokens to its colours. See [Theming](https://www.effectcn.com/docs/theming.md) for the themes.

`src/styles/tokens.css`

```css
/*
 * Theme tokens. A theme is `data-theme` on the root element, and a mode is
 * `data-mode="light"` or `"dark"`. Beyond the usual names there are `--brand`
 * (what is selected or filled), `--brand-soft` (a lighter brand fill that
 * carries dark text) and `--brand-wash` (a tint).
 */

:root {
  --font-sans: "Inter Variable", "Inter", -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  --font-mono: "Geist Mono", "SF Mono", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;

  --radius: 8px;
  --radius-sm: calc(var(--radius) - 2px);
  --radius-xs: calc(var(--radius) - 4px);
  --radius-lg: calc(var(--radius) + 4px);

  --shadow-sm: 0 1px 2px color-mix(in oklab, var(--shadow-color) 6%, transparent);
  --shadow-float:
    0 1px 2px color-mix(in oklab, var(--shadow-color) 6%, transparent),
    0 10px 32px -8px color-mix(in oklab, var(--shadow-color) 22%, transparent);
}

/* Effect: black on white, nothing else. The default. */
:root,
[data-theme="effect"] {
  color-scheme: light;
  --background: #ffffff;
  --foreground: #18181a;
  --card: #ffffff;
  --popover: #ffffff;
  --muted: #f5f5f5;
  --muted-foreground: #6e6e73;
  --accent: #f0f0f0;
  --accent-foreground: #18181a;
  --border: #e6e6e6;
  --input: #dcdcdc;
  --ring: #18181a;
  --primary: #18181a;
  --primary-foreground: #ffffff;
  --secondary: #f2f2f2;
  --secondary-foreground: #18181a;
  --brand: #18181a;
  --brand-foreground: #ffffff;
  --brand-soft: #dedee2;
  --brand-soft-foreground: #18181a;
  --brand-wash: #f3f3f4;
  --destructive: #c62828;
  --destructive-foreground: #ffffff;
  --success: #15803d;
  --warning: #b45309;
  --shadow-color: #18181a;
}

[data-theme="effect"][data-mode="dark"],
:root[data-mode="dark"]:not([data-theme]),
[data-mode="dark"] [data-theme="effect"]:not([data-mode]) {
  color-scheme: dark;
  --background: #0c0c0d;
  --foreground: #f4f4f5;
  --card: #121214;
  --popover: #161618;
  --muted: #1b1b1e;
  --muted-foreground: #9a9aa2;
  --accent: #222226;
  --accent-foreground: #f4f4f5;
  --border: #26262a;
  --input: #303035;
  --ring: #f4f4f5;
  --primary: #f4f4f5;
  --primary-foreground: #18181a;
  --secondary: #1f1f23;
  --secondary-foreground: #f4f4f5;
  --brand: #f4f4f5;
  --brand-foreground: #18181a;
  --brand-soft: #3a3a40;
  --brand-soft-foreground: #f4f4f5;
  --brand-wash: #1c1c1f;
  --destructive: #ef5350;
  --destructive-foreground: #18181a;
  --success: #4ade80;
  --warning: #fbbf24;
  --shadow-color: #000000;
}

/*
 * Cornflower: the theme of our billing and Artifacts apps. Buttons are ink,
 * the brand fill is cornflower #8b9fe8 carrying ink, and its strong step,
 * indigo #4b3f9e, is the focus ring, a checkbox's tick and a meter's fill.
 */
[data-theme="cornflower"] {
  color-scheme: light;
  --background: #ffffff;
  --foreground: #16161a;
  --card: #ffffff;
  --popover: #ffffff;
  --muted: #fafaf9;
  --muted-foreground: #6e6e76;
  --accent: #f5f5f4;
  --accent-foreground: #16161a;
  --border: #e5e5e5;
  --input: #e0e0e0;
  --ring: #4b3f9e;
  --primary: #111827;
  --primary-foreground: #ffffff;
  --secondary: #f5f5f4;
  --secondary-foreground: #16161a;
  --brand: #4b3f9e;
  --brand-foreground: #ffffff;
  --brand-soft: #8b9fe8;
  --brand-soft-foreground: #16161a;
  --brand-wash: #eef1fc;
  --destructive: #b91c1c;
  --destructive-foreground: #ffffff;
  --success: #15803d;
  --warning: #b45309;
  --shadow-color: #1c1917;
}

[data-theme="cornflower"][data-mode="dark"] {
  color-scheme: dark;
  --background: #0e0f14;
  --foreground: #ececf1;
  --card: #14151c;
  --popover: #181a22;
  --muted: #1a1c25;
  --muted-foreground: #9799a8;
  --accent: #222430;
  --accent-foreground: #ececf1;
  --border: #272935;
  --input: #31344a;
  --ring: #a9b8f0;
  --primary: #ececf1;
  --primary-foreground: #111827;
  --secondary: #1f212c;
  --secondary-foreground: #ececf1;
  --brand: #8b9fe8;
  --brand-foreground: #12131a;
  --brand-soft: #8b9fe8;
  --brand-soft-foreground: #12131a;
  --brand-wash: #1d2038;
  --destructive: #f87171;
  --destructive-foreground: #16161a;
  --success: #86c97a;
  --warning: #f2b34b;
  --shadow-color: #000000;
}

/* Sage: a quiet green on warm paper. */
[data-theme="sage"] {
  color-scheme: light;
  --background: #fcfcfa;
  --foreground: #1b1f1c;
  --card: #ffffff;
  --popover: #ffffff;
  --muted: #f3f4f0;
  --muted-foreground: #68706a;
  --accent: #ebede7;
  --accent-foreground: #1b1f1c;
  --border: #e2e5de;
  --input: #d7dbd2;
  --ring: #3f6b52;
  --primary: #1f2b24;
  --primary-foreground: #ffffff;
  --secondary: #eef0ea;
  --secondary-foreground: #1b1f1c;
  --brand: #3f6b52;
  --brand-foreground: #ffffff;
  --brand-soft: #a9c4b1;
  --brand-soft-foreground: #1b1f1c;
  --brand-wash: #eaf1ec;
  --destructive: #b3261e;
  --destructive-foreground: #ffffff;
  --success: #2e7d4f;
  --warning: #a8620a;
  --shadow-color: #1b1f1c;
}

[data-theme="sage"][data-mode="dark"] {
  color-scheme: dark;
  --background: #0d100e;
  --foreground: #e8ece9;
  --card: #131714;
  --popover: #171c19;
  --muted: #191e1b;
  --muted-foreground: #939c96;
  --accent: #202722;
  --accent-foreground: #e8ece9;
  --border: #252c27;
  --input: #303932;
  --ring: #9cc4a9;
  --primary: #e8ece9;
  --primary-foreground: #131714;
  --secondary: #1d231f;
  --secondary-foreground: #e8ece9;
  --brand: #8fbf9f;
  --brand-foreground: #0d100e;
  --brand-soft: #8fbf9f;
  --brand-soft-foreground: #0d100e;
  --brand-wash: #18251d;
  --destructive: #f2867c;
  --destructive-foreground: #131714;
  --success: #8fd19e;
  --warning: #e8b35a;
  --shadow-color: #000000;
}

/* Ember: a single warm orange on stone. */
[data-theme="ember"] {
  color-scheme: light;
  --background: #fffdfb;
  --foreground: #1c1917;
  --card: #ffffff;
  --popover: #ffffff;
  --muted: #f7f5f2;
  --muted-foreground: #78716c;
  --accent: #f0ece8;
  --accent-foreground: #1c1917;
  --border: #e9e4df;
  --input: #ddd6cf;
  --ring: #b4541a;
  --primary: #1c1917;
  --primary-foreground: #ffffff;
  --secondary: #f3efeb;
  --secondary-foreground: #1c1917;
  --brand: #b4541a;
  --brand-foreground: #ffffff;
  --brand-soft: #f0b88f;
  --brand-soft-foreground: #1c1917;
  --brand-wash: #fcf1e8;
  --destructive: #b91c1c;
  --destructive-foreground: #ffffff;
  --success: #15803d;
  --warning: #a16207;
  --shadow-color: #1c1917;
}

[data-theme="ember"][data-mode="dark"] {
  color-scheme: dark;
  --background: #100e0c;
  --foreground: #f1ece7;
  --card: #171412;
  --popover: #1c1815;
  --muted: #1e1a17;
  --muted-foreground: #a39a92;
  --accent: #28231f;
  --accent-foreground: #f1ece7;
  --border: #2c2622;
  --input: #3a322c;
  --ring: #f0a56c;
  --primary: #f1ece7;
  --primary-foreground: #171412;
  --secondary: #231e1a;
  --secondary-foreground: #f1ece7;
  --brand: #ee9a5c;
  --brand-foreground: #171412;
  --brand-soft: #ee9a5c;
  --brand-soft-foreground: #171412;
  --brand-wash: #2a1c12;
  --destructive: #f87171;
  --destructive-foreground: #171412;
  --success: #86c97a;
  --warning: #eab308;
  --shadow-color: #000000;
}
```

`src/styles/tailwind.css`

```css
@import "tailwindcss";
@import "./tokens.css";
/* Optional: more themes. */
@import "./themes.css";

/* `dark:` follows the mode on the root element, not the OS. */
@custom-variant dark (&:where([data-mode="dark"], [data-mode="dark"] *));

/* Map the theme tokens to Tailwind colours: `bg-background`, `text-muted-foreground`, `border-input`, `bg-brand` ... */
@theme inline {
  --font-sans: var(--font-sans);
  --font-mono: var(--font-mono);

  --color-background: var(--background);
  --color-foreground: var(--foreground);
  --color-card: var(--card);
  --color-popover: var(--popover);
  --color-muted: var(--muted);
  --color-muted-foreground: var(--muted-foreground);
  --color-accent: var(--accent);
  --color-accent-foreground: var(--accent-foreground);
  --color-border: var(--border);
  --color-input: var(--input);
  --color-ring: var(--ring);
  --color-primary: var(--primary);
  --color-primary-foreground: var(--primary-foreground);
  --color-secondary: var(--secondary);
  --color-secondary-foreground: var(--secondary-foreground);
  --color-brand: var(--brand);
  --color-brand-foreground: var(--brand-foreground);
  --color-brand-soft: var(--brand-soft);
  --color-brand-soft-foreground: var(--brand-soft-foreground);
  --color-brand-wash: var(--brand-wash);
  --color-destructive: var(--destructive);
  --color-destructive-foreground: var(--destructive-foreground);
  --color-success: var(--success);
  --color-warning: var(--warning);

  --radius-xs: var(--radius-xs);
  --radius-sm: var(--radius-sm);
  --radius-md: var(--radius);
  --radius-lg: var(--radius-lg);

  --shadow-sm: var(--shadow-sm);
  --shadow-float: var(--shadow-float);
}

/* An indeterminate bar that slides across its track: `animate-indeterminate`. */
@theme {
  --animate-indeterminate: indeterminate 1.4s ease-in-out infinite;

  @keyframes indeterminate {
    from {
      transform: translateX(-100%);
    }
    to {
      transform: translateX(350%);
    }
  }
}

@layer base {
  * {
    border-color: var(--border);
  }

  html {
    font-family: var(--font-sans);
    -webkit-font-smoothing: antialiased;
    scrollbar-color: color-mix(in oklab, var(--foreground) 25%, transparent) transparent;
    scrollbar-width: thin;
  }

  body {
    font-size: 14px;
    line-height: 1.5;
    background: var(--background);
    color: var(--foreground);
  }

  :focus-visible {
    outline: 2px solid var(--ring);
    outline-offset: 2px;
  }

  ::selection {
    background: color-mix(in oklab, var(--brand-soft) 45%, transparent);
  }

  button:not(:disabled),
  [role="button"]:not([aria-disabled="true"]) {
    cursor: pointer;
  }

  /* The kit shows popups with the Popover API. Clear the browser's defaults so the classes decide. */
  [popover],
  dialog {
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: inherit;
    overflow: visible;
    inset: auto;
  }

  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      animation-duration: 0.01ms !important;
      transition-duration: 0.01ms !important;
    }
  }
}
```

## 5. Add the import aliases and the cn helper

Components import each other through `#ui/*` and the helper through `#lib/*`. Node, TypeScript and Vite all read the imports field, with no other config.

`package.json`

```json
{
  "imports": {
    "#ui/*": "./src/ui/*.ts",
    "#lib/*": "./src/lib/*.ts"
  }
}
```

`src/lib/cn.ts`

```ts
import { twMerge } from "tailwind-merge"

/** Joins class names, drops the falsy ones, and lets a later Tailwind class win over an earlier one. */
export const cn = (...classes: ReadonlyArray<string | false | null | undefined>): string =>
  twMerge(classes.filter(Boolean).join(" "))
```

## 6. Copy a component

Each component page has its file under Installation. This is [Button](https://www.effectcn.com/components/button.md).

`src/ui/button.ts`

```ts
import { Button as KitButton, type ButtonElement, type ButtonProps } from "@effect/ui-kit"
import { cn } from "#lib/cn"

const base =
  "inline-flex h-8.5 shrink-0 items-center justify-center gap-1.5 rounded-md border border-transparent px-3.5 " +
  "text-[13px] leading-none font-medium whitespace-nowrap no-underline select-none " +
  "transition-[background-color,border-color,color,opacity] duration-150 active:translate-y-px " +
  "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 [&_svg]:size-[15px] [&_svg]:shrink-0 " +
  "data-[loading]:before:size-3 data-[loading]:before:animate-spin data-[loading]:before:rounded-full " +
  "data-[loading]:before:border-2 data-[loading]:before:border-current data-[loading]:before:border-r-transparent"

const variants = {
  default: "bg-primary text-primary-foreground hover:bg-primary/85",
  secondary: "bg-secondary text-secondary-foreground hover:bg-accent",
  outline: "border-input bg-background text-foreground shadow-sm hover:bg-accent hover:text-accent-foreground",
  ghost: "text-foreground hover:bg-accent hover:text-accent-foreground",
  brand: "bg-brand-soft text-brand-soft-foreground hover:bg-brand-soft/85",
  destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/85",
  link: "h-auto px-0 text-foreground underline decoration-transparent underline-offset-4 hover:decoration-current"
} as const

const sizes = {
  default: "",
  sm: "h-7 rounded-sm px-2.5 text-xs",
  lg: "h-10 px-5 text-sm",
  icon: "w-8.5 px-0",
  "icon-sm": "h-7 w-7 rounded-sm px-0"
} as const

export type ButtonVariant = keyof typeof variants
export type ButtonSize = keyof typeof sizes

/** The button's classes, for an element that should look like one: a link, a kit trigger. */
export const buttonVariants = (
  options: {
    readonly variant?: ButtonVariant | undefined
    readonly size?: ButtonSize | undefined
    readonly class?: string | undefined
  } = {}
): string => cn(base, variants[options.variant ?? "default"], sizes[options.size ?? "default"], options.class)

/**
 * The kit's Button with a look. Pass the message for a press as `onPress`
 * and chain the label: `Button(h, { variant: "outline", onPress }).children("Save")`.
 * `loading` shows a spinner and keeps the button focusable while it's disabled.
 */
export const Button = <Msg, R = never>(
  h: { readonly button: ButtonElement<Msg> },
  options: {
    readonly variant?: ButtonVariant | undefined
    readonly size?: ButtonSize | undefined
    readonly class?: string | undefined
    readonly loading?: boolean | undefined
    readonly element?: ButtonElement<Msg, R> | undefined
    readonly props?: ButtonProps | undefined
    readonly onPress?: NoInfer<Msg> | undefined
  } = {}
): ButtonElement<Msg, R> =>
  KitButton<Msg, R>(h, {
    element: options.element,
    onPress: options.onPress,
    props: options.loading === true
      ? { ...options.props, disabled: true, focusableWhenDisabled: true }
      : options.props
  })
    .class(buttonVariants(options))
    .attr("data-loading", options.loading === true ? "" : undefined)
```

## 7. Write a component

Button wraps the kit's Button: call it with h, pass the message for a press, and chain the label.

`src/Counter.ts`

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

export const CounterMessage = UiSchema.TaggedEnum({
  PressedDecrement: {},
  PressedIncrement: {}
})

export const Counter = Html.component({
  name: "Counter",
  model: Schema.Struct({ count: Schema.Number }),
  message: CounterMessage
}).make({
  init: () => ({ count: 0 }),
  update: {
    PressedDecrement: ($) => $.evolve({ count: (count) => count - 1 }),
    PressedIncrement: ($) => $.evolve({ count: (count) => count + 1 })
  },
  view: (h, model) =>
    h.div.class("flex items-center gap-3").children(
      Button(h, { variant: "outline", size: "icon", onPress: CounterMessage.PressedDecrement() }).children("−"),
      h.output.class("w-8 text-center tabular-nums").children(String(model.count)),
      Button(h, { size: "icon", onPress: CounterMessage.PressedIncrement() }).children("+")
    )
})
```

## 8. Mount it

`src/main.ts`

```ts
import { BrowserHost } from "@effect/ui/html"
import { Effect, Layer } from "effect"
import { Counter } from "./Counter.ts"
import "./styles/tailwind.css"

const Main = BrowserHost.mount(Counter, {}, document.getElementById("app")!)

Layer.launch(Main).pipe(Effect.runFork)
```
