{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"shatter-away","type":"registry:component","title":"Shatter away","description":"One call to discard: the element cracks into shards that kick outward and drop, bottom first.","author":"Hairline UI","dependencies":[],"files":[{"path":"registry/shatter-away.tsx","type":"registry:component","content":"// Hairline UI · shatter-away · MIT License\n// Copyright (c) 2026 Hairline UI · https://hairlineui.com/license#components\n\n\"use client\";\n\n/* ─────────────────────────────────────────────────────────\n * SHATTER AWAY: it cracks into shards and drops\n *\n * One call on any element: `await shatterAway(element)`. The\n * picture cracks into shards along a hidden grid; the bottom ones\n * go first, each shard kicked outward and then pulled down as one\n * piece. For rejecting and discarding: the takes you didn't keep.\n * `undoAway(element)` puts the shards back.\n *\n * Nothing about your app is taken over: no list, no toast, no\n * timer, no API. Pictures (an <img>, <canvas> or <video>, or an\n * element holding one) are drawn grain by grain from their real\n * pixels in a layer over the page while the element itself is\n * hidden in place; anything else, or a picture from another\n * origin without CORS, keeps its own element behind a moving\n * mask with grains in its own colours.\n * ───────────────────────────────────────────────────────── */\n\nimport { isAway, playAway, undoAway, type AwayOptions } from \"./image-grains\";\n\nexport type { AwayOptions };\nexport { isAway, undoAway };\n\n/** Removes the element. Resolves true once it has gone (it stays hidden in place until you remove it), or false if undoAway brought it back first. */\nexport function shatterAway(element: HTMLElement | null | undefined, options?: AwayOptions): Promise<boolean> {\n  return playAway(element, \"shatter\", options);\n}\n"},{"path":"registry/image-grains.ts","type":"registry:component","content":"// Hairline UI · shatter-away · MIT License\n// Copyright (c) 2026 Hairline UI · https://hairlineui.com/license#components\n\n/* ─────────────────────────────────────────────────────────\n * IMAGE GRAINS: the engine under sand-away, burn-away,\n * shatter-away, drain-away, grain-reveal and variant-pick.\n *\n * An element is cut into a grid of grains that carry its real\n * colours. Each grain lets go when a front reaches it (the front\n * is what tells the four styles apart: wind from one side, a burn\n * from a corner, shards from the bottom, a drain into the middle)\n * and then moves on its own path. Time runs both ways, so every\n * removal can be played back to undo it.\n *\n * Pictures (an <img>, <canvas> or <video>, or an element holding\n * one) are read pixel by pixel. Anything else, or a picture from\n * another origin without CORS, keeps its own element on screen\n * behind a moving mask, and its grains take the element's own\n * colours: nothing fails silently.\n * ───────────────────────────────────────────────────────── */\n\nexport type GrainStyle = \"sand\" | \"burn\" | \"shatter\" | \"drain\";\n\nexport interface AwayOptions {\n  /** Milliseconds for the whole removal. Defaults by style, and shorter on rows. */\n  duration?: number;\n}\n\n/* ── noise ─────────────────────────────────────────────── */\n\nexport const hash2 = (x: number, y: number) => {\n  const n = Math.sin(x * 127.1 + y * 311.7) * 43758.5453123;\n  return n - Math.floor(n);\n};\n\nexport function noise2(x: number, y: number) {\n  const xi = Math.floor(x);\n  const yi = Math.floor(y);\n  const xf = x - xi;\n  const yf = y - yi;\n  const u = xf * xf * (3 - 2 * xf);\n  const v = yf * yf * (3 - 2 * yf);\n  const a = hash2(xi, yi);\n  const b = hash2(xi + 1, yi);\n  const c = hash2(xi, yi + 1);\n  const d = hash2(xi + 1, yi + 1);\n  return a + (b - a) * u + (c + (d - c) * u - (a + (b - a) * u)) * v;\n}\n\nexport const clamp01 = (n: number) => (n < 0 ? 0 : n > 1 ? 1 : n);\nconst clamp = (n: number, lo: number, hi: number) => (n < lo ? lo : n > hi ? hi : n);\n\nexport function reducedMotion() {\n  return typeof window !== \"undefined\" && !!window.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches;\n}\n\n/* ── where each grain lets go, and how it moves ────────── */\n\n/** When (0 to 0.58 of the run) the grain at nx, ny lets go. */\nexport function releaseAt(style: GrainStyle | \"gather\", nx: number, ny: number, n: number) {\n  const centre = Math.min(1, Math.hypot(nx - 0.5, ny - 0.5) * 1.42);\n  let raw: number;\n  if (style === \"sand\") raw = nx * 0.58 + ny * 0.1 + n * 0.24;\n  else if (style === \"burn\") raw = (1 - nx) * 0.42 + (1 - ny) * 0.34 + n * 0.22;\n  else if (style === \"drain\") raw = (1 - centre) * 0.54 + n * 0.24;\n  else if (style === \"shatter\") raw = hash2(Math.floor(nx * 7), Math.floor(ny * 9)) * 0.42 + (1 - ny) * 0.28 + n * 0.14;\n  else raw = centre * 0.38 + n * 0.32;\n  return (clamp(raw, 0, 0.78) / 0.78) * 0.58;\n}\n\nexport interface Motion {\n  dx: number;\n  dy: number;\n  alpha: number;\n  scale: number;\n  heat: number;\n}\n\nconst MOTION: Motion = { dx: 0, dy: 0, alpha: 1, scale: 1, heat: 0 };\n\n/** Where a grain is at `life` (0 just let go, 1 gone), written into one shared object (no allocation per grain). */\nexport function move(style: GrainStyle | \"gather\", life: number, nx: number, ny: number, a: number, b: number): Motion {\n  const t = clamp01(life);\n  const out = MOTION;\n  out.heat = 0;\n  if (style === \"sand\") {\n    const ease = 1 - Math.pow(1 - t, 2.4);\n    // The front crosses left to right; grains blow back behind it, never over what is still whole.\n    out.dx = (-18 - a * 46) * ease + Math.sin(t * 5 + b * 6.28) * 7 * t;\n    out.dy = (-24 - b * 60) * Math.pow(t, 1.35);\n    out.alpha = t < 0.14 ? 1 : Math.max(0, 1 - Math.pow((t - 0.14) / 0.86, 1.25));\n    out.scale = 1 - t * 0.42;\n  } else if (style === \"burn\") {\n    const ease = 1 - Math.pow(1 - t, 2.4);\n    out.dx = (a - 0.5) * 34 * ease + Math.sin(t * 7 + b * 10) * 7 * t;\n    out.dy = (-34 - b * 84) * Math.pow(t, 1.45);\n    out.heat = t < 0.5 ? Math.sin((t / 0.5) * Math.PI) : 0;\n    out.alpha = Math.max(0, 1 - Math.pow(t, 1.35));\n    out.scale = 1 + out.heat * 0.3 - t * 0.5;\n  } else if (style === \"shatter\") {\n    const ease = 1 - Math.pow(1 - t, 2.4);\n    out.dx = (nx - 0.5) * 64 * ease + (a - 0.5) * 26 * t;\n    out.dy = -12 * ease + (90 + b * 80) * t * t;\n    out.alpha = Math.max(0, 1 - Math.pow(t, 1.6));\n    out.scale = 1;\n  } else if (style === \"drain\") {\n    const pull = t * t * 0.97;\n    const relX = (0.5 - nx) * 2;\n    const relY = (0.5 - ny) * 2;\n    const angle = t * t * (2.4 + a * 1.2);\n    const cos = Math.cos(angle) - 1;\n    const sin = Math.sin(angle);\n    const restX = relX * (1 - pull);\n    const restY = relY * (1 - pull);\n    // relX/relY are in half-sizes; the caller scales dx/dy by the element's half width and height.\n    out.dx = relX * pull + (restX * cos - restY * sin) * 0.35;\n    out.dy = relY * pull + (restX * sin + restY * cos) * 0.35;\n    out.alpha = Math.max(0, 1 - Math.pow(t, 1.7));\n    out.scale = Math.max(0.25, 1 - t * 0.68);\n  } else {\n    // gather: from scattered (t = 0) to home (t = 1), inside the frame.\n    const inv = 1 - t;\n    const spread = Math.pow(inv, 2.15);\n    const angle = a * Math.PI * 2 + inv * 1.9;\n    const radius = (18 + b * 44) * spread;\n    out.dx = Math.cos(angle) * radius;\n    out.dy = Math.sin(angle) * radius;\n    out.alpha = clamp01(0.24 + t * 0.82);\n    out.scale = 0.68 + t * 0.32;\n  }\n  return out;\n}\n\n/* ── reading an element's pixels ───────────────────────── */\n\ntype Media = HTMLImageElement | HTMLCanvasElement | HTMLVideoElement;\n\nexport function findMedia(el: Element): Media | null {\n  if (el instanceof HTMLImageElement || el instanceof HTMLCanvasElement || el instanceof HTMLVideoElement) return el;\n  return el.querySelector<Media>(\"img, canvas, video\");\n}\n\nconst naturalSize = (m: Media) =>\n  m instanceof HTMLImageElement ? [m.naturalWidth, m.naturalHeight] : m instanceof HTMLVideoElement ? [m.videoWidth, m.videoHeight] : [m.width, m.height];\n\n/** Draws a picture into a box the way object-fit (cover or contain) and object-position would. */\nexport function drawFitted(ctx: CanvasRenderingContext2D, m: Media | CanvasImageSource, iw: number, ih: number, x: number, y: number, w: number, h: number, fit = \"cover\") {\n  if (!iw || !ih) return;\n  const scale = fit === \"contain\" ? Math.min(w / iw, h / ih) : fit === \"fill\" ? 0 : Math.max(w / iw, h / ih);\n  if (!scale) {\n    ctx.drawImage(m as CanvasImageSource, x, y, w, h);\n    return;\n  }\n  const dw = iw * scale;\n  const dh = ih * scale;\n  ctx.drawImage(m as CanvasImageSource, x + (w - dw) / 2, y + (h - dh) / 2, dw, dh);\n}\n\nfunction roundedPath(ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) {\n  const rr = Math.min(r, w / 2, h / 2);\n  ctx.beginPath();\n  ctx.moveTo(x + rr, y);\n  ctx.arcTo(x + w, y, x + w, y + h, rr);\n  ctx.arcTo(x + w, y + h, x, y + h, rr);\n  ctx.arcTo(x, y + h, x, y, rr);\n  ctx.arcTo(x, y, x + w, y, rr);\n  ctx.closePath();\n}\n\nfunction parseColour(value: string): [number, number, number, number] | null {\n  const m = value.match(/rgba?\\(([^)]+)\\)/);\n  if (!m) return null;\n  const parts = m[1].split(/[\\s,/]+/).filter(Boolean).map(Number);\n  if (parts.length < 3 || parts.some((p) => Number.isNaN(p))) return null;\n  return [parts[0], parts[1], parts[2], parts[3] ?? 1];\n}\n\n/** Turns any CSS colour (oklch, a variable…) into rgb through the browser. */\nfunction resolveColour(value: string): [number, number, number, number] | null {\n  const direct = parseColour(value);\n  if (direct) return direct;\n  const probe = document.createElement(\"canvas\").getContext(\"2d\");\n  if (!probe) return null;\n  probe.fillStyle = \"#000\";\n  probe.fillStyle = value;\n  probe.fillRect(0, 0, 1, 1);\n  const d = probe.getImageData(0, 0, 1, 1).data;\n  return [d[0], d[1], d[2], d[3] / 255];\n}\n\n/** The colours an element shows without a picture: its own background (or the first one behind it), its text and its edge. */\nexport function elementPalette(el: Element): [number, number, number][] {\n  let bg: [number, number, number, number] | null = null;\n  for (let node: Element | null = el; node && !bg; node = node.parentElement) {\n    const c = resolveColour(getComputedStyle(node).backgroundColor);\n    if (c && c[3] > 0.5) bg = c;\n  }\n  const style = getComputedStyle(el);\n  const fg = resolveColour(style.color);\n  const edge = resolveColour(style.borderTopColor);\n  const base: [number, number, number] = bg ? [bg[0], bg[1], bg[2]] : [128, 128, 128];\n  const ink: [number, number, number] = fg ? [fg[0], fg[1], fg[2]] : [60, 60, 60];\n  const line: [number, number, number] = edge ? [edge[0], edge[1], edge[2]] : ink;\n  return [base, base, base, ink, line];\n}\n\nexport interface Snapshot {\n  w: number;\n  h: number;\n  radius: number;\n  /** The element's picture at device resolution, rounded like the element; null when it couldn't be read. */\n  sharp: HTMLCanvasElement | null;\n  /** Colours per grain cell, row by row, or null to use the palette. */\n  cells: Uint8ClampedArray | null;\n  palette: [number, number, number][];\n  cols: number;\n  rows: number;\n  step: number;\n}\n\nconst MAX_GRAINS = 7000;\n\nexport function gridFor(w: number, h: number, minStep = 3) {\n  const step = Math.max(minStep, Math.sqrt((w * h) / MAX_GRAINS));\n  return { step, cols: Math.max(4, Math.round(w / step)), rows: Math.max(2, Math.round(h / step)) };\n}\n\n/** Reads an element into grains: its picture's real colours where it can, its own colours where it can't. */\nexport function snapshot(el: HTMLElement, dpr = Math.min(window.devicePixelRatio || 1, 2)): Snapshot {\n  const rect = el.getBoundingClientRect();\n  const w = Math.max(1, rect.width);\n  const h = Math.max(1, rect.height);\n  const radius = parseFloat(getComputedStyle(el).borderTopLeftRadius) || 0;\n  const { step, cols, rows } = gridFor(w, h, h < 90 ? 2.5 : 3);\n  const palette = elementPalette(el);\n  const media = findMedia(el);\n  const shot: Snapshot = { w, h, radius, sharp: null, cells: null, palette, cols, rows, step };\n  if (!media) return shot;\n  // A thumbnail inside a row isn't the row: only a picture that fills most of the element is drawn from its pixels.\n  const area = media.getBoundingClientRect();\n  if (area.width * area.height < w * h * 0.6) return shot;\n  const [iw, ih] = naturalSize(media);\n  if (!iw || !ih) return shot;\n\n  const sharp = document.createElement(\"canvas\");\n  sharp.width = Math.round(w * dpr);\n  sharp.height = Math.round(h * dpr);\n  const ctx = sharp.getContext(\"2d\");\n  if (!ctx) return shot;\n  ctx.scale(dpr, dpr);\n  roundedPath(ctx, 0, 0, w, h, radius);\n  ctx.clip();\n  const [r, g, b] = palette[0];\n  ctx.fillStyle = `rgb(${r},${g},${b})`;\n  ctx.fillRect(0, 0, w, h);\n  const mr = media.getBoundingClientRect();\n  const fit = getComputedStyle(media).objectFit || \"fill\";\n  drawFitted(ctx, media, iw, ih, mr.left - rect.left, mr.top - rect.top, mr.width, mr.height, media instanceof HTMLCanvasElement ? \"fill\" : fit);\n\n  const small = document.createElement(\"canvas\");\n  small.width = cols;\n  small.height = rows;\n  const sctx = small.getContext(\"2d\", { willReadFrequently: true });\n  if (!sctx) return shot;\n  sctx.drawImage(sharp, 0, 0, cols, rows);\n  try {\n    shot.cells = sctx.getImageData(0, 0, cols, rows).data;\n    shot.sharp = sharp;\n  } catch {\n    // Another origin without CORS: the canvas is tainted. Fall back to the element's own colours and its own pixels on screen.\n  }\n  return shot;\n}\n\n/* ── grains ────────────────────────────────────────────── */\n\nexport interface Grain {\n  x: number;\n  y: number;\n  nx: number;\n  ny: number;\n  gx: number;\n  gy: number;\n  release: number;\n  a: number;\n  b: number;\n  /** fillStyle strings: the grain's colour, then hotter steps for a burn. */\n  colours: string[];\n}\n\nconst HOT: [number, number, number] = [255, 168, 62];\n\nexport function makeGrains(shot: Snapshot, style: GrainStyle | \"gather\"): Grain[] {\n  const { cols, rows, w, h, radius, cells, palette } = shot;\n  const cw = w / cols;\n  const ch = h / rows;\n  const out: Grain[] = [];\n  for (let gy = 0; gy < rows; gy++) {\n    for (let gx = 0; gx < cols; gx++) {\n      const x = (gx + 0.5) * cw;\n      const y = (gy + 0.5) * ch;\n      if (radius > 0 && outsideCorner(x, y, w, h, radius)) continue;\n      const nx = cols > 1 ? gx / (cols - 1) : 0.5;\n      const ny = rows > 1 ? gy / (rows - 1) : 0.5;\n      let r: number;\n      let g: number;\n      let b: number;\n      if (cells) {\n        const i = (gy * cols + gx) * 4;\n        r = cells[i];\n        g = cells[i + 1];\n        b = cells[i + 2];\n      } else {\n        [r, g, b] = palette[Math.floor(hash2(gx * 3.7, gy * 5.1) * palette.length)];\n      }\n      const colours = [`rgb(${r},${g},${b})`];\n      if (style === \"burn\") {\n        for (let k = 1; k <= 4; k++) {\n          const t = k / 4;\n          colours.push(`rgb(${Math.round(r + (HOT[0] - r) * t)},${Math.round(g + (HOT[1] - g) * t)},${Math.round(b * (1 - t * 0.7) + 40 * t)})`);\n        }\n      }\n      // Shards move as one: every grain in a shard shares its seeds.\n      const sa = style === \"shatter\" ? hash2(Math.floor(nx * 7) * 1.7 + 3, Math.floor(ny * 9) * 2.3 + 11) : hash2(gx * 1.7 + 3, gy * 2.3 + 11);\n      const sb = style === \"shatter\" ? hash2(Math.floor(nx * 7) * 3.1 + 19, Math.floor(ny * 9) * 1.3 + 5) : hash2(gx * 3.1 + 19, gy * 1.3 + 5);\n      out.push({ x, y, nx, ny, gx, gy, release: releaseAt(style, nx, ny, noise2(nx * 6.5, ny * 8)), a: sa, b: sb, colours });\n    }\n  }\n  return out;\n}\n\nfunction outsideCorner(x: number, y: number, w: number, h: number, r: number) {\n  const cx = x < r ? r : x > w - r ? w - r : x;\n  const cy = y < r ? r : y > h - r ? h - r : y;\n  if (cx === x || cy === y) return false;\n  return Math.hypot(x - cx, y - cy) > r;\n}\n\n/** Draws every grain that has let go at time t (0 to 1), offset by ox, oy. */\nexport function drawGrains(ctx: CanvasRenderingContext2D, grains: Grain[], style: GrainStyle | \"gather\", t: number, shot: Snapshot, ox: number, oy: number, span = 0.42) {\n  // Shards are solid pieces (grains meet, the cracks open as shards part); everything else is loose grain with a hairline gap.\n  const size = Math.max(1.2, (shot.w / shot.cols) * (style === \"shatter\" ? 1.06 : 0.92));\n  const hw = shot.w / 2;\n  const hh = shot.h / 2;\n  for (let i = 0; i < grains.length; i++) {\n    const g = grains[i];\n    if (t < g.release) continue;\n    const life = (t - g.release) / span;\n    if (life >= 1) continue;\n    const m = move(style, life, g.nx, g.ny, g.a, g.b);\n    if (m.alpha <= 0.02) continue;\n    const dx = style === \"drain\" ? m.dx * hw : m.dx;\n    const dy = style === \"drain\" ? m.dy * hh : m.dy;\n    const s = size * m.scale;\n    ctx.globalAlpha = m.alpha;\n    ctx.fillStyle = m.heat > 0.08 ? g.colours[Math.min(4, Math.max(1, Math.round(m.heat * 4)))] ?? g.colours[0] : g.colours[0];\n    ctx.fillRect(ox + g.x + dx - s / 2, oy + g.y + dy - s / 2, s, s);\n  }\n  ctx.globalAlpha = 1;\n}\n\n/** Draws what is still whole at time t: the sharp picture through a mask of the grains that haven't let go. */\nexport function drawRemaining(ctx: CanvasRenderingContext2D, shot: Snapshot, grains: Grain[], t: number, ox: number, oy: number, scratch: { mask: HTMLCanvasElement; img: ImageData; out: HTMLCanvasElement }) {\n  if (!shot.sharp) return;\n  const d = scratch.img.data;\n  d.fill(0);\n  let any = false;\n  for (let i = 0; i < grains.length; i++) {\n    const g = grains[i];\n    if (t < g.release) {\n      const k = (g.gy * shot.cols + g.gx) * 4;\n      d[k] = d[k + 1] = d[k + 2] = d[k + 3] = 255;\n      any = true;\n    }\n  }\n  if (!any) return;\n  const mctx = scratch.mask.getContext(\"2d\");\n  const octx = scratch.out.getContext(\"2d\");\n  if (!mctx || !octx) return;\n  mctx.putImageData(scratch.img, 0, 0);\n  octx.globalCompositeOperation = \"copy\";\n  octx.drawImage(shot.sharp, 0, 0);\n  octx.globalCompositeOperation = \"destination-in\";\n  octx.imageSmoothingEnabled = false;\n  octx.drawImage(scratch.mask, 0, 0, scratch.out.width, scratch.out.height);\n  octx.globalCompositeOperation = \"source-over\";\n  ctx.drawImage(scratch.out, ox, oy, shot.w, shot.h);\n}\n\nexport function makeScratch(shot: Snapshot) {\n  const mask = document.createElement(\"canvas\");\n  mask.width = shot.cols;\n  mask.height = shot.rows;\n  const out = document.createElement(\"canvas\");\n  out.width = shot.sharp?.width ?? 1;\n  out.height = shot.sharp?.height ?? 1;\n  const img = new ImageData(shot.cols, shot.rows);\n  return { mask, img, out };\n}\n\n/* ── removals: one call on any element ─────────────────── */\n\nconst DURATION: Record<GrainStyle, number> = { sand: 1000, burn: 1250, shatter: 1050, drain: 1000 };\nconst PAD = 150;\nconst BACK_SPEED = 1.3;\n\ninterface Run {\n  el: HTMLElement;\n  style: GrainStyle;\n  shot: Snapshot;\n  grains: Grain[];\n  scratch: ReturnType<typeof makeScratch>;\n  canvas: HTMLCanvasElement;\n  dpr: number;\n  t: number;\n  dir: 1 | -1;\n  duration: number;\n  last: number;\n  frame: number;\n  visibility: string;\n  mask: string;\n  waiting: { dir: 1 | -1; resolve: (done: boolean) => void }[];\n}\n\nconst runs = new WeakMap<HTMLElement, Run>();\n\nlet layer: HTMLDivElement | null = null;\nfunction topLayer() {\n  if (layer && layer.isConnected) return layer;\n  layer = document.createElement(\"div\");\n  layer.setAttribute(\"aria-hidden\", \"true\");\n  layer.style.cssText = \"position:fixed;inset:0;pointer-events:none;z-index:2147483000;contain:layout style;overflow:clip;\";\n  document.body.appendChild(layer);\n  return layer;\n}\n\n/** The mask that hides an element up to the front at time t, for elements drawn by the page itself (rows, cards, pictures from another origin). */\nfunction frontMask(style: GrainStyle, t: number) {\n  const p = clamp01(t / 0.58);\n  const soft = 9;\n  if (style === \"sand\") {\n    const x = p * (100 + soft) - soft;\n    return `linear-gradient(90deg, transparent ${x}%, #000 ${x + soft}%)`;\n  }\n  if (style === \"burn\") {\n    const x = p * (100 + soft) - soft;\n    return `linear-gradient(315deg, transparent ${x}%, #000 ${x + soft}%)`;\n  }\n  if (style === \"shatter\") {\n    const x = p * (100 + soft) - soft;\n    return `linear-gradient(0deg, transparent ${x}%, #000 ${x + soft}%)`;\n  }\n  const r = (1 - p) * (100 + soft);\n  return `radial-gradient(farthest-corner, #000 ${Math.max(0, r - soft)}%, transparent ${r}%)`;\n}\n\nfunction paint(run: Run) {\n  const { el, canvas, shot, dpr } = run;\n  // Follow the element through scrolling and layout; if the app has already removed it, stay where it was.\n  if (el.isConnected) {\n    const rect = el.getBoundingClientRect();\n    canvas.style.transform = `translate(${rect.left - PAD}px, ${rect.top - PAD}px)`;\n  }\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return;\n  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n  ctx.clearRect(0, 0, shot.w + PAD * 2, shot.h + PAD * 2);\n  if (shot.sharp) {\n    drawRemaining(ctx, shot, run.grains, run.t, PAD, PAD, run.scratch);\n  } else {\n    const m = run.t <= 0 ? run.mask : frontMask(run.style, run.t);\n    el.style.setProperty(\"mask-image\", m);\n    el.style.setProperty(\"-webkit-mask-image\", m);\n  }\n  drawGrains(ctx, run.grains, run.style, run.t, shot, PAD, PAD);\n}\n\nfunction settle(run: Run, done: boolean, dir: 1 | -1) {\n  const ready = run.waiting.filter((w) => w.dir === dir);\n  run.waiting = run.waiting.filter((w) => w.dir !== dir);\n  ready.forEach((w) => w.resolve(done));\n}\n\nfunction tick(run: Run, now: number) {\n  const dt = Math.min(48, now - run.last);\n  run.last = now;\n  run.t += (run.dir * dt * (run.dir < 0 ? BACK_SPEED : 1)) / run.duration;\n  if (run.dir > 0 && run.t >= 1) {\n    run.t = 1;\n    paint(run);\n    run.canvas.remove();\n    run.frame = 0;\n    settle(run, true, 1);\n    return;\n  }\n  if (run.dir < 0 && run.t <= 0) {\n    run.t = 0;\n    run.canvas.remove();\n    run.el.style.visibility = run.visibility;\n    run.el.style.setProperty(\"mask-image\", run.mask);\n    run.el.style.setProperty(\"-webkit-mask-image\", run.mask);\n    runs.delete(run.el);\n    run.frame = 0;\n    settle(run, true, -1);\n    return;\n  }\n  paint(run);\n  run.frame = requestAnimationFrame((n) => tick(run, n));\n}\n\nfunction start(run: Run) {\n  if (!run.canvas.isConnected) topLayer().appendChild(run.canvas);\n  if (run.frame) return;\n  run.last = performance.now();\n  run.frame = requestAnimationFrame((n) => tick(run, n));\n}\n\nfunction fade(el: HTMLElement, out: boolean) {\n  const anim = el.animate([{ opacity: out ? 1 : 0 }, { opacity: out ? 0 : 1 }], { duration: 160, easing: \"ease-out\", fill: \"forwards\" });\n  return anim.finished.then(() => {\n    if (!out) anim.cancel();\n    return true;\n  });\n}\n\nconst faded = new WeakSet<HTMLElement>();\n\n/**\n * Plays a removal on any element and resolves true once it has gone (it stays hidden in the page until you remove it\n * from your state), or false if undoAway turned it back first.\n */\nexport function playAway(el: HTMLElement | null | undefined, style: GrainStyle, options: AwayOptions = {}): Promise<boolean> {\n  if (!el) return Promise.resolve(false);\n  if (reducedMotion()) {\n    faded.add(el);\n    return fade(el, true);\n  }\n  let run = runs.get(el);\n  if (!run) {\n    const shot = snapshot(el);\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n    const canvas = document.createElement(\"canvas\");\n    canvas.width = Math.round((shot.w + PAD * 2) * dpr);\n    canvas.height = Math.round((shot.h + PAD * 2) * dpr);\n    canvas.style.cssText = `position:absolute;left:0;top:0;width:${shot.w + PAD * 2}px;height:${shot.h + PAD * 2}px;will-change:transform;`;\n    const base = DURATION[style] * (shot.h < 90 ? 0.6 : 1);\n    run = {\n      el,\n      style,\n      shot,\n      grains: makeGrains(shot, style),\n      scratch: makeScratch(shot),\n      canvas,\n      dpr,\n      t: 0,\n      dir: 1,\n      duration: options.duration ?? base,\n      last: 0,\n      frame: 0,\n      visibility: el.style.visibility,\n      mask: el.style.getPropertyValue(\"mask-image\"),\n      waiting: [],\n    };\n    runs.set(el, run);\n    topLayer().appendChild(canvas);\n    paint(run);\n    // Drawn pictures take over from the element in the same frame, so nothing blinks.\n    if (shot.sharp) el.style.visibility = \"hidden\";\n  } else if (run.dir === 1 && run.t >= 1) {\n    return Promise.resolve(true);\n  }\n  run.dir = 1;\n  settle(run, false, -1);\n  const r = run;\n  const done = new Promise<boolean>((resolve) => r.waiting.push({ dir: 1, resolve }));\n  start(run);\n  return done;\n}\n\n/** Plays back whatever removal ran on the element, from wherever it is, and shows it again. Resolves true once it's back. */\nexport function undoAway(el: HTMLElement | null | undefined): Promise<boolean> {\n  if (!el) return Promise.resolve(false);\n  if (faded.has(el)) {\n    faded.delete(el);\n    return fade(el, false);\n  }\n  const run = runs.get(el);\n  if (!run) return Promise.resolve(false);\n  run.dir = -1;\n  settle(run, false, 1);\n  const done = new Promise<boolean>((resolve) => run.waiting.push({ dir: -1, resolve }));\n  start(run);\n  return done;\n}\n\n/** True while the element has been removed (or is being removed) and not brought back. */\nexport function isAway(el: HTMLElement | null | undefined) {\n  if (!el) return false;\n  if (faded.has(el)) return true;\n  const run = runs.get(el);\n  return !!run && run.dir === 1;\n}\n"}],"css":{},"docs":"Docs and live preview: https://hairlineui.com/ui/shatter-away. MIT licensed: https://hairlineui.com/license#components","meta":{"license":"MIT"}}