# Device Pairing

A card for pairing a home battery. The stage shows a line drawing of the battery and its solar panels, with rings pulsing from the battery while it waits. Type or paste the code from the device: each digit lights up in its LCD cell, the next cell glows, and Backspace steps back. When every cell is full, Pair (or Enter) starts pairing, the progress row moves on and the stage turns green. The countdown clears the code when a new one is due.

<demo-device-pairing>



</demo-device-pairing>

## Installation

```bash
npx shadcn-vue@latest add "https://nxui.geoql.in/r/device-pairing.json"
```

## Usage

```vue
<script setup lang="ts">
  import { ref } from 'vue';
  import DevicePairing from '~/components/ui/device-pairing/DevicePairing.vue';
  import type { DevicePairingStatus } from '~/components/ui/device-pairing/types';

  const code = ref('');
  const status = ref<DevicePairingStatus>('idle');

  async function onPair(value: string): Promise<void> {
    const res = await fetch('/api/pair', { method: 'POST', body: value });
    status.value = res.ok ? 'paired' : 'error';
  }
</script>

<template>
  <DevicePairing
    v-model="code"
    v-model:status="status"
    :auto-confirm="0"
    @pair="onPair"
  />
</template>
```

With the default `auto-confirm` the card marks itself paired 1.4 s after Pair, which suits a demo. Set it to `0` and set `status` yourself once your backend answers. `'error'` shakes the cells and tints them red until the user edits the code.

The component exposes `pair()`, `reset()` and `focus()` through a template ref.

## Props

<table>
<thead>
  <tr>
    <th>
      Prop
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        model-value
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      <code>
        v-model
      </code>
      
      . Typed digits.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        status
      </code>
    </td>
    
    <td>
      <code>
        'idle' | 'pairing' | 'paired' | 'error'
      </code>
    </td>
    
    <td>
      <code>
        'idle'
      </code>
    </td>
    
    <td>
      <code>
        v-model:status
      </code>
      
      . Where the flow is.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        length
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        6
      </code>
    </td>
    
    <td>
      Number of code cells.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        title
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Devices'
      </code>
    </td>
    
    <td>
      Header title.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        subtitle
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Nothing paired yet'
      </code>
    </td>
    
    <td>
      Header subtitle before pairing.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        paired-subtitle
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Battery paired'
      </code>
    </td>
    
    <td>
      Header subtitle after pairing.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        count
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        0
      </code>
    </td>
    
    <td>
      Devices already paired, shown in the pill.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        icon
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'lucide:plug'
      </code>
    </td>
    
    <td>
      Icon in the header chip.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        stage-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Pairing'
      </code>
    </td>
    
    <td>
      State label at the top left of the stage.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        nearby-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        '1 battery nearby'
      </code>
    </td>
    
    <td>
      Label at the top right of the stage.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        channel
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Ch 11 · 2.4 GHz'
      </code>
    </td>
    
    <td>
      Radio details at the bottom left of the stage.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        heading
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Pair your battery'
      </code>
    </td>
    
    <td>
      Heading above the code.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        helper
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Two lines
    </td>
    
    <td>
      Helper text. <code>
        \n
      </code>
      
       starts a new line.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        code-lifetime
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        120
      </code>
    </td>
    
    <td>
      Seconds a code lives.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        code-remaining
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        102
      </code>
    </td>
    
    <td>
      Seconds left on the first code.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        steps
      </code>
    </td>
    
    <td>
      <code>
        DevicePairingStep[]
      </code>
    </td>
    
    <td>
      Power on, Enter code, Name it
    </td>
    
    <td>
      Progress row labels. The second step is the code step.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        auto-confirm
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        1400
      </code>
    </td>
    
    <td>
      Milliseconds until the card sets itself paired. <code>
        0
      </code>
      
       turns it off.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        accent
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        '#ff9a2e'
      </code>
    </td>
    
    <td>
      Lit digits, caret and pairing state.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        success-color
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        '#4ade80'
      </code>
    </td>
    
    <td>
      Paired state.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        scan-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Scan QR code'
      </code>
    </td>
    
    <td>
      Text of the scan button.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        pair-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Pair battery'
      </code>
    </td>
    
    <td>
      Text of the pair button.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        class
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      Extra classes for the root element.
    </td>
  </tr>
</tbody>
</table>

## Events

<table>
<thead>
  <tr>
    <th>
      Event
    </th>
    
    <th>
      Payload
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        update:modelValue
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The digits changed.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        update:status
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The status changed.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        complete
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Every cell is filled.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        pair
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Pair or Enter was pressed with a full code.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        scan
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      Scan QR code was pressed.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        expire
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      The countdown ran out and the code cleared.
    </td>
  </tr>
</tbody>
</table>

## Accessibility

- One real text input sits over the cells, with `inputmode="numeric"` and `autocomplete="one-time-code"`, so typing, paste, Backspace and phone code autofill all work.
- The input is described by the countdown, and the stage state is an `aria-live` region.
- The progress row is a list and marks the current step with `aria-current="step"`.
- The pulsing rings, the cable flow and the caret blink stop when the user prefers reduced motion or the card is off screen.
