snice 1.14.3 → 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.
Files changed (185) hide show
  1. package/bin/templates/base/tsconfig.json +5 -4
  2. package/components/accordion/demo.html +403 -0
  3. package/components/accordion/snice-accordion-item.css +85 -0
  4. package/components/accordion/snice-accordion-item.ts +226 -0
  5. package/components/accordion/snice-accordion.css +31 -0
  6. package/components/accordion/snice-accordion.ts +182 -0
  7. package/components/accordion/snice-accordion.types.ts +32 -0
  8. package/components/alert/demo.html +445 -0
  9. package/components/alert/snice-alert.css +195 -0
  10. package/components/alert/snice-alert.ts +141 -0
  11. package/components/alert/snice-alert.types.ts +12 -0
  12. package/components/avatar/demo.html +598 -0
  13. package/components/avatar/snice-avatar.css +131 -0
  14. package/components/avatar/snice-avatar.ts +136 -0
  15. package/components/avatar/snice-avatar.types.ts +13 -0
  16. package/components/badge/demo.html +523 -0
  17. package/components/badge/snice-badge.css +161 -0
  18. package/components/badge/snice-badge.ts +117 -0
  19. package/components/badge/snice-badge.types.ts +16 -0
  20. package/components/breadcrumbs/demo.html +404 -0
  21. package/components/breadcrumbs/snice-breadcrumbs.css +133 -0
  22. package/components/breadcrumbs/snice-breadcrumbs.ts +191 -0
  23. package/components/breadcrumbs/snice-breadcrumbs.types.ts +26 -0
  24. package/components/breadcrumbs/snice-crumb.ts +26 -0
  25. package/components/button/demo.html +42 -0
  26. package/components/button/snice-button.css +230 -0
  27. package/components/button/snice-button.ts +169 -0
  28. package/components/button/snice-button.types.ts +25 -0
  29. package/components/card/demo.html +525 -0
  30. package/components/card/snice-card.css +140 -0
  31. package/components/card/snice-card.ts +102 -0
  32. package/components/card/snice-card.types.ts +10 -0
  33. package/components/checkbox/demo.html +253 -0
  34. package/components/checkbox/snice-checkbox.css +164 -0
  35. package/components/checkbox/snice-checkbox.ts +223 -0
  36. package/components/checkbox/snice-checkbox.types.ts +22 -0
  37. package/components/chip/demo.html +383 -0
  38. package/components/chip/snice-chip.css +195 -0
  39. package/components/chip/snice-chip.ts +139 -0
  40. package/components/chip/snice-chip.types.ts +15 -0
  41. package/components/date-picker/README.md +233 -0
  42. package/components/date-picker/demo.html +191 -0
  43. package/components/date-picker/snice-date-picker.css +330 -0
  44. package/components/date-picker/snice-date-picker.ts +777 -0
  45. package/components/date-picker/snice-date-picker.types.ts +83 -0
  46. package/components/divider/demo.html +233 -0
  47. package/components/divider/snice-divider.css +155 -0
  48. package/components/divider/snice-divider.ts +69 -0
  49. package/components/divider/snice-divider.types.ts +15 -0
  50. package/components/drawer/demo.html +328 -0
  51. package/components/drawer/snice-drawer.css +476 -0
  52. package/components/drawer/snice-drawer.ts +287 -0
  53. package/components/drawer/snice-drawer.types.ts +17 -0
  54. package/components/global.d.ts +14 -0
  55. package/components/input/demo.html +303 -0
  56. package/components/input/snice-input.css +257 -0
  57. package/components/input/snice-input.ts +442 -0
  58. package/components/input/snice-input.types.ts +59 -0
  59. package/components/input/test.html +77 -0
  60. package/components/layout/README.md +260 -0
  61. package/components/layout/demo.html +538 -0
  62. package/components/layout/snice-layout-blog.css +129 -0
  63. package/components/layout/snice-layout-blog.ts +48 -0
  64. package/components/layout/snice-layout-card.css +104 -0
  65. package/components/layout/snice-layout-card.ts +35 -0
  66. package/components/layout/snice-layout-centered.css +51 -0
  67. package/components/layout/snice-layout-centered.ts +22 -0
  68. package/components/layout/snice-layout-dashboard.css +98 -0
  69. package/components/layout/snice-layout-dashboard.ts +45 -0
  70. package/components/layout/snice-layout-fullscreen.css +72 -0
  71. package/components/layout/snice-layout-fullscreen.ts +34 -0
  72. package/components/layout/snice-layout-landing.css +92 -0
  73. package/components/layout/snice-layout-landing.ts +47 -0
  74. package/components/layout/snice-layout-minimal.css +16 -0
  75. package/components/layout/snice-layout-minimal.ts +19 -0
  76. package/components/layout/snice-layout-sidebar.css +117 -0
  77. package/components/layout/snice-layout-sidebar.ts +48 -0
  78. package/components/layout/snice-layout-split.css +103 -0
  79. package/components/layout/snice-layout-split.ts +29 -0
  80. package/components/layout/snice-layout.css +72 -0
  81. package/components/layout/snice-layout.ts +35 -0
  82. package/components/layout/snice-layout.types.ts +5 -0
  83. package/components/login/demo-auth-controller.ts +185 -0
  84. package/components/login/demo.html +470 -0
  85. package/components/login/snice-login.css +204 -0
  86. package/components/login/snice-login.ts +337 -0
  87. package/components/login/snice-login.types.ts +34 -0
  88. package/components/modal/demo.html +291 -0
  89. package/components/modal/snice-modal.css +203 -0
  90. package/components/modal/snice-modal.ts +233 -0
  91. package/components/modal/snice-modal.types.ts +21 -0
  92. package/components/pagination/demo.html +395 -0
  93. package/components/pagination/snice-pagination.ts +333 -0
  94. package/components/pagination/snice-pagination.types.ts +21 -0
  95. package/components/progress/demo.html +510 -0
  96. package/components/progress/snice-progress.css +267 -0
  97. package/components/progress/snice-progress.ts +247 -0
  98. package/components/progress/snice-progress.types.ts +19 -0
  99. package/components/radio/demo.html +287 -0
  100. package/components/radio/snice-radio.css +171 -0
  101. package/components/radio/snice-radio.ts +218 -0
  102. package/components/radio/snice-radio.types.ts +21 -0
  103. package/components/select/demo.html +511 -0
  104. package/components/select/snice-option.ts +52 -0
  105. package/components/select/snice-option.types.ts +14 -0
  106. package/components/select/snice-select.css +392 -0
  107. package/components/select/snice-select.ts +796 -0
  108. package/components/select/snice-select.types.ts +55 -0
  109. package/components/skeleton/demo.html +514 -0
  110. package/components/skeleton/snice-skeleton.css +109 -0
  111. package/components/skeleton/snice-skeleton.ts +126 -0
  112. package/components/skeleton/snice-skeleton.types.ts +11 -0
  113. package/components/switch/demo.html +284 -0
  114. package/components/switch/snice-switch.css +221 -0
  115. package/components/switch/snice-switch.ts +229 -0
  116. package/components/switch/snice-switch.types.ts +23 -0
  117. package/components/symbols.ts +23 -0
  118. package/components/table/demo-table-controller.ts +100 -0
  119. package/components/table/demo.html +480 -0
  120. package/components/table/snice-cell-boolean.ts +112 -0
  121. package/components/table/snice-cell-date.ts +210 -0
  122. package/components/table/snice-cell-duration.ts +91 -0
  123. package/components/table/snice-cell-filesize.ts +90 -0
  124. package/components/table/snice-cell-number.ts +165 -0
  125. package/components/table/snice-cell-progress.ts +83 -0
  126. package/components/table/snice-cell-rating.ts +82 -0
  127. package/components/table/snice-cell-sparkline.ts +253 -0
  128. package/components/table/snice-cell-text.ts +125 -0
  129. package/components/table/snice-cell.css +296 -0
  130. package/components/table/snice-cell.ts +473 -0
  131. package/components/table/snice-column.ts +353 -0
  132. package/components/table/snice-header.css +243 -0
  133. package/components/table/snice-header.ts +261 -0
  134. package/components/table/snice-progress.ts +66 -0
  135. package/components/table/snice-rating.ts +45 -0
  136. package/components/table/snice-row.css +255 -0
  137. package/components/table/snice-row.ts +331 -0
  138. package/components/table/snice-table.css +241 -0
  139. package/components/table/snice-table.ts +737 -0
  140. package/components/table/snice-table.types.ts +158 -0
  141. package/components/tabs/demo.html +487 -0
  142. package/components/tabs/snice-tab-panel.css +264 -0
  143. package/components/tabs/snice-tab-panel.ts +47 -0
  144. package/components/tabs/snice-tab.css +96 -0
  145. package/components/tabs/snice-tab.ts +65 -0
  146. package/components/tabs/snice-tabs.css +189 -0
  147. package/components/tabs/snice-tabs.ts +332 -0
  148. package/components/tabs/snice-tabs.types.ts +28 -0
  149. package/components/theme/theme.css +234 -0
  150. package/components/toast/demo.html +329 -0
  151. package/components/toast/snice-toast-container.ts +256 -0
  152. package/components/toast/snice-toast.css +213 -0
  153. package/components/toast/snice-toast.ts +276 -0
  154. package/components/toast/snice-toast.types.ts +35 -0
  155. package/components/tooltip/demo.html +350 -0
  156. package/components/tooltip/snice-tooltip-portal.css +79 -0
  157. package/components/tooltip/snice-tooltip.css +117 -0
  158. package/components/tooltip/snice-tooltip.ts +612 -0
  159. package/components/tooltip/snice-tooltip.types.ts +32 -0
  160. package/components/transitions.ts +94 -0
  161. package/components/tsconfig.json +18 -0
  162. package/dist/index.cjs +441 -329
  163. package/dist/index.cjs.map +1 -1
  164. package/dist/index.cjs.min.map +1 -1
  165. package/dist/index.esm.js +441 -329
  166. package/dist/index.esm.js.map +1 -1
  167. package/dist/index.esm.min.js +3 -3
  168. package/dist/index.esm.min.js.map +1 -1
  169. package/dist/index.iife.js +441 -329
  170. package/dist/index.iife.js.map +1 -1
  171. package/dist/index.iife.min.js +3 -3
  172. package/dist/index.iife.min.js.map +1 -1
  173. package/dist/symbols.esm.js +1 -1
  174. package/dist/transitions.esm.js +1 -1
  175. package/dist/types/controller.d.ts +1 -1
  176. package/dist/types/element.d.ts +10 -10
  177. package/dist/types/events.d.ts +2 -2
  178. package/dist/types/index.d.ts +1 -1
  179. package/dist/types/observe.d.ts +1 -1
  180. package/dist/types/request-response.d.ts +2 -3
  181. package/dist/types/router.d.ts +1 -1
  182. package/package.json +9 -3
  183. package/dist/index.cjs.min +0 -15
  184. package/dist/symbols.cjs +0 -103
  185. package/dist/transitions.cjs +0 -219
