vue3-color-gradient-picker
v1.0.3
Published
Color and gradient picker for vue3.js
Maintainers
Readme
vue3-color-gradient-picker
A color and gradient picker component for Vue 3.

Features
- Solid color picking with saturation / value area, hue slider and alpha slider
- Linear and radial gradients with any number of color stops
- Adjustable gradient angle by dragging the dial or clicking it to rotate 45°
- HEX (including 8-digit
#RRGGBBAA) and RGBA inputs - Start / change / end callbacks with a ready-to-use CSS value
Installation
npm install vue3-color-gradient-picker
# or
yarn add vue3-color-gradient-picker
# or
pnpm add vue3-color-gradient-pickerRequires Vue 3.3 or later.
Usage
Import the component and its stylesheet:
import { ColorPicker } from 'vue3-color-gradient-picker';
import 'vue3-color-gradient-picker/dist/style.css';Solid color
<script setup>
import { ref } from 'vue';
import { ColorPicker } from 'vue3-color-gradient-picker';
import 'vue3-color-gradient-picker/dist/style.css';
const color = ref({ red: 255, green: 0, blue: 0, alpha: 1 });
function onChange(value) {
color.value = { ...value };
console.log(value.style); // rgba(255, 0, 0, 1)
}
</script>
<template>
<ColorPicker :color="color" :onChange="onChange" />
</template>Gradient
<script setup>
import { ColorPicker } from 'vue3-color-gradient-picker';
import 'vue3-color-gradient-picker/dist/style.css';
const gradient = {
type: 'linear',
degree: 90,
points: [
{ left: 0, red: 0, green: 0, blue: 0, alpha: 1 },
{ left: 100, red: 255, green: 0, blue: 0, alpha: 1 },
],
};
function onChange(value) {
console.log(value.style); // linear-gradient(90deg,rgba(0, 0, 0, 1) 0%,rgba(255, 0, 0, 1) 100%)
}
</script>
<template>
<ColorPicker is-gradient :gradient="gradient" :onChange="onChange" />
</template>Gradient interactions:
| Action | Result |
| --- | --- |
| Click the gradient bar | Add a color stop at that position |
| Drag a color stop | Move the stop |
| Double-click a stop, or press Delete / Backspace | Remove the active stop (at least two stops are kept) |
| Click the angle dial | Rotate the linear gradient by 45° |
| Drag the angle dial | Set the angle freely |
Props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| isGradient | Boolean | false | Show the gradient picker instead of the solid color picker |
| color | Object | { red: 255, green: 0, blue: 0, alpha: 1 } | Solid color. red / green / blue range from 0 to 255, alpha from 0 to 1. Optional hue, saturation and value can be passed to keep the HSV position exact |
| gradient | Object | Black to red, linear, 0° | Initial gradient, see the structure below. It is read when the component is mounted |
| onStartChange | Function | — | Called when a drag starts |
| onChange | Function | — | Called on every change |
| onEndChange | Function | — | Called when a drag ends |
Gradient structure:
{
type: 'linear', // 'linear' | 'radial'
degree: 90, // angle of the linear gradient, 0 - 360
points: [
// left: position of the stop in percent, 0 - 100
{ left: 0, red: 0, green: 0, blue: 0, alpha: 1 },
{ left: 100, red: 255, green: 0, blue: 0, alpha: 1 },
],
}Callback value
Solid mode:
{ red, green, blue, alpha, hue, saturation, value, style } // style: 'rgba(r, g, b, a)'Gradient mode:
{ points, type, degree, style } // style: a CSS linear-gradient() / radial-gradient() valueAdding or removing a stop and changing the gradient type or angle only trigger onChange. To persist the gradient, listen to onChange.
Styles
The stylesheet must be imported manually from vue3-color-gradient-picker/dist/style.css. Note that it applies box-sizing: border-box to all elements on the page.
Development
npm install
npm run dev # start the demo with hot reload
npm run build # build the library into dist/
npm run lintChangelog
1.0.3
- Fixed pointer offsets when the page is scrolled
- Fixed the picker not working when mounted inside a hidden container, such as a popover or dialog
- Fixed a small offset when clicking the hue and alpha sliders
- Fixed the angle dial calculating a wrong angle
- Fixed
Delete/Backspaceremoving gradient stops while typing in an input - Fixed the keyboard listener not being removed when the component is unmounted
- Fixed the active color not updating after a gradient stop is removed
- Fixed 8-digit HEX input setting alpha to 0
- Limited the alpha input to 0 - 100
- Removed the invalid
browserfield frompackage.json
License
MIT © Bamzc
