This component is available on GitHub.

Stimulus Color Picker

A Stimulus controller to create color picker.

This controller is using @simonwep/pickr behind the scene.

Installation

$ yarn add stimulus-color-picker

And use it in your JS file:

// Probably in app/javascript/controllers/index.js

import { Application } from '@hotwired/stimulus'
import ColorPicker from 'stimulus-color-picker'

const application = Application.start()
application.register('color-picker', ColorPicker)

πŸ‘€ Demo

See demo

Usage

Before starting, your must import the theme you want to use in your js or in your sass:

// In your application.js (for example)
import '@simonwep/pickr/dist/themes/classic.min.css'
// import '@simonwep/pickr/dist/themes/monolith.min.css'
// import '@simonwep/pickr/dist/themes/nano.min.css'

// Or in your sass
@import '~@simonwep/pickr/dist/themes/classic.min.css'
// @import '~@simonwep/pickr/dist/themes/monolith.min.css'
// @import '~@simonwep/pickr/dist/themes/nano.min.css'

Basic usage:

<div data-controller="color-picker">
  <input type="hidden" name="color" value="#667EEA" data-color-picker-target="input" />

  <div data-color-picker-target="button"></div>
</div>

πŸ›  Configuration

AttributeDefaultDescriptionOptional
data-color-picker-theme-valueclassicPickr theme you want to use.βœ…

See the Pickr documentation for more info in the library itself.

πŸŽ› Extending Controller

You can use inheritance to extend the functionality of any Stimulus component:

import ColorPicker from 'stimulus-color-picker'

export default class extends ColorPicker {
  connect() {
    super.connect()
    console.log('Do what you want here.')

    // Pickr instance
    this.picker
  }

  // Callback when the color is saved
  onSave(color) {
    super.onSave(color)
  }

  // You can override the components options with this getter.
  // Here are the default options.
  get componentOptions() {
    return {
      preview: true,
      hue: true,

      interaction: {
        input: true,
        clear: true,
        save: true,
      },
    }
  }

  // You can override the swatches with this getter.
  // Here are the default options.
  get swatches() {
    return [
      '#A0AEC0',
      '#F56565',
      '#ED8936',
      '#ECC94B',
      '#48BB78',
      '#38B2AC',
      '#4299E1',
      '#667EEA',
      '#9F7AEA',
      '#ED64A6',
    ]
  }
}

This controller will automatically have access to targets defined in the parent class.

If you override the connect, disconnect or any other methods from the parent, you'll want to call super.method() to make sure the parent functionality is executed.

β˜•οΈ Sponsors

Stimulus Component is an MIT licensed open source project and completely free to use. However, the amount of effort needed to maintain and develop new features for the project is not sustainable without proper financial backing. You can support Stimulus Components development on Github Sponsors. πŸ™

πŸ‘· Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

Don’t forget to drop a 🌟 on Github to support the project.

πŸ“ License

This project is released under the MIT license.