@@ -0,0 +1,612 @@
1
+ import { element, property, query, on, watch, ready, dispose } from 'snice';
2
+ import css from './snice-tooltip.css?inline';
3
+ import portalCss from './snice-tooltip-portal.css?inline';
4
+ import type { TooltipPosition, TooltipTrigger, SniceTooltipElement } from './snice-tooltip.types';
5
+
6
+ @element('snice-tooltip')
7
+ export class SniceTooltip extends HTMLElement implements SniceTooltipElement {
8
+ private static portalStylesInjected = false;
9
+ @property({ reflect: true })
10
+ content: string = '';
11
+
12
+ @property({ reflect: true })
13
+ position: TooltipPosition = 'top';
14
+
15
+ @property({ reflect: true })
16
+ trigger: TooltipTrigger = 'hover';
17
+
18
+ @property({ type: Number, reflect: true })
19
+ delay: number = 0;
20
+
21
+ @property({ type: Number, attribute: 'hide-delay', reflect: true })
22
+ hideDelay: number = 0;
23
+
24
+ @property({ type: Number, reflect: true })
25
+ offset: number = 12;
26
+
27
+ @property({ type: Boolean, reflect: true })
28
+ arrow: boolean = true;
29
+
30
+ @property({ type: Boolean, reflect: true })
31
+ open: boolean = false;
32
+
33
+ @property({ type: Number, attribute: 'max-width', reflect: true })
34
+ maxWidth: number = 250;
35
+
36
+ @property({ type: Number, attribute: 'z-index', reflect: true })
37
+ zIndex: number = 10000;
38
+
39
+ @property({ type: Boolean, attribute: 'strict-positioning', reflect: true })
40
+ strictPositioning: boolean = false;
41
+
42
+ @query('.tooltip')
43
+ tooltipElement?: HTMLElement;
44
+
45
+ @query('.tooltip__arrow')
46
+ arrowElement?: HTMLElement;
47
+
48
+ private showTimeout?: number;
49
+ private hideTimeout?: number;
50
+ private activePosition: TooltipPosition = 'top';
51
+ private portalElement?: HTMLElement;
52
+
53
+ private static ensurePortalStyles() {
54
+ if (SniceTooltip.portalStylesInjected) return;
55
+
56
+ try {
57
+ // Use Constructable Stylesheets if supported
58
+ if ('adoptedStyleSheets' in document && 'CSSStyleSheet' in window) {
59
+ const sheet = new CSSStyleSheet();
60
+ sheet.replaceSync(portalCss);
61
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
62
+ } else {
63
+ // Fallback for older browsers
64
+ const style = document.createElement('style');
65
+ style.textContent = portalCss;
66
+ document.head.appendChild(style);
67
+ }
68
+
69
+ SniceTooltip.portalStylesInjected = true;
70
+ } catch (error) {
71
+ console.warn('Failed to inject tooltip portal styles:', error);
72
+ }
73
+ }
74
+
75
+ html() {
76
+ return /*html*/`
77
+ <div class="tooltip-trigger">
78
+ <slot></slot>
79
+ </div>
80
+ <div class="tooltip tooltip--${this.position}" role="tooltip" hidden>
81
+ <div class="tooltip__content">${this.content}</div>
82
+ <div class="tooltip__arrow"></div>
83
+ </div>
84
+ `;
85
+ }
86
+
87
+ css() {
88
+ return css;
89
+ }
90
+
91
+ @ready()
92
+ init() {
93
+ if (this.tooltipElement) {
94
+ this.tooltipElement.style.setProperty('--tooltip-max-width', `${this.maxWidth}px`);
95
+ this.tooltipElement.style.setProperty('--tooltip-z-index', `${this.zIndex}`);
96
+ }
97
+
98
+ if (this.open && this.trigger === 'manual') {
99
+ this.show();
100
+ }
101
+ }
102
+
103
+ @dispose()
104
+ cleanup() {
105
+ this.clearTimeouts();
106
+ if (this.portalElement) {
107
+ this.portalElement.remove();
108
+ this.portalElement = undefined;
109
+ }
110
+ }
111
+
112
+ @watch('content')
113
+ updateContent() {
114
+ const contentEl = this.shadowRoot?.querySelector('.tooltip__content');
115
+ if (contentEl) {
116
+ contentEl.textContent = this.content;
117
+ }
118
+ }
119
+
120
+ @watch('arrow')
121
+ updateArrow() {
122
+ if (this.arrowElement) {
123
+ if (this.arrow) {
124
+ this.arrowElement.removeAttribute('hidden');
125
+ } else {
126
+ this.arrowElement.setAttribute('hidden', '');
127
+ }
128
+ }
129
+ }
130
+
131
+ @watch('maxWidth')
132
+ updateMaxWidth() {
133
+ if (this.tooltipElement) {
134
+ this.tooltipElement.style.setProperty('--tooltip-max-width', `${this.maxWidth}px`);
135
+ }
136
+ }
137
+
138
+ @watch('zIndex')
139
+ updateZIndex() {
140
+ if (this.tooltipElement) {
141
+ this.tooltipElement.style.setProperty('--tooltip-z-index', `${this.zIndex}`);
142
+ }
143
+ }
144
+
145
+ @watch('open')
146
+ handleOpenChange() {
147
+ if (this.trigger === 'manual') {
148
+ if (this.open) {
149
+ this.show();
150
+ } else {
151
+ this.hide();
152
+ }
153
+ }
154
+ }
155
+
156
+ @on('mouseenter')
157
+ handleMouseEnter() {
158
+ if (this.trigger === 'hover') {
159
+ this.scheduleShow();
160
+ }
161
+ }
162
+
163
+ @on('mouseleave')
164
+ handleMouseLeave() {
165
+ if (this.trigger === 'hover') {
166
+ this.scheduleHide();
167
+ }
168
+ }
169
+
170
+ @on('focusin')
171
+ handleFocusIn() {
172
+ if (this.trigger === 'focus') {
173
+ this.scheduleShow();
174
+ }
175
+ }
176
+
177
+ @on('focusout')
178
+ handleFocusOut() {
179
+ if (this.trigger === 'focus') {
180
+ this.scheduleHide();
181
+ }
182
+ }
183
+
184
+ @on('click')
185
+ handleClick() {
186
+ if (this.trigger === 'click') {
187
+ this.toggle();
188
+ }
189
+ }
190
+
191
+ private scheduleShow() {
192
+ this.clearTimeouts();
193
+ if (this.delay > 0) {
194
+ this.showTimeout = window.setTimeout(() => this.show(), this.delay);
195
+ } else {
196
+ this.show();
197
+ }
198
+ }
199
+
200
+ private scheduleHide() {
201
+ this.clearTimeouts();
202
+ if (this.hideDelay > 0) {
203
+ this.hideTimeout = window.setTimeout(() => this.hide(), this.hideDelay);
204
+ } else {
205
+ this.hide();
206
+ }
207
+ }
208
+
209
+ private clearTimeouts() {
210
+ if (this.showTimeout) {
211
+ clearTimeout(this.showTimeout);
212
+ this.showTimeout = undefined;
213
+ }
214
+ if (this.hideTimeout) {
215
+ clearTimeout(this.hideTimeout);
216
+ this.hideTimeout = undefined;
217
+ }
218
+ }
219
+
220
+ show() {
221
+ if (!this.content) return;
222
+
223
+ // Ensure portal styles are injected
224
+ SniceTooltip.ensurePortalStyles();
225
+
226
+ // Create portal element if it doesn't exist
227
+ if (!this.portalElement) {
228
+ this.portalElement = this.createPortalElement();
229
+ this.updateArrowStyles(this.position);
230
+ }
231
+
232
+ this.portalElement.style.display = 'block';
233
+ this.updatePortalContent();
234
+
235
+ // Force reflow before adding visible class
236
+ void this.portalElement.offsetHeight;
237
+
238
+ this.updatePosition();
239
+ this.portalElement.classList.add('snice-tooltip--visible');
240
+
241
+ if (this.trigger === 'click') {
242
+ // Add click outside listener
243
+ setTimeout(() => {
244
+ document.addEventListener('click', this.handleClickOutside);
245
+ }, 0);
246
+ }
247
+ }
248
+
249
+ hide() {
250
+ if (!this.portalElement) return;
251
+
252
+ this.portalElement.classList.remove('snice-tooltip--visible');
253
+
254
+ setTimeout(() => {
255
+ if (this.portalElement) {
256
+ this.portalElement.style.display = 'none';
257
+ }
258
+ }, 200); // Match transition duration
259
+
260
+ if (this.trigger === 'click') {
261
+ document.removeEventListener('click', this.handleClickOutside);
262
+ }
263
+ }
264
+
265
+ toggle() {
266
+ if (!this.portalElement || this.portalElement.style.display === 'none') {
267
+ this.show();
268
+ } else {
269
+ this.hide();
270
+ }
271
+ }
272
+
273
+ updatePosition() {
274
+ if (!this.portalElement) return;
275
+
276
+ // Make sure tooltip is visible but off-screen to get accurate dimensions
277
+ this.portalElement.style.visibility = 'hidden';
278
+ this.portalElement.style.left = '-9999px';
279
+ this.portalElement.style.top = '-9999px';
280
+
281
+ const triggerRect = this.getBoundingClientRect();
282
+ const tooltipRect = this.portalElement.getBoundingClientRect();
283
+
284
+ // Restore visibility
285
+ this.portalElement.style.visibility = '';
286
+
287
+ // Calculate position with smart viewport detection
288
+ const position = this.calculatePosition(triggerRect, tooltipRect);
289
+
290
+ // Update tooltip position class if it changed
291
+ if (position.adjustedPosition !== this.activePosition) {
292
+ this.portalElement.classList.remove(`snice-tooltip--${this.activePosition}`);
293
+ this.portalElement.classList.add(`snice-tooltip--${position.adjustedPosition}`);
294
+ this.activePosition = position.adjustedPosition;
295
+
296
+ // Update arrow styles for new position
297
+ this.updateArrowStyles(position.adjustedPosition);
298
+ }
299
+
300
+ // Apply calculated position
301
+ this.portalElement.style.left = `${position.left}px`;
302
+ this.portalElement.style.top = `${position.top}px`;
303
+ }
304
+
305
+ private updateArrowStyles(position: TooltipPosition) {
306
+ if (!this.portalElement || !this.arrow) return;
307
+
308
+ const arrow = this.portalElement.querySelector('.snice-tooltip__arrow') as HTMLElement;
309
+ if (!arrow) return;
310
+
311
+ // Reset styles - set to explicit values instead of empty strings
312
+ arrow.style.top = 'auto';
313
+ arrow.style.bottom = 'auto';
314
+ arrow.style.left = 'auto';
315
+ arrow.style.right = 'auto';
316
+ arrow.style.transform = 'none';
317
+ arrow.style.borderTopColor = 'transparent';
318
+ arrow.style.borderBottomColor = 'transparent';
319
+ arrow.style.borderLeftColor = 'transparent';
320
+ arrow.style.borderRightColor = 'transparent';
321
+
322
+ // Apply position-specific styles
323
+ switch (position) {
324
+ case 'top':
325
+ case 'top-start':
326
+ case 'top-end':
327
+ arrow.style.bottom = '-6px';
328
+ arrow.style.borderWidth = '6px 6px 0';
329
+ arrow.style.borderTopColor = '#333';
330
+ arrow.style.borderLeftColor = 'transparent';
331
+ arrow.style.borderRightColor = 'transparent';
332
+ if (position === 'top') {
333
+ arrow.style.left = '50%';
334
+ arrow.style.transform = 'translateX(-50%)';
335
+ } else if (position === 'top-start') {
336
+ arrow.style.left = '16px';
337
+ } else {
338
+ arrow.style.right = '16px';
339
+ }
340
+ break;
341
+
342
+ case 'bottom':
343
+ case 'bottom-start':
344
+ case 'bottom-end':
345
+ arrow.style.top = '-6px';
346
+ arrow.style.borderWidth = '0 6px 6px';
347
+ arrow.style.borderBottomColor = '#333';
348
+ arrow.style.borderLeftColor = 'transparent';
349
+ arrow.style.borderRightColor = 'transparent';
350
+ if (position === 'bottom') {
351
+ arrow.style.left = '50%';
352
+ arrow.style.transform = 'translateX(-50%)';
353
+ } else if (position === 'bottom-start') {
354
+ arrow.style.left = '16px';
355
+ } else {
356
+ arrow.style.right = '16px';
357
+ }
358
+ break;
359
+
360
+ case 'left':
361
+ case 'left-start':
362
+ case 'left-end':
363
+ arrow.style.right = '-6px';
364
+ arrow.style.borderWidth = '6px 0 6px 6px';
365
+ arrow.style.borderLeftColor = '#333';
366
+ arrow.style.borderTopColor = 'transparent';
367
+ arrow.style.borderBottomColor = 'transparent';
368
+ if (position === 'left') {
369
+ arrow.style.top = '50%';
370
+ arrow.style.transform = 'translateY(-50%)';
371
+ } else if (position === 'left-start') {
372
+ arrow.style.top = '16px';
373
+ } else {
374
+ arrow.style.bottom = '16px';
375
+ }
376
+ break;
377
+
378
+ case 'right':
379
+ case 'right-start':
380
+ case 'right-end':
381
+ arrow.style.left = '-6px';
382
+ arrow.style.borderWidth = '6px 6px 6px 0';
383
+ arrow.style.borderRightColor = '#333';
384
+ arrow.style.borderTopColor = 'transparent';
385
+ arrow.style.borderBottomColor = 'transparent';
386
+ if (position === 'right') {
387
+ arrow.style.top = '50%';
388
+ arrow.style.transform = 'translateY(-50%)';
389
+ } else if (position === 'right-start') {
390
+ arrow.style.top = '16px';
391
+ } else {
392
+ arrow.style.bottom = '16px';
393
+ }
394
+ break;
395
+ }
396
+ }
397
+
398
+ private calculatePosition(triggerRect: DOMRect, tooltipRect: DOMRect) {
399
+ const viewport = {
400
+ width: window.innerWidth,
401
+ height: window.innerHeight,
402
+ scrollX: window.scrollX,
403
+ scrollY: window.scrollY
404
+ };
405
+
406
+ let position = this.position;
407
+ let left = 0;
408
+ let top = 0;
409
+
410
+ // Calculate base position
411
+ const positions = this.getPositionCoordinates(triggerRect, tooltipRect, position);
412
+ left = positions.left;
413
+ top = positions.top;
414
+
415
+ // If strict positioning is disabled, check if tooltip fits in viewport
416
+ if (!this.strictPositioning) {
417
+ const fitsInViewport =
418
+ left >= 0 &&
419
+ top >= 0 &&
420
+ left + tooltipRect.width <= viewport.width &&
421
+ top + tooltipRect.height <= viewport.height;
422
+
423
+ // If it doesn't fit, try alternative positions
424
+ if (!fitsInViewport) {
425
+ const alternativePositions = this.getAlternativePositions(position);
426
+
427
+ for (const altPosition of alternativePositions) {
428
+ const altCoords = this.getPositionCoordinates(triggerRect, tooltipRect, altPosition);
429
+
430
+ if (
431
+ altCoords.left >= 0 &&
432
+ altCoords.top >= 0 &&
433
+ altCoords.left + tooltipRect.width <= viewport.width &&
434
+ altCoords.top + tooltipRect.height <= viewport.height
435
+ ) {
436
+ position = altPosition;
437
+ left = altCoords.left;
438
+ top = altCoords.top;
439
+ break;
440
+ }
441
+ }
442
+ }
443
+ }
444
+
445
+ return { left, top, adjustedPosition: position };
446
+ }
447
+
448
+ private getPositionCoordinates(triggerRect: DOMRect, tooltipRect: DOMRect, position: TooltipPosition) {
449
+ let left = 0;
450
+ let top = 0;
451
+
452
+ const centerX = triggerRect.left + triggerRect.width / 2;
453
+ const centerY = triggerRect.top + triggerRect.height / 2;
454
+
455
+ // Different offsets for different directions
456
+ // Left/right need more spacing, top/bottom need less
457
+ const arrowSize = this.arrow ? 6 : 0;
458
+ const verticalOffset = this.offset; // Just base offset for top/bottom
459
+ const horizontalOffset = this.offset + arrowSize; // More offset for left/right
460
+
461
+ switch (position) {
462
+ case 'top':
463
+ left = centerX - tooltipRect.width / 2;
464
+ top = triggerRect.top - tooltipRect.height - verticalOffset;
465
+ break;
466
+ case 'top-start':
467
+ left = triggerRect.left;
468
+ top = triggerRect.top - tooltipRect.height - verticalOffset;
469
+ break;
470
+ case 'top-end':
471
+ left = triggerRect.right - tooltipRect.width;
472
+ top = triggerRect.top - tooltipRect.height - verticalOffset;
473
+ break;
474
+ case 'bottom':
475
+ left = centerX - tooltipRect.width / 2;
476
+ top = triggerRect.bottom + verticalOffset;
477
+ break;
478
+ case 'bottom-start':
479
+ left = triggerRect.left;
480
+ top = triggerRect.bottom + verticalOffset;
481
+ break;
482
+ case 'bottom-end':
483
+ left = triggerRect.right - tooltipRect.width;
484
+ top = triggerRect.bottom + verticalOffset;
485
+ break;
486
+ case 'left':
487
+ left = triggerRect.left - tooltipRect.width - horizontalOffset;
488
+ top = centerY - tooltipRect.height / 2;
489
+ break;
490
+ case 'left-start':
491
+ left = triggerRect.left - tooltipRect.width - horizontalOffset;
492
+ top = triggerRect.top;
493
+ break;
494
+ case 'left-end':
495
+ left = triggerRect.left - tooltipRect.width - horizontalOffset;
496
+ top = triggerRect.bottom - tooltipRect.height;
497
+ break;
498
+ case 'right':
499
+ left = triggerRect.right + horizontalOffset;
500
+ top = centerY - tooltipRect.height / 2;
501
+ break;
502
+ case 'right-start':
503
+ left = triggerRect.right + horizontalOffset;
504
+ top = triggerRect.top;
505
+ break;
506
+ case 'right-end':
507
+ left = triggerRect.right + horizontalOffset;
508
+ top = triggerRect.bottom - tooltipRect.height;
509
+ break;
510
+ }
511
+
512
+ return { left, top };
513
+ }
514
+
515
+ private getAlternativePositions(position: TooltipPosition): TooltipPosition[] {
516
+ const opposites: Record<string, TooltipPosition[]> = {
517
+ 'top': ['bottom', 'left', 'right'],
518
+ 'top-start': ['bottom-start', 'left', 'right'],
519
+ 'top-end': ['bottom-end', 'left', 'right'],
520
+ 'bottom': ['top', 'left', 'right'],
521
+ 'bottom-start': ['top-start', 'left', 'right'],
522
+ 'bottom-end': ['top-end', 'left', 'right'],
523
+ 'left': ['right', 'top', 'bottom'],
524
+ 'left-start': ['right-start', 'top', 'bottom'],
525
+ 'left-end': ['right-end', 'top', 'bottom'],
526
+ 'right': ['left', 'top', 'bottom'],
527
+ 'right-start': ['left-start', 'top', 'bottom'],
528
+ 'right-end': ['left-end', 'top', 'bottom']
529
+ };
530
+
531
+ return opposites[position] || ['top', 'bottom', 'left', 'right'];
532
+ }
533
+
534
+ private handleClickOutside = (e: MouseEvent) => {
535
+ if (!this.contains(e.target as Node)) {
536
+ this.hide();
537
+ }
538
+ };
539
+
540
+ private createPortalElement(): HTMLElement {
541
+ const portal = document.createElement('div');
542
+ portal.className = `snice-tooltip snice-tooltip--${this.position}`;
543
+ portal.setAttribute('role', 'tooltip');
544
+ portal.style.cssText = `
545
+ position: fixed;
546
+ z-index: ${this.zIndex};
547
+ padding: 8px 12px;
548
+ background: #333;
549
+ color: white;
550
+ border-radius: 6px;
551
+ font-size: 14px;
552
+ line-height: 1.4;
553
+ max-width: ${this.maxWidth}px;
554
+ width: max-content;
555
+ pointer-events: none;
556
+ opacity: 0;
557
+ transform: scale(0.95);
558
+ transition: opacity 0.2s, transform 0.2s;
559
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
560
+ display: none;
561
+ `;
562
+
563
+ // Create content element
564
+ const content = document.createElement('div');
565
+ content.className = 'snice-tooltip__content';
566
+ portal.appendChild(content);
567
+
568
+ // Create arrow element if needed
569
+ if (this.arrow) {
570
+ const arrow = document.createElement('div');
571
+ arrow.className = 'snice-tooltip__arrow';
572
+ arrow.style.cssText = `
573
+ position: absolute;
574
+ width: 0;
575
+ height: 0;
576
+ border-style: solid;
577
+ `;
578
+ portal.appendChild(arrow);
579
+ }
580
+
581
+ // Add to body
582
+ document.body.appendChild(portal);
583
+
584
+ return portal;
585
+ }
586
+
587
+ private updatePortalContent() {
588
+ if (!this.portalElement) return;
589
+
590
+ const content = this.portalElement.querySelector('.snice-tooltip__content');
591
+ if (content) {
592
+ content.textContent = this.content;
593
+ }
594
+
595
+ // Update or create arrow if needed
596
+ let arrow = this.portalElement.querySelector('.snice-tooltip__arrow') as HTMLElement;
597
+ if (this.arrow && !arrow) {
598
+ // Create arrow if it doesn't exist but should
599
+ arrow = document.createElement('div');
600
+ arrow.className = 'snice-tooltip__arrow';
601
+ arrow.style.cssText = `
602
+ position: absolute;
603
+ width: 0;
604
+ height: 0;
605
+ border-style: solid;
606
+ `;
607
+ this.portalElement.appendChild(arrow);
608
+ } else if (arrow) {
609
+ arrow.style.display = this.arrow ? '' : 'none';
610
+ }
611
+ }
612
+ }
@@ -0,0 +1,32 @@
1
+ export type TooltipPosition =
2
+ | 'top'
3
+ | 'bottom'
4
+ | 'left'
5
+ | 'right'
6
+ | 'top-start'
7
+ | 'top-end'
8
+ | 'bottom-start'
9
+ | 'bottom-end'
10
+ | 'left-start'
11
+ | 'left-end'
12
+ | 'right-start'
13
+ | 'right-end';
14
+
15
+ export type TooltipTrigger = 'hover' | 'click' | 'focus' | 'manual';
16
+
17
+ export interface SniceTooltipElement extends HTMLElement {
18
+ content: string;
19
+ position: TooltipPosition;
20
+ trigger: TooltipTrigger;
21
+ delay: number;
22
+ hideDelay: number;
23
+ offset: number;
24
+ arrow: boolean;
25
+ open: boolean;
26
+ maxWidth: number;
27
+ zIndex: number;
28
+ show(): void;
29
+ hide(): void;
30
+ toggle(): void;
31
+ updatePosition(): void;
32
+ }