@molecule/app-color-swatch-picker-angular
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-angularHow it works
@molecule/app-color-swatch-picker-angular 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 { Component, signal } from '@angular/core'
import { ColorSwatchPicker } from '@molecule/app-color-swatch-picker-angular'
@Component({
selector: 'app-tag-color',
standalone: true,
imports: [ColorSwatchPicker],
template: `
<div
mol-color-swatch-picker
[swatches]="swatches"
[value]="selected()"
[onChange]="pick"
[ariaLabel]="'Tag color'"
>
<div slot="preview">Live preview</div>
</div>
`,
})
export class TagColorPicker {
swatches = [
{ value: 'red', color: '#ef4444', label: 'Red' },
{ value: 'blue', color: '#3b82f6', label: 'Blue' },
{ value: 'green', color: '#22c55e', label: 'Green' },
]
selected = signal('red')
pick = (value: string): void => this.selected.set(value)
}Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular
Reference
Angular color-swatch picker — the Angular counterpart of
@molecule/app-color-swatch-picker-react, with the same props (React
names) and the same DOM for the same input.
Exports <ColorSwatchPicker> — grid of colored circles with single-select
state, for tag colors, label colors, theme accent swatches, etc. 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.tsJSDoc, not this file.
Quick Start
import { Component, signal } from '@angular/core'
import { ColorSwatchPicker } from '@molecule/app-color-swatch-picker-angular'
@Component({
selector: 'app-tag-color',
standalone: true,
imports: [ColorSwatchPicker],
template: `
<div
mol-color-swatch-picker
[swatches]="swatches"
[value]="selected()"
[onChange]="pick"
[ariaLabel]="'Tag color'"
>
<div slot="preview">Live preview</div>
</div>
`,
})
export class TagColorPicker {
swatches = [
{ value: 'red', color: '#ef4444', label: 'Red' },
{ value: 'blue', color: '#3b82f6', label: 'Blue' },
{ value: 'green', color: '#22c55e', label: 'Green' },
]
selected = signal('red')
pick = (value: string): void => this.selected.set(value)
}
Type
feature
Installation
npm install @molecule/app-color-swatch-picker-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular
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 ColorSwatchPicker — the same props
@molecule/app-color-swatch-picker-react accepts. The node prop
(preview) is projected content in Angular (an input cannot carry a
node), so it is typed unknown: pass it in the host template, not as a
value.
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), projected as `[slot=preview]` content. */
preview?: unknown
/** Extra classes. */
className?: string
/** `aria-label` for the group. */
ariaLabel?: string
}
Classes
ColorSwatchPicker
ColorSwatchPicker component.
Grid of colored circles with single-select state — the Angular counterpart
of @molecule/app-color-swatch-picker-react's ColorSwatchPicker: the
same props (React names), the same ClassMap tokens, and the same DOM for
the same input. Used for tag colors, label colors, theme accent swatches,
etc. The host IS the outer <div> (div[mol-color-swatch-picker]),
matching React's root exactly; the node prop preview is projected as
[slot=preview] content, rendered below the group like React's preview
element.
@Component({
selector: 'div[mol-color-swatch-picker]',
standalone: true,
inputs: ['swatches', 'value', 'onChange', 'size', 'gap', 'className', 'ariaLabel'],
host: {
'[class]': 'classes()',
},
template: `<div role="radiogroup" [attr.aria-label]="ariaLabel ?? null" [class]="groupClasses()">
@for (swatch of swatches; track swatch.value) {
<button
type="button"
role="radio"
[attr.aria-checked]="isSelected(swatch) ? 'true' : 'false'"
[attr.aria-label]="swatchLabel(swatch)"
[attr.data-mol-id]="'color-swatch-' + swatch.value"
[attr.data-selected]="isSelected(swatch) ? 'true' : 'false'"
(click)="pick(swatch)"
[class]="buttonClasses()"
[style]="buttonStyle(swatch)"
></button>
}
</div>
<ng-content select="[slot=preview]"></ng-content>`,
})
export class ColorSwatchPicker {
/** Swatch definitions. */
swatches: ColorSwatch[] = []
/** Currently selected swatch value. */
value?: string
/**
* Called when a swatch is picked (React's `onChange`) — the component only
* calls it on a click, so it is a plain input.
*/
onChange?: (value: string) => void
/** Swatch diameter in pixels (React's `size` default 28, applied at read time). */
size?: number
/** Gap between swatches (React's `gap` default `'sm'`, applied at read time). */
gap?: 'xs' | 'sm' | 'md'
/** Extra classes. */
className?: string
/** `aria-label` for the group. */
ariaLabel?: string
/**
* The current class map.
* @returns The UIClassMap.
*/
cm(): UIClassMap {
return getClassMap()
}
/**
* The wrapper class list (React's `cm.cn(cm.stack(2), className)`).
* @returns The class string.
*/
classes(): string {
return this.cm().cn(this.cm().stack(2), this.className)
}
/**
* The group class list (React's
* `cm.flex({ align: 'center', gap, wrap: 'wrap' })`).
* @returns The class string.
*/
groupClasses(): string {
return this.cm().flex({ align: 'center', gap: this.gap ?? 'sm', wrap: 'wrap' })
}
/**
* One swatch button's class list (React's
* `cm.cn(cm.roundedFull, cm.cursorPointer, cm.touchTargetCompact)`).
* @returns The class string.
*/
buttonClasses(): string {
return this.cm().cn(
this.cm().roundedFull,
this.cm().cursorPointer,
this.cm().touchTargetCompact,
)
}
/**
* Whether a swatch is the selected one (React's `s.value === value`).
* @param swatch - The swatch.
* @returns Whether it is selected.
*/
isSelected(swatch: ColorSwatch): boolean {
return swatch.value === this.value
}
/**
* A swatch's accessible label (React's `s.label ?? s.value`).
* @param swatch - The swatch.
* @returns The label.
*/
swatchLabel(swatch: ColorSwatch): string {
return swatch.label ?? swatch.value
}
/**
* One swatch button's inline style text (React's
* `style={{ width: size, height: size, background: s.color, outline: …,
* outlineOffset: 2, border: 'none' }}`), serialized the way React writes
* it; null when there is nothing to set.
*
* The background IS the caller's data (the colour being picked), not a
* design token, and a `cm.button()` fill would hide it — hence the one
* inline style. Cursor and the compact touch floor come from the ClassMap.
*
* @param swatch - The swatch.
* @returns The style text or null.
*/
buttonStyle(swatch: ColorSwatch): string | null {
return styleText({
width: this.size ?? 28,
height: this.size ?? 28,
background: swatch.color,
outline: this.isSelected(swatch) ? '2px solid currentColor' : 'none',
outlineOffset: 2,
border: 'none',
})
}
/**
* Activate a swatch: report the pick through `onChange` (React's
* `onClick={() => onChange(s.value)}`).
* @param swatch - The swatch the user picked.
*/
pick(swatch: ColorSwatch): void {
this.onChange?.(swatch.value)
}
}
Injection Notes
Requirements
Peer dependencies:
@angular/common^22.0.0@angular/core^22.0.0@angular/platform-browser^22.0.0@molecule/app-angular^3.5.0 || ^4.0.0@molecule/app-ui^1.1.0@molecule/app-ui-angular^1.1.0
Runtime Dependencies
@angular/common@angular/core@angular/platform-browser@molecule/app-angular@molecule/app-ui@molecule/app-ui-angular
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 the picked value and
re-render with the new value input. Swatches render as role="radio"
buttons sized by the size input (default 28px) with the CSS color you
provide. The node prop preview is projected content: selected by
[slot=preview] and rendered below the group.