@thednp/color-picker 1.0.1 → 2.0.0-alpha10
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc.cjs +199 -0
- package/.lgtm.yml +9 -0
- package/.prettierrc.json +15 -0
- package/.stylelintrc.json +236 -0
- package/README.md +55 -72
- package/compile.js +51 -0
- package/cypress/e2e/color-palette.cy.ts +128 -0
- package/cypress/e2e/color-picker.cy.ts +909 -0
- package/cypress/fixtures/colorNamesFrench.js +3 -0
- package/cypress/fixtures/componentLabelsFrench.js +21 -0
- package/cypress/fixtures/format.js +3 -0
- package/cypress/fixtures/getMarkup.js +35 -0
- package/cypress/fixtures/getRandomInt.js +6 -0
- package/cypress/fixtures/sampleWebcolors.js +18 -0
- package/cypress/fixtures/testSample.js +8 -0
- package/cypress/plugins/esbuild-istanbul.ts +50 -0
- package/cypress/plugins/tsCompile.ts +34 -0
- package/cypress/support/commands.ts +0 -0
- package/cypress/support/e2e.ts +21 -0
- package/cypress/test.html +23 -0
- package/cypress.config.ts +29 -0
- package/dist/css/color-picker.css +25 -54
- package/dist/css/color-picker.min.css +2 -2
- package/dist/css/color-picker.rtl.css +25 -54
- package/dist/css/color-picker.rtl.min.css +2 -2
- package/dist/js/color-picker.cjs +8 -0
- package/dist/js/color-picker.cjs.map +1 -0
- package/dist/js/color-picker.d.ts +296 -0
- package/dist/js/color-picker.js +5 -3570
- package/dist/js/color-picker.js.map +1 -0
- package/dist/js/color-picker.mjs +2618 -0
- package/dist/js/color-picker.mjs.map +1 -0
- package/dts.config.ts +15 -0
- package/package.json +116 -108
- package/src/scss/_variables.scss +0 -1
- package/src/scss/color-picker.rtl.scss +4 -0
- package/src/scss/color-picker.scss +93 -51
- package/src/ts/colorPalette.ts +89 -0
- package/src/{js/color-picker.js → ts/index.ts} +498 -509
- package/src/ts/interface/ColorNames.ts +20 -0
- package/src/ts/interface/colorPickerLabels.ts +20 -0
- package/src/ts/interface/colorPickerOptions.ts +11 -0
- package/src/ts/interface/paletteOptions.ts +6 -0
- package/src/ts/util/colorNames.ts +21 -0
- package/src/{js/util/colorPickerLabels.js → ts/util/colorPickerLabels.ts} +4 -2
- package/src/ts/util/getColorControls.ts +90 -0
- package/src/{js/util/getColorForm.js → ts/util/getColorForm.ts} +28 -18
- package/src/{js/util/getColorMenu.js → ts/util/getColorMenu.ts} +21 -30
- package/src/ts/util/isValidJSON.ts +19 -0
- package/src/{js/util/setMarkup.js → ts/util/setMarkup.ts} +61 -50
- package/tsconfig.json +29 -0
- package/vite.config.ts +34 -0
- package/dist/js/color-esm.js +0 -1164
- package/dist/js/color-esm.min.js +0 -2
- package/dist/js/color-palette-esm.js +0 -1235
- package/dist/js/color-palette-esm.min.js +0 -2
- package/dist/js/color-palette.js +0 -1243
- package/dist/js/color-palette.min.js +0 -2
- package/dist/js/color-picker-element-esm.js +0 -3718
- package/dist/js/color-picker-element-esm.min.js +0 -2
- package/dist/js/color-picker-element.js +0 -3726
- package/dist/js/color-picker-element.min.js +0 -2
- package/dist/js/color-picker-esm.js +0 -3565
- package/dist/js/color-picker-esm.min.js +0 -2
- package/dist/js/color-picker.min.js +0 -2
- package/dist/js/color.js +0 -1172
- package/dist/js/color.min.js +0 -2
- package/src/js/color-palette.js +0 -75
- package/src/js/color-picker-element.js +0 -107
- package/src/js/color.js +0 -1104
- package/src/js/index.js +0 -8
- package/src/js/util/colorNames.js +0 -6
- package/src/js/util/getColorControls.js +0 -103
- package/src/js/util/isValidJSON.js +0 -13
- package/src/js/util/nonColors.js +0 -5
- package/src/js/util/roundPart.js +0 -9
- package/src/js/util/setCSSProperties.js +0 -12
- package/src/js/util/tabindex.js +0 -3
- package/src/js/util/toggleCEAttr.js +0 -70
- package/src/js/util/version.js +0 -5
- package/src/js/version.js +0 -5
- package/types/cp.d.ts +0 -558
- package/types/index.d.ts +0 -44
- package/types/source/source.ts +0 -4
- package/types/source/types.d.ts +0 -92
- /package/src/{js/util/vHidden.js → ts/util/vHidden.ts} +0 -0
package/dist/js/color.min.js
DELETED
@@ -1,2 +0,0 @@
|
|
1
|
-
// Color v1.0.1 | thednp © 2022 | MIT-License
|
2
|
-
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t="undefined"!=typeof globalThis?globalThis:t||self).Color=r()}(this,(function(){"use strict";const{head:t}=document;function r(t,r){const e=getComputedStyle(t);return r in e?e[r]:""}const e=(t,r)=>Object.assign(t,r),n=(t,r)=>{e(t.style,r)},o=["transparent","currentColor","inherit","revert","initial"];function s(t){const r=Math.floor(t);return t-r<.5?r:Math.round(t)}const i=["rgb","hex","hsl","hsv","hwb"],a="[-\\+]?\\d*\\.?\\d+(?:deg|rad|grad|turn)?",h="(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)",u=`(?:${h})|(?:${a})`,c="(?:[,|\\s]+)",l=`(?:[\\s|\\(\\s|\\s\\(\\s]+)?(${u})${c}(${h})${c}(${h})(?:[,|\\/\\s]*)?(${h})?(?:[\\s|\\)\\s]+)?`,g={CSS_UNIT:new RegExp(u),hwb:new RegExp("hwb"+l),rgb:new RegExp("rgb(?:a)?"+l),hsl:new RegExp("hsl(?:a)?"+l),hsv:new RegExp("hsv(?:a)?"+l),hex3:/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,hex6:/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,hex4:/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,hex8:/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/};function b(t){return(""+t).includes(".")&&1===parseFloat(t)}function f(t){return(""+t).includes("%")}function m(e){return!o.includes(e)&&!["#",...i].some(t=>e.includes(t))&&(!!["black","white"].includes(e)||["rgb(255, 255, 255)","rgb(0, 0, 0)"].every(o=>{n(t,{color:e});const s=r(t,"color");return n(t,{color:""}),s!==o}))}function d(t){return Boolean(g.CSS_UNIT.exec(String(t)))}function x(t,r){let e=t;if("number"==typeof t&&0===Math.min(t,0)&&1===Math.max(t,1))return t;b(t)&&(e="100%");const n=f(e);return e=360===r?parseFloat(e):Math.min(r,Math.max(0,parseFloat(e))),n&&(e=e*r/100),Math.abs(e-r)<1e-6?1:(e=360===r?(e<0?e%r+r:e%r)/r:e%r/r,e)}function S(t){let r=parseFloat(""+t);return(Number.isNaN(r)||r<0||r>1)&&(r=1),r}function p(t){return Math.min(1,Math.max(0,t))}function A(e){n(t,{color:e});const o=r(t,"color");return n(t,{color:""}),o}function $(t){return s(255*t).toString(16)}function w(t){return H(t)/255}function H(t){return parseInt(t,16)}function v(t){return 1===t.length?"0"+t:String(t)}function R(t,r,e){const n=Math.max(t,r,e),o=Math.min(t,r,e);let s=0,i=0;const a=(n+o)/2;if(n===o)i=0,s=0;else{const h=n-o;i=a>.5?h/(2-n-o):h/(n+o),n===t&&(s=(r-e)/h+(r<e?6:0)),n===r&&(s=(e-t)/h+2),n===e&&(s=(t-r)/h+4),s/=6}return{h:s,s:i,l:a}}function F(t,r,e){let n=e;return n<0&&(n+=1),n>1&&(n-=1),n<1/6?t+6*n*(r-t):n<.5?r:n<2/3?t+(r-t)*(2/3-n)*6:t}function T(t,r,e){let n=0,o=0,s=0;if(0===r)o=e,s=e,n=e;else{const i=e<.5?e*(1+r):e+r-e*r,a=2*e-i;n=F(a,i,t+1/3),o=F(a,i,t),s=F(a,i,t-1/3)}return{r:n,g:o,b:s}}function y(t,r,e){let n=0,o=0;const s=Math.min(t,r,e),i=Math.max(t,r,e),a=1-i;if(i===s)return{h:0,w:s,b:a};t===s?(n=r-e,o=3):(n=r===s?e-t:t-r,o=r===s?5:1);const h=(o-n/(i-s))/6;return{h:1===h?0:h,w:s,b:a}}function M(t,r,e){if(r+e>=1){const t=r/(r+e);return{r:t,g:t,b:t}}let{r:n,g:o,b:s}=T(t,1,.5);return[n,o,s]=[n,o,s].map(t=>t*(1-r-e)+r),{r:n,g:o,b:s}}function C(t,r,e){const n=Math.max(t,r,e),o=Math.min(t,r,e);let s=0;const i=n-o;return n===o?s=0:(t===n&&(s=(r-e)/i+(r<e?6:0)),r===n&&(s=(e-t)/i+2),e===n&&(s=(t-r)/i+4),s/=6),{h:s,s:0===n?0:i/n,v:n}}function E(t,r,e){const n=6*t,o=r,s=e,i=Math.floor(n),a=n-i,h=s*(1-o),u=s*(1-a*o),c=s*(1-(1-a)*o),l=i%6;return{r:[s,u,h,h,c,s][l],g:[c,s,s,u,h,h][l],b:[h,h,c,s,s,u][l]}}function N(t,r,e,n){const o=[v(s(t).toString(16)),v(s(r).toString(16)),v(s(e).toString(16))];return n&&o[0].charAt(0)===o[0].charAt(1)&&o[1].charAt(0)===o[1].charAt(1)&&o[2].charAt(0)===o[2].charAt(1)?o[0].charAt(0)+o[1].charAt(0)+o[2].charAt(0):o.join("")}function I(t,r,e,n,o){const i=[v(s(t).toString(16)),v(s(r).toString(16)),v(s(e).toString(16)),v($(n))];return o&&i[0].charAt(0)===i[0].charAt(1)&&i[1].charAt(0)===i[1].charAt(1)&&i[2].charAt(0)===i[2].charAt(1)&&i[3].charAt(0)===i[3].charAt(1)?i[0].charAt(0)+i[1].charAt(0)+i[2].charAt(0)+i[3].charAt(0):i.join("")}function j(t){let r=t.trim().toLowerCase();if(0===r.length)return{r:0,g:0,b:0,a:1};if(m(r))r=A(r);else if(o.includes(r)){return{r:0,g:0,b:0,a:"transparent"===r?0:1,format:"rgb",ok:!0}}let[,e,n,s,i]=g.rgb.exec(r)||[];return e&&n&&s?{r:e,g:n,b:s,a:void 0!==i?i:1,format:"rgb"}:([,e,n,s,i]=g.hsl.exec(r)||[],e&&n&&s?{h:e,s:n,l:s,a:void 0!==i?i:1,format:"hsl"}:([,e,n,s,i]=g.hsv.exec(r)||[],e&&n&&s?{h:e,s:n,v:s,a:void 0!==i?i:1,format:"hsv"}:([,e,n,s,i]=g.hwb.exec(r)||[],e&&n&&s?{h:e,w:n,b:s,a:void 0!==i?i:1,format:"hwb"}:([,e,n,s,i]=g.hex8.exec(r)||[],e&&n&&s&&i?{r:H(e),g:H(n),b:H(s),a:w(i),format:"hex"}:([,e,n,s]=g.hex6.exec(r)||[],e&&n&&s?{r:H(e),g:H(n),b:H(s),format:"hex"}:([,e,n,s,i]=g.hex4.exec(r)||[],e&&n&&s&&i?{r:H(e+e),g:H(n+n),b:H(s+s),a:w(i+i),format:"hex"}:([,e,n,s]=g.hex3.exec(r)||[],!!(e&&n&&s)&&{r:H(e+e),g:H(n+n),b:H(s+s),format:"hex"})))))))}function k(t){let r={r:0,g:0,b:0},e=t,n=1,o=null,s=null,a=null,h=null,u=null,c=null,l=null,g=null,b=!1;const m="object"==typeof e&&e.format;let p=m&&i.includes(m)?m:"rgb";return"string"==typeof t&&(e=j(t),e&&(b=!0)),"object"==typeof e&&(d(e.r)&&d(e.g)&&d(e.b)&&(({r:l,g:g,b:u}=e),[l,g,u]=[l,g,u].map(t=>x(t,f(t)?100:255)),r={r:l,g:g,b:u},b=!0,p=e.format||"rgb"),d(e.h)&&d(e.s)&&d(e.v)&&(({h:c,s:o,v:s}=e),c=x(c,360),o=x(o,100),s=x(s,100),r=E(c,o,s),b=!0,p="hsv"),d(e.h)&&d(e.s)&&d(e.l)&&(({h:c,s:o,l:a}=e),c=x(c,360),o=x(o,100),a=x(a,100),r=T(c,o,a),b=!0,p="hsl"),d(e.h)&&d(e.w)&&d(e.b)&&(({h:c,w:h,b:u}=e),c=x(c,360),h=x(h,100),u=x(u,100),r=M(c,h,u),b=!0,p="hwb"),d(e.a)&&(n=e.a,n=f(""+n)||parseFloat(n)>1?x(n,100):n)),void 0===e&&(b=!0),{ok:b,format:p,r:r.r,g:r.g,b:r.b,a:S(n)}}class _{constructor(t,r){let e=t;const n=r&&i.includes(r)?r:"";e instanceof _&&(e=k(e));const{r:o,g:s,b:a,a:h,ok:u,format:c}=k(e);this.originalInput=t,this.r=o,this.g=s,this.b=a,this.a=h,this.ok=u,this.format=n||c}get isValid(){return this.ok}get isDark(){return this.brightness<120}get luminance(){const{r:t,g:r,b:e}=this;let n=0,o=0,s=0;return n=t<=.03928?t/12.92:((t+.055)/1.055)**2.4,o=r<=.03928?r/12.92:((r+.055)/1.055)**2.4,s=e<=.03928?e/12.92:((e+.055)/1.055)**2.4,.2126*n+.7152*o+.0722*s}get brightness(){const{r:t,g:r,b:e}=this.toRgb();return(299*t+587*r+114*e)/1e3}toRgb(){let{r:t,g:r,b:e,a:n}=this;return[t,r,e]=[t,r,e].map(t=>s(255*t*100)/100),n=s(100*n)/100,{r:t,g:r,b:e,a:n}}toRgbString(){const{r:t,g:r,b:e,a:n}=this.toRgb(),[o,i,a]=[t,r,e].map(s);return 1===n?`rgb(${o}, ${i}, ${a})`:`rgba(${o}, ${i}, ${a}, ${n})`}toRgbCSS4String(){const{r:t,g:r,b:e,a:n}=this.toRgb(),[o,i,a]=[t,r,e].map(s);return`rgb(${o} ${i} ${a}${1===n?"":` / ${s(100*n)}%`})`}toHex(t){const{r:r,g:e,b:n,a:o}=this.toRgb();return 1===o?N(r,e,n,t):I(r,e,n,o,t)}toHexString(t){return"#"+this.toHex(t)}toHex8(t){const{r:r,g:e,b:n,a:o}=this.toRgb();return I(r,e,n,o,t)}toHex8String(t){return"#"+this.toHex8(t)}toHsv(){const{r:t,g:r,b:e,a:n}=this,{h:o,s:s,v:i}=C(t,r,e);return{h:o,s:s,v:i,a:n}}toHsl(){const{r:t,g:r,b:e,a:n}=this,{h:o,s:s,l:i}=R(t,r,e);return{h:o,s:s,l:i,a:n}}toHslString(){let{h:t,s:r,l:e,a:n}=this.toHsl();return t=s(360*t),r=s(100*r),e=s(100*e),n=s(100*n)/100,1===n?`hsl(${t}, ${r}%, ${e}%)`:`hsla(${t}, ${r}%, ${e}%, ${n})`}toHslCSS4String(){let{h:t,s:r,l:e,a:n}=this.toHsl();t=s(360*t),r=s(100*r),e=s(100*e),n=s(100*n);return`hsl(${t}deg ${r}% ${e}%${n<100?` / ${s(n)}%`:""})`}toHwb(){const{r:t,g:r,b:e,a:n}=this,{h:o,w:s,b:i}=y(t,r,e);return{h:o,w:s,b:i,a:n}}toHwbString(){let{h:t,w:r,b:e,a:n}=this.toHwb();t=s(360*t),r=s(100*r),e=s(100*e),n=s(100*n);return`hwb(${t}deg ${r}% ${e}%${n<100?` / ${s(n)}%`:""})`}setAlpha(t){return"number"!=typeof t||(this.a=S(t)),this}saturate(t){if("number"!=typeof t)return this;const{h:r,s:n,l:o}=this.toHsl(),{r:s,g:i,b:a}=T(r,p(n+t/100),o);return e(this,{r:s,g:i,b:a}),this}desaturate(t){return"number"==typeof t?this.saturate(-t):this}greyscale(){return this.saturate(-100)}lighten(t){if("number"!=typeof t)return this;const{h:r,s:n,l:o}=this.toHsl(),{r:s,g:i,b:a}=T(r,n,p(o+t/100));return e(this,{r:s,g:i,b:a}),this}darken(t){return"number"==typeof t?this.lighten(-t):this}spin(t){if("number"!=typeof t)return this;const{h:r,s:n,l:o}=this.toHsl(),{r:s,g:i,b:a}=T(p((360*r+t)%360/360),n,o);return e(this,{r:s,g:i,b:a}),this}clone(){return new _(this)}toString(t){const{format:r}=this;return"hex"===r?this.toHexString(t):"hsl"===r?this.toHslString():"hwb"===r?this.toHwbString():this.toRgbString()}}return e(_,{ANGLES:"deg|rad|grad|turn",CSS_ANGLE:a,CSS_INTEGER:"[-\\+]?\\d+%?",CSS_NUMBER:"[-\\+]?\\d*\\.\\d+%?",CSS_UNIT:h,CSS_UNIT2:u,PERMISSIVE_MATCH:l,matchers:g,isOnePointZero:b,isPercentage:f,isValidCSSUnit:d,isColorName:m,pad2:v,clamp01:p,bound01:x,boundAlpha:S,getRGBFromName:A,convertHexToDecimal:w,convertDecimalToHex:$,rgbToHsl:R,rgbToHex:N,rgbToHsv:C,rgbToHwb:y,rgbaToHex:I,hslToRgb:T,hsvToRgb:E,hueToRgb:F,hwbToRgb:M,parseIntFromHex:H,stringInputToObject:j,inputToRGB:k,roundPart:s,getElementStyle:r,setElementStyle:n,ObjectAssign:e}),_}));
|
package/src/js/color-palette.js
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
import ObjectAssign from 'shorter-js/src/misc/ObjectAssign';
|
2
|
-
|
3
|
-
import roundPart from './util/roundPart';
|
4
|
-
import Color from './color';
|
5
|
-
|
6
|
-
/**
|
7
|
-
* @class
|
8
|
-
* Returns a color palette with a given set of parameters.
|
9
|
-
* @example
|
10
|
-
* new ColorPalette(0, 12, 10);
|
11
|
-
* // => { hue: 0, hueSteps: 12, lightSteps: 10, colors: Array<Color> }
|
12
|
-
*/
|
13
|
-
export default class ColorPalette {
|
14
|
-
/**
|
15
|
-
* The `hue` parameter is optional, which would be set to 0.
|
16
|
-
* @param {number[]} args represeinting hue, hueSteps, lightSteps
|
17
|
-
* * `args.hue` the starting Hue [0, 360]
|
18
|
-
* * `args.hueSteps` Hue Steps Count [5, 24]
|
19
|
-
* * `args.lightSteps` Lightness Steps Count [5, 12]
|
20
|
-
*/
|
21
|
-
constructor(...args) {
|
22
|
-
let hue = 0;
|
23
|
-
let hueSteps = 12;
|
24
|
-
let lightSteps = 10;
|
25
|
-
let lightnessArray = [0.5];
|
26
|
-
|
27
|
-
if (args.length === 3) {
|
28
|
-
[hue, hueSteps, lightSteps] = args;
|
29
|
-
} else if (args.length === 2) {
|
30
|
-
[hueSteps, lightSteps] = args;
|
31
|
-
if ([hueSteps, lightSteps].some((n) => n < 1)) {
|
32
|
-
throw TypeError('ColorPalette: both arguments must be higher than 0.');
|
33
|
-
}
|
34
|
-
}
|
35
|
-
|
36
|
-
/** @type {*} */
|
37
|
-
const colors = [];
|
38
|
-
const hueStep = 360 / hueSteps;
|
39
|
-
const half = roundPart((lightSteps - (lightSteps % 2 ? 1 : 0)) / 2);
|
40
|
-
const steps1To13 = [0.25, 0.2, 0.15, 0.11, 0.09, 0.075];
|
41
|
-
const lightSets = [[1, 2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13]];
|
42
|
-
const closestSet = lightSets.find((set) => set.includes(lightSteps));
|
43
|
-
|
44
|
-
// find a lightStep that won't go beyond black and white
|
45
|
-
// something within the [10-90] range of lightness
|
46
|
-
const lightStep = closestSet
|
47
|
-
? steps1To13[lightSets.indexOf(closestSet)]
|
48
|
-
: (100 / (lightSteps + (lightSteps % 2 ? 0 : 1)) / 100);
|
49
|
-
|
50
|
-
// light tints
|
51
|
-
for (let i = 1; i < half + 1; i += 1) {
|
52
|
-
lightnessArray = [...lightnessArray, (0.5 + lightStep * (i))];
|
53
|
-
}
|
54
|
-
|
55
|
-
// dark tints
|
56
|
-
for (let i = 1; i < lightSteps - half; i += 1) {
|
57
|
-
lightnessArray = [(0.5 - lightStep * (i)), ...lightnessArray];
|
58
|
-
}
|
59
|
-
|
60
|
-
// feed `colors` Array
|
61
|
-
for (let i = 0; i < hueSteps; i += 1) {
|
62
|
-
const currentHue = ((hue + i * hueStep) % 360) / 360;
|
63
|
-
lightnessArray.forEach((l) => {
|
64
|
-
colors.push(new Color({ h: currentHue, s: 1, l }));
|
65
|
-
});
|
66
|
-
}
|
67
|
-
|
68
|
-
this.hue = hue;
|
69
|
-
this.hueSteps = hueSteps;
|
70
|
-
this.lightSteps = lightSteps;
|
71
|
-
this.colors = colors;
|
72
|
-
}
|
73
|
-
}
|
74
|
-
|
75
|
-
ObjectAssign(ColorPalette, { Color });
|
@@ -1,107 +0,0 @@
|
|
1
|
-
import createElement from 'shorter-js/src/misc/createElement';
|
2
|
-
import ObjectAssign from 'shorter-js/src/misc/ObjectAssign';
|
3
|
-
import setAttribute from 'shorter-js/src/attr/setAttribute';
|
4
|
-
import getAttribute from 'shorter-js/src/attr/getAttribute';
|
5
|
-
|
6
|
-
import Color from './color';
|
7
|
-
import ColorPicker from './color-picker';
|
8
|
-
import ColorPalette from './color-palette';
|
9
|
-
import toggleCEAttr from './util/toggleCEAttr';
|
10
|
-
import Version from './util/version';
|
11
|
-
|
12
|
-
let CPID = 0;
|
13
|
-
|
14
|
-
/**
|
15
|
-
* `ColorPickerElement` Web Component.
|
16
|
-
* @example
|
17
|
-
* <label for="UNIQUE_ID">Label</label>
|
18
|
-
* <color-picker data-id="UNIQUE_ID" data-value="red" data-format="hex">
|
19
|
-
* </color-picker>
|
20
|
-
* // or
|
21
|
-
* <label for="UNIQUE_ID">Label</label>
|
22
|
-
* <color-picker data-id="UNIQUE_ID" data-value="red" data-format="hex"></color-picker>
|
23
|
-
*/
|
24
|
-
class ColorPickerElement extends HTMLElement {
|
25
|
-
constructor() {
|
26
|
-
super();
|
27
|
-
this.attachShadow({ mode: 'open' });
|
28
|
-
}
|
29
|
-
|
30
|
-
/**
|
31
|
-
* Returns the current color value.
|
32
|
-
* @returns {string | undefined}
|
33
|
-
*/
|
34
|
-
get value() { return this.input && this.input.value; }
|
35
|
-
|
36
|
-
connectedCallback() {
|
37
|
-
const self = this;
|
38
|
-
if (self.input) return;
|
39
|
-
|
40
|
-
let id = getAttribute(self, 'data-id');
|
41
|
-
const value = getAttribute(self, 'data-value') || '#fff';
|
42
|
-
const format = getAttribute(self, 'data-format') || 'rgb';
|
43
|
-
const placeholder = getAttribute(self, 'data-placeholder') || '';
|
44
|
-
|
45
|
-
if (!id) {
|
46
|
-
id = `color-picker-${format}-${CPID}`;
|
47
|
-
CPID += 1;
|
48
|
-
}
|
49
|
-
|
50
|
-
const input = createElement({
|
51
|
-
tagName: 'input',
|
52
|
-
type: 'text',
|
53
|
-
className: 'color-preview btn-appearance',
|
54
|
-
});
|
55
|
-
|
56
|
-
setAttribute(input, 'id', id);
|
57
|
-
setAttribute(input, 'name', id);
|
58
|
-
setAttribute(input, 'autocomplete', 'off');
|
59
|
-
setAttribute(input, 'spellcheck', 'false');
|
60
|
-
setAttribute(input, 'value', value);
|
61
|
-
setAttribute(input, 'placeholder', placeholder);
|
62
|
-
self.append(input);
|
63
|
-
|
64
|
-
/** @type {HTMLInputElement} */
|
65
|
-
self.input = input;
|
66
|
-
|
67
|
-
self.colorPicker = new ColorPicker(input);
|
68
|
-
|
69
|
-
self.shadowRoot.append(createElement('slot'));
|
70
|
-
|
71
|
-
// remove Attributes
|
72
|
-
toggleCEAttr(self);
|
73
|
-
}
|
74
|
-
|
75
|
-
/** @this {ColorPickerElement} */
|
76
|
-
disconnectedCallback() {
|
77
|
-
const self = this;
|
78
|
-
const { input, colorPicker, shadowRoot } = self;
|
79
|
-
|
80
|
-
const callback = () => {
|
81
|
-
// remove markup
|
82
|
-
input.remove();
|
83
|
-
colorPicker.dispose();
|
84
|
-
shadowRoot.innerHTML = '';
|
85
|
-
|
86
|
-
ObjectAssign(self, {
|
87
|
-
colorPicker: undefined,
|
88
|
-
input: undefined,
|
89
|
-
});
|
90
|
-
};
|
91
|
-
|
92
|
-
// re-add Attributes
|
93
|
-
toggleCEAttr(self, callback);
|
94
|
-
}
|
95
|
-
}
|
96
|
-
|
97
|
-
ObjectAssign(ColorPickerElement, {
|
98
|
-
Color,
|
99
|
-
ColorPicker,
|
100
|
-
ColorPalette,
|
101
|
-
getInstance: ColorPicker.getInstance,
|
102
|
-
Version,
|
103
|
-
});
|
104
|
-
|
105
|
-
customElements.define('color-picker', ColorPickerElement);
|
106
|
-
|
107
|
-
export default ColorPickerElement;
|