vael-ui v0.5.7

Forms & inputs

Layout

ColorPicker

A swatch that opens a picker with a saturation and brightness plane, a hue slider, a hex field, optional presets and an eyedropper where supported. Use it when any color is allowed, not only a few.

Validating a form? vael-form wires this up with a schema

Install

import { ColorPicker } from 'vael-ui'

Playground

Placement, behaviour and animation

Usage
<script setup lang="ts">
import { ref } from 'vue'
import { ColorPicker } from 'vael-ui'

const value = ref("#000000")
const open = ref(false)
</script>

<template>
  <ColorPicker v-model="value" v-model:open="open" />
</template>

Props

NameTypeDefaultDescription
presetsstring[]Colors offered as swatches, as #rrggbb.
presetsPlacement'inline' | 'panel''panel'Where the presets show: in the panel, or inline beside the swatch, which then opens the panel for any other color.
showEyeDropperbooleantrueShows the eyedropper button in browsers that have one.
labelstring'Color'Accessible label for the swatch and the panel.
namestringNative name, so a plain <form> submits the hex.
disabledbooleanfalseDisables the swatch.
size'sm' | 'md' | 'lg''md'Swatch size.
teleportTostring | HTMLElement'body'Where the panel is teleported to.
motionColorPickerMotiontrueHow it moves: the swatch's appearance ({ initial: { swatch: { opacity: 0 } } }) and states ({ states: { disabled: { swatch: { opacity: 0.5 } } } }), and the panel's like Popover's ({ initial: { panel: { opacity: 0, y: -4 } } }). false skips the built-in transitions; use the exposed elements for your own.
uiPartial<Record<'root' | 'swatch' | 'panel' | 'value' | 'presets' | 'preset', UiPartValue>>Class and style overrides for each part.
modelValuestring'#000000'The color as #rrggbb.
openbooleanfalseWhether the picker panel is open.

Events

NameTypeDescription
update:modelValue(value: string)Fires when modelValue changes (v-model).
update:open(value: boolean)Fires when open changes (v-model:open).

Slots

NameTypeDescription
trigger{ color: string; open: boolean; }Replaces the swatch; a click on it opens the panel.
preset{ color: string; selected: boolean; }Content inside each preset swatch, such as a check on the selected one.
default{ color: string; pick: (color: string) => void; }Extra panel content under the presets: recent colors, a note, your own controls.

Exposed

NameTypeDescription
elHTMLElement | nullRoot element.
swatchElHTMLButtonElement | nullThe default swatch button; null with a #trigger slot.
panelElHTMLElement | nullThe open panel; null while closed.
areaElHTMLElement | nullThe saturation and brightness plane; null while closed.
thumbElHTMLElement | nullThe plane's thumb; null while closed.
parts{ positioner: HTMLElement | null; panel: HTMLElement | null; }Its elements by part name, for your own animation; a repeated part is an array.

Motion New

ColorPicker: the swatch's appearance and states, and the panel like Popover's.

<ColorPicker
  :motion="{
    initial: { panel: { opacity: 0, y: 8, scale: 0.98 } },
    spring: { damping: 0.85, response: 0.35 },
  }"
/>
NameTypeDescription
initialexitswatchpositionerpanelSets where each part comes in from and leaves to: any CSS property, plus x, y and blur. A plain number is px, or degrees for rotate.
statesdisabledSets values per part that hold while a state is on. An array is a set of keyframes that plays once as the state turns on, such as a shake on invalid.
enterleave(parts, context) => animationAnimates the parts with GSAP, motion or element.animate(). Return the animation and the component waits for it, and stops it if interrupted.
morphlinkedexitOrder'trigger' | Record<string, LinkedMotion> | 'reverse'morph: 'trigger' grows it out of the element that opened it. linked moves other elements with it, such as the page receding behind it. exitOrder: 'reverse' closes its children first.
springdurationeasing{ damping, response } | number | stringTimes all of it: a spring, or a duration in ms with an easing. Never both.
falsebooleanTurns its motion off, for an animation of your own on its exposed elements.

Accessibility

KeyAction
EnterSpaceOpens or closes the panel from the swatch.
TabMoves focus from the swatch into the panel and through its controls.
ShiftTabMoves focus back, closing the panel when it leaves the first control.
EscapeCloses the panel.
ArrowLeftArrowRightLowers or raises saturation by 1% on the color area.
ArrowUpArrowDownRaises or lowers brightness by 1% on the color area.
ArrowRightArrowUpRaises the hue by 1° on the hue slider.
ArrowLeftArrowDownLowers the hue by 1° on the hue slider.
PageUpPageDownRaises or lowers the hue by 10°.
HomeEndSets the hue to 0° or 360°.
  • The swatch is a button with aria-haspopup="dialog" and aria-expanded. The label prop (default “Color”) names both the swatch and the panel, which has role="dialog". Label a custom #trigger yourself.
  • Opening keeps focus on the swatch. Tabbing past the last panel control closes the panel and moves on. Closing it while focus is inside returns focus to the swatch.
  • The color area is a role="slider" with an aria-valuetext that reads saturation and brightness; hold Shift for 10% steps. The hue slider, the hex field and the eyedropper button have labels, and presets use aria-pressed.
  • Right to left, ArrowLeft and ArrowRight swap on the hue slider, and its hues run from the right. On the color area they keep their direction, because saturation always runs left to right.