@thednp/color-picker 0.0.1-alpha3 → 0.0.2-alpha2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ // Color v0.0.2alpha2 | 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thednp/color-picker",
3
- "version": "0.0.1alpha3",
3
+ "version": "0.0.2alpha2",
4
4
  "description": "Modern Color Picker Web Component",
5
5
  "main": "dist/js/color-picker.min.js",
6
6
  "module": "dist/js/color-picker.esm.js",
@@ -18,7 +18,11 @@
18
18
  "registry": "https://registry.npmjs.org/"
19
19
  },
20
20
  "scripts": {
21
- "test": "echo \"Error: no test specified\" && exit 1",
21
+ "instrument": "npx nyc instrument --compact=false src/js cypress/instrumented",
22
+ "test": "npm run instrument && npx cypress run",
23
+ "cypress": "npm run instrument && npx cypress open",
24
+ "coverage:report": "npx nyc report --exclude=src/js/util --reporter=lcov --reporter=json --reporter=text --reporter=json-summary",
25
+ "coverage:badge": "npx --check-coverage update-badge",
22
26
  "fix:js": "eslint src/ --config .eslintrc --fix",
23
27
  "lint:js": "eslint src/ --config .eslintrc",
24
28
  "fix:css": "stylelint --config .stylelintrc.json -s --fix scss \"src/scss/*.scss\"",
@@ -31,6 +35,14 @@
31
35
  "build-ce-min": "rollup --environment FORMAT:umd,MIN:true,NAME:ColorPickerElement,INPUTFILE:src/js/color-picker-element.js,OUTPUTFILE:dist/js/color-picker-element.min.js -c",
32
36
  "build-ce-esm": "rollup --environment FORMAT:esm,MIN:false,NAME:ColorPickerElement,INPUTFILE:src/js/color-picker-element.js,OUTPUTFILE:dist/js/color-picker-element-esm.js -c",
33
37
  "build-ce-esm-min": "rollup --environment FORMAT:esm,MIN:true,NAME:ColorPickerElement,INPUTFILE:src/js/color-picker-element.js,OUTPUTFILE:dist/js/color-picker-element-esm.min.js -c",
38
+ "build-cl": "rollup --environment FORMAT:umd,MIN:false,NAME:Color,INPUTFILE:src/js/color.js,OUTPUTFILE:dist/js/color.js -c",
39
+ "build-cl-min": "rollup --environment FORMAT:umd,MIN:true,NAME:Color,INPUTFILE:src/js/color.js,OUTPUTFILE:dist/js/color.min.js -c",
40
+ "build-cl-esm": "rollup --environment FORMAT:esm,MIN:false,NAME:Color,INPUTFILE:src/js/color.js,OUTPUTFILE:dist/js/color-esm.js -c",
41
+ "build-cl-esm-min": "rollup --environment FORMAT:esm,MIN:true,NAME:Color,INPUTFILE:src/js/color.js,OUTPUTFILE:dist/js/color-esm.min.js -c",
42
+ "build-cpl": "rollup --environment FORMAT:umd,MIN:false,NAME:ColorPalette,INPUTFILE:src/js/color-palette.js,OUTPUTFILE:dist/js/color-palette.js -c",
43
+ "build-cpl-min": "rollup --environment FORMAT:umd,MIN:true,NAME:ColorPalette,INPUTFILE:src/js/color-palette.js,OUTPUTFILE:dist/js/color-palette.min.js -c",
44
+ "build-cpl-esm": "rollup --environment FORMAT:esm,MIN:false,NAME:ColorPalette,INPUTFILE:src/js/color-palette.js,OUTPUTFILE:dist/js/color-palette-esm.js -c",
45
+ "build-cpl-esm-min": "rollup --environment FORMAT:esm,MIN:true,NAME:ColorPalette,INPUTFILE:src/js/color-palette.js,OUTPUTFILE:dist/js/color-palette-esm.min.js -c",
34
46
  "build-minjs": "rollup --environment FORMAT:umd,MIN:true -c",
35
47
  "build-esm": "rollup --environment FORMAT:esm,MIN:false -c",
36
48
  "build-esmjs": "rollup --environment FORMAT:esm,MIN:true -c",
@@ -68,15 +80,22 @@
68
80
  "shorter-js": "^0.3.4"
69
81
  },
