# Reusable components with Cotton

Interlace gives you the *reactive* half, state and behaviour in a Python component. For the *markup*
half, [django-cotton](https://django-cotton.com) lets you build reusable HTML tags (`<c-button>`,
`<c-field>`) with slots and attributes. They compose: Cotton for presentational pieces you reuse,
Interlace for the state that drives them.

Cotton is **optional**, it is not an Interlace dependency. Add it only for this workflow:

```bash
pip install django-cotton
```

```python
# settings.py
INSTALLED_APPS = [..., "django_cotton", "interlace"]
```

Cotton expands `<c-...>` tags server-side, before Interlace renders, in a `template_name` file **or**
in an `inline_template()` string (when django-cotton is installed, Interlace runs Cotton's compiler
over inline sources too). `<c-field lace:model="email">` then becomes a plain
`<input lace:model="email">`, and binding, validation and morphing all behave exactly as if you had
written the input by hand.

## A reusable field

Most forms repeat the same label + input + error markup. Wrap it once as `<c-field>` and let Cotton's
`{{ attrs }}` forward **any** attribute, `lace:model`, `type`, `placeholder`, straight to the input.
The `error` prop is a normal `{{ }}` expression pulled from the component's `errors` dict:

```interlace-demo
FieldDemo
```

`{{ attrs }}` is the whole trick: anything you put on `<c-field>` that it did not declare in `<c-vars>`
lands on the `<input>`. That is why `lace:model.live="email"` binds normally, and why the same field
serves emails, passwords or search boxes without touching `field.html` again.

## Wrapping a third-party JS library

Some widgets, date pickers, maps, rich-text editors, own their DOM and must not be touched by
morphing. Interlace has [`lace:ignore`](/docs/lace-ignore/) for exactly this: the element is skipped
during morph, so the widget survives re-renders. Combine it with Cotton for a clean, reusable tag.

Here is a date field backed by [flatpickr](https://flatpickr.js.org). Open it, pick a date, and the
value flows back into Python state:

```interlace-demo
DateDemo
```

- **`lace:ignore`** stops the morpher re-rendering the input, so flatpickr's calendar and listeners
  survive when the rest of the component updates.
- **`x-init`** initialises the library once; its `onChange` fires a native `change` event, so
  `lace:model.change` syncs the picked value back into Python state.

The same shape, `lace:ignore` + `x-init` + a bound input (or a method call), wraps almost any library:
Quill / TinyMCE editors, Choices.js selects, Chart.js canvases, Leaflet maps.

## Which tool for what

- **`<c-...>` (Cotton)** handles reusable *markup*: fields, cards, buttons, layout. No state of its own.
- **`Component` (Interlace)** handles *state and behaviour*: properties, methods, validation, polling.
- **`lace:ignore`** protects a subtree (usually a third-party widget) from morphing.

Build presentational pieces with Cotton, pass them `lace:` attributes, and let Interlace drive the
reactivity.
