Coder Social home page Coder Social logo

web-padawan / vanilla-colorful Goto Github PK

View Code? Open in Web Editor NEW
764.0 8.0 29.0 1.69 MB

A tiny color picker custom element for modern web apps (2.7 KB) ๐ŸŽจ

Home Page: https://web-padawan.github.io/vanilla-colorful/

License: MIT License

JavaScript 5.42% TypeScript 89.88% CSS 2.14% HTML 2.57%
webcomponents custom-elements vanilla web-components color-picker custom-element web-component colorpicker color

vanilla-colorful's Introduction

vanilla-colorful is a port of react-colorful to vanilla Custom Elements.

Features

  • ๐Ÿ—œ Small: Just 2,7 KB (minified and gzipped). Size Limit controls the size.
  • ๐Ÿš€ Fast: Built with standards based Custom Elements.
  • ๐Ÿ›ก Bulletproof: Written in strict TypeScript and has 100% test coverage.
  • ๐Ÿ—‚ Typed: Ships with types included.
  • ๐Ÿ˜ Simple: The interface is straightforward and easy to use.
  • ๐Ÿ’ฌ Accessible: Follows the WAI-ARIA guidelines to support users of assistive technologies.
  • ๐Ÿ“ฒ Mobile-friendly: Works well on mobile devices and touch screens.
  • ๐Ÿ‘ซ Framework-agnostic: Can be used with any framework.
  • ๐Ÿ’จ No dependencies

Live demos

Install

npm install vanilla-colorful --save

Or use one of the following content delivery networks:

unpkg.com CDN:

<script type="module" src="https://unpkg.com/vanilla-colorful?module"></script>

Skypack CDN:

<script type="module" src="https://cdn.skypack.dev/vanilla-colorful"></script>

JSPM CDN:

<script type="module" src="https://jspm.dev/vanilla-colorful"></script>

ESM CDN:

<script type="module" src="https://esm.sh/vanilla-colorful"></script>

Usage

<hex-color-picker color="#1e88e5"></hex-color-picker>
<script type="module">
  import 'vanilla-colorful';

  const picker = document.querySelector('hex-color-picker');
  picker.addEventListener('color-changed', (event) => {
    // get updated color value
    const newColor = event.detail.value;
  });

  // get current color value
  console.log(picker.color);
</script>

ES modules

vanilla-colorful is authored using ES modules which are natively supported by modern browsers. However, all the code examples listed here use so-called "bare module specifiers": import 'vanilla-colorful'.

There is now a feature in the HTML Standard called import maps that enables resolving bare module specifiers without requiring any tools. As of October 2022, import maps are not yet shipped in all browsers.

In the meantime, we recommend using one of the tools that leverage ES modules based development, such as vite, @web/dev-server, or wmr. None of these tools are needed when importing from CDN.

Supported color models

The default vanilla-colorful's input/output format is a HEX string (like #ffffff). In case if you need another color model, we provide 12 additional color picker bundles.

How to use another color model

Available pickers

File to import HTML element Value example
"hex-color-picker.js" <hex-color-picker> "#ffffff"
"hex-alpha-color-picker.js" <hex-alpha-color-picker> "#ffffff88"
"hsl-color-picker.js" <hsl-color-picker> { h: 0, s: 0, l: 100 }
"hsl-string-color-picker.js" <hsl-string-color-picker> "hsl(0, 0%, 100%)"
"hsla-color-picker.js" <hsla-color-picker> { h: 0, s: 0, l: 100, a: 1 }
"hsla-string-color-picker.js" <hsla-string-color-picker> "hsla(0, 0%, 100%, 1)"
"hsv-color-picker.js" <hsv-color-picker> { h: 0, s: 0, v: 100 }
"hsv-string-color-picker.js" <hsv-string-color-picker> "hsv(0, 0%, 100%)"
"hsva-color-picker.js" <hsva-color-picker> { h: 0, s: 0, v: 100, a: 1 }
"hsva-string-color-picker.js" <hsva-string-color-picker> "hsva(0, 0%, 100%, 1)"
"rgb-color-picker.js" <rgb-color-picker> { r: 255, g: 255, b: 255 }
"rgba-color-picker.js" <rgba-color-picker> { r: 255, g: 255, b: 255, a: 1 }
"rgb-string-color-picker.js" <rgb-string-color-picker> "rgb(255, 255, 255)"
"rgba-string-color-picker.js" <rgba-string-color-picker> "rgba(255, 255, 255, 1)"

Code example

<rgba-color-picker></rgba-color-picker>
<script type="module">
  import 'vanilla-colorful/rgba-color-picker.js';

  const picker = document.querySelector('rgba-color-picker');
  picker.color = { r: 50, g: 100, b: 150, a: 1 };