70
82
  "devDependencies": {
83
+ "@bahmutov/cypress-esbuild-preprocessor": "^2.1.3",
84
+ "@cypress/code-coverage": "^3.9.12",
71
85
  "@rollup/plugin-buble": "^0.21.3",
86
+ "@rollup/plugin-commonjs": "^21.0.3",
72
87
  "@rollup/plugin-json": "^4.1.0",
73
88
  "@rollup/plugin-node-resolve": "^7.1.3",
89
+ "babel-plugin-istanbul": "^6.1.1",
90
+ "check-code-coverage": "^1.10.0",
91
+ "cypress": "^9.5.4",
92
+ "esbuild": "^0.14.30",
74
93
  "eslint": "^7.22.0",
75
94
  "eslint-config-airbnb-base": "^14.2.1",
76
95
  "eslint-plugin-import": "^2.22.1",
77
96
  "eslint-plugin-vue": "^7.7.0",
78
97
  "npm-run-all": "^4.1.5",
79
- "rollup": "^1.32.1",
98
+ "rollup": "^2.70.1",
80
99
  "rollup-plugin-terser": "^5.3.1",
81
100
  "sass": "^1.39.0",
82
101
  "stylelint": "^13.12.0",
@@ -1,3 +1,5 @@
1
+ import ObjectAssign from 'shorter-js/src/misc/ObjectAssign';
2
+
1
3
  import roundPart from './util/roundPart';
2
4
  import Color from './color';
3
5
 
@@ -6,7 +8,7 @@ import Color from './color';
6
8
  * Returns a color palette with a given set of parameters.
7
9
  * @example
8
10
  * new ColorPalette(0, 12, 10);
9
- * // => { hue: 0, hueSteps: 12, lightSteps: 10, colors: array }
11
+ * // => { hue: 0, hueSteps: 12, lightSteps: 10, colors: Array<Color> }
10
12
  */
11
13
  export default class ColorPalette {
12
14
  /**
@@ -26,24 +28,24 @@ export default class ColorPalette {
26
28
  [hue, hueSteps, lightSteps] = args;
27
29
  } else if (args.length === 2) {
28
30
  [hueSteps, lightSteps] = args;
29
- } else {
30
- throw TypeError('ColorPalette requires minimum 2 arguments');
31
+ if ([hueSteps, lightSteps].some((n) => n < 1)) {
32
+ throw TypeError('ColorPalette: both arguments must be higher than 0.');
33
+ }
31
34
  }
32
35
 
33
- /** @type {string[]} */
36
+ /** @type {*} */
34
37
  const colors = [];
35
-
36
38
  const hueStep = 360 / hueSteps;
37
39
  const half = roundPart((lightSteps - (lightSteps % 2 ? 1 : 0)) / 2);
38
- const estimatedStep = 100 / (lightSteps + (lightSteps % 2 ? 0 : 1)) / 100;
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));
39
43
 
40
- let lightStep = 0.25;
41
- lightStep = [4, 5].includes(lightSteps) ? 0.2 : lightStep;
42
- lightStep = [6, 7].includes(lightSteps) ? 0.15 : lightStep;
43
- lightStep = [8, 9].includes(lightSteps) ? 0.11 : lightStep;
44
- lightStep = [10, 11].includes(lightSteps) ? 0.09 : lightStep;
45
- lightStep = [12, 13].includes(lightSteps) ? 0.075 : lightStep;
46
- lightStep = lightSteps > 13 ? estimatedStep : lightStep;
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);
47
49
 
48
50
  // light tints
