@molecule/app-color-swatch-picker-vue

Feature · color-swatch-picker · App (browser) · v1.1.2 · Apache-2.0

Grid of colored circle swatches with single-select state, for tag colors / labels / themes

npm install @molecule/app-color-swatch-picker-vue

npm · Source on GitHub

How it works

@molecule/app-color-swatch-picker-vue is a ready-made color-swatch-picker feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { defineComponent, h, ref } from 'vue'
import { ColorSwatchPicker } from '@molecule/app-color-swatch-picker-vue'

const swatches = [
  { value: 'red', color: '#ef4444', label: 'Red' },
  { value: 'blue', color: '#3b82f6', label: 'Blue' },
  { value: 'green', color: '#22c55e', label: 'Green' },
]

export default defineComponent({
  setup() {
    const selected = ref('red')
    return () =>
      h(ColorSwatchPicker, {
        swatches,
        value: selected.value,
        onChange: (value: string) => (selected.value = value),
        ariaLabel: 'Tag color',
      })
  },
})

Works with: @molecule/app-ui, @molecule/app-ui-vue, @molecule/app-vue

Reference

Vue color-swatch picker.

Exports <ColorSwatchPicker> — grid of colored circles with single-select state. It is the Vue counterpart of @molecule/app-color-swatch-picker-react: same props (React names) and the same DOM for the same input. Apps switching framework change only the package name in their imports.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { defineComponent, h, ref } from 'vue'
import { ColorSwatchPicker } from '@molecule/app-color-swatch-picker-vue'

const swatches = [
  { value: 'red', color: '#ef4444', label: 'Red' },
  { value: 'blue', color: '#3b82f6', label: 'Blue' },
  { value: 'green', color: '#22c55e', label: 'Green' },
]

export default defineComponent({
  setup() {
    const selected = ref('red')
    return () =>
      h(ColorSwatchPicker, {
        swatches,
        value: selected.value,
        onChange: (value: string) => (selected.value = value),
        ariaLabel: 'Tag color',
      })
  },
})

Type

feature

Installation

npm install @molecule/app-color-swatch-picker-vue @molecule/app-ui @molecule/app-ui-vue @molecule/app-vue vue

API

Interfaces

ColorSwatch

A single color option in the swatch picker with its value, CSS color, and optional label.

interface ColorSwatch {
  /** Value stored in state (can be the raw color or a semantic id). */
  value: string
  /** CSS color (any valid color string). */
  color: string
  /** Optional label shown as tooltip / aria-label. */
  label?: string
}

ColorSwatchPickerProps

Props accepted by the ColorSwatchPicker component — the same props @molecule/app-color-swatch-picker-react accepts. preview is node-valued: pass it as a prop or as the same-named slot — both work.

interface ColorSwatchPickerProps {
  /** Swatch definitions. */
  swatches: ColorSwatch[]
  /** Currently selected swatch value. */
  value: string
  /** Called when a swatch is picked. */
  onChange: (value: string) => void
  /** Swatch diameter in pixels. Defaults to 28. */
  size?: number
  /** Gap between swatches. */
  gap?: 'xs' | 'sm' | 'md'
  /** Optional child rendered below (e.g. a live preview). */
  preview?: VNodeChild
  /** Extra classes. */
  className?: string
  /** `aria-label` for the group. */
  ariaLabel?: string
}

Constants

ColorSwatchPicker

Grid of colored circles with single-select state. Each swatch renders as a role="radio" button sized by size (default 28) and painted with its own color — the one inline style that is the caller's data, not a design token; a cm.button() fill would hide it.

const ColorSwatchPicker: DefineComponent<
  ExtractPropTypes<{
    swatches: { type: PropType<ColorSwatchPickerProps['swatches']>; required: true }
    value: { type: StringConstructor; required: true }
    onChange: PropType<ColorSwatchPickerProps['onChange']>
    size: { type: NumberConstructor; default: number }
    gap: { type: PropType<'xs' | 'sm' | 'md'>; default: string }
    preview: null
    className: StringConstructor
    ariaLabel: StringConstructor
  }>,
  () => VNode,
  {},
  {},
  {},
  ComponentOptionsMixin,
  ComponentOptionsMixin,
  {},
  string,
  PublicProps,
  Readonly<
    ExtractPropTypes<{
      swatches: { type: PropType<ColorSwatchPickerProps['swatches']>; required: true }
      value: { type: StringConstructor; required: true }
      onChange: PropType<ColorSwatchPickerProps['onChange']>
      size: { type: NumberConstructor; default: number }
      gap: { type: PropType<'xs' | 'sm' | 'md'>; default: string }
      preview: null
      className: StringConstructor
      ariaLabel: StringConstructor
    }>
  > &
    Readonly<{}>,
  { size: number; gap: 'xs' | 'sm' | 'md' },
  {},
  {},
  {},
  string,
  ComponentProvideOptions,
  true,
  {},
  any
>
  • props — Component props (see ColorSwatchPickerProps).

Returns: The swatch picker component.

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-ui ^1.1.0
  • @molecule/app-ui-vue ^1.1.0
  • @molecule/app-vue ^3.5.0 || ^4.0.0
  • vue ^3.4.0

Runtime Dependencies

  • @molecule/app-ui
  • @molecule/app-ui-vue
  • @molecule/app-vue
  • vue

All text (label per swatch, ariaLabel for the group) is consumer-provided — pass translated strings via t(); the component has no built-in copy. Selection is fully controlled: persist onChange(value) and re-render with the new value. Swatches render as role="radio" buttons sized by the size prop (default 28px) with the CSS color you provide. The node-valued preview prop arrives as a prop AND a same-named slot; both work.