</script>

Overriding styles

vanilla-colorful exposes CSS Shadow Parts allowing to override the default styles:

hex-color-picker {
  height: 250px;
}

hex-color-picker::part(saturation) {
  bottom: 30px;
  border-radius: 3px 3px 0 0;
}

hex-color-picker::part(hue) {
  height: 30px;
  border-radius: 0 0 3px 3px;
}

hex-color-picker::part(saturation-pointer) {
  border-radius: 5px;
}

hex-color-picker::part(hue-pointer) {
  border-radius: 2px;
  width: 15px;
  height: inherit;
}

HEX input

vanilla-colorful provides an additional <hex-input> element that can be used to type a color:

<hex-input color="#1e88e5"></hex-input>
<script type="module">
  import 'vanilla-colorful/hex-input.js';

  const input = document.querySelector('hex-input');
  input.addEventListener('color-changed', (event) => {
    const newColor = event.detail.value;
  });
</script>

<hex-input> renders an unstyled <input> element inside a slot and exposes it for styling using part. You can also pass your own <input> element as a child if you want to fully configure it.

In addition to color property, <hex-input> supports the following boolean properties:

Property Default Description
alpha false Allows #rgba and #rrggbbaa color formats
prefixed false Enables # prefix displaying

Base classes

vanilla-colorful provides a set of base classes that can be imported without registering custom elements. This is useful if you want to create your own color picker with a different tag name.

import { RgbBase } from 'vanilla-colorful/lib/entrypoints/rgb.js';

customElements.define('custom-color-picker', class extends RgbBase {});

Code Recipes

TypeScript support

vanilla-colorful supports TypeScript and ships with types in the library itself; no need for any other install.

How you can get the most from our TypeScript support

Custom types

While not only typing its own class methods and variables, it can also help you type yours. Depending on the element you are using, you can also import the type that is associated with the element. For example, if you are using our <hsl-color-picker> element, you can also import the HslColor type.

import type { HslColor } from 'vanilla-colorful/hsl-color-picker';

const myHslValue: HslColor = { h: 0, s: 0, l: 0 };

Typed events

All the included custom elements provide overrides for addEventListener and removeEventListener methods to include typings for the color-changed custom event detail property:

const picker = document.querySelector('rgba-color-picker');

picker.addEventListener('color-changed', (event) => {
  console.log(event.detail.value.a); // (property) RgbaColor.a: number
});

Lit plugin

All the included custom elements are compatible with lit-analyzer and lit-plugin extension for Visual Studio Code, so you can benefit from type checking in Lit templates, for example validating binding names.

Browser support

vanilla-colorful uses Custom Elements and Shadow DOM, and does not support IE11 or legacy Edge.

Why vanilla-colorful?

vanilla-colorful has all the benefits of react-colorful with one important difference.

While react-colorful claims to have zero dependencies, it still expects you to use React or Preact. This means that Angular, Vue, Svelte or vanilla JS users would have an extra dependency in their apps.

Now when all the evergreen browsers support standards based Custom Elements, it's perfect time to build such tiny and lightweight UI controls as web components rather than framework components.

vanilla-colorful's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vanilla-colorful's Issues

Add vertical orientation for hue and alpha sliders

Hi, I am using vanilla-colorful web component in one of my projects. I would like to change the direction of hue pointer drag position from top to bottom ( currently I can able to drag left to right)

image

Problem with CDN

I tryed add element from CDN <script type="module" src="https://unpkg.com/vanilla-colorful?module"></script>
and get problem on FF browser http://prntscr.com/uma6a1 // and unfortunately, I still could not get to display the value of the current color of picker

Can't clear the hex-input

If you backspace the hex-input value and then move focus from the input, it resets back to the last known valid value. It'd be great to be able to leave the field blank though, because in my case the field is optional.

Issue with hex-input slot

Hello,

This html <hex-input><input class="form-control form-unit__input mb-1" part="input" spellcheck="false"></hex-input>

Produces this:
image

DOM nodes:
image

Alternate event to `color-changed`

Hi guys. Thanks for the great package.

When using JS components in Laravel Livewire, every event triggers a server request. So using color-changed can be quite brutal on the server because a request is fired for every pixel moved when users are dragging the selector.

Would it be possible to add an additional event which fires only when dragging is complete? That would allow us to send a single request after a user has stopped moving the selector.

Vue example is broken

Vue example is broken:
image

Update the index.vue to make it work:
<hex-color-picker :color="color.value" @color-changed="this.handleColorChanged"></hex-color-picker>
to
<hex-color-picker :color="color.value" @color-changed="handleColorChanged"></hex-color-picker>

