kitzo 2.1.29 → 2.2.0

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.
@@ -1,597 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.kitzo = factory());
5
- })(this, (function () { 'use strict';
6
-
7
- //! Copy function
8
- function legecyCopy(docs) {
9
- try {
10
- const textarea = document.createElement('textarea');
11
- textarea.value = docs;
12
- document.body.appendChild(textarea);
13
- textarea.select();
14
- document.execCommand('copy');
15
- document.body.removeChild(textarea);
16
- } catch (error) {
17
- alert('Couldn’t complete. Please copy manually.');
18
- console.error(error);
19
- }
20
- }
21
-
22
- async function copyText(docs) {
23
- if (navigator.clipboard && navigator.clipboard.writeText) {
24
- try {
25
- await navigator.clipboard.writeText(docs);
26
- } catch (error) {
27
- legecyCopy(docs);
28
- console.error(error);
29
- }
30
- } else {
31
- legecyCopy(docs);
32
- }
33
- }
34
-
35
- function copy(doc) {
36
- if (typeof doc === 'string' || typeof doc === 'number') {
37
- copyText(doc);
38
- } else {
39
- copyText(JSON.stringify(doc));
40
- }
41
- }
42
-
43
- function debounce(fn, delay = 300) {
44
- let timer;
45
-
46
- return (...args) => {
47
- clearTimeout(timer);
48
- timer = setTimeout(() => fn(...args), delay);
49
- };
50
- }
51
-
52
- //! Helper functions
53
- // Get elements from dom
54
- function getButtons(element) {
55
- if (typeof element === 'string') {
56
- return document.querySelectorAll(element);
57
- }
58
- if (element instanceof Element) {
59
- return [element];
60
- }
61
- if (element instanceof NodeList || element instanceof HTMLCollection) {
62
- return element;
63
- }
64
- }
65
-
66
- // Add style tags
67
- let tooltipStyleAdded = false;
68
- let rippleStyleAdded = false;
69
- let clippathStyleAdded = false;
70
-
71
- function addStyleTag(styles) {
72
- const style = document.createElement('style');
73
- style.innerHTML = styles;
74
- document.head.appendChild(style);
75
- }
76
-
77
- function addStyleTagToHtmlHead(type, styles) {
78
- if (type === 'tooltip' && !tooltipStyleAdded) {
79
- addStyleTag(styles);
80
- tooltipStyleAdded = true;
81
- }
82
- if (type === 'ripple' && !rippleStyleAdded) {
83
- addStyleTag(styles);
84
- rippleStyleAdded = true;
85
- }
86
- if (type === 'clippath' && !clippathStyleAdded) {
87
- addStyleTag(styles);
88
- clippathStyleAdded = true;
89
- }
90
- }
91
-
92
- function rippleStyles() {
93
- return `.kitzo-ripples {
94
- display: block;
95
- position: absolute;
96
- top: 0;
97
- left: 0;
98
- transform: translate(-50%, -50%);
99
- width: 0;
100
- height: 0;
101
- background-color: var(--ripples-color);
102
- z-index: 5;
103
- border-radius: 50%;
104
- opacity: 1;
105
- pointer-events: none;
106
- }
107
-
108
- .kitzo-ripples.expand {
109
- animation: expand-ripple var(--ripples-duration) linear forwards;
110
- }
111
-
112
- @keyframes expand-ripple {
113
- 0% {
114
- width: 0;
115
- height: 0;
116
- opacity: var(--ripples-opacity);
117
- }
118
- 100% {
119
- width: var(--ripples-size);
120
- height: var(--ripples-size);
121
- opacity: 0;
122
- }
123
- }`;
124
- }
125
-
126
- //! Ripple effect
127
- let rippleListenerAdded = false;
128
- const rippleConfigMap = new WeakMap();
129
-
130
- function ripple(element, config = {}) {
131
- if (!element) {
132
- console.error('[kitzo.ripple] A button element/selector is expected');
133
- return;
134
- }
135
-
136
- addStyleTagToHtmlHead('ripple', rippleStyles());
137
-
138
- config = Object.assign(
139
- {
140
- opacity: 0.5,
141
- duration: 1,
142
- color: 'white',
143
- size: null,
144
- },
145
- config
146
- );
147
-
148
- const allButtons = getButtons(element);
149
- if (!allButtons) {
150
- console.error('[kitzo.ripple] No elements found for kitzoRipple');
151
- return;
152
- }
153
- allButtons.forEach((btn) => {
154
- btn.setAttribute('data-kitzo-ripple', true);
155
- rippleConfigMap.set(btn, config);
156
- const { position, overflow } = window.getComputedStyle(btn);
157
- if (position === 'static') {
158
- btn.style.position = 'relative';
159
- }
160
- if (overflow === 'visible') {
161
- btn.style.overflow = 'hidden';
162
- }
163
- });
164
-
165
- if (!rippleListenerAdded) {
166
- document.addEventListener('mousedown', (e) => {
167
- const btn = e.target.closest('[data-kitzo-ripple]');
168
- if (btn) {
169
- const { opacity, duration, color, size } = rippleConfigMap.get(btn);
170
- const span = document.createElement('span');
171
- span.className = 'kitzo-ripples';
172
- btn.appendChild(span);
173
-
174
- const { left, top, width } = btn.getBoundingClientRect();
175
- span.style.left = e.clientX - left + 'px';
176
- span.style.top = e.clientY - top + 'px';
177
-
178
- btn.style.setProperty('--ripples-opacity', opacity);
179
- btn.style.setProperty('--ripples-duration', duration + 's');
180
- btn.style.setProperty('--ripples-size', `${size || width * 2}px`);
181
- btn.style.setProperty('--ripples-color', color);
182
-
183
- span.classList.add('expand');
184
-
185
- span.addEventListener('animationend', () => span.remove());
186
- }
187
- });
188
-
189
- rippleListenerAdded = true;
190
- }
191
- }
192
-
193
- function tooltipStyles() {
194
- return `:root {
195
- --tooltip-bg-clr: hsl(0, 0%, 10%);
196
- --tooltip-text-clr: hsl(0, 0%, 90%);
197
- }
198
-
199
- @media (prefers-color-scheme: dark) {
200
- :root {
201
- --tooltip-bg-clr: hsl(0, 0%, 95%);
202
- --tooltip-text-clr: hsl(0, 0%, 10%);
203
- }
204
- }
205
-
206
- .kitzo-tooltip {
207
- --tooltip-arrow-clr: var(--tooltip-bg-clr);
208
-
209
- box-sizing: border-box;
210
- font-family: inherit;
211
- text-align: center;
212
-
213
- position: fixed;
214
- top: 0;
215
- left: 0;
216
- z-index: 999999;
217
-
218
- background-color: var(--tooltip-bg-clr);
219
- color: var(--tooltip-text-clr);
220
- padding-block: 0.325rem;
221
- padding-inline: 0.625rem;
222
- border-radius: 4px;
223
- box-shadow: 0 2px 6px hsla(235, 0%, 0%, 0.25);
224
-
225
- opacity: 0 !important;
226
-
227
- transition: opacity 200ms;
228
- transition-delay: 100ms;
229
- pointer-events: none;
230
- }
231
-
232
- .kitzo-tooltip.show {
233
- opacity: 1 !important;
234
- }
235
-
236
- .kitzo-tooltip-top::before,
237
- .kitzo-tooltip-right::before,
238
- .kitzo-tooltip-bottom::before,
239
- .kitzo-tooltip-left::before {
240
-
241
- content: '';
242
- position: absolute;
243
- border: 6px solid;
244
- }
245
-
246
- .kitzo-tooltip-top::before {
247
- top: calc(100% - 1px);
248
- left: 50%;
249
- translate: -50% 0;
250
- border-color: var(--tooltip-arrow-clr) transparent transparent transparent;
251
- }
252
-
253
- .kitzo-tooltip-right::before {
254
- top: 50%;
255
- right: calc(100% - 1px);
256
- translate: 0 -50%;
257
- border-color: transparent var(--tooltip-arrow-clr) transparent transparent;
258
- }
259
-
260
- .kitzo-tooltip-bottom::before {
261
- bottom: calc(100% - 1px);
262
- left: 50%;
263
- translate: -50% 0;
264
- border-color: transparent transparent var(--tooltip-arrow-clr) transparent;
265
- }
266
-
267
- .kitzo-tooltip-left::before {
268
- left: calc(100% - 1px);
269
- top: 50%;
270
- translate: 0 -50%;
271
- border-color: transparent transparent transparent var(--tooltip-arrow-clr);
272
- }`;
273
- }
274
-
275
- //! Tooltip
276
- let tooltipDiv;
277
- let tooltipListenerAdded = false;
278
- const tooltipConfigMap = new WeakMap();
279
-
280
- function tooltip(element, config = {}) {
281
- if (window.matchMedia('(pointer: coarse)').matches) return;
282
-
283
- if (!element) {
284
- console.error('A button element/selector is expected');
285
- return;
286
- }
287
-
288
- addStyleTagToHtmlHead('tooltip', tooltipStyles());
289
-
290
- config = Object.assign(
291
- {
292
- tooltip: 'Tool tip',
293
- direction: 'bottom',
294
- arrow: 'off',
295
- offset: 10,
296
- customClass: '',
297
- style: {},
298
- },
299
- config
300
- );
301
-
302
- const allButtons = getButtons(element);
303
- if (!allButtons) {
304
- console.error('[kitzo.tooltip] No elements found for kitzoTooltip');
305
- return;
306
- }
307
-
308
- const disAllowedStyles = ['top', 'left', 'right', 'bottom', 'position', 'zIndex', 'opacity', 'transform', 'translate', 'scale', 'rotate', 'perspective'];
309
- for (const key of disAllowedStyles) {
310
- if (key in config.style) {
311
- console.warn(`[kitzo.tooltip] "${key}" style is managed internally and will be ignored.`);
312
- delete config.style[key];
313
- }
314
- }
315
-
316
- allButtons.forEach((btn) => {
317
- btn.setAttribute('data-kitzo-tooltip', true);
318
- tooltipConfigMap.set(btn, config);
319
- });
320
-
321
- if (!tooltipDiv) {
322
- tooltipDiv = document.createElement('div');
323
- tooltipDiv.style.opacity = '0';
324
- document.body.appendChild(tooltipDiv);
325
- }
326
-
327
- function getPosition(btn, dir, offset) {
328
- const { width, height, top, left } = btn.getBoundingClientRect();
329
- let posX;
330
- let posY;
331
-
332
- if (dir === 'top') {
333
- posX = left + width / 2 - tooltipDiv.offsetWidth / 2;
334
- posY = top - tooltipDiv.offsetHeight - offset;
335
- return { posX, posY };
336
- }
337
-
338
- if (dir === 'right') {
339
- posX = left + width + offset;
340
- posY = top + height / 2 - tooltipDiv.offsetHeight / 2;
341
- return { posX, posY };
342
- }
343
-
344
- if (dir === 'bottom') {
345
- posX = left + width / 2 - tooltipDiv.offsetWidth / 2;
346
- posY = top + height + offset;
347
- return { posX, posY };
348
- }
349
-
350
- if (dir === 'left') {
351
- posX = left - tooltipDiv.offsetWidth - offset;
352
- posY = top + height / 2 - tooltipDiv.offsetHeight / 2;
353
- return { posX, posY };
354
- }
355
- }
356
-
357
- if (!tooltipListenerAdded) {
358
- document.addEventListener('mouseover', (e) => {
359
- const btn = e.target.closest('[data-kitzo-tooltip]');
360
- if (btn) {
361
- const { tooltip, direction, offset, customClass, style, arrow } = tooltipConfigMap.get(btn);
362
-
363
- tooltipDiv.removeAttribute('style');
364
- Object.assign(tooltipDiv.style, {
365
- position: 'fixed',
366
- top: '0px',
367
- left: '0px',
368
- zIndex: '999999',
369
- ...style,
370
- });
371
-
372
- const isArrowOn = arrow === 'on';
373
- tooltipDiv.textContent = tooltip;
374
- tooltipDiv.className = `kitzo-tooltip ${isArrowOn ? `kitzo-tooltip-${direction}` : ''} ${customClass.trim() ? customClass : ''}`;
375
-
376
- if (isArrowOn) {
377
- const color = getComputedStyle(tooltipDiv).backgroundColor;
378
- tooltipDiv.style.setProperty('--tooltip-arrow-clr', color);
379
- }
380
-
381
- const { posX, posY } = getPosition(btn, direction, offset);
382
- tooltipDiv.style.transform = `translate(${posX}px, ${posY}px)`;
383
-
384
- requestAnimationFrame(() => {
385
- tooltipDiv.classList.add('show');
386
- });
387
- }
388
- });
389
-
390
- document.addEventListener('mouseout', (e) => {
391
- const btn = e.target.closest('[data-kitzo-tooltip]');
392
- if (btn) {
393
- tooltipDiv.classList.remove('show');
394
- }
395
- });
396
-
397
- tooltipListenerAdded = true;
398
- }
399
- }
400
-
401
- function clippathStyles() {
402
- return `.kitzo-clippath-div {
403
- position: fixed;
404
- top: 0;
405
- left: 0;
406
- width: 0;
407
- height: 0;
408
- pointer-events: none;
409
- opacity: 0;
410
- clip-path: circle(0 at var(--kitzo-clippath-pos-x) var(--kitzo-clippath-pos-y));
411
- transition: var(--kitzo-clippath-transition);
412
- font-family: inherit;
413
- overflow: hidden;
414
- }
415
- .kitzo-clippath-div.show {
416
- opacity: 1;
417
- clip-path: circle(var(--kitzo-clippath-size) at var(--kitzo-clippath-pos-x) var(--kitzo-clippath-pos-y));
418
- }
419
- [data-kitzo-clippath] * {
420
- pointer-events: none !important;
421
- }`;
422
- }
423
-
424
- function getClippathSize(size) {
425
- if (size?.trim?.() === '') {
426
- return '20%';
427
- }
428
- if (typeof size === 'number') {
429
- if (size < 0) {
430
- console.warn("[kitzo.clippath] please provide a string value or positive number(px). Default is '20%'");
431
- return `20%`;
432
- }
433
- return `${size}px`;
434
- }
435
- if (typeof size === 'string') {
436
- return `${size}`;
437
- }
438
- console.warn("[kitzo.clippath] please provide a string value or positive number(px). Default is '20%'");
439
- return '20%';
440
- }
441
-
442
- const clippathConfigMap = new WeakMap();
443
- let isClippathListenersAdded = false;
444
- let clippathDiv;
445
-
446
- function clippath(element, config = {}) {
447
- if (window.matchMedia('(pointer:coarse)').matches) return;
448
-
449
- if (!element) {
450
- console.error('[kitzo.clippath] A button element/selector is expected');
451
- return;
452
- }
453
-
454
- addStyleTagToHtmlHead('clippath', clippathStyles());
455
-
456
- config = Object.assign(
457
- {
458
- textOption: null,
459
- clippathSize: '20%',
460
- smooth: true,
461
- class: '',
462
- style: {},
463
- },
464
- config,
465
- );
466
-
467
- const allButtons = getButtons(element);
468
- if (!allButtons) {
469
- console.error('[kitzo.clippath] No elements found for kitzoTooltip');
470
- return;
471
- }
472
-
473
- const disAllowedStyles = ['top', 'left', 'right', 'bottom', 'position', 'opacity', 'transform', 'translate', 'scale', 'rotate', 'perspective'];
474
- for (const key of disAllowedStyles) {
475
- if (key in config.style) {
476
- console.warn(`[kitzo.clippath] "${key}" style is managed internally and will be ignored.`);
477
- delete config.style[key];
478
- }
479
- }
480
-
481
- allButtons.forEach((btn) => {
482
- btn.setAttribute('data-kitzo-clippath', true);
483
- clippathConfigMap.set(btn, config);
484
- });
485
-
486
- if (!clippathDiv) {
487
- clippathDiv = document.createElement('div');
488
- clippathDiv.className = 'kitzo-clippath-div';
489
- document.body.appendChild(clippathDiv);
490
- }
491
-
492
- if (!isClippathListenersAdded) {
493
- let isHovering = false;
494
-
495
- document.addEventListener('mouseover', (e) => {
496
- const btn = e.target.closest('[data-kitzo-clippath]');
497
- if (btn) {
498
- isHovering = true;
499
- clippathDiv.removeAttribute('style');
500
- const { textOption, clippathSize, smooth, style } = clippathConfigMap.get(btn);
501
-
502
- const { top, left, width, height } = btn.getBoundingClientRect();
503
-
504
- const cloned = btn.cloneNode(true);
505
- cloned.className = cloned.className + ` ${clippathConfigMap.get(btn).class}`;
506
- cloned.removeAttribute('data-kitzo-clippath');
507
- cloned.setAttribute('data-temp-clippath-el', true);
508
-
509
- setTimeout(() => {
510
- const fontFamily = window.getComputedStyle(btn).fontFamily;
511
- Object.assign(cloned.style, {
512
- backgroundColor: '#01c2b8',
513
- color: 'white',
514
- fontFamily,
515
- margin: 0,
516
- width: '100%',
517
- ...style,
518
- });
519
- }, 0);
520
-
521
- if (textOption && textOption instanceof Object) {
522
- requestAnimationFrame(() => {
523
- const target = typeof textOption.selector === 'string' ? clippathDiv.querySelector(textOption.selector.trim() ? textOption.selector.trim() : cloned.tagName) : cloned;
524
-
525
- if (target && (typeof textOption.value === 'string' || typeof textOption.value === 'number')) {
526
- target.textContent = textOption.value;
527
- }
528
- });
529
- }
530
-
531
- clippathDiv.style.width = `${width}px`;
532
- clippathDiv.style.height = `${height}px`;
533
- clippathDiv.style.translate = `${left}px ${top}px`;
534
- clippathDiv.style.setProperty('--kitzo-clippath-size', getClippathSize(clippathSize));
535
- clippathDiv.style.setProperty('--kitzo-clippath-transition', smooth ? 'clip-path 150ms ease-out, opacity 150ms' : 'none');
536
- clippathDiv.appendChild(cloned);
537
- requestAnimationFrame(() => {
538
- clippathDiv.classList.add('show');
539
- });
540
- }
541
- });
542
-
543
- document.addEventListener('mouseout', (e) => {
544
- const btn = e.target.closest('[data-kitzo-clippath]');
545
- if (btn) {
546
- const { smooth } = clippathConfigMap.get(btn);
547
- clippathDiv.classList.remove('show');
548
- setTimeout(
549
- () => {
550
- clippathDiv.querySelectorAll('[data-temp-clippath-el]').forEach((el) => el.remove());
551
- },
552
- smooth ? 150 : 150,
553
- );
554
- }
555
- });
556
-
557
- document.addEventListener('mousemove', (e) => {
558
- if (!isHovering) return;
559
-
560
- const btn = e.target.closest('[data-kitzo-clippath]');
561
- if (btn) {
562
- const { left, top } = btn.getBoundingClientRect();
563
- const x = e.clientX - left;
564
- const y = e.clientY - top;
565
-
566
- requestAnimationFrame(() => {
567
- clippathDiv.style.setProperty('--kitzo-clippath-pos-x', `${x}px`);
568
- clippathDiv.style.setProperty('--kitzo-clippath-pos-y', `${y}px`);
569
- });
570
- }
571
- });
572
-
573
- isClippathListenersAdded = true;
574
- }
575
- }
576
-
577
- function getType(value) {
578
- if (value === null) return 'null';
579
-
580
- if (Array.isArray(value)) return 'array';
581
-
582
- if (value instanceof Date) return 'date';
583
-
584
- if (value instanceof RegExp) return 'regexp';
585
-
586
- if (typeof value === 'object') return 'object';
587
-
588
- return typeof value;
589
- }
590
-
591
- // button effects
592
-
593
- const kitzo = { copy, debounce, ripple, tooltip, clippath, getType };
594
-
595
- return kitzo;
596
-
597
- }));