
npm install vue-swatches --save
yarn add vue-swatches
<link rel="stylesheet" href="https://unpkg.com/vue-swatches@1/dist/vue-swatches.min.css">
<script src="https://unpkg.com/vue-swatches@1"></script>
<!-- Vue component -->
<template>
<div>
<swatches v-model="color" />
</div>
</template>
<script>
import Swatches from 'vue-swatches'
// Import the styles too, globally
import "vue-swatches/dist/vue-swatches.min.css"
export default {
components: { Swatches }, // window.VueSwatches.default - from CDN
data () {
return {
color: '#1CA085'
}
}
}
</script><div class="form__field">
<div class="form__label">
<strong>Please choose a color:</strong>
</div>
<div class="form__input">
<swatches v-model="color" popover-to="left"></swatches>
</div>
</div>
Vue Swatches has a bunch of presets ready to use. Make sure to check it out!
<div class="form__field">
<div class="form__label">
<strong>Please choose a color:</strong>
</div>
<div class="form__input">
<swatches v-model="color" colors="text-advanced" popover-to="left"></swatches>
</div>
</div>
You can use your custom trigger. Useful to show buttons, icons, custom inputs!
<div class="form__field">
<div class="form__input">
<swatches v-model="color">
<input slot="trigger" :value="color" class="form__input__element" readonly>
</swatches>
</div>
</div>
Remember to use always 6-digits hexadecimal colors.
If you want to provide a transparent/no-color option you can use a empty string ('').
<div class="form__field">
<div class="form__label">
<strong>Please choose a color:</strong>
</div>
<div class="form__input">
<swatches
v-model="color"
:colors="colors"
row-length="6"
shapes="circles"
show-border
popover-to="left"
></swatches>
</div>
</div>
import Swatches from 'vue-swatches'
export default {
components: {
Swatches
},
data () {
return {
color: '#F64272',
colors: ['#F64272', '#F6648B', '#F493A7', '#F891A6', '#FFCCD5', '']
}
}
}
You can use your own colors in a nested arrays.
<div class="form__field">
<div class="form__label">
<strong>Please choose a color:</strong>
</div>
<div class="form__input">
<swatches v-model="color" :colors="colors" row-length="5" popover-to="left"></swatches>
</div>
</div>
import Swatches from 'vue-swatches'
export default {
components: {
Swatches
},
data () {
return {
color: '#b9f5f1',
colors: [
['#F64272', '#F6648B', '#F493A7', '#F891A6', '#FFCCD5' ],
['#8b5aff', '#a27bff', '#b99cff', '#d0bdff', '#e8deff' ],
['#51e5db', '#74ebe3', '#96f0ea', '#b9f5f1', '#dcfaf8' ],
['#ffa51a', '#ffb748', '#ffc976', '#ffdba3', '#ffedd1' ]
]
}
}
}
Inline Mode hides the trigger and show all the swatches right away! Useful for Advanced UI
<div class="form__field">
<div class="form__label">
<strong>Please choose a color:</strong>
<swatches v-model="color" inline></swatches>
</div>
</div>
<div class="form__field">
<div class="form__label">
<button @click="$modal.show('my-custom-ui')">Please choose a color</button>
<modal name="my-custom-ui" :adaptive="true" height="192">
<div class="custom-ui-class">
<swatches v-model="color" inline></swatches>
</div>
</modal>
</div>
</div>
You can hide or disable colors, using exceptions and exception-mode
<div class="form__field">
<div class="form__label">
<strong>Please choose a color:</strong>
</div>
<div class="form__input">
<swatches
v-model="color"
colors="text-basic"
:exceptions="exceptions"
exception-mode="disabled"
row-length="4"
show-border
popover-to="left"
></swatches>
</div>
</div>
import Swatches from 'vue-swatches'
export default {
components: {
Swatches
},
data () {
return {
color: '#FFCC00',
exceptions: ['#FFFFFF', '#000000']
}
}
}
If your user wants to use its own color you can use a fallback input and customizing it with fallback-input-class, fallback-ok-class and 'fallback-ok-text'
<div class="form__field">
<div class="form__label">
<strong>Please choose a color:</strong>
</div>
<div class="form__input">
<swatches
v-model="color"
show-fallback
popover-to="left"
></swatches>
</div>
</div>
You can set the fallback input type to color and get a visual color picker interface
<div class="form__field">
<div class="form__label">
<strong>Please choose a color:</strong>
</div>
<div class="form__input">
<swatches
v-model="color"
show-fallback
fallback-input-type="color"
popover-to="left"
></swatches>
</div>
</div>
A basic collection of colors for general purposes.
A basic collection of colors for styling text.
This preset also sets showBorder
A complete collection of colors for styling text. This preset uses nested colors.
Inspired by Google Docs
This preset also sets borderRadius, rowLength, swatchSize and spacingSize
A basic collection of material colors.
A basic collection of light material colors. Useful to show dark text inside.
A basic collection of dark material colors. Useful to show light text inside.
| Name | Type | Default | Examples | Description |
|---|---|---|---|---|
| background-color | String | '#ffffff' |
| Sets the background color. |
| close-on-select | Boolean | true |
| Hides the popover after select a color. |
| colors | Array, Object or String | 'basic' |
| The colors that will be shown in the swatches. You should always use 6-digits hex colors.
|
| exceptions | Array | [] |
| Colors that will be excluded. You should always use 6-digits hex colors.
|
| exception-mode | String | 'disabled' |
| The behaviour of the exceptions. Use 'disabled' or 'hidden'
|
| disabled | Boolean | false |
| Set it to 'true' to disable the swatch. It won't be possible to pick another color. |
| fallbackInputClass | Array, Object or String | null |
| Sets the class for the fallback input |
| fallbackOkClass | Array, Object or String | null |
| Sets the class for the fallback button |
| fallbackOkText | String | 'Ok' |
| Sets the text for the fallback button |
| fallbackInputType | String | 'text' |
| Sets the input type for the fallback input Use 'text' or 'color' |
| inline | Boolean | false |
| Wheter the swatches should be shown in a popover or not (inline).
|
| max-height | Number or String | 300 |
| The maximum height for the popover. This don't affect inline mode. |
| shapes | String | 'squares' |
| The shape that will be used for the swatches and the trigger. Use 'circles' or 'squares' |
| popover-to | String | 'right' |
| Set the position of the popover. Use 'right' or 'left' This don't affect inline mode. |
| row-length | Number or String | 4 |
| The number of swatches to show in a row. This don't affect inline mode. |
| show-border | Boolean | false |
| Whether the swatches should be showing borders or not. Useful for making lighter colors more noticeable. |
| show-fallback | Boolean | false |
| Shows an input to change the color from there as well. Useful for custom color codes. |
| swatch-size | Number or String | 42 |
| Sets the swatches size. |
| swatch-style | Object or Array | {} |
| Sets the inline style for every swatch |
| trigger-style | Object or Array | {} |
| Sets the inline style for the trigger |
| wrapper-style | Object or Array | {} |
| Sets the inline style for the wrapper |
| value | String | null |
| Sets the selected color. Use `''` for transparent/no-color. |
| Name | Payload | Description |
|---|---|---|
| @input | value | Emitted after when this.value changes |
| @open | Emitted when the popover is opened. | |
| @close | value | Emitted when the popover is closed. |
| Name | Description |
|---|---|
| trigger | Use to replace the trigger. Useful for showing custom icons, buttons or inputs. |