Updating picker color without dispatching a color-changed event

It seems there is no public method to update a picker color without triggering a color-changed event.

Looking at the implementation, there is a private update method:
https://github.com/web-padawan/vanilla-colorful/blob/master/src/lib/components/color-picker.ts#L103

For people implementing their own input-color element, such a method would be really convenient, or simply an optional argument to the existing color setter to specify whether an event must be dispatched.

Opacity slider?

Congratulations, the colorpicker looks great. We could use it in our project but we need an opacity slider as well. Can you add it?

Question about polyfilling

Hey, thank you so much for this package, I loved it a lot.

I have a problem where I've been using this package (due to how small and beautiful it is) on my Svelte app, it had no problems until I ported it over to tauri. Tauri uses webview, and on Windows machine, Tauri uses webview from legacy Edge.

I read your README, and found out that your package does not support legacy Edge. So I thought about polyfills, and try to get vanilla-colorful working without needing me to use other packages as alternatives (because frankly, they don't look as good and my styling skills are questionable).

I've found webcomponent's polyfills and used their shadydom and shadycss modules. But I am not sure how I can apply their ShadyCSS interface with vanilla-colorful, since I don't know where to apply their prepareTemplate function with vanilla-colorful, may I please have some guidance regarding this?

I'm not sure if this is the place to ask questions, but I'm going to give it a shot anyway since I don't really know where else to ask, if this is the wrong place to ask, please do direct me to the correct direction. Thank you so much for your time and effort!

Jest has an issue when trying to import the library

Current Behavior

When trying to run Jest in a repo that is importing vanilla-colorful, I get the following error message: "Cannot use import statement outside a module"

Steps to Reproduce

  1. Import vanilla-colorful into project
  2. Try to run Jest

Context (Environment)

  • vanilla-colorful 0.7.2
  • jest 27.5.1
  • vue 2.6.14

Detailed Description

Currently I am importing vanilla-colorful in to a Vue project. When I try to run Jest I get the following error message:
image

I have tried what Jest suggests; adding experimental decorators and get the following message:
image

I have also tried tried adding vanilla-colorful as a custom transformIgnorePattern.

Any insight on this would be appreciated, thanks.

Event delegation is not supported because color-changed event does not bubble

Hi,

Loading the vanilla-colorful module works well on a static HTML page : the color picker shows and the "color-changed" event is fired properly.

However, if I dynamically add a element to the DOM using jQuery, the color picker does appear in the page, but no "color-changed" event is fired.

image
You can see the difference in the inspector between the 8th line from the top (static, in the DOM before importing the module) and the 4th line from the bottom (dynamically added element).

Is there any way to "reload" the module dynamically, or to force adding the custom element ?

I import the module through UnPKG.

Thank you!

HEX picker with alpha

Thanks for your great work on this library!

Is it possible to have a HEX picker including the alpha channel, resulting in 4-byte outputs like #aabbccff? I don't see it as an option in the "Available pickers" table.

Thanks for element

Can you add example to FAQ how to return of chosen value of component ? For example to console.log

Get access to the class without registering the custom element

hey there ๐Ÿ‘‹

that looks very nice ๐Ÿ‘

any chance we can get an import that does not register the custom element? e.g. if you wanna use it with a scoped registry... or you need to allow 2 versions of the component to be loaded.

strawman proposal

import { ColorPickerHex } from 'vanilla-colorful/ColorPickerHex.js';

PS: could be archived by providing an export map ๐Ÿ‘

Consider using CSS instead of SCSS

I couldnโ€™t find any trace of SCSS syntax in src/lib/styles, so I wonder if using plain CSS files would be better. Less complicated, more future-proof.

I know that the whole project is 100% pure TypeScript, but at least there is a reason for that ๐Ÿ˜€ The only use for Sass I could find is to combine and compress files during the build process. I would suggest using CSSO for that. It would also save you 28 bytes! (1871 vs 1843) Not too much, but every JS byte counts, right?

Sorry for just an issue, not PR. But I hope youโ€™d like the idea.

Bug: On devices that support both mouse and touch.

Windows 11.
Chrome: Version 119.0.6045.160 (Official Build) (64-bit)
Breaks on devices that support both touch and mouse.

  1. If I start by using the mouse I can later switch to touch. - Expected.
  2. If I start by using touch I cannot switch to mouse. - Not as expected.
  3. If I start by using the mouse and then switch to touch I cannot switch back to mouse. - Not as expected.

Mouse and touch should work interchangeably in all of the above scenarios.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.