# Getting Started

This guide walks through adding Phoenix Duskmoon UI to a Phoenix LiveView project.

## Prerequisites

- Elixir >= 1.18
- Phoenix ~> 1.8.1
- Phoenix LiveView ~> 1.1.0
- DuskmoonBundler ~> 10.0 (includes built-in Tailwind v4 and JS bundler)

## Installation

### 1. Add the Hex dependency

```elixir
# mix.exs
defp deps do
  [
    {:phoenix_duskmoon, "~> 10.0"}
  ]
end
```

```bash
mix deps.get
```

### 2. Install frontend packages

```bash
mix npm.install @duskmoon-dev/core @duskmoon-dev/elements
```

`@duskmoon-dev/core` provides the CSS design system (theme variables, component
classes, design tokens). `@duskmoon-dev/elements` is a meta-package that installs
all `el-dm-*` custom element packages.

### 3. Import components in your view helpers

In your `lib/my_app_web.ex`, add the component imports to `html_helpers/0`:

```elixir
defp html_helpers do
  quote do
    # Import all Duskmoon UI components (dm_btn, dm_card, dm_input, etc.)
    use PhoenixDuskmoon.Component

    # Import CSS art components (dm_art_snow, dm_art_plasma_ball, etc.)
    use PhoenixDuskmoon.ArtComponent

    # ... your other imports
  end
end
```

### 4. Configure CSS

In your `assets/css/app.css`:

```css
@source "../js/**/*.js";
@source "../../lib/**/*.exs";
@source "../../lib/**/*.ex";
@source "../../deps/phoenix_duskmoon/lib";
@source "../../deps/phoenix_duskmoon/assets/js";

@import "tailwindcss";
@import "@duskmoon-dev/core/base.css";
@plugin "@duskmoon-dev/core/plugin";
```

The `@plugin` directive registers the Duskmoon design system with Tailwind v4,
providing all theme variables, component classes, and design tokens.

Tailwind v4 skips gitignored `deps/` during automatic detection. Keep the
dependency sources so the library's HEEX and JavaScript utilities are generated.
Paths are relative to this CSS file; for an umbrella app use
`../../../../deps/phoenix_duskmoon/lib` and
`../../../../deps/phoenix_duskmoon/assets/js`.

#### DuskmoonBundler source configuration

With DuskmoonBundler 10.1.3, also add these entries to your existing profile's
`tailwind.sources` in `config/config.exs`; keep your application's sources:

```elixir
config :duskmoon_bundler, :my_app,
  # Keep your existing root, entry, outdir and other profile settings.
  tailwind: [
    css: "assets/css/app.css",
    sources: [
      %{base: "assets", pattern: "**/*.{js,css}"},
      %{base: "lib", pattern: "**/*.{ex,exs,heex}"},
      %{base: "deps/phoenix_duskmoon/lib", pattern: "**/*.{ex,exs,heex}"},
      %{base: "deps/phoenix_duskmoon/assets/js", pattern: "**/*.js"}
    ]
  ]
```

These paths are relative to the project root where the build runs, unlike CSS
`@source` paths. When running from an umbrella root, use
`apps/my_app/assets` and `apps/my_app/lib` for your application paths; the
`deps/phoenix_duskmoon/...` entries remain relative to the umbrella root.

WORKAROUND(upstream): [duskmoon-bundler#23](https://github.com/duskmoon-dev/duskmoon-bundler/issues/23).
Bundler 10.1.3 scans its configured sources and ignores CSS `@source` declarations.
TODO(upstream): remove the duplicate dependency entries only after upgrading to a
published Bundler fix and verifying that CSS-only sources generate the same
utilities. Until then, keep these entries when upgrading Phoenix Duskmoon UI.

### 5. Register custom elements

In your `assets/js/app.js`, import the element packages you use:

```javascript
// Register individual elements
import "@duskmoon-dev/el-button/register";
import "@duskmoon-dev/el-input/register";
// ... add more as needed

// Or register all elements at once
import "@duskmoon-dev/elements/register";
```

Each custom element must be explicitly registered for its tag to be defined in
the browser. Without registration, content inside the element's `<template>` is
inert and invisible.

### 6. Set up LiveView hooks

Some components require JavaScript hooks for client-side interactions. Import and
register them with your LiveSocket:

```javascript
import { LiveSocket } from "phoenix_live_view";
import * as DuskmoonHooks from "phoenix_duskmoon/hooks";

let liveSocket = new LiveSocket("/live", Socket, {
  params: { _csrf_token: csrfToken },
  hooks: DuskmoonHooks,
});
```

To merge with your own hooks:

```javascript
import * as DuskmoonHooks from "phoenix_duskmoon/hooks";
import MyHooks from "./my_hooks";

let hooks = { ...DuskmoonHooks, ...MyHooks };

let liveSocket = new LiveSocket("/live", Socket, {
  params: { _csrf_token: csrfToken },
  hooks: hooks,
});
```

See the [Hooks](hooks.md) guide for details on each hook.

## Usage

All components use the `dm_` prefix:

```heex
<%!-- Buttons --%>
<.dm_btn variant="primary">Save</.dm_btn>
<.dm_btn variant="error" shape="circle">×</.dm_btn>

<%!-- Cards with slots --%>
<.dm_card>
  <:title>Card Title</:title>
  Content goes here
  <:footer><.dm_btn variant="primary">Action</.dm_btn></:footer>
</.dm_card>

<%!-- Icons --%>
<.dm_mdi name="home" />
<.dm_bsi name="house" />

<%!-- Forms with field binding --%>
<.dm_form for={@form} phx-submit="save">
  <.dm_input field={@form[:name]} label="Name" />
  <.dm_select field={@form[:role]} label="Role" options={[{"admin", "Admin"}, {"user", "User"}]} />
  <.dm_btn variant="primary" type="submit">Save</.dm_btn>
</.dm_form>
```

### Common attributes

| Attribute | Values | Description |
|-----------|--------|-------------|
| `variant` | `primary`, `secondary`, `accent`, `info`, `success`, `warning`, `error`, `ghost`, `link`, `outline` | Color variant |
| `size` | `xs`, `sm`, `md`, `lg` | Size variant |
| `shape` | `square`, `circle` | Shape variant |
| `loading` | boolean | Shows loading spinner |
| `disabled` | boolean | Disables interaction |
| `class` | string or list | Additional CSS classes |

### Form field binding

Data entry components support direct `Phoenix.HTML.FormField` binding via the
`field` attribute, which automatically sets `name`, `id`, `value`, and error state:

```heex
<.dm_input field={@form[:email]} label="Email" type="email" />
<.dm_checkbox field={@form[:terms]} label="I agree to the terms" />
<.dm_switch field={@form[:notifications]} label="Enable notifications" />
```

For bulk record selection, give each checkbox its record ID as `value`, share an
array name, and set `hidden_input={false}` so unchecked records contribute no
`"false"` entries. Set `checked` explicitly for each record:

```heex
<.dm_checkbox id="rule-a" name="rule_ids[]" value="rule-a" hidden_input={false} checked={true} label="Select Rule A" />
<.dm_checkbox id="rule-b" name="rule_ids[]" value="rule-b" hidden_input={false} checked={false} label="Select Rule B" />
```

This selection submits `rule_ids` as `["rule-a"]`. Boolean checkboxes retain the
default value `"true"` and hidden `"false"` input. With a bound `field`, `multiple`
appends `[]` to its name; `hidden_input` controls the hidden input separately.
