# Distortion Wave

A transition between two views of the same card. When `revealed` turns true, the view first draws in toward the button you pressed, then a wave leaves it and crosses the card, leaning toward the press point. The front bends what it passes, splits the colour channels at its edge, glows in a band of colour with sparks behind it, and leaves the revealed view sharpening in its wake. The rim keeps a soft glow for a moment after. Set `revealed` back to false and the wave folds back into its source. Controls in the `#overlay` slot stay live and crisp above the wave. In the demo, press Confirm, Buy 500 credits or the retry button on the toast.

<demo-distortion-wave>



</demo-distortion-wave>

## Installation

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

## Usage

```vue
<script setup lang="ts">
  import { ref } from 'vue';
  import DistortionWave from '~/components/ui/distortion-wave/DistortionWave.vue';

  const paid = ref(false);
  const button = ref<HTMLButtonElement | null>(null);
</script>

<template>
  <DistortionWave
    v-model:revealed="paid"
    :source="button"
    class="w-80 overflow-hidden rounded-4xl border border-border bg-card"
  >
    <div class="p-6 pb-22">Review the payment…</div>
    <template #revealed>
      <div class="p-6 pb-22">Payment sent.</div>
    </template>
    <template #overlay>
      <button
        ref="button"
        class="absolute inset-x-5 bottom-5 h-11 rounded-full bg-foreground text-background"
        @click="paid = !paid"
      >
        {{ paid ? 'Done' : 'Confirm' }}
      </button>
    </template>
  </DistortionWave>
</template>
```

Give the element a solid background (or put one behind it), rounded corners and `overflow-hidden`; the glow follows its corner radius. Both views share one grid cell and the element takes the size of the larger one.

To drive it by hand, take a template ref and call `play()`, `reverse()` or `reset()`.

## Props

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

<tbody>
  <tr>
    <td>
      <code>
        revealed
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      Which view shows. Changing it runs the wave. Use with <code>
        v-model:revealed
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        source
      </code>
    </td>
    
    <td>
      <code>
        Element | component | { x, y, width, height } | null
      </code>
    </td>
    
    <td>
      last pressed control
    </td>
    
    <td>
      Where the wave leaves from: an element or template ref, or a box in the element's own CSS pixels. Without one, the control last pressed inside the element, else the bottom centre.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        colors
      </code>
    </td>
    
    <td>
      <code>
        'prism' | 'meadow' | 'lagoon' | 'ember' | string[]
      </code>
    </td>
    
    <td>
      <code>
        'prism'
      </code>
    </td>
    
    <td>
      A preset or up to four <code>
        #rrggbb
      </code>
      
       colours for the glowing band and the rim.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        duration
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        1.6
      </code>
    </td>
    
    <td>
      Seconds the wave takes to cross, glow included.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        return-duration
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        0.9
      </code>
    </td>
    
    <td>
      Seconds the wave takes to fold back.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        refraction
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        1
      </code>
    </td>
    
    <td>
      How strongly the front bends what it passes (0–2).
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        aberration
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        1
      </code>
    </td>
    
    <td>
      How far the colour channels split at the front (0–2).
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        lean
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        0.2
      </code>
    </td>
    
    <td>
      How far the front leans toward the press point (0–1).
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        sparks
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        true
      </code>
    </td>
    
    <td>
      Fine sparks just behind the front.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        radius
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      the element's own
    </td>
    
    <td>
      Corner radius of the glow in CSS pixels.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        motion
      </code>
    </td>
    
    <td>
      <code>
        'auto' | 'full' | 'reduce'
      </code>
    </td>
    
    <td>
      <code>
        'auto'
      </code>
    </td>
    
    <td>
      <code>
        auto
      </code>
      
       crossfades when the visitor asks for reduced motion; <code>
        reduce
      </code>
      
       always crossfades; <code>
        full
      </code>
      
       always runs the wave.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        class
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      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:revealed
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      The view now showing, when a wave started by <code>
        play()
      </code>
      
       or <code>
        reverse()
      </code>
      
       ends.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        start
      </code>
    </td>
    
    <td>
      <code>
        revealed: boolean
      </code>
    </td>
    
    <td>
      A wave starts toward this view.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        finish
      </code>
    </td>
    
    <td>
      <code>
        revealed: boolean
      </code>
    </td>
    
    <td>
      A wave ends on this view.
    </td>
  </tr>
</tbody>
</table>

## Slots

<table>
<thead>
  <tr>
    <th>
      Slot
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      default
    </td>
    
    <td>
      The first view.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        revealed
      </code>
    </td>
    
    <td>
      The view the wave leaves behind. Without it the wave crosses the first view and leaves it as it was.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        overlay
      </code>
    </td>
    
    <td>
      Live controls above the wave. They are not bent or tinted, and they take clicks over both views.
    </td>
  </tr>
</tbody>
</table>

## Methods

<table>
<thead>
  <tr>
    <th>
      Method
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        play({ source?, point? })
      </code>
    </td>
    
    <td>
      Runs the wave to the revealed view, from a source and press point if given.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        reverse()
      </code>
    </td>
    
    <td>
      Folds the wave back to the first view.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        reset()
      </code>
    </td>
    
    <td>
      Jumps to the first view with no wave.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        prepare()
      </code>
    </td>
    
    <td>
      Takes the pictures of both views now, so the next wave starts at once.
    </td>
  </tr>
</tbody>
</table>

## How it works

The wave runs on a WebGL 2 canvas laid over the element only while it moves. Before a run, each view is drawn into a picture (computed styles inlined into an SVG, with the page's same-origin fonts embedded). The shader then bends, blurs and splits those pictures around the front. Images from another origin without CORS, closed shadow roots and pseudo-elements do not appear in the picture; they come back the moment the wave ends. Add `data-wave-skip` to leave an element out of the picture.

## Accessibility

- The hidden view is `inert` and `aria-hidden`, so focus and screen readers only reach the view that shows. The swap happens when the wave ends.
- Controls in `#overlay` keep focus and keyboard access through the whole run; announce state changes there (for example with `role="status"`).
- Under `prefers-reduced-motion: reduce`, or without WebGL 2, the views crossfade in 300 ms instead.
- The canvas is `aria-hidden` and takes no pointer events.
