nuxt-color-picker

nuxt-color-picker

A module that adds a convenient, customizable ColorPicker component to select colors in your application.

nuxt-color-picker

npm versionnpm downloadsLicenseNuxt

Nuxt Color Picker

A module that adds a convenient, customizable ColorPicker component to select colors in your application.

This module works with Nuxt 3 only

DEMO

Features

  • Easy connection
  • Full component customisation
  • Two use cases (as color-picker and as color-picker-block)
  • EyeDropper support
  • Color selection history
  • Initial color display
  • Alpha range availability

Usage

Install the module:

npm install nuxt-color-picker

Configuration

export default defineNuxtConfig({
  modules: ["nuxt-color-picker"]
})

Components

The component is a wrapper for any html block, to implement its own ColorPicker menu disclosure button.

type Props = {
  modelValue?: string;
  storageKey?: string;
  withAlpha?: boolean;
  withInitialColor?: boolean;
  withEyeDropper?: boolean;
  withHexInput?: boolean;
  withRgbInput?: boolean;
  withColorsHistory?: boolean | number;
  immediateEmit?: boolean;
};
type Emits = {
  (e: 'update:modelValue', v: string): void;
  (e: 'change', v: ModuleColorMultiType): void;
  (e: 'close'): void;
};
type Slot = {
  color: Ref<string | undefined>;
  hide: () => void;
  show: (event: MouseEvent) => void;
};
<script lang="ts" setup>
  const refVariable = ref('#000');
</script>

<template>
  <color-picker
    v-model="refVariable"
    v-slot="{ color, show }"
    @change="console.log('New color:', $event)"
    @close="console.log('ColorPicker is closed')"
  >
    Current color: {{ color }}
    <button @click="show">OPEN</button>
  </color-picker>
</template>

The component is the main block of the Color Picker. It is always open and can be integrated as a block, anywhere in the application.

type Props = {
  modelValue?: string;
  storageKey?: string;
  withAlpha?: boolean;
  withInitialColor?: boolean;
  withEyeDropper?: boolean;
  withHexInput?: boolean;
  withRgbInput?: boolean;
  withColorsHistory?: boolean | number;
  immediateEmit?: boolean;
};
type Emits = {
  (e: 'update:modelValue', v: string): void;
  (e: 'change', v: ModuleColorMultiType): void;
};
<script lang="ts" setup>
  const refVariable = ref('#000');
</script>

<template>
  Current color: {{ refVariable }}
  <color-picker-block
    v-model="refVariable"
    @change="console.log('New color:', $event)"
  />
</template>

Components configuration

Properties

NameTypeDefaultDescription
modelValuestring#000000v-model variable that adds two-way communication
storageKeystringhistoryA modifier key in the storage to save the color history
withAlphabooleanfalseIt includes an alpha spectrum block and the ability to work with the alpha range of colors
withInitialColorbooleanfalseEnables the display of the initial color with which the block was originally opened with the possibility of rolling back
withEyeDropperbooleanfalseIncludes the EyeDropper block (if supported), which allows you to use color copying
withHexInputbooleanfalseIncludes a block for manually entering the hex value color. When WithAlpha is enabled, the alpha range is added
withRgbInputbooleanfalseIncludes a block for manually entering the rgb color value. When WithAlpha is enabled, the alpha range is added
withColorsHistoryboolean | numberfalseIncludes the color history block. Specifying a number - sets the number of colors to display (1<=n<=9). Specifying true sets the maximum possible number of colors. When withAlpha is turned off, the maximum number of colors is 8
immediateEmitbooleanfalseEnables calling a color change event when mounting a component

Events

NameTypeDescription
update:modelValuestringv-model system event for updating the value
changeModuleColorMultiTypeCalled when the color changes and returns a new color in rgba, hsv and hex values
closevoidCalled when the ColorPicker window is closed

Slots

NameTypeDescription
colorRef<string | undefined>Current, selected, reactive color
hide() => voidThe function that closes the ColorPicker window
show(event: MouseEvent) => voidThe function that opens the ColorPicker window (requires MouseEvent to calculate the position of the window)

Types

type ModuleRGB = {
  r: number;
  g: number;
  b: number;
};
type ModuleRGBA = ModuleRGB & {
  a: number;
};
type ModuleHSV = {
  h: number;
  s: number;
  v: number;
};
type ModuleHEX = string;

type ModuleColorMultiType = {
  rgba: ModuleRGBA;
  hsv: ModuleHSV;
  hex: ModuleHEX;
};

Configuration examples

all-offall-onhex-4colors-without-alpha
EquipmentBasicFullOnly HEX with 4 colors
Propertieswith-alpha
with-initial-color
with-eye-dropper
with-hex-input
with-rgb-input
with-colors-history
with-hex-input
:with-colors-history="4"
only-alphargba-initialwithout-alpha
EquipmentOnly alphaRGBA with initialAll without alpha
Propertieswith-alphawith-alpha
with-initial-color
with-rgb-input
with-initial-color
with-eye-dropper
with-hex-input
with-rgb-input
with-colors-history

Development

  • Run npm run dev:prepare to generate type stubs.
  • Use npm run dev to start playground in development mode.

Deploy

Deploy the application on the Edge with NuxtHub on your Cloudflare account:

npm run deploy

Then checkout your server logs, analaytics and more in the NuxtHub Admin.

You can also deploy using Cloudflare Pages CI.

License

MIT License