Vue Color Kit
Vue Color Kit is a dedicated component library providing a highly customizable color picker for Vue 3 applications. Currently stable at version 1.0.6, it offers both dark and light themes, supports various color formats (HEX, RGBA), and includes features like a color history palette and an optional 'sucker' tool for picking colors directly from the screen. While its release cadence appears to be infrequent, with recent updates focusing on minor improvements, it serves as a lightweight and focused solution for integrating color selection capabilities into Vue 3 projects. Its key differentiators include native Vue 3 support, minimal dependencies, and a direct approach to color picking without relying on external UI frameworks, offering a straightforward developer experience for basic to moderately complex color input requirements.
Common errors
-
[Vue warn]: Failed to resolve component: ColorPicker
cause The ColorPicker component has not been correctly imported and registered within the Vue application or component where it's being used.fixEnsure you have `import { ColorPicker } from 'vue-color-kit';` and included `ColorPicker` in the `components` option of your Vue component (e.g., `components: { ColorPicker }`). -
Color picker renders without any styles or appears as basic HTML elements.
cause The required CSS file for `vue-color-kit` has not been imported into the project.fixAdd `import 'vue-color-kit/dist/vue-color-kit.css'` to your application's entry file (e.g., `main.js`/`main.ts`) or within the `<script>` section of the Vue component where `ColorPicker` is used. -
The eyedropper/sucker tool icon appears, but clicking it does nothing or causes an error.
cause The `sucker-canvas` and `sucker-area` props, which are essential for the eyedropper functionality, have not been correctly provided or initialized.fixImplement the `openSucker` event handler to dynamically create an `HTMLCanvasElement` and set it to the `sucker-canvas` prop, along with defining the capture region in `sucker-area`. Remember to clean up the canvas when the sucker tool is closed.
Warnings
- gotcha The component's styling is not automatically bundled or injected. Developers must manually import the CSS file `vue-color-kit/dist/vue-color-kit.css` in their entry point or component script, otherwise, the color picker will render unstyled.
- gotcha The 'sucker' (eyedropper) tool requires manual setup of an `HTMLCanvasElement` for `sucker-canvas` and an array of coordinates for `sucker-area` to function correctly. Without these, clicking the sucker icon will open the tool but it won't actually pick colors from the screen.
- gotcha The `changeColor` event emits an object containing both `rgba` and `hex` values. If you only need one format, remember to destructure or access the specific property, e.g., `color.rgba` or `color.hex`.
Install
-
npm install vue-color-kit -
yarn add vue-color-kit -
pnpm add vue-color-kit
Imports
- ColorPicker
const ColorPicker = require('vue-color-kit')import { ColorPicker } from 'vue-color-kit' - CSS Styles
import 'vue-color-kit/vue-color-kit.css'
import 'vue-color-kit/dist/vue-color-kit.css'
- Type for Color Change Event
import { ColorPickerChangeEvent } from 'vue-color-kit'import type { ColorPickerChangeEvent } from 'vue-color-kit'
Quickstart
<template>
<div :style="{ background: color, minHeight: '100vh', display: 'flex', alignItems: 'center', justifyContent: 'center' }">
<ColorPicker
theme="dark"
:color="color"
:sucker-hide="false"
:sucker-canvas="suckerCanvas"
:sucker-area="suckerArea"
@changeColor="changeColor"
@openSucker="openSucker"
/>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import { ColorPicker } from 'vue-color-kit';
import 'vue-color-kit/dist/vue-color-kit.css';
interface RGBA { r: number; g: number; b: number; a: number; }
interface ColorResult { rgba: RGBA; hex: string; }
export default defineComponent({
name: 'ColorPickerExample',
components: {
ColorPicker,
},
setup() {
const color = ref<string>('#59c7f9');
const suckerCanvas = ref<HTMLCanvasElement | null>(null);
const suckerArea = ref<number[]>([]);
const changeColor = (newColor: ColorResult) => {
const { r, g, b, a } = newColor.rgba;
color.value = `rgba(${r}, ${g}, ${b}, ${a})`;
};
const openSucker = (isOpen: boolean) => {
// In a real application, you would initialize/destroy your canvas here.
// For simplicity, this example just logs the state.
if (isOpen) {
console.log('Sucker tool opened. User needs to set suckerCanvas and suckerArea if actual picking is desired.');
// Example: suckerCanvas.value = document.createElement('canvas');
// Example: suckerArea.value = [0, 0, window.innerWidth, window.innerHeight];
} else {
console.log('Sucker tool closed.');
// Example: suckerCanvas.value?.remove();
// Example: suckerCanvas.value = null;
}
};
return {
color,
suckerCanvas,
suckerArea,
changeColor,
openSucker,
};
},
});
</script>