jizy-tooltip 2.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019-present Joffrey Demetz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # JiZy platform
2
+
@@ -0,0 +1 @@
1
+ @keyframes tooltip-anim{0%{opacity:0;transform:matrix(.5,0,0,.8,0,0)}20%{transform:matrix(1,0,0,1,0,0)}40%{opacity:1}70%{transform:matrix(1,0,0,1,0,0)}to{transform:matrix(1,0,0,1,0,0)}}.tooltip{animation:tooltip-anim .7s;background-color:#333;border-radius:4px;box-shadow:0 0 4px hsla(0,0%,100%,.7);color:#fff;font-family:inherit;font-size:14px;line-height:1;max-width:170px;opacity:0;position:absolute;text-align:center;transform:translateZ(0);transition:opacity .8s;z-index:234567891}.tooltip:empty{display:none!important;opacity:0!important}.tooltip .tip-header{border-bottom:1px solid #fff;padding:2px 4px}.tooltip .tip-content{padding:8px 10px}.tooltip.arrow:after{border:0 solid transparent;box-shadow:0 0 4px hsla(0,0%,100%,.7);content:"";display:block;position:absolute}.tooltip.arrow.left:after,.tooltip.arrow.right:after{top:50%;transform:translateY(-50%)}.tooltip.arrow.bottom:after,.tooltip.arrow.top:after{left:50%;transform:translateX(-50%)}.tooltip.arrow.left:after{border-left-color:#333;border-width:6px 0 6px 8px;right:-8px}.tooltip.arrow.right:after{border-right-color:#333;border-width:6px 8px 6px 0;left:-8px}.tooltip.arrow.bottom:after{border-top-color:#333;border-width:0 6px 8px;top:-8px}.tooltip.arrow.top:after{border-bottom-color:#333;border-width:8px 6px 0;bottom:-8px}
@@ -0,0 +1,2 @@
1
+ /*! jTooltip v2.1.0 | 2025-08-28T14:31Z | [default] */
2
+ !function(t){"use strict";if("object"!=typeof t||!t||!t.document)throw new Error("jTooltip requires a window and a document");if(void 0!==t.jTooltip)throw new Error("jTooltip is already defined");!function(){class t{constructor(t){this.el=t,this.uuid=t.dataset.tipId||autoUuid(),this.header=t.dataset.tipHeader||"",this.content=t.dataset.tip||"",this.theme=t.dataset.tipTheme||"",this.position=t.dataset.tipPosition||"top",this.coords=null}setCoords(){const t=this.el.getBoundingClientRect();this.coords={top:t.top+window.scrollY,left:t.left+window.scrollX,right:t.right+window.scrollX,bottom:t.bottom+window.scrollY,width:t.width,height:t.height}}autoUuid(){return crypto.randomUUID?crypto.randomUUID():"tip-"+Date.now()+Math.random().toString(16).slice(2)}}}();t.jTooltip=Tooltip}("undefined"!=typeof window?window:this);
package/lib/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import Tooltip from './js/Tooltip.js';
2
+ import Tip from './js/Tip.js';
3
+
4
+ export default { Tooltip, Tip };
package/lib/js/Tip.js ADDED
@@ -0,0 +1,31 @@
1
+ export default class Tip {
2
+ constructor(el) {
3
+ this.el = el;
4
+ this.uuid = el.dataset.tipId || autoUuid();
5
+ this.header = el.dataset.tipHeader || '';
6
+ this.content = el.dataset.tip || '';
7
+ this.theme = el.dataset.tipTheme || '';
8
+ this.position = el.dataset.tipPosition || 'top';
9
+ this.coords = null;
10
+ }
11
+
12
+ setCoords() {
13
+ const rect = this.el.getBoundingClientRect();
14
+
15
+ this.coords = {
16
+ top: rect.top + window.scrollY,
17
+ left: rect.left + window.scrollX,
18
+ right: rect.right + window.scrollX,
19
+ bottom: rect.bottom + window.scrollY,
20
+ width: rect.width,
21
+ height: rect.height
22
+ };
23
+ }
24
+
25
+ autoUuid() {
26
+ if (crypto.randomUUID) {
27
+ return crypto.randomUUID();
28
+ }
29
+ return 'tip-' + Date.now() + Math.random().toString(16).slice(2);
30
+ }
31
+ };
@@ -0,0 +1,163 @@
1
+ import Tip from './Tip.js';
2
+
3
+ export default class Tooltip {
4
+ constructor(id = 'jTip', distance = 10) {
5
+ this.id = id;
6
+ this.distance = distance;
7
+ this.shown = null;
8
+ this.customRenderer = null;
9
+ this.element = null;
10
+ this.uuid = 1;
11
+ }
12
+
13
+ setRenderer(callback) {
14
+ if (typeof callback === 'function') {
15
+ this.customRenderer = callback;
16
+ }
17
+ return this;
18
+ }
19
+
20
+ ready() {
21
+ this.element = this.getElement();
22
+
23
+ document.body.addEventListener('keyup', (e) => {
24
+ if (e.key === 'Escape' || e.key === 'Esc') {
25
+ this.hide();
26
+ }
27
+ });
28
+
29
+ document.body.addEventListener('click', (e) => {
30
+ if (!this.element.contains(e.target)) {
31
+ this.hide();
32
+ }
33
+ });
34
+
35
+ document.body.addEventListener('touchstart', (e) => {
36
+ if (!this.element.contains(e.target)) {
37
+ this.hide();
38
+ }
39
+ });
40
+
41
+ return this;
42
+ }
43
+
44
+ getElement() {
45
+ let element = document.querySelector('#' + this.id);
46
+
47
+ if (!element || element.length === 0) {
48
+ element = document.createElement('div');
49
+ element.id = this.id;
50
+ document.body.appendChild(element);
51
+ }
52
+
53
+ element.setAttribute('role', 'tooltip');
54
+ element.setAttribute('aria-hidden', 'true');
55
+ element.classList.add('jtip', 'hidden');
56
+ return element;
57
+ }
58
+
59
+ position(trigger, position) {
60
+ let left, top;
61
+ const tipW = this.element.offsetWidth;
62
+ const tipH = this.element.offsetHeight;
63
+ const viewportW = document.documentElement.clientWidth;
64
+
65
+ if (position === 'center') {
66
+ top = trigger.top + trigger.height / 2 - tipH / 2;
67
+ left = trigger.left + trigger.width / 2 - tipW / 2;
68
+ } else if (position === 'left' || position === 'right') {
69
+ top = (trigger.top + trigger.bottom) / 2 - tipH / 2;
70
+ if (position === 'left') {
71
+ left = trigger.left - this.distance - tipW;
72
+ if (left < 0) left = this.distance;
73
+ } else {
74
+ left = trigger.right + this.distance;
75
+ if (left + tipW > viewportW) left = viewportW - tipW - this.distance;
76
+ }
77
+ } else {
78
+ left = trigger.left + (trigger.width - tipW) / 2;
79
+ top = position === 'bottom' ? trigger.bottom + this.distance : trigger.top - tipH - this.distance;
80
+ }
81
+
82
+ if (left < 0) left = trigger.left;
83
+ if (top < 0) top = trigger.bottom + this.distance;
84
+
85
+ this.element.style.left = left + 'px';
86
+ this.element.style.top = top + window.pageYOffset + 'px';
87
+ }
88
+
89
+ cleanClasses() {
90
+ this.element.className.split(' ').forEach((item) => {
91
+ if (item.match(/theme-|arrow|top|right|bottom|left/)) {
92
+ this.element.classList.remove(item);
93
+ }
94
+ });
95
+ }
96
+
97
+ show(tip) {
98
+ if (this.shown === tip.uuid) return;
99
+
100
+ tip.setCoords();
101
+ this.shown = tip.uuid;
102
+ this.cleanClasses();
103
+
104
+ this.element.innerHTML = this.renderer ? this.renderer(tip) : this.getTemplate(tip);
105
+ this.element.classList.add(tip.position);
106
+
107
+ if (tip.theme) {
108
+ tip.theme.split(' ').forEach(theme => {
109
+ this.element.classList.add(theme);
110
+ });
111
+ }
112
+
113
+ this.position(tip.coords, tip.position);
114
+
115
+ this.element.setAttribute('aria-hidden', 'false');
116
+ this.element.classList.add('fade-in');
117
+ this.element.classList.remove('hidden');
118
+ }
119
+
120
+ hide(tip) {
121
+ if (this.shown && (!tip || tip.uuid === this.shown)) {
122
+ this.shown = null;
123
+ this.element.classList.remove('fade-in');
124
+ this.element.classList.add('fade-out');
125
+
126
+ setTimeout(() => {
127
+ this.element.innerHTML = '';
128
+ this.cleanClasses();
129
+ this.element.classList.add('hidden');
130
+ this.element.classList.remove('fade-out');
131
+ this.element.setAttribute('aria-hidden', 'true');
132
+ }, 200);
133
+ }
134
+ }
135
+
136
+ getTemplate(tip) {
137
+ let html = '<div>';
138
+ if (tip.header) {
139
+ html += '<div class="tip-header">' + tip.header + '</div>';
140
+ }
141
+ html += '<div class="tip-content">' + tip.content + '</div>';
142
+ html += '</div>';
143
+ return html;
144
+ }
145
+
146
+ fromElement(el) {
147
+ if (!el) return;
148
+
149
+ if (!el.dataset.tipId) {
150
+ el.dataset.tipId = 'd-' + this.uuid++;
151
+ }
152
+
153
+ const tip = new Tip(el);
154
+
155
+ this.show(tip);
156
+ }
157
+
158
+ // @COMPAT
159
+
160
+ init() {
161
+ this.ready();
162
+ }
163
+ };
@@ -0,0 +1,22 @@
1
+ @keyframes tooltip-anim {
2
+ 0% {
3
+ opacity: 0;
4
+ transform: matrix(0.5, 0, 0, 0.8, 0, 0);
5
+ }
6
+
7
+ 20% {
8
+ transform: matrix(1, 0, 0, 1, 0, 0);
9
+ }
10
+
11
+ 40% {
12
+ opacity: 1;
13
+ }
14
+
15
+ 70% {
16
+ transform: matrix(1, 0, 0, 1, 0, 0);
17
+ }
18
+
19
+ 100% {
20
+ transform: matrix(1, 0, 0, 1, 0, 0);
21
+ }
22
+ }
@@ -0,0 +1,90 @@
1
+ .tooltip {
2
+ // display: block;
3
+ max-width: @tooltip-width;
4
+ position: absolute;
5
+ opacity: 0;
6
+ border-radius: 4px;
7
+ background-color: @tooltip-bg-color;
8
+ font-family: @tooltip-font-family;
9
+ font-size: @tooltip-font-size;
10
+ line-height: 1;
11
+ color: @tooltip-fg-color;
12
+ text-align: center;
13
+ transition: opacity .8s;
14
+ transform: translateZ(0); // GPU
15
+ box-shadow: 0 0 4px rgba(255, 255, 255, .7);
16
+ animation: tooltip-anim .7s;
17
+ z-index: 234567891;
18
+
19
+ &:empty {
20
+ display: none !important;
21
+ opacity: 0 !important;
22
+ }
23
+
24
+ .tip-header {
25
+ padding: 2px 4px;
26
+ border-bottom: 1px solid @tooltip-fg-color;
27
+ }
28
+
29
+ .tip-content {
30
+ padding: 8px 10px;
31
+ }
32
+
33
+ &.arrow {
34
+ &::after {
35
+ position: absolute;
36
+ display: block;
37
+ content: "";
38
+ border: 0 solid transparent;
39
+ box-shadow: 0 0 4px rgba(255, 255, 255, .7);
40
+ }
41
+
42
+ // position arrow in the center Y
43
+ &.left::after,
44
+ &.right::after {
45
+ top: 50%;
46
+ transform: translateY(-50%);
47
+ }
48
+
49
+ // position arrow in the center X
50
+ &.top::after,
51
+ &.bottom::after {
52
+ left: 50%;
53
+ transform: translateX(-50%);
54
+ }
55
+
56
+ // position arrow on the right
57
+ &.left::after {
58
+ right: -8px;
59
+ border-width: 6px 0 6px 8px;
60
+ border-left-color: @tooltip-bg-color;
61
+ }
62
+
63
+ // position arrow on the left
64
+ &.right::after {
65
+ left: -8px;
66
+ border-width: 6px 8px 6px 0;
67
+ border-right-color: @tooltip-bg-color;
68
+ }
69
+
70
+ // position arrow in the top
71
+ &.bottom::after {
72
+ top: -8px;
73
+ border-width: 0 6px 8px 6px;
74
+ border-top-color: @tooltip-bg-color;
75
+ }
76
+
77
+ // position arrow in the bottom
78
+ &.top::after {
79
+ bottom: -8px;
80
+ border-width: 8px 6px 0 6px;
81
+ border-bottom-color: @tooltip-bg-color;
82
+ }
83
+ }
84
+ }
85
+
86
+ // [data-tooltip] {
87
+ // cursor: pointer;
88
+ // color: #7cb342;
89
+ // display: inline-block;
90
+ // }
@@ -0,0 +1,5 @@
1
+ @tooltip-font-family: inherit;
2
+ @tooltip-font-size: 14px;
3
+ @tooltip-width: 170px;
4
+ @tooltip-bg-color: #333;
5
+ @tooltip-fg-color: #FFF;
@@ -0,0 +1,4 @@
1
+ @import (optional) url("less/_variables.less");
2
+ @import url("less/variables.less");
3
+ @import url("less/animations.less");
4
+ @import url("less/structure.less");
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "jizy-tooltip",
3
+ "version": "2.1.0",
4
+ "browser": "dist/js/jizy-tooltip.min.js",
5
+ "main": "lib/index.js",
6
+ "type": "module",
7
+ "jizy": "dist/",
8
+ "files": [
9
+ "dist/*",
10
+ "lib/*"
11
+ ],
12
+ "scripts": {
13
+ "jpack:export": "node ./cli/jpack.js --action build --name perso",
14
+ "jpack:export-debug": "node ./cli/jpack.js --action build --name perso --debug",
15
+ "jpack:dist": "node ./cli/jpack.js",
16
+ "jpack:dist-debug": "node ./cli/jpack.js --debug"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/joffreydemetz/jizy-tooltip.git"
21
+ },
22
+ "keywords": [
23
+ "JiZy",
24
+ "JS",
25
+ "Tooltip"
26
+ ],
27
+ "author": "Joffrey Demetz <joffrey.demetz@gmail.com> (https://joffreydemetz.com/)",
28
+ "license": "MIT",
29
+ "description": "A lightweight JS tooltip module",
30
+ "devDependencies": {
31
+ "jizy-packer": "^2.1"
32
+ }
33
+ }