# Color input

SvColorInput on its own: a swatch that opens an animated, portalled popover with a hex field, the native picker and a preset palette. Emits a hex string.

Uses `@svgrid/grid` (MIT). Category: Inputs. Live demo: https://svgrid.com/demos/304-color-input/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvColorInput on its own in Svelte 5: a swatch that opens an animated, portalled popover with a hex field, the native color picker and a preset palette, emitting a hex string through onChange. It is the SvGrid color cell editor, standalone.

SvColorInput on its own - a swatch that opens a portalled popover with a hex field, the native picker and a preset palette. Emits a hex string; the popover animates in. The SvGrid color cell editor, standalone.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### What value does it emit?

A hex string such as #ff6600. Typing in the hex field, choosing a palette swatch or using the native picker all report through the same onChange.

### Can I supply my own palette?

Yes. Pass a palette array of hex values and the popover shows those swatches instead of the default set.

### Why does the popover portal?

So it can open from inside a scrolling panel or a grid cell without being clipped, positioned next to the swatch and closed on outside click or Escape.

## Related documentation

- [SvColorInput](https://svgrid.com/docs/help/ui-components/sv-color-input.md)

## Related articles

- [SvGrid UI Components Tips and Tricks: Svelte 5 Buttons, Inputs, Tree, Forms](https://svgrid.com/blog/svgrid-ui-components-tips-and-tricks/) - Practical tips for the SvGrid UI component kit - SvButton states, a Cmd+K command palette, a virtualized searchable tree, schema-driven forms, a parsing date/time field, and the shared field contract - each with a code snippet.
- [Conditional Formatting - Color Cells by Their Value](https://svgrid.com/blog/conditional-formatting/) - Four rule types, one prop - add heatmaps, data bars, icon sets, and threshold highlights to any SvGrid column without custom cell renderers.

## Source code (304-color-input.svelte)

```svelte
<script lang="ts">
  /**
   * SvColorInput on its own - a swatch that opens a portalled popover with a hex
   * field, the native picker and a preset palette. Emits a hex string; the
   * popover animates in. The SvGrid color cell editor, standalone.
   */
  import { SvColorInput } from '@svgrid/grid'

  let brand = $state('#2563eb')
  let accent = $state('#f59e0b')
  let custom = $state('#10b981')
  const palette = ['#ef4444', '#f97316', '#eab308', '#22c55e', '#10b981', '#06b6d4', '#3b82f6', '#8b5cf6', '#ec4899', '#0f172a']
</script>

<div class="wrap">
  <header>
    <h2>Color input</h2>
    <p><code>SvColorInput</code> - swatch + hex field + native picker + preset palette in a portalled, animated popover.</p>
  </header>

  <div class="row">
    <label class="cell">Brand
      <SvColorInput value={brand} onChange={(v) => (brand = v)} />
      <span class="val">{brand}</span>
    </label>
    <label class="cell">Accent
      <SvColorInput value={accent} onChange={(v) => (accent = v)} />
      <span class="val">{accent}</span>
    </label>
    <label class="cell">Custom palette
      <SvColorInput value={custom} {palette} onChange={(v) => (custom = v)} />
      <span class="val">{custom}</span>
    </label>
  </div>

  <div class="preview" style:--a={brand} style:--b={accent} style:--c={custom}>
    <span>Live preview</span>
  </div>
</div>

<style>
  .wrap { padding: 20px; max-width: 600px; display: flex; flex-direction: column; gap: 20px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  code { background: var(--sg-row-hover-bg, #eef2ff); padding: 1px 5px; border-radius: 5px; font-size: 12px; }
  .row { display: flex; gap: 22px; flex-wrap: wrap; }
  .cell { display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 600; color: var(--sg-muted, #64748b); }
  .val { font-size: 12px; color: var(--sg-muted, #94a3b8); font-weight: 400; font-variant-numeric: tabular-nums; text-transform: lowercase; }
  .preview {
    height: 80px; border-radius: 12px; display: grid; place-items: center; color: #fff; font-weight: 700; letter-spacing: 0.02em;
    background: linear-gradient(120deg, var(--a), var(--b), var(--c));
  }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/304-color-input.svelte)
