@thednp/color-picker 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. package/README.md +4 -4
  2. package/dist/css/color-picker.css +1 -1
  3. package/dist/css/color-picker.min.css +1 -1
  4. package/dist/css/color-picker.rtl.css +1 -1
  5. package/dist/css/color-picker.rtl.min.css +1 -1
  6. package/dist/js/color-picker.cjs +2 -2
  7. package/dist/js/color-picker.cjs.map +1 -1
  8. package/dist/js/color-picker.js +2 -2
  9. package/dist/js/color-picker.js.map +1 -1
  10. package/dist/js/color-picker.mjs +243 -239
  11. package/dist/js/color-picker.mjs.map +1 -1
  12. package/package.json +23 -26
  13. package/src/ts/colorPalette.ts +2 -2
  14. package/src/ts/index.ts +20 -19
  15. package/test/color-palette.test.ts +137 -0
  16. package/test/color-picker.test.ts +705 -0
  17. package/{cypress → test}/fixtures/getMarkup.js +8 -7
  18. package/test/fixtures/swipe.ts +33 -0
  19. package/test/fixtures/write.ts +37 -0
  20. package/tsconfig.json +45 -27
  21. package/vite.config.mts +5 -13
  22. package/vitest.config-ui.ts +26 -0
  23. package/vitest.config.ts +26 -0
  24. package/cypress/e2e/color-palette.cy.ts +0 -128
  25. package/cypress/e2e/color-picker.cy.ts +0 -909
  26. package/cypress/plugins/esbuild-istanbul.ts +0 -50
  27. package/cypress/plugins/tsCompile.ts +0 -34
  28. package/cypress/support/commands.ts +0 -0
  29. package/cypress/support/e2e.ts +0 -21
  30. package/cypress/test.html +0 -23
  31. package/cypress.config.ts +0 -30
  32. /package/{cypress → test}/fixtures/colorNamesFrench.js +0 -0
  33. /package/{cypress → test}/fixtures/componentLabelsFrench.js +0 -0
  34. /package/{cypress → test}/fixtures/format.js +0 -0
  35. /package/{cypress → test}/fixtures/getRandomInt.js +0 -0
  36. /package/{cypress → test}/fixtures/sampleWebcolors.js +0 -0
  37. /package/{cypress → test}/fixtures/testSample.js +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thednp/color-picker",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "author": "thednp",
5
5
  "license": "MIT",
6
6
  "description": "🎨 Modern Color Picker Component",
@@ -56,41 +56,38 @@
56
56
  "typescript"
57
57
  ],
58
58
  "dependencies": {
59
- "@thednp/color": "^1.0.9",
60
- "@thednp/shorty": "2.0.3"
59
+ "@thednp/color": "^1.0.11",
60
+ "@thednp/shorty": "^2.0.4"
61
61
  },
62
62
  "devDependencies": {
63
- "@bahmutov/cypress-esbuild-preprocessor": "^2.2.2",
64
- "@cypress/code-coverage": "^3.12.44",
65
- "@types/istanbul-lib-instrument": "^1.7.7",
66
- "@types/node": "^22.2.0",
63
+ "@types/node": "^22.5.5",
67
64
  "@typescript-eslint/eslint-plugin": "^6.21.0",
68
65
  "@typescript-eslint/parser": "^6.21.0",
69
- "cypress": "^13.13.2",
66
+ "@vitest/browser": "^2.1.1",
67
+ "@vitest/coverage-istanbul": "^2.1.1",
68
+ "@vitest/ui": "^2.1.1",
70
69
  "dts-bundle-generator": "^9.5.1",
71
- "eslint": "^8.57.0",
70
+ "eslint": "^8.57.1",
72
71
  "eslint-plugin-jsdoc": "^46.10.1",
73
72
  "eslint-plugin-prefer-arrow": "^1.2.3",
74
73
  "eslint-plugin-prettier": "^5.2.1",
75
- "istanbul-lib-coverage": "^3.2.2",
76
- "istanbul-lib-instrument": "^6.0.3",
77
- "ncp": "^2.0.0",
78
- "nyc": "^15.1.0",
74
+ "playwright": "^1.47.1",
79
75
  "prettier": "^3.3.3",
80
- "sass": "^1.77.8",
76
+ "sass": "^1.79.1",
81
77
  "stylelint": "^15.11.0",
82
78
  "stylelint-config-standard": "^34.0.0",
83
79
  "stylelint-config-standard-scss": "^11.1.0",
84
80
  "stylelint-order": "^6.0.4",
85
81
  "stylelint-scss": "^5.3.2",
86
- "typescript": "^5.5.4",
87
- "vite": "^5.4.0"
82
+ "typescript": "^5.6.2",
83
+ "vite": "^5.4.6",
84
+ "vitest": "^2.1.1"
88
85
  },
