Skip to content

ColorSwatch

A color preview element that displays a color with a checkerboard background for visualizing alpha transparency.

Preview

Source code
vue
<script setup lang="ts">
import { shallowRef } from "vue";
import { Color } from "internationalized-color";
import "internationalized-color/css";
import { ColorSwatchRoot } from "@urcolor/vue";
import { Check } from "lucide-vue-next";

const colors = [
  Color.parse("hsl(210, 80%, 50%)")!,
  Color.parse("hsl(350, 90%, 60%)")!,
  Color.parse("hsl(120, 60%, 45%)")!,
  Color.parse("hsla(45, 100%, 55%, 0.5)")!,
];

const selected = shallowRef(colors[0]);
</script>

<template>
  <div class="flex items-center gap-3">
    <ColorSwatchRoot
      v-for="(color, i) in colors"
      :key="i"
      :model-value="color"
      alpha
      class="flex size-10 cursor-pointer items-center justify-center rounded-lg"
      @click="selected = color"
    >
      <Check
        class="
          size-5 text-white drop-shadow-[0_1px_2px_rgba(0,0,0,0.5)]
          transition-opacity duration-150
        "
        :class="selected === color ? 'opacity-100' : 'opacity-0'"
      />
    </ColorSwatchRoot>
  </div>
</template>

Anatomy

vue
<template>
  <ColorSwatchRoot />
</template>

Examples

Basic

A set of color swatches, including one with alpha transparency.

Source code
vue
<script setup lang="ts">
import { shallowRef } from "vue";
import { Color } from "internationalized-color";
import "internationalized-color/css";
import { ColorSwatchRoot } from "@urcolor/vue";
import { Check } from "lucide-vue-next";

const colors = [
  Color.parse("hsl(210, 80%, 50%)")!,
  Color.parse("hsl(350, 90%, 60%)")!,
  Color.parse("hsl(120, 60%, 45%)")!,
  Color.parse("hsla(45, 100%, 55%, 0.5)")!,
];

const selected = shallowRef(colors[0]);
</script>

<template>
  <div class="flex items-center gap-3">
    <ColorSwatchRoot
      v-for="(color, i) in colors"
      :key="i"
      :model-value="color"
      alpha
      class="flex size-10 cursor-pointer items-center justify-center rounded-lg"
      @click="selected = color"
    >
      <Check
        class="
          size-5 text-white drop-shadow-[0_1px_2px_rgba(0,0,0,0.5)]
          transition-opacity duration-150
        "
        :class="selected === color ? 'opacity-100' : 'opacity-0'"
      />
    </ColorSwatchRoot>
  </div>
</template>

API Reference

ColorSwatchRoot

Renders a color preview with an automatic checkerboard background.

PropTypeDefaultDescription
modelValueColor | string | nullThe color value to display.
alphabooleanfalseWhen true, reflects the color's alpha channel. When false, displays the color as fully opaque.
checkerSizenumber16The checkerboard tile size in pixels.

CSS Variables

The component exposes CSS custom properties on the root element for advanced styling:

VariableDescription
--swatch-colorThe resolved CSS color string (with or without alpha based on the alpha prop).
--swatch-color-opaqueThe color at full opacity.
--swatch-alphaThe color's alpha value (0–1).
--swatch-checkerboardThe checkerboard background gradient.

Accessibility

ColorSwatch is a purely visual element that displays a color preview.

ARIA Labels

AttributeDescription
role="img"Identifies the swatch as a presentational image for screen readers.
aria-labelDescribes the displayed color value for assistive technologies.