@thednp/color-picker 0.0.1 → 0.0.2-alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -43,7 +43,8 @@ export default function getColorMenu(self, colorsSource, menuClass) {
43
43
  optionSize = fit > 5 && isMultiLine ? 1.5 : optionSize;
44
44
  const menuHeight = `${(rowCount || 1) * optionSize}rem`;
45
45
  const menuHeightHover = `calc(${rowCountHover} * ${optionSize}rem + ${rowCountHover - 1} * ${gap})`;
46
-
46
+ /** @type {HTMLUListElement} */
47
+ // @ts-ignore -- <UL> is an `HTMLElement`
47
48
  const menu = createElement({
48
49
  tagName: 'ul',
49
50
  className: finalClass,
@@ -51,7 +52,7 @@ export default function getColorMenu(self, colorsSource, menuClass) {
51
52
  setAttribute(menu, 'role', 'listbox');
52
53
  setAttribute(menu, ariaLabel, menuLabel);
53
54
 
54
- if (isScrollable) { // @ts-ignore
55
+ if (isScrollable) {
55
56
  setCSSProperties(menu, {
56
57
  '--grid-item-size': `${optionSize}rem`,
57
58
  '--grid-fit': fit,
@@ -62,15 +63,19 @@ export default function getColorMenu(self, colorsSource, menuClass) {
62
63
  }
63
64
 
64
65
  colorsArray.forEach((x) => {
65
- const [value, label] = x.trim().split(':');
66
- const xRealColor = new Color(value, format).toString();
67
- const isActive = xRealColor === getAttribute(input, 'value');
66
+ let [value, label] = typeof x === 'string' ? x.trim().split(':') : [];
67
+ if (x instanceof Color) {
68
+ value = x.toHexString();
69
+ label = value;
70
+ }
71
+ const color = new Color(x instanceof Color ? x : value, format);
72
+ const isActive = color.toString() === getAttribute(input, 'value');
68
73
  const active = isActive ? ' active' : '';
69
74
 
70
75
  const option = createElement({
71
76
  tagName: 'li',
72
77
  className: `color-option${active}`,
73
- innerText: `${label || x}`,
78
+ innerText: `${label || value}`,
74
79
  });
75
80
 
76
81
  setAttribute(option, tabIndex, '0');
@@ -79,7 +84,7 @@ export default function getColorMenu(self, colorsSource, menuClass) {
79
84
  setAttribute(option, ariaSelected, isActive ? 'true' : 'false');
80
85
 
81
86
  if (isOptionsMenu) {
82
- setElementStyle(option, { backgroundColor: x });
87
+ setElementStyle(option, { backgroundColor: value });
83
88
  }
84
89
 
85
90
  menu.append(option);
@@ -0,0 +1,122 @@
1
+ import getAttribute from 'shorter-js/src/attr/getAttribute';
2
+ import setAttribute from 'shorter-js/src/attr/setAttribute';
3
+ import toUpperCase from 'shorter-js/src/misc/toUpperCase';
4
+ // import ariaLabel from 'shorter-js/src/strings/ariaLabel';
5
+ import ariaExpanded from 'shorter-js/src/strings/ariaExpanded';
6
+ import ariaHasPopup from 'shorter-js/src/strings/ariaHasPopup';
7
+ import ariaHidden from 'shorter-js/src/strings/ariaHidden';
8
+ import ariaLabelledBy from 'shorter-js/src/strings/ariaLabelledBy';
9
+ import createElement from 'shorter-js/src/misc/createElement';
10
+ import createElementNS from 'shorter-js/src/misc/createElementNS';
11
+
12
+ import getColorForm from './getColorForm';
13
+ import getColorControls from './getColorControls';
14
+ import getColorMenu from './getColorMenu';
15
+ import nonColors from './nonColors';
16
+ import vHidden from './vHidden';
17
+ import tabIndex from './tabindex';
18
+
19
+ import Color from '../color';
20
+ import ColorPalette from '../color-palette';
21
+
22
+ /**
23
+ * Generate HTML markup and update instance properties.
24
+ * @param {CP.ColorPicker} self
25
+ */
26
+ export default function setMarkup(self) {
27
+ const {
28
+ input, parent, format, id, componentLabels, colorKeywords, colorPresets,
29
+ } = self;
30
+ const colorValue = getAttribute(input, 'value') || '#fff';
31
+
32
+ const {
33
+ toggleLabel, pickerLabel, formatLabel, hexLabel,
34
+ } = componentLabels;
35
+
36
+ // update color
37
+ const color = nonColors.includes(colorValue) ? '#fff' : colorValue;
38
+ self.color = new Color(color, format);
39
+
40
+ // set initial controls dimensions
41
+ const formatString = format === 'hex' ? hexLabel : toUpperCase(format);
42
+
43
+ const pickerBtn = createElement({
44
+ id: `picker-btn-${id}`,
45
+ tagName: 'button',
46
+ className: 'picker-toggle btn-appearance',
47
+ });
48
+ setAttribute(pickerBtn, ariaExpanded, 'false');
49
+ setAttribute(pickerBtn, ariaHasPopup, 'true');
50
+ pickerBtn.append(createElement({
51
+ tagName: 'span',
52
+ className: vHidden,
53
+ innerText: `${pickerLabel}. ${formatLabel}: ${formatString}`,
54
+ }));
55
+
56
+ const pickerDropdown = createElement({
57
+ tagName: 'div',
58
+ className: 'color-dropdown picker',
59
+ });
60
+ setAttribute(pickerDropdown, ariaLabelledBy, `picker-btn-${id}`);
61
+ setAttribute(pickerDropdown, 'role', 'group');
62
+
63
+ const colorControls = getColorControls(self);
64
+ const colorForm = getColorForm(self);
65
+
66
+ pickerDropdown.append(colorControls, colorForm);
67
+ input.before(pickerBtn);
68
+ parent.append(pickerDropdown);
69
+
70
+ // set colour key menu template
71
+ if (colorKeywords || colorPresets) {
72
+ const presetsDropdown = createElement({
73
+ tagName: 'div',
74
+ className: 'color-dropdown scrollable menu',
75
+ });
76
+
77
+ // color presets
78
+ if ((colorPresets instanceof Array && colorPresets.length)
79
+ || (colorPresets instanceof ColorPalette && colorPresets.colors)) {
80
+ const presetsMenu = getColorMenu(self, colorPresets, 'color-options');
81
+ presetsDropdown.append(presetsMenu);
82
+ }
83
+
84
+ // explicit defaults [reset, initial, inherit, transparent, currentColor]
85
+ if (colorKeywords && colorKeywords.length) {
86
+ const keywordsMenu = getColorMenu(self, colorKeywords, 'color-defaults');
87
+ presetsDropdown.append(keywordsMenu);
88
+ }
89
+
90
+ const presetsBtn = createElement({
91
+ tagName: 'button',
92
+ className: 'menu-toggle btn-appearance',
93
+ });
94
+ setAttribute(presetsBtn, tabIndex, '-1');
95
+ setAttribute(presetsBtn, ariaExpanded, 'false');
96
+ setAttribute(presetsBtn, ariaHasPopup, 'true');
97
+
98
+ const xmlns = encodeURI('http://www.w3.org/2000/svg');
99
+ const presetsIcon = createElementNS(xmlns, { tagName: 'svg' });
100
+ setAttribute(presetsIcon, 'xmlns', xmlns);
101
+ setAttribute(presetsIcon, 'viewBox', '0 0 512 512');
102
+ setAttribute(presetsIcon, ariaHidden, 'true');
103
+
104
+ const path = createElementNS(xmlns, { tagName: 'path' });
105
+ setAttribute(path, 'd', 'M98,158l157,156L411,158l27,27L255,368L71,185L98,158z');
106
+ setAttribute(path, 'fill', '#fff');
107
+ presetsIcon.append(path);
108
+ presetsBtn.append(createElement({
109
+ tagName: 'span',
110
+ className: vHidden,
111
+ innerText: `${toggleLabel}`,
112
+ }), presetsIcon);
113
+
114
+ parent.append(presetsBtn, presetsDropdown);
115
+ }
116
+
117
+ // solve non-colors after settings save
118
+ if (colorKeywords && nonColors.includes(colorValue)) {
119
+ self.value = colorValue;
120
+ }
121
+ setAttribute(input, tabIndex, '-1');
122
+ }
@@ -0,0 +1,6 @@
1
+ // @ts-ignore
2
+ import { version } from '../../../package.json';
3
+
4
+ const Version = version;
5
+
6
+ export default Version;
package/types/cp.d.ts CHANGED
@@ -5,14 +5,21 @@ declare module "color-picker/src/js/util/nonColors" {
5
5
  */
6
6
  const nonColors: string[];
7
7
  }
8
+ declare module "color-picker/src/js/util/roundPart" {
9
+ /**
10
+ * Round colour components, for all formats except HEX.
11
+ * @param {number} v one of the colour components
12
+ * @returns {number} the rounded number
13
+ */
14
+ export default function roundPart(v: number): number;
15
+ }
8
16
  declare module "color-picker/src/js/color" {
9
- export default Color;
10
17
  /**
11
18
  * @class
12
19
  * Returns a new `Color` instance.
13
20
  * @see https://github.com/bgrins/TinyColor
14
21
  */
15
- class Color {
22
+ export default class Color {
16
23
  /**
17
24
  * @constructor
18
25
  * @param {CP.ColorInput} input the given colour value
@@ -202,15 +209,16 @@ declare module "color-picker/src/js/color-palette" {
202
209
  * The `hue` parameter is optional, which would be set to 0.
203
210
  * @param {number[]} args represeinting hue, hueSteps, lightSteps
204
211
  * * `args.hue` the starting Hue [0, 360]
205
- * * `args.hueSteps` Hue Steps Count [5, 13]
206
- * * `args.lightSteps` Lightness Steps Count [8, 10]
212
+ * * `args.hueSteps` Hue Steps Count [5, 24]
213
+ * * `args.lightSteps` Lightness Steps Count [5, 12]
207
214
  */
208
215
  constructor(...args: number[]);
209
216
  hue: number;
210
217
  hueSteps: number;
211
218
  lightSteps: number;
212
- colors: string[];
219
+ colors: Color[];
213
220
  }
221
+ import Color from "color-picker/src/js/color";
214
222
  }
215
223
  declare module "color-picker/src/js/util/colorPickerLabels" {
216
224
  export default colorPickerLabels;
@@ -225,6 +233,22 @@ declare module "color-picker/src/js/util/colorNames" {
225
233
  */
226
234
  const colorNames: string[];
227
235
  }
236
+ declare module "color-picker/src/js/util/tabindex" {
237
+ export default tabIndex;
238
+ const tabIndex: "tabindex";
239
+ }
240
+ declare module "color-picker/src/js/util/isValidJSON" {
241
+ /**
242
+ * Check if a string is valid JSON string.
243
+ * @param {string} str the string input
244
+ * @returns {boolean} the query result
245
+ */
246
+ export default function isValidJSON(str: string): boolean;
247
+ }
248
+ declare module "color-picker/src/js/version" {
249
+ export default Version;
250
+ const Version: string;
251
+ }
228
252
  declare module "color-picker/src/js/util/vHidden" {
229
253
  export default vHidden;
230
254
  const vHidden: "v-hidden";
@@ -247,6 +271,14 @@ declare module "color-picker/src/js/util/getColorControls" {
247
271
  */
248
272
  export default function getColorControls(self: CP.ColorPicker): HTMLElement | Element;
249
273
  }
274
+ declare module "color-picker/src/js/util/setCSSProperties" {
275
+ /**
276
+ * Helps setting CSS variables to the color-menu.
277
+ * @param {HTMLElement} element
278
+ * @param {Record<string,any>} props
279
+ */
280
+ export default function setCSSProperties(element: HTMLElement, props: Record<string, any>): void;
281
+ }
250
282
  declare module "color-picker/src/js/util/getColorMenu" {
251
283
  /**
252
284
  * Returns a color-defaults with given values and class.
@@ -257,19 +289,16 @@ declare module "color-picker/src/js/util/getColorMenu" {
257
289
  */
258
290
  export default function getColorMenu(self: CP.ColorPicker, colorsSource: CP.ColorPalette | string[], menuClass: string): HTMLElement | Element;
259
291
  }
260
- declare module "color-picker/src/js/util/isValidJSON" {
292
+ declare module "color-picker/src/js/util/setMarkup" {
261
293
  /**
262
- * Check if a string is valid JSON string.
263
- * @param {string} str the string input
264
- * @returns {boolean} the query result
265
- */
266
- export default function isValidJSON(str: string): boolean;
267
- }
268
- declare module "color-picker/src/js/version" {
269
- export default Version;
270
- const Version: string;
294
+ * Generate HTML markup and update instance properties.
295
+ * @param {CP.ColorPicker} self
296
+ */
297
+ export default function setMarkup(self: CP.ColorPicker): void;
271
298
  }
272
299
  declare module "color-picker/src/js/color-picker" {
300
+ /** @type {CP.GetInstance<ColorPicker>} */
301
+ export const getColorPickerInstance: CP.GetInstance<ColorPicker>;
273
302
  /**
274
303
  * Color Picker Web Component
275
304
  * @see http://thednp.github.io/color-picker
@@ -482,8 +511,9 @@ declare module "color-picker/src/js/color-picker-element" {
482
511
  /**
483
512
  * `ColorPickerElement` Web Component.
484
513
  * @example
485
- * <color-picker>
486
- * <input type="text">
514
+ * <label for="UNIQUE_ID">Label</label>
515
+ * <color-picker data-format="hex" data-value="#075">
516
+ * <input id="UNIQUE_ID" type="text" class="color-preview btn-appearance">
487
517
  * </color-picker>
488
518
  */
489
519
  class ColorPickerElement extends HTMLElement {
@@ -1,10 +0,0 @@
1
- /** A future project to allow using custom `ColorPicker` templates */
2
- const templates = {
3
- control: {
4
- tagName: 'div',
5
- className: 'color-control',
6
- },
7
-
8
- };
9
-
10
- export default templates;