89
86
  "scripts": {
90
87
  "pre-test": "pnpm clean-coverage",
91
- "test": "pnpm pre-test && cypress run",
92
- "cypress": "pnpm pre-test && npx cypress open",
93
- "badges": "npx -p dependency-version-badge update-badge typescript cypress eslint prettier vite",
88
+ "test": "pnpm pre-test && vitest --config vitest.config.ts",
89
+ "test-ui": "pnpm pre-test && vitest --config vitest.config-ui.ts --browser=chrome",
90
+ "badges": "npx -p dependency-version-badge update-badge typescript eslint prettier vitest vite",
94
91
  "clean-coverage": "rm -rf coverage && rm -rf .nyc_output",
95
92
  "coverage:report": "nyc report --reporter=lcov --reporter=json --reporter=text --reporter=json-summary",
96
93
  "format": "prettier --write \"src/**/*.ts\"",
@@ -100,14 +97,14 @@
100
97
  "check:ts": "tsc --noEmit",
101
98
  "fix:css": "stylelint --config .stylelintrc.json --fix scss \"src/scss/*.scss\"",
102
99
  "lint:css": "stylelint --config .stylelintrc.json scss \"src/scss/*.scss\"",
103
- "build": "pnpm lint && pnpm build-vite && pnpm dts && pnpm docs",
104
- "build-vite": "vite build",
100
+ "build": "vite build && pnpm dts",
105
101
  "dev": "vite --open ./docs/dev.html --port 8577",
106
- "dts": "dts-bundle-generator --config ./dts.config.ts",
107
- "docs": "ncp dist/js/color-picker.js ./docs/js/color-picker.js && ncp dist/js/color-picker.js.map docs/js/color-picker.js.map",
108
- "copy": "pnpm copy-css-cp && pnpm copy-css-rtl",
109
- "copy-css-cp": "ncp dist/css/color-picker.css docs/css/color-picker.css",
110
- "copy-css-rtl": "ncp dist/css/color-picker.rtl.css docs/css/color-picker.rtl.css",
102
+ "dts": "pnpm dts-bundle-generator --config ./dts.config.ts",
103
+ "copy": "pnpm copy-css-cp && pnpm copy-css-rtl && pnpm copy-js && pnpm copy-js-map",
104
+ "copy-js": "cp dist/js/color-picker.js docs/js/color-picker.js",
105
+ "copy-js-map": "cp dist/js/color-picker.js.map docs/js/color-picker.js.map",
106
+ "copy-css-cp": "cp dist/css/color-picker.css docs/css/color-picker.css",
107
+ "copy-css-rtl": "cp dist/css/color-picker.rtl.css docs/css/color-picker.rtl.css",
111
108
  "compile": "pnpm lint:css && pnpm compile-scss && pnpm compile-scss-min && pnpm compile-scss-rtl && pnpm compile-scss-rtl-min && pnpm copy",
112
109
  "compile-scss": "node compile.cjs",
113
110
  "compile-scss-min": "node compile.cjs MIN:true",
@@ -73,9 +73,9 @@ export default class ColorPalette {
73
73
 
74
74
  // feed `colors` Array
75
75
  for (let i = 0; i < hueSteps; i += 1) {
76
- const currentHue = ((hue + i * hueStep) % 360) / 360;
76
+ const currentHue = (hue + i * hueStep) % 360;
77
77
  lightnessArray.forEach(l => {
78
- const newColor = new Color({ h: currentHue, s: 1, l });
78
+ const newColor = new Color({ h: currentHue, s: 100, l: l * 100 });
79
79
  colors.push(saturation < 100 ? newColor.saturate(saturation - 100) : newColor);
80
80
  });
81
81
  }
package/src/ts/index.ts CHANGED
@@ -117,8 +117,8 @@ const toggleEventsOnShown = (self: ColorPicker, action?: boolean) => {
117
117
  const doc = getDocument(input);
118
118
  const win = getWindow(doc);
119
119
 
120
- fn(self.controls, pointerdownEvent, self.pointerDown as EventListener);
121
- self.controlKnobs.forEach(x => fn(x, keydownEvent, self.handleKnobs as EventListener));
120
+ fn(self.controls, pointerdownEvent, self.pointerDown);
121
+ self.controlKnobs.forEach(x => fn(x, keydownEvent, self.handleKnobs));
122
122
 
123
123
  fn(win, scrollEvent, self.handleScroll);
124
124
  fn(win, resizeEvent, self.update);
@@ -127,13 +127,13 @@ const toggleEventsOnShown = (self: ColorPicker, action?: boolean) => {
127
127
 
128
128
  if (colorMenu) {
129
129
  fn(colorMenu, mouseclickEvent, self.menuClickHandler);
130
- fn(colorMenu, keydownEvent, self.menuKeyHandler as EventListener);
130
+ fn(colorMenu, keydownEvent, self.menuKeyHandler);
131
131
  }
132
132
 
133
- fn(doc, pointermoveEvent, self.pointerMove as EventListener);
134
- fn(doc, pointerupEvent, self.pointerUp as EventListener);
135
- fn(parent, focusoutEvent, self.handleFocusOut as EventListener);
136
- fn(doc, keyupEvent, self.handleDismiss as EventListener);
133
+ fn(doc, pointermoveEvent, self.pointerMove);
134
+ fn(doc, pointerupEvent, self.pointerUp);
135
+ fn(parent, focusoutEvent, self.handleFocusOut);
136
+ fn(doc, keyupEvent, self.handleDismiss);
137
137
  };
138
138
 
139
139
  /**
@@ -465,15 +465,15 @@ export default class ColorPicker {
465
465
  const [v1, v2, v3] = visuals;
466
466
  const { offsetHeight } = v1;
467
467
  const hue = controlPositions.c2y / offsetHeight;
468
- const { r, g, b } = new Color({ h: hue, s: 1, l: 0.5 }).toRgb();
468
+ const { r, g, b } = new Color({ h: hue * 360, s: 100, l: 50 }).toRgb();
469
469
  const whiteGrad = 'linear-gradient(rgb(255,255,255) 0%, rgb(255,255,255) 100%)';
470
470
  const alpha = 1 - controlPositions.c3y / offsetHeight;
471
471
  const roundA = roundPart(alpha * 100) / 100;
472
472
 
473
473
  const fill = new Color({
474
- h: hue,
475
- s: 1,
476
- l: 0.5,
474
+ h: hue * 360,
475
+ s: 100,
476
+ l: 50,
477
477
  a: alpha,
478
478
  }).toRgbString();
479
479
  const hueGradient = `linear-gradient(
@@ -547,8 +547,9 @@ export default class ColorPicker {
547
547
  const { previousElementSibling, nextElementSibling, parentElement } = target;
548
548
  const isColorOptionsMenu = parentElement && hasClass(parentElement, 'color-options');
549
549
  const allSiblings = parentElement ? [...parentElement.children] : [];
550
- const columnsCount =
551
- isColorOptionsMenu && getElementStyle(parentElement, 'grid-template-columns').split(' ').length;
550
+ // const columnsCount =
551
+ // isColorOptionsMenu && getElementStyle(parentElement, 'grid-template-columns').split(' ').length;
552
+ const columnsCount = isColorOptionsMenu && Number(getElementStyle(parentElement, '--grid-fit'));
552
553
  const currentIndex = allSiblings.indexOf(target);
553
554
  const previousElement = currentIndex > -1 && columnsCount && allSiblings[currentIndex - columnsCount];
554
555
  const nextElement = currentIndex > -1 && columnsCount && allSiblings[currentIndex + columnsCount];
@@ -861,9 +862,9 @@ export default class ColorPicker {
861
862
 
862
863
  // new color
863
864
  const { r, g, b, a } = new Color({
864
- h: hue,
865
- s: saturation,
866
- v: lightness,
865
+ h: hue * 360,
866
+ s: saturation * 100,
867
+ v: lightness * 100,
867
868
  a: alpha,
868
869
  });
869
870
 
@@ -908,9 +909,9 @@ export default class ColorPicker {
908
909
 
909
910
  // new color
910
911
  const { r, g, b, a } = new Color({
911
- h: hue,
912
- s: saturation,
913
- v: lightness,
912
+ h: hue * 360,
913
+ s: saturation * 100,
914
+ v: lightness * 100,
914
915
  a: alpha,
915
916
  });
916
917
 
@@ -0,0 +1,137 @@
1
+ import { expect, it, describe } from 'vitest';
2
+ import Color from "@thednp/color";
3
+ import ColorPalette from "~/ts/colorPalette";
4
+
5
+ describe("ColorPalette Class Test", () => {
6
+ it("Test init with no parameter, use all default values", () => {
7
+ const cpl = new ColorPalette();
8
+ expect(cpl).to.be.instanceOf(ColorPalette);
9
+ expect(cpl.hue).to.equal(0);
10
+ expect(cpl.hueSteps).to.equal(12);
11
+ expect(cpl.lightSteps).to.equal(10);
12
+ expect(cpl.colors.length).to.equal(120);
13
+ });
14
+
15
+ it("Test init with 1 parameter, throws error", () => {
16
+ try {
17
+ new ColorPalette(0, undefined);
18
+ } catch (error) {
19
+ expect(error).to.be.instanceOf(TypeError);
20
+ expect(error).to.have.property(
21
+ "message",
22
+ 'ColorPalette: the two minimum arguments must be numbers higher than 0.'
23
+ );
24
+ }
25
+ });
26
+
27
+ it("Test init with 2 invalid parameters, throws error", () => {
28
+ try {
29
+ new ColorPalette(0, 5);
30
+ } catch (error) {
31
+ expect(error).to.be.instanceOf(TypeError);
32
+ expect(error).to.have.property(
33
+ "message",
34
+ 'ColorPalette: the two minimum arguments must be numbers higher than 0.'
35
+ );
36
+ }
37
+ });
38
+
39
+ it("Test init with 2 valid parameters", () => {
40
+ // generated with new `ColorPalette(1, 5)`
41
+ const testSample = ["#330000", "#990000", "#ff0000", "#ff6666", "#ffcccc"];
42
+ const cpl = new ColorPalette(1, 5);
43
+
44
+ expect(cpl).to.be.instanceOf(ColorPalette);
45
+ expect(cpl.hue).to.equal(0);
46
+ expect(cpl.hueSteps).to.equal(1);
47
+ expect(cpl.lightSteps).to.equal(5);
48
+ expect(cpl.saturation).to.equal(100);
49
+ expect(cpl.colors.length).to.equal(5);
50
+
51
+ cpl.colors.forEach((color, i) => {
52
+ expect(color).to.be.instanceOf(Color);
53
+ expect(color.toHexString()).to.equal(testSample[i]);
54
+ })
55
+ });
56
+
57
+ it("Test init with 14 lightSteps", () => {
58
+ // generated with `new ColorPalette(1, 14)`
59
+ const testSample = [
60
+ "#330000", "#550000", "#770000", "#990000", "#bb0000",
61
+ "#dd0000", "#ff0000", "#ff2222", "#ff4444", "#ff6666",
62
+ "#ff8888", "#ffaaaa", "#ffcccc", "#ffeeee",
63
+ ];
64
+ const cpl = new ColorPalette(1, 14);
65
+
66
+ expect(cpl).to.be.instanceOf(ColorPalette);
67
+ expect(cpl.hue).to.equal(0);
68
+ expect(cpl.hueSteps).to.equal(1);
69
+ expect(cpl.lightSteps).to.equal(14);
70
+ expect(cpl.colors.length).to.equal(14);
71
+
72
+ cpl.colors.forEach((color, i) => {
73
+ expect(color).to.be.instanceOf(Color);
74
+ // expect(color.ok).to.be.true;
75
+ expect(color.toHexString()).to.equal(testSample[i]);
76
+ })
77
+ });
78
+
79
+ it("Test init with 15 lightSteps", () => {
80
+ // generated with `new ColorPalette(1, 15)`
81
+ const testSample = [
82
+ "#110000", "#330000", "#550000", "#770000",
83
+ "#990000", "#bb0000", "#dd0000", "#ff0000",
84
+ "#ff2222", "#ff4444", "#ff6666", "#ff8888",
85
+ "#ffaaaa", "#ffcccc", "#ffeeee",
86
+ ];
87
+ const cpl = new ColorPalette(1, 15);
88
+
89
+ expect(cpl).to.be.instanceOf(ColorPalette);
90
+ expect(cpl.hue).to.equal(0);
91
+ expect(cpl.hueSteps).to.equal(1);
92
+ expect(cpl.lightSteps).to.equal(15);
93
+ expect(cpl.colors.length).to.equal(15);
94
+
95
+ cpl.colors.forEach((color, i) => {
96
+ expect(color).to.be.instanceOf(Color);
97
+ // expect(color.ok).to.be.true;
98
+ expect(color.toHexString()).to.equal(testSample[i]);
99
+ })
100
+ });
101
+
102
+ it("Test init with 3 valid parameters", () => {
103
+ // generated with `new ColorPalette(270, 1, 10)`
104
+ const testSample = [
105
+ "#240047", "#3b0075", "#5200a3", "#6900d1",
106
+ "#7f00ff", "#962eff", "#ad5cff", "#c48aff",
107
+ "#dbb8ff", "#f2e5ff",
108
+ ];
109
+ const cpl = new ColorPalette(270, 1, 10);
110
+
111
+ expect(cpl).to.be.instanceOf(ColorPalette);
112
+ expect(cpl.hue).to.equal(270);
113
+ expect(cpl.hueSteps).to.equal(1);
114
+ expect(cpl.lightSteps).to.equal(10);
115
+ expect(cpl.saturation).to.equal(100);
116
+ expect(cpl.colors.length).to.equal(10);
117
+ expect(cpl.colors.map(c => c.toHexString())).to.deep.equal(testSample);
118
+ });
119
+
120
+ it("Test init with 4 valid parameters", () => {
121
+ // generated with `new ColorPalette(270, 1, 10)`
122
+ const testSample = [
123
+ "#240740", "#3b0c6a", "#521093", "#6915bc",
124
+ "#7f19e6", "#9643ea", "#ad6cef", "#c495f3",
125
+ "#dbbff8", "#f2e8fc",
126
+ ];
127
+ const cpl = new ColorPalette(270, 1, 10, 80);
128
+
129
+ expect(cpl).to.be.instanceOf(ColorPalette);
130
+ expect(cpl.hue).to.equal(270);
131
+ expect(cpl.hueSteps).to.equal(1);
132
+ expect(cpl.lightSteps).to.equal(10);
133
+ expect(cpl.saturation).to.equal(80);
134
+ expect(cpl.colors.length).to.equal(10);
135
+ expect(cpl.colors.map(c => c.toHexString())).to.deep.equal(testSample);
136
+ });
137
+ });