sefianecho / alwan

A simple, lightweight, customizable, touch friendly color picker, written in vanilla javascript with zero dependencies.
https://sefianecho.github.io/alwan/
MIT License
38 stars 6 forks source link
color color-picker hex hsl javascript-vanilla picker rgb typescript ui ux widget

Alwan

   

alwan light theme       alwan dark theme      

   

npm bundle size npm bundle size npm

A simple, lightweight, customizable, touch friendly color picker, written in vanilla javascript with zero dependencies.

Features

Note

If you are using React, use react-alwan instead.

Demo

Click here to try it

Getting started

Install using package manager:

npm install alwan

or

yarn add alwan

Import files

// Import javascript.
import alwan from 'alwan';
// Import css.
import 'alwan/dist/css/alwan.min.css';

CDN

Add it to your page.

<!-- Style -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/alwan/dist/css/alwan.min.css" />

<!-- Script (UMD) -->
<script src="https://cdn.jsdelivr.net/npm/alwan/dist/js/alwan.min.js"></script>
<!-- Style -->
<link rel="stylesheet" href="https://unpkg.com/alwan/dist/css/alwan.min.css" />

<!-- Script (UMD) -->
<script src="https://unpkg.com/alwan/dist/js/alwan.min.js"></script>
Other CDN links
// ES Module
https://cdn.jsdelivr.net/npm/alwan/dist/js/esm/alwan.min.js
https://unpkg.com/alwan/dist/js/esm/alwan.min.js

// IIFE (since v2.0.3)
https://cdn.jsdelivr.net/npm/alwan/dist/js/iife/alwan.min.js
https://unpkg.com/alwan/dist/js/iife/alwan.min.js

Usage

Alwan can take two arguments, a reference element either a selector or an HTML element (required), and options object (optional).

const alwan = new Alwan('#reference', {
    // Options...
});

Options

You can try these options in the demo, play around with it

Note: In the reference element you can access the CSS custom property --color (--alwan-color before v2.0.0) to get color value

Accessibility (since v1.4)

Unlabeled interactive elements has a ARIA label attribute with a default values in English. You can change these labels in the options by modifying the i18n object.

Note:: The title attribute of the copy button and the change format button is the same as the ARIA label, and for the swatch button its title is the color value in the swatches array.

// Labels default values.
    {
        ...options,
        i18n: {
            palette: 'Color picker', // Palette's marker (picker) ARIA label.
            buttons: {
                copy: 'Copy color to clipboard', // Copy button ARIA label and title.
                changeFormat: 'Change color format', // Change format button ARIA label and title.
                swatch: 'Color swatch', // Swatch button ARIA label.
                toggleSwatches: 'Toggle Swatches' // Toggle swatches button ARIA label and title (since v2.0.0).
            },
            sliders: {
                hue: 'Change hue', // Hue slider ARIA label.
                alpha: 'Change opacity' // Alpha slider ARIA label.
            }
        }
    }

Events

Use the method on, that has two parameters, event and handler (callback function).

alwan.on('event', (ev) => {
    // ...
});
Event Argument Description
open event Fires when the picker get opened
close event Fires when the picker get closed
change event Fires when an alternation to the color is committed by the user, similar to the DOM change event
color event Similar to the input event, fires every time the color changes

Event object (since v1.3)

// e.g.
alwan.on('change', (ev) => {
    ev.type; // change
    ev.source; // Color picker instance.

    // HSL color components.
    ev.h;
    ev.s;
    ev.l;

    // RGB color components.
    ev.r;
    ev.g;
    ev.b;

    // Alpha channel.
    ev.a;

    // Colors strings.
    ev.rgb;
    ev.hsl;
    ev.hex;
});

Methods

Static methods:

Instance methods:

// Set color 'purple' and trigger 'change' event.
picker.setColor('purple').trigger('change');