kitzo 2.1.28 → 2.1.30

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