@thednp/color-picker 0.0.1-alpha2 → 0.0.2-alpha1
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.
- package/README.md +32 -15
- package/dist/css/color-picker.css +38 -15
- package/dist/css/color-picker.min.css +2 -2
- package/dist/css/color-picker.rtl.css +38 -15
- package/dist/css/color-picker.rtl.min.css +2 -2
- package/dist/js/color-esm.js +1178 -0
- package/dist/js/color-esm.min.js +2 -0
- package/dist/js/color-palette-esm.js +1252 -0
- package/dist/js/color-palette-esm.min.js +2 -0
- package/dist/js/color-palette.js +1260 -0
- package/dist/js/color-palette.min.js +2 -0
- package/dist/js/color-picker-element-esm.js +433 -424
- package/dist/js/color-picker-element-esm.min.js +2 -2
- package/dist/js/color-picker-element.js +435 -426
- package/dist/js/color-picker-element.min.js +2 -2
- package/dist/js/color-picker-esm.js +745 -739
- package/dist/js/color-picker-esm.min.js +2 -2
- package/dist/js/color-picker.js +747 -741
- package/dist/js/color-picker.min.js +2 -2
- package/dist/js/color.js +1186 -0
- package/dist/js/color.min.js +2 -0
- package/package.json +19 -3
- package/src/js/color-palette.js +28 -12
- package/src/js/color-picker-element.js +8 -4
- package/src/js/color-picker.js +84 -172
- package/src/js/color.js +125 -131
- package/src/js/util/getColorControls.js +3 -3
- package/src/js/util/getColorForm.js +0 -1
- package/src/js/util/getColorMenu.js +31 -33
- package/src/js/util/roundPart.js +9 -0
- package/src/js/util/setCSSProperties.js +12 -0
- package/src/js/util/setMarkup.js +122 -0
- package/src/js/util/tabindex.js +3 -0
- package/src/js/util/version.js +6 -0
- package/src/scss/color-picker.scss +35 -16
- package/types/cp.d.ts +48 -20
- package/src/js/util/templates.js +0 -10
@@ -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
|
+
}
|
@@ -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,
|
@@ -223,16 +221,30 @@ color-picker:focus,
|
|
223
221
|
padding: 0;
|
224
222
|
margin: 0;
|
225
223
|
list-style: none;
|
224
|
+
--grid-item-size: 2rem; // grid item width / height
|
225
|
+
--grid-fit: 5; // grid columns
|
226
|
+
--grid-gap: .25rem; // grid vertical / horizontal spacing
|
227
|
+
--grid-height: auto; // default height
|
228
|
+
--grid-hover-height: auto; // height on hover
|
229
|
+
grid-template-columns: repeat(var(--grid-fit), var(--grid-item-size));
|
230
|
+
grid-template-rows: repeat(auto-fill, var(--grid-item-size));
|
231
|
+
gap: var(--grid-gap);
|
226
232
|
}
|
227
233
|
.color-options.scrollable {
|
234
|
+
height: var(--grid-height);
|
228
235
|
margin: 0 -.5rem 0 0; // 0.5rem is the scrollbar width
|
229
236
|
overflow-y: scroll;
|
230
237
|
transition: height .33s ease;
|
231
238
|
}
|
239
|
+
.color-options.scrollable:hover {
|
240
|
+
height: var(--grid-hover-height);
|
241
|
+
}
|
242
|
+
.color-options + .color-defaults {
|
243
|
+
margin-top: .25rem;
|
244
|
+
}
|
232
245
|
.multiline + .color-defaults {
|
233
246
|
flex-direction: row;
|
234
247
|
flex-wrap: wrap;
|
235
|
-
margin-top: .25rem;
|
236
248
|
.color-option {
|
237
249
|
padding: .25rem .33rem; font-size: 12px;
|
238
250
|
}
|
@@ -240,20 +252,29 @@ color-picker:focus,
|
|
240
252
|
.color-options .color-option {
|
241
253
|
// hide any text
|
242
254
|
position: relative;
|
255
|
+
width: var(--grid-item-size);
|
256
|
+
height: var(--grid-item-size);
|
243
257
|
overflow: hidden;
|
244
258
|
text-indent: 2.1rem;
|
245
|
-
background: #eee;
|
246
|
-
opacity: .8;
|
247
|
-
}
|
248
|
-
.color-options .color-option:hover,
|
249
|
-
.color-options .color-option:active,
|
250
|
-
.color-options .color-option:focus {
|
251
|
-
opacity: 1;
|
252
259
|
}
|
253
260
|
.color-options .color-option:active,
|
254
261
|
.color-options .color-option:focus {
|
255
262
|
outline: none;
|
256
263
|
}
|
264
|
+
.color-options .color-option::before {
|
265
|
+
position: absolute;
|
266
|
+
top: 0;
|
267
|
+
right: 0;
|
268
|
+
bottom: 0;
|
269
|
+
left: 0;
|
270
|
+
}
|
271
|
+
.color-options .color-option:hover::before,
|
272
|
+
.color-options .color-option:active::before,
|
273
|
+
.color-options .color-option:focus::before {
|
274
|
+
content: "";
|
275
|
+
border: 3px solid rgba(255,255,255.3,.75);
|
276
|
+
mix-blend-mode: difference;
|
277
|
+
}
|
257
278
|
|
258
279
|
.color-options .color-option:active,
|
259
280
|
.color-options .color-option:focus {
|
@@ -261,7 +282,6 @@ color-picker:focus,
|
|
261
282
|
}
|
262
283
|
|
263
284
|
.color-options .color-option.active {
|
264
|
-
opacity: 1;
|
265
285
|
&:after {
|
266
286
|
position: absolute;
|
267
287
|
top: 50%;
|
@@ -269,7 +289,7 @@ color-picker:focus,
|
|
269
289
|
width: 4px;
|
270
290
|
height: 4px;
|
271
291
|
margin: -2px 0 0 -2px;
|
272
|
-
content: "
|
292
|
+
content: "";
|
273
293
|
border-radius: 4px;
|
274
294
|
}
|
275
295
|
}
|
@@ -286,8 +306,7 @@ color-picker:focus,
|
|
286
306
|
flex-direction: row;
|
287
307
|
flex-wrap: wrap;
|
288
308
|
align-items: center;
|
289
|
-
|
290
|
-
padding: .5rem 0 0;
|
309
|
+
padding: .25rem 0 0;
|
291
310
|
font: 12px sans-serif;
|
292
311
|
}
|
293
312
|
|
@@ -373,6 +392,7 @@ color-picker:focus,
|
|
373
392
|
user-select: none;
|
374
393
|
background-color: #000;
|
375
394
|
border: 1px solid #fff;
|
395
|
+
border-radius: 5px;
|
376
396
|
outline: none;
|
377
397
|
will-change: transform;
|
378
398
|
}
|
@@ -390,7 +410,6 @@ color-picker:focus,
|
|
390
410
|
width: 7px;
|
391
411
|
background-color: transparent;
|
392
412
|
border: 0;
|
393
|
-
border-radius: 5px;
|
394
413
|
}
|
395
414
|
|
396
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
|
@@ -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,
|
206
|
-
* * `args.lightSteps` Lightness Steps Count [
|
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:
|
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/
|
292
|
+
declare module "color-picker/src/js/util/setMarkup" {
|
261
293
|
/**
|
262
|
-
|
263
|
-
|
264
|
-
|
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
|
@@ -406,7 +435,7 @@ declare module "color-picker/src/js/color-picker" {
|
|
406
435
|
/** Returns the current colour value */
|
407
436
|
get value(): string;
|
408
437
|
/** Check if the colour presets include any non-colour. */
|
409
|
-
get
|
438
|
+
get hasNonColor(): boolean;
|
410
439
|
/** Check if the parent of the target is a `ColorPickerElement` instance. */
|
411
440
|
get isCE(): boolean;
|
412
441
|
/** Returns hexadecimal value of the current colour. */
|
@@ -466,8 +495,6 @@ declare module "color-picker/src/js/color-picker" {
|
|
466
495
|
* @param {boolean=} isPrevented when `true`, the component original event is prevented
|
467
496
|
*/
|
468
497
|
updateInputs(isPrevented?: boolean | undefined): void;
|
469
|
-
/** Shows the `ColorPicker` dropdown or the presets menu. */
|
470
|
-
show(): void;
|
471
498
|
/**
|
472
499
|
* Hides the currently open `ColorPicker` dropdown.
|
473
500
|
* @param {boolean=} focusPrevented
|
@@ -484,8 +511,9 @@ declare module "color-picker/src/js/color-picker-element" {
|
|
484
511
|
/**
|
485
512
|
* `ColorPickerElement` Web Component.
|
486
513
|
* @example
|
487
|
-
* <
|
488
|
-
*
|
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">
|
489
517
|
* </color-picker>
|
490
518
|
*/
|
491
519
|
class ColorPickerElement extends HTMLElement {
|