Pickr - A simple, multi-themed, responsive and hackable Color-Picker library. No dependencies, no jQuery. Compatible with all CSS Frameworks e.g. Bootstrap,
Pickr - A simple, multi-themed, responsive and hackable Color-Picker library. No dependencies, no jQuery. Compatible with all CSS Frameworks e.g. Bootstrap,
</a>
<a href="https://www.buymeacoffee.com/aVc3krbXQ" target="_blank">
</a>
</a>
[!WARNING] As of
v1.10.0there is noes5version anymore. If you need to support older browsers consult the docs for your bundler to transpile dependencies.
[!IMPORTANT] This project might continue to get important security- and bug-related updates but its feature set is frozen, and it's highly unlikely that it'll get new features or enhancements.
The reason behind this decision is the way this tool has been built (monolithic, the core is one single file, everything is in plain JS etc.) which makes it incredibly hard to maintain, tests become impossible at this stage without a complete rewrite, and the fun is gone at such a level of cramped complexity.
Personally I recommend building these UI-Related "widgets" directly into the app with the framework you're using which takes more time but in return gives you full power of how it should work and look like. Frameworks such as (p)react, vue and svelte will make it a breeze to develop such things within a day.
| Classic | Monolith | Nano |
|---|---|---|
Nano uses css-grid thus it won't work in older browsers.
Note: The readme is always up-to-date with the latest commit. See Releases for installation instructions regarding to the latest version.
Install via npm:
$ npm install @simonwep/pickrInstall via yarn:
$ yarn add @simonwep/pickrInclude code and style:
// One of the following themes
import '@simonwep/pickr/dist/themes/classic.min.css'; // 'classic' theme
import '@simonwep/pickr/dist/themes/monolith.min.css'; // 'monolith' theme
import '@simonwep/pickr/dist/themes/nano.min.css'; // 'nano' theme
// Your bundler will pick the correct import for you
import Pickr from '@simonwep/pickr';jsdelivr:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/themes/classic.min.css"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/themes/monolith.min.css"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/themes/nano.min.css"/>
<script src="https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/pickr.min.mjs" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/pickr.min.js"></script>Be sure to load the pickr.min.js after pickr.min.css.
Moreover the script tag doesn't work with the defer attribute.
…You can find more examples here.
Since version 0.4.x Pickr is event-driven. Use the on(event, cb) and off(event, cb) functions to bind / unbind event listener.
| Event | Description | Arguments |
|---|---|---|
init |
Initialization done - pickr can be used | PickrInstance |
hide |
Pickr got closed | PickrInstance |
show |
Pickr got opened | HSVaColorObject, PickrInstance |
save |
User clicked the save / clear button. Also fired on clear with null as color. |
HSVaColorObject or null, PickrInstance |
clear |
User cleared the color. | PickrInstance |
change |
Color has changed (but not saved). Also fired on swatchselect |
HSVaColorObject, eventSource, PickrInstance |
changestop |
User stopped to change the color | eventSource, PickrInstance |
cancel |
User clicked the cancel button (return to previous color). | PickrInstance |
swatchselect |
User clicked one of the swatches | HSVaColorObject, PickrInstance |
Example:
…Where source can be
slider - Any slider in the UI.input - The user input field.swatch - One of the swatches.…Example setup:
</div>
</div>
</div>To select the .pickr element you can use the custom >> shadow-dom-selector in el:
el: '.entry >> .innr .another >> .pickr'Every ShadowRoot of the query-result behind a >> gets used in the next query selection.
An alternative would be to provide the target-element itself as el.
As default color representation is hsva (hue, saturation, value and alpha) used, but you can also convert it to other formats as listed below.
The toString() is overridden, so you can get a color representation string.
hsva.toRGBA(); // Returns [r, g, b, a]
hsva.toRGBA().toString(); // Returns rgba(r, g, b, a) with highest precision
hsva.toRGBA().toString(3); // Returns rgba(r, g, b, a), rounded to the third decimal:Number,s:Number,v:Number,a:Float, silent:Boolean) - Set an color, returns true if the color has been accepted.:String | null, silent:Boolean):Boolean - Parses a string which represents a color (e.g. #fff, rgb(10, 156, 23)) or name e.g. 'magenta', returns true if the color has been accepted. null will clear the color.If silent is true (Default is false), the button won't change the current color.
:String, cb:Function):Pickr - Appends an event listener to the given corresponding event-name (see section Events).:String, cb:Function):Pickr - Removes an event listener from the given corresponding event-name (see section Events).:Pickr - Shows the color-picker.:Pickr - Hides the color-picker.:Pickr - Disables pickr and adds the disabled class to the button.:Pickr - Enables pickr and removes the disabled class from the button.:Pickr - Returns true if the color picker is currently open.:Object - Returns the dom-tree of pickr as tree-structure.:HSVaColor - Returns the current HSVaColor object.:HSVaColor - Returns the currently applied color.:String):Boolean - Change the current color-representation. Valid options are HEX, RGBA, HSVA, HSLA and CMYK, returns false if type was invalid.:String - Returns the currently used color-representation (eg. HEXA, RGBA...):Boolean):Pickr - Same as pressing the save button. If silent is true the onSave event won't be called.:String):Boolean - Adds a color to the swatch palette. Returns true if the color has been successful added to the palette.:Number):Boolean- Removes a color from the swatch palette by its index, returns true if successful.Pickr
:Object):Pickr - Creates a new instance.Pickr.utils
:HTMLElement, event:String, fn:Function[, options :Object]) - Attach an event handle which will be fired only once:HTMLElement(s), events:String(s), fn:Function[, options :Object]) - Attach an event handler function.:HTMLElement(s), event:String(s), fn:Function[, options :Object]) - Remove an event handler.:String):HTMLElement - Creates an new HTML Element out of this string.:Event):[HTMLElement] - A polyfill for the event-path event property.:String) - See inline documentation.:String|HTMLElement) - Resolves a HTMLElement, supports >>> as shadow dom selector.:InputElement, mapper:Function) - Creates the possibility to change the numbers in an inputfield via mouse scrolling.
The mapper function takes three arguments: the matched number, an multiplier and the index of the match.Use this utils carefully, it's not for sure that they will stay forever!
How do I initialize multiple pickr's? Can I access the instance via
classorid?
No, you can't. You need to keep track of your instance variables - pickr is (not yet) a web-component.
The best option would be to create new elements via document.createElement and directly pass it as el.
example.
I want to use pickr in a form, how can I do that?
You can use useAsButton: true and pass a reference (or selector) of your input-element as el. Then you can update the input-element whenever a change was made. example.
I want to update options after mounting pickr, is that possible?
Unfortunately not. The core-code of this project is rather old (over 2 years), and I made it in my early js-days - the widget is not able to dynamically re-render itself in that way. You have to destroy and re-initialize it.
If you want to open a issue, create a Pull Request or simply want to know how you can run it on your local machine, please read the Contributing guide.
No open issues yet, or sync has not completed.