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

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;
@@ -180,11 +180,9 @@ color-picker:focus,
180
180
  display: none;
181
181
  flex-wrap: wrap;
182
182
  flex-flow: column;
183
- // max-height: 160px;
184
183
  margin: 0;
185
- // overflow-y: auto;
186
184
  list-style: none;
187
- padding-inline: 0;
185
+ padding-inline: 0; // Firefox
188
186
  }
189
187
 
190
188
  .color-dropdown.menu .color-defaults,
@@ -258,7 +256,6 @@ color-picker:focus,
258
256
  height: var(--grid-item-size);
259
257
  overflow: hidden;
260
258
  text-indent: 2.1rem;
261
- // background: #eee;
262
259
  }
263
260
  .color-options .color-option:active,
264
261
  .color-options .color-option:focus {
@@ -309,7 +306,6 @@ color-picker:focus,
309
306
  flex-direction: row;
310
307
  flex-wrap: wrap;
311
308
  align-items: center;
312
- // max-width: fit-content;
313
309
  padding: .25rem 0 0;
314
310
  font: 12px sans-serif;
315
311
  }
@@ -396,8 +392,9 @@ color-picker:focus,
396
392
  user-select: none;
397
393
  background-color: #000;
398
394
  border: 1px solid #fff;
395
+ border-radius: 5px;
399
396
  outline: none;
400
- // will-change: transform;
397
+ will-change: transform;
401
398
  }
402
399
  .knob:hover {
403
400
  box-shadow: 0 0 0 6px rgba(255,255,255,.5);
@@ -413,7 +410,6 @@ color-picker:focus,
413
410
  width: 7px;
414
411
  background-color: transparent;
415
412
  border: 0;
416
- border-radius: 5px;
417
413
  }
418
414
 
