# Lamp Switch

A ceramic pendant lamp hangs over a liquid switch. On mount the lamp drops in from the ceiling, bounces on its cord, swings, and then switches itself on with a flicker. Tap the switch, press Space or Enter on it, or drag its knob; the knob follows the pointer and lands on the side a flick would carry it to. Grab the shade to swing the lamp, or pull it down past the detent to click the light like a pull-chain.

<demo-lamp-switch>



</demo-lamp-switch>

## Installation

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

## Usage

```vue
<script setup lang="ts">
  import { computed, ref } from 'vue';
  import LampSwitch from '~/components/ui/lamp-switch/LampSwitch.vue';

  const colorMode = useColorMode();
  const theme = computed(() => (colorMode.value === 'dark' ? 'dark' : 'light'));
  const on = ref(false);

  function onToggle(value: boolean) {
    console.log('light', value ? 'on' : 'off');
  }
</script>

<template>
  <LampSwitch
    v-model:on="on"
    :size="354"
    :auto-on="true"
    :theme="theme"
    @toggle="onToggle"
  />
</template>
```

The card is drawn at 412 × 412 and scaled as one object. It is never larger than `size`, and it shrinks to fit a narrow container.

## Props

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

<tbody>
  <tr>
    <td>
      <code>
        on
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        false
      </code>
    </td>
    
    <td>
      <code>
        v-model:on
      </code>
      
      . Whether the light is on. Setting it flips it.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        auto-on
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        true
      </code>
    </td>
    
    <td>
      Switch the light on by itself once the lamp has landed.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        theme
      </code>
    </td>
    
    <td>
      <code>
        'light' | 'dark'
      </code>
    </td>
    
    <td>
      <code>
        'dark'
      </code>
    </td>
    
    <td>
      Colour theme. Pass your site's colour mode.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        size
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        354
      </code>
    </td>
    
    <td>
      The card's size in px at most.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Lamp'
      </code>
    </td>
    
    <td>
      Accessible name of the switch.
    </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>
        toggle
      </code>
    </td>
    
    <td>
      <code>
        on: boolean
      </code>
    </td>
    
    <td>
      Fires whenever the light turns on or off.
    </td>
  </tr>
</tbody>
</table>

## Accessibility

- The switch is a native `button` with `role="switch"` and `aria-checked`. Space and Enter flip it.
- The switch has a visible focus ring for keyboard users.
- The lamp is decoration (`aria-hidden`); pulling it does the same thing as the switch.
- With reduced motion the lamp does not drop or swing, the light switches without a flicker, and the knob moves without a spring.

Ported from [Lamp Switch](https://pinui.xyz/components/lamp-switch) by Pin UI, MIT licence.
