# `ixfx-snap-container`

A container that positions its child element at configurable snap positions along an axis. The child can be dragged, clicked to snap, or controlled via keyboard.

```html
<ixfx-snap-container snap-positions="3" orientation="horizontal">
  <button>Drag me</button>
</ixfx-snap-container>
```

---

## Properties / attributes

| Attribute / Property    | Type                          | Default     | Description                                                        |
|------------------------|-------------------------------|-------------|--------------------------------------------------------------------|
| `orientation`          | `'horizontal'` \| `'vertical'` | `'horizontal'` | Axis along which snapping occurs. Horizontal uses x-axis; vertical uses y-axis |
| `snap-positions`       | number                        | `3`         | Number of snap positions                                           |
| `snap-logic`           | `'centre'` \| `'balanced'`    | `'balanced'` | How snap positions are distributed (see [Snap logic](#snap-logic)) |
| `click-snap`           | `'closest'` \| `'step'`       | `'closest'` | How clicking in empty space snaps (see [Click snap modes](#click-snap-modes)) |
| `snap`                 | number                        | `0`         | Current snap index (0-based)                                       |

---

## Snap logic

### `balanced` (default)

First snap position aligns with container's left/top edge. Last snap position aligns with container's right/bottom edge. Middle positions are evenly distributed.

```
Position 0 ─┬── Position 1 ─┬── Position 2 ─┬── Position 3 ─┬── Position 4
            │                │                │                │
────────────┼────────────────┼────────────────┼────────────────┼────────────
Left edge   │    evenly      │    evenly      │    evenly      │ Right edge
            │    spaced      │    spaced      │    spaced      │
```

### `centre`

All snap positions are evenly distributed, ignoring edge alignment.

```
Position 0 ─┬── Position 1 ─┬── Position 2 ─┬── Position 3 ─┬── Position 4
            │                │                │                │
────────────┼────────────────┼────────────────┼────────────────┼────────────
            │                │    centred      │                │
            │                │                │                │
```

---

## Click snap modes

### `closest` (default)

Clicking anywhere in the container (not on the child) moves the child to the nearest snap position to the click point.

### `step`

Clicking to the **right** of the child's center moves it one position right. Clicking to the **left** moves it one position left. Similarly for vertical orientation (up/down).

This is useful for incremental navigation without dragging.

---

## Events

| Event         | Detail                              | Description                              |
|---------------|-------------------------------------|------------------------------------------|
| `snap-change` | `{ previousSnap, newSnap }`         | Fired when snap index changes            |
| `snap-input`  | `{ index }`                         | Fired continuously during drag operation |

Both events bubble and are composed.

```js
container.addEventListener('snap-change', (e) => {
  console.log(`Moved from ${e.detail.previousSnap} to ${e.detail.newSnap}`);
});
```

---

## CSS custom properties

| Property               | Default    | Description                                      |
|------------------------|------------|--------------------------------------------------|
| `--transition-duration`| `200ms`   | Duration of snap transitions                     |

The child element also receives a `--snap-index` custom property reflecting its current position:

```css
child {
  background: hsl(calc(var(--snap-index) * 60), 70%, 50%);
}
```

---

## Keyboard navigation

When the container or its child has focus:

| Orientation   | Key       | Action                        |
|---------------|-----------|-------------------------------|
| horizontal    | LEFT      | Move to previous snap position|
| horizontal    | RIGHT     | Move to next snap position    |
| vertical      | UP        | Move to previous snap position|
| vertical      | DOWN      | Move to next snap position    |

---

## Programmatic control

All properties are writable at runtime:

```js
const container = document.querySelector('ixfx-snap-container');

container.snap = 2;              // Jump to position 2
container.snapPositions = 5;     // Change to 5 positions
container.orientation = 'vertical'; // Switch to vertical
```

---

## Usage notes

- The child element should have a fixed or intrinsic size
- The container has `overflow: hidden` and positions the child absolutely
- Drag requires moving at least 10px before snapping engages
- In `step` mode, clicking when already at the boundary (0 or max) does nothing