419
415
  .txt-dark .color-pointer {
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
@@ -195,21 +202,21 @@ declare module "color-picker/src/js/color-palette" {
195
202
  * Returns a color palette with a given set of parameters.
196
203
  * @example
197
204
  * new ColorPalette(0, 12, 10);
198
- * // => { hue: 0, hueSteps: 12, lightSteps: 10, colors: array }
205
+ * // => { hue: 0, hueSteps: 12, lightSteps: 10, colors: Array<Color> }
199
206
  */
200
207
  export default class ColorPalette {
201
208
  /**
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: any;
213
220
  }
214
221
  }
215
222
  declare module "color-picker/src/js/util/colorPickerLabels" {
@@ -225,6 +232,18 @@ declare module "color-picker/src/js/util/colorNames" {
225
232
  */
226
233
  const colorNames: string[];
227
234
  }
235
+ declare module "color-picker/src/js/util/tabindex" {
236
+ export default tabIndex;
237
+ const tabIndex: "tabindex";
238
+ }
239
+ declare module "color-picker/src/js/util/isValidJSON" {
240
+ /**
241
+ * Check if a string is valid JSON string.
242
+ * @param {string} str the string input
243
+ * @returns {boolean} the query result
244
+ */
245
+ export default function isValidJSON(str: string): boolean;
246
+ }
228
247
  declare module "color-picker/src/js/util/vHidden" {
229
248
  export default vHidden;
230
249
  const vHidden: "v-hidden";
@@ -247,6 +266,14 @@ declare module "color-picker/src/js/util/getColorControls" {
247
266
  */
248
267
  export default function getColorControls(self: CP.ColorPicker): HTMLElement | Element;
249
268
  }
269
+ declare module "color-picker/src/js/util/setCSSProperties" {
270
+ /**
271
+ * Helps setting CSS variables to the color-menu.
272
+ * @param {HTMLElement} element
273
+ * @param {Record<string,any>} props
274
+ */
275
+ export default function setCSSProperties(element: HTMLElement, props: Record<string, any>): void;
276
+ }
250
277
  declare module "color-picker/src/js/util/getColorMenu" {
251
278
  /**
252
279
  * Returns a color-defaults with given values and class.
@@ -257,15 +284,14 @@ declare module "color-picker/src/js/util/getColorMenu" {
257
284
  */
258
285
  export default function getColorMenu(self: CP.ColorPicker, colorsSource: CP.ColorPalette | string[], menuClass: string): HTMLElement | Element;
259
286
  }
260
- declare module "color-picker/src/js/util/isValidJSON" {
287
+ declare module "color-picker/src/js/util/setMarkup" {
261
288
  /**
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;
289
+ * Generate HTML markup and update instance properties.
290
+ * @param {CP.ColorPicker} self
291
+ */
292
+ export default function setMarkup(self: CP.ColorPicker): void;
267
293
  }
268
- declare module "color-picker/src/js/version" {
294
+ declare module "color-picker/src/js/util/version" {
269
295
  export default Version;
270
296
  const Version: string;
271
297
  }
@@ -308,14 +334,24 @@ declare module "color-picker/src/js/color-picker" {
308
334
  format: CP.ColorFormats;
309
335
  /** Shows the `ColorPicker` dropdown. */
310
336
  showPicker(): void;
337
+ /**
338
+ * The `Space` & `Enter` keys specific event listener.
339
+ * Toggle visibility of the `ColorPicker` / the presets menu, showing one will hide the other.
340
+ * @param {KeyboardEvent} e
341
+ * @this {ColorPicker}
342
+ */
311
343
  /**
312
344
  * Toggle the `ColorPicker` dropdown visibility.
313
- * @param {Event} e
345
+ * @param {Event=} e
314
346
  * @this {ColorPicker}
315
347
  */
316
- togglePicker(this: ColorPicker, e: Event): void;
317
- /** Toggles the visibility of the `ColorPicker` presets menu. */
318
- toggleMenu(): void;
348
+ togglePicker(this: ColorPicker, e?: Event | undefined): void;
349
+ /**
350
+ * Toggles the visibility of the `ColorPicker` presets menu.
351
+ * @param {Event=} e
352
+ * @this {ColorPicker}
353
+ */
354
+ toggleMenu(this: ColorPicker, e?: Event | undefined): void;
319
355
  /**
320
356
  * The `ColorPicker` click event listener for the colour menu presets / defaults.
321
357
  * @param {Partial<Event>} e
@@ -371,13 +407,6 @@ declare module "color-picker/src/js/color-picker" {
371
407
  * @this {ColorPicker}
372
408
  */
373
409
  handleDismiss(this: ColorPicker, { code }: KeyboardEvent): void;
374
- /**
375
- * The `Space` & `Enter` keys specific event listener.
376
- * Toggle visibility of the `ColorPicker` / the presets menu, showing one will hide the other.
377
- * @param {KeyboardEvent} e
378
- * @this {ColorPicker}
379
- */
380
- keyToggle(this: ColorPicker, e: KeyboardEvent): void;
381
410
  /**
382
411
  * The `ColorPicker` *keydown* event listener for control knobs.
383
412
  * @param {KeyboardEvent} e
@@ -427,6 +456,8 @@ declare module "color-picker/src/js/color-picker" {
427
456
  get isDark(): boolean;
428
457
  /** Checks if the current input value is a valid colour. */
429
458
  get isValid(): boolean;
459
+ /** Returns the colour appearance, usually the closest colour name for the current value. */
460
+ get appearance(): string | undefined;
430
461
  /** Updates `ColorPicker` visuals. */
431
462
  updateVisuals(): void;
432
463
  /**
@@ -482,27 +513,28 @@ declare module "color-picker/src/js/color-picker-element" {
482
513
  /**
483
514
  * `ColorPickerElement` Web Component.
484
515
  * @example
516
+ * <label for="UNIQUE_ID">Label</label>
485
517
  * <color-picker>
486
- * <input type="text">
518
+ * <input id="UNIQUE_ID" value="red" format="hex" class="color-preview btn-appearance">
487
519
  * </color-picker>
520
+ * // or
521
+ * <label for="UNIQUE_ID">Label</label>
522
+ * <color-picker data-id="UNIQUE_ID" data-value="red" data-format="hex"></color-picker>
488
523
  */
489
524
  class ColorPickerElement extends HTMLElement {
490
- /** @type {boolean} */
491
- isDisconnected: boolean;
492
525
  /**
493
526
  * Returns the current color value.
494
- * @returns {string?}
527
+ * @returns {string | undefined}
495
528
  */
496
- get value(): string | null;
529
+ get value(): string | undefined;
497
530
  connectedCallback(): void;
498
531
  /** @type {HTMLInputElement} */
499
532
  input: HTMLInputElement | undefined;
500
533
  colorPicker: ColorPicker | undefined;
501
- color: Color | undefined;
502
- disconnectedCallback(): void;
534
+ /** @this {ColorPickerElement} */
535
+ disconnectedCallback(this: ColorPickerElement): void;
503
536
  }
504
537
  import ColorPicker from "color-picker/src/js/color-picker";
505
- import Color from "color-picker/src/js/color";
506
538
  }
507
539
  declare module "color-picker/types/source/source" {
508
540
  export { default as Color } from "color-picker/src/js/color";
@@ -83,7 +83,7 @@ export interface ColorPickerOptions {
83
83
  colorKeywords?: string[];
84
84
  }
85
85
 
86
- export type ColorInput = string | number | RGB | RGBA | HSL | HSLA | HSV | HSVA | HWB | ColorObject;
86
+ export type ColorInput = string | RGB | RGBA | HSL | HSLA | HSV | HSVA | HWB | ColorObject;
87
87
  export type ColorFormats = string | 'rgb' | 'hex' | 'hex3' | 'hex4' | 'hex6' | 'hex8' | 'hsl' | 'hsv' | 'hwb';
88
88
 
89
89
  export type GetInstance<T> = (element: string | HTMLInputElement) => T | null;
@@ -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;