49
51
  for (let i = 1; i < half + 1; i += 1) {
@@ -59,7 +61,7 @@ export default class ColorPalette {
59
61
  for (let i = 0; i < hueSteps; i += 1) {
60
62
  const currentHue = ((hue + i * hueStep) % 360) / 360;
61
63
  lightnessArray.forEach((l) => {
62
- colors.push(new Color({ h: currentHue, s: 1, l }).toHexString());
64
+ colors.push(new Color({ h: currentHue, s: 1, l }));
63
65
  });
64
66
  }
65
67
 
@@ -69,3 +71,5 @@ export default class ColorPalette {
69
71
  this.colors = colors;
70
72
  }
71
73
  }
74
+
75
+ ObjectAssign(ColorPalette, { Color });
@@ -5,9 +5,9 @@ import setAttribute from 'shorter-js/src/attr/setAttribute';
5
5
  import getAttribute from 'shorter-js/src/attr/getAttribute';
6
6
 
7
7
  import Color from './color';
8
- import ColorPicker, { getColorPickerInstance } from './color-picker';
8
+ import ColorPicker from './color-picker';
9
9
  import ColorPalette from './color-palette';
10
- import Version from './version';
10
+ import Version from './util/version';
11
11
 
12
12
  let CPID = 0;
13
13
 
@@ -15,90 +15,82 @@ let CPID = 0;
15
15
  * `ColorPickerElement` Web Component.
16
16
  * @example
17
17
  * <label for="UNIQUE_ID">Label</label>
18
- * <color-picker data-format="hex" data-value="#075">
19
- * <input id="UNIQUE_ID" type="text" class="color-preview btn-appearance">
18
+ * <color-picker>
19
+ * <input id="UNIQUE_ID" value="red" format="hex" class="color-preview btn-appearance">
20
20
  * </color-picker>
21
+ * // or
22
+ * <label for="UNIQUE_ID">Label</label>
23
+ * <color-picker data-id="UNIQUE_ID" data-value="red" data-format="hex"></color-picker>
21
24
  */
22
25
  class ColorPickerElement extends HTMLElement {
23
26
  constructor() {
24
27
  super();
25
- /** @type {boolean} */
26
- this.isDisconnected = true;
27
28
  this.attachShadow({ mode: 'open' });
28
29
  }
29
30
 
30
31
  /**
31
32
  * Returns the current color value.
32
- * @returns {string?}
33
+ * @returns {string | undefined}
33
34
  */
34
- get value() { return this.input ? this.input.value : null; }
35
+ get value() { return this.input && this.input.value; }
35
36
 
36
37
  connectedCallback() {
37
- if (this.colorPicker) {
38
- if (this.isDisconnected) {
39
- this.isDisconnected = false;
40
- }
41
- return;
42
- }
38
+ if (this.input) return;
43
39
 
44
- const inputs = getElementsByTagName('input', this);
40
+ let [input] = getElementsByTagName('input', this);
41
+ const value = (input && getAttribute(input, 'value')) || getAttribute(this, 'data-value') || '#fff';
42
+ const format = (input && getAttribute(input, 'format')) || getAttribute(this, 'data-format') || 'rgb';
43
+ let id = (input && getAttribute(input, 'id')) || getAttribute(this, 'data-id');
45
44
 
46
- if (!inputs.length) {
47
- const label = getAttribute(this, 'data-label');
48
- const value = getAttribute(this, 'data-value') || '#069';
49
- const format = getAttribute(this, 'data-format') || 'rgb';
50
- const newInput = createElement({
45
+ if (!id) {
46
+ id = `color-picker-${format}-${CPID}`;
47
+ CPID += 1;
48
+ }
49
+
50
+ if (!input) {
51
+ input = createElement({
51
52
  tagName: 'input',
52
53
  type: 'text',
53
54
  className: 'color-preview btn-appearance',
54
55
  });
55
- let id = getAttribute(this, 'data-id');
56
- if (!id) {
57
- id = `color-picker-${format}-${CPID}`;
58
- CPID += 1;
59
- }
60
-
61
- const labelElement = createElement({ tagName: 'label', innerText: label || 'Color Picker' });
62
- this.before(labelElement);
63
- setAttribute(labelElement, 'for', id);
64
- setAttribute(newInput, 'id', id);
65
- setAttribute(newInput, 'name', id);
66
- setAttribute(newInput, 'autocomplete', 'off');
67
- setAttribute(newInput, 'spellcheck', 'false');
68
- setAttribute(newInput, 'value', value);
69
- this.append(newInput);
70
- }
71
56
 
72
- const [input] = inputs;
73
-
74
- if (input) {
75
- /** @type {HTMLInputElement} */
76
- // @ts-ignore - `HTMLInputElement` is `HTMLElement`
77
- this.input = input;
78
-
79
- // @ts-ignore - `HTMLInputElement` is `HTMLElement`
80
- this.colorPicker = new ColorPicker(input);
81
- this.color = this.colorPicker.color;
57
+ setAttribute(input, 'id', id);
58
+ setAttribute(input, 'name', id);
59
+ setAttribute(input, 'autocomplete', 'off');
60
+ setAttribute(input, 'spellcheck', 'false');
61
+ setAttribute(input, 'value', value);
62
+ this.append(input);
63
+ }
64
+ /** @type {HTMLInputElement} */
65
+ // @ts-ignore - `HTMLInputElement` is `HTMLElement`
66
+ this.input = input;
82
67
 
83
- if (this.shadowRoot) {
84
- this.shadowRoot.append(createElement('slot'));
85
- }
68
+ // @ts-ignore - `HTMLInputElement` is `HTMLElement`
69
+ this.colorPicker = new ColorPicker(input);
86
70
 
87
- this.isDisconnected = false;
88
- }
71
+ // @ts-ignore - `shadowRoot` is defined in the constructor
72
+ this.shadowRoot.append(createElement('slot'));
89
73
  }
90
74
 
75
+ /** @this {ColorPickerElement} */
91
76
  disconnectedCallback() {
92
- if (this.colorPicker) this.colorPicker.dispose();
93
- this.isDisconnected = true;
77
+ const { input, colorPicker, shadowRoot } = this;
78
+ if (colorPicker) colorPicker.dispose();
79
+ if (input) input.remove();
80
+ if (shadowRoot) shadowRoot.innerHTML = '';
81
+
82
+ ObjectAssign(this, {
83
+ colorPicker: undefined,
84
+ input: undefined,
85
+ });
94
86
  }
95
87
  }
96
88
 
97
89
  ObjectAssign(ColorPickerElement, {
98
90
  Color,
99
91
  ColorPicker,
100
- ColorPalette,
101
- getInstance: getColorPickerInstance,
92
+ ColorPalette, // @ts-ignore
93
+ getInstance: ColorPicker.getInstance,
102
94
  Version,
103
95
  });
104
96