tolltop 2.0.0 → 3.0.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/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # tolltop
2
2
 
3
- One-attribute CSS tooltips. No JavaScript required.
3
+ Tiny, dependency-free tooltips with smart edge-aware positioning. One attribute to add a
4
+ tooltip, one call to configure them all, no build step, and it works in every browser
5
+ released since mid-2023 (Chrome/Edge 114+, Firefox 114+, Safari 16.5+).
4
6
 
5
7
  ## Install
6
8
 
@@ -10,65 +12,65 @@ npm install tolltop
10
12
 
11
13
  ## Usage
12
14
 
13
- ### CDN (recommended)
14
-
15
15
  ```html
16
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tolltop/tolltop.css">
17
- ```
18
-
19
- ### CSS import
20
-
21
- ```css
22
- @import 'tolltop/tolltop.css';
23
- ```
24
-
25
- ### JS bundler
26
-
27
- ```js
28
- import 'tolltop'
29
- ```
16
+ <script src="https://cdn.jsdelivr.net/npm/tolltop/tolltop.js"></script>
30
17
 
31
- This auto-injects the stylesheet a convenience wrapper, not a runtime dependency.
32
-
33
- ### Then just add the attribute
34
-
35
- ```html
18
+ <!-- mark any element with the attribute -->
36
19
  <button data-tooltip="Save your work">Save</button>
37
20
  ```
38
21
 
39
- ## How it works
40
-
41
- The entire library is CSS:
42
-
43
- - `::after` pseudo-element with `content: attr(data-tooltip)`
44
- - CSS Anchor Positioning (`position-area: top` + `position-try-fallbacks: flip-block`) for viewport flipping
45
- - `light-dark()` for automatic color inversion based on `color-scheme`
46
- - `:hover` for show/hide (works as tap on mobile)
22
+ Tooltips show on hover and on keyboard focus, and hide on `Esc`. The script injects its own
23
+ styles, so loading `tolltop.css` is optional. Link it (before the script) only if you want
24
+ to edit the CSS directly:
47
25
 
48
- ## Customization
49
-
50
- Override with CSS variables (globally or per-element):
51
-
52
- ```css
53
- :root {
54
- --tt-bg: #1e3a5f;
55
- --tt-color: #fff;
56
- --tt-radius: 8px;
57
- --tt-font-size: 14px;
58
- --tt-line-height: 1.5;
59
- }
26
+ ```html
27
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tolltop/tolltop.css">
28
+ <script src="https://cdn.jsdelivr.net/npm/tolltop/tolltop.js"></script>
60
29
  ```
61
30
 
62
- ## Color inversion
31
+ ## Configuration
63
32
 
64
- Set `color-scheme` on your page or sections:
33
+ Style and behavior are global. Call `tolltop()` once with any subset of options (it merges
34
+ into the current config and applies immediately):
65
35
 
66
- ```css
67
- body { color-scheme: dark; }
68
- .light-section { color-scheme: light; }
36
+ ```js
37
+ tolltop({
38
+ bg: '#18181b',
39
+ color: '#e4e4e7',
40
+ radius: 6, // number = px, or any CSS length string
41
+ fontSize: 12, // same
42
+ padding: '6px 9px',
43
+ maxWidth: 240, // px
44
+ placement: 'auto', // 'auto' | 'top' | 'bottom'
45
+ gap: 8, // px between the trigger and the tooltip
46
+ edge: 24, // px min gap from each viewport side
47
+ });
69
48
  ```
70
49
 
71
- Tooltips auto-invert based on the nearest `color-scheme`.
50
+ | Option | Example | Default |
51
+ | ------------ | ---------------- | ---------- |
52
+ | `bg` | `'#1e3a5f'` | `#18181b` |
53
+ | `color` | `'#fff'` | `#e4e4e7` |
54
+ | `radius` | `10` or `'10px'` | `6` |
55
+ | `fontSize` | `14` or `'.9rem'`| `12` |
56
+ | `padding` | `'8px 12px'` | `6px 9px` |
57
+ | `maxWidth` | `320` | `240` |
58
+ | `placement` | `'top'` | `'auto'` |
59
+ | `gap` | `10` | `8` |
60
+ | `edge` | `16` | `24` |
61
+
62
+ Calling `tolltop()` with no arguments returns the current config. The only per-element
63
+ attribute is `data-tooltip="text"`, which marks an element and supplies its text.
64
+
65
+ ## Positioning
66
+
67
+ - Shows above the trigger by default.
68
+ - Flips below if there isn't room above. Set `placement` to force a side (it still flips if
69
+ the forced side won't fit).
70
+ - Centers horizontally on the trigger, then shifts left or right just enough to stay on
71
+ screen, leaving `edge` px of room on each side (the vertical scrollbar is excluded).
72
+ - Long text is capped at the available width and wraps instead of running off screen.
73
+ - The arrow stays centered on the trigger even after the box shifts, and points toward it.
72
74
 
73
75
  ## License
74
76
 
package/package.json CHANGED
@@ -1,32 +1,25 @@
1
1
  {
2
2
  "name": "tolltop",
3
- "version": "2.0.0",
4
- "description": "One-attribute CSS tooltips. CSS Anchor Positioning + light-dark().",
5
- "main": "index.js",
3
+ "version": "3.0.0",
4
+ "description": "Tiny dependency-free tooltips with smart edge-aware positioning. One attribute, no build, works in browsers back to mid-2023.",
5
+ "main": "tolltop.js",
6
6
  "style": "tolltop.css",
7
- "types": "index.d.ts",
8
7
  "files": [
9
- "index.js",
10
- "index.d.ts",
8
+ "tolltop.js",
11
9
  "tolltop.css"
12
10
  ],
13
- "scripts": {
14
- "build": "node build.js"
15
- },
16
11
  "keywords": [
17
12
  "tooltip",
18
- "css",
19
- "anchor-positioning",
20
- "light-dark",
13
+ "tooltips",
14
+ "vanilla",
15
+ "no-dependencies",
16
+ "positioning",
21
17
  "lightweight"
22
18
  ],
23
19
  "author": "David Miranda",
24
20
  "license": "MIT",
25
21
  "repository": {
26
22
  "type": "git",
27
- "url": "https://github.com/davidmiranda/tolltop"
28
- },
29
- "devDependencies": {
30
- "esbuild": "^0.27.4"
23
+ "url": "git+https://github.com/panphora/tolltop.git"
31
24
  }
32
25
  }
package/tolltop.css CHANGED
@@ -1,31 +1,60 @@
1
- [data-tooltip]:hover { anchor-name: --tt; }
1
+ .tolltop {
2
+ --tt-bg: #18181b;
3
+ --tt-color: #e4e4e7;
4
+ --tt-radius: 6px;
5
+ --tt-font-size: 12px;
6
+ --tt-padding: 6px 9px;
7
+ --tt-arrow: 6px;
8
+ --tt-arrow-x: 50%;
2
9
 
3
- [data-tooltip]::after {
4
- content: attr(data-tooltip);
5
10
  position: fixed;
6
- position-anchor: --tt;
7
- position-area: top;
8
- position-try-fallbacks: flip-block;
9
- inset: auto;
10
- margin: 0 0 8px;
11
- padding: 5px 9px;
12
- font-size: var(--tt-font-size, 12px);
13
- font-weight: 500;
14
- line-height: var(--tt-line-height, 1.4);
15
- font-family: ui-monospace, monospace;
16
- border-radius: var(--tt-radius, 5px);
17
- max-width: 240px;
11
+ top: 0;
12
+ left: 0;
13
+ z-index: 2147483647;
14
+ box-sizing: border-box;
15
+ margin: 0;
18
16
  width: max-content;
17
+ max-width: 240px;
18
+ padding: var(--tt-padding);
19
+ border-radius: var(--tt-radius);
20
+ background: var(--tt-bg);
21
+ color: var(--tt-color);
22
+ font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
23
+ font-size: var(--tt-font-size);
24
+ font-weight: 500;
25
+ line-height: 1.4;
19
26
  text-align: center;
27
+ white-space: normal;
28
+ overflow-wrap: break-word;
20
29
  pointer-events: none;
21
- z-index: 2147483647;
22
- background: var(--tt-bg, light-dark(#18181b, #fafafa));
23
- color: var(--tt-color, light-dark(#e4e4e7, #18181b));
24
- box-shadow:
25
- 0 2px 10px light-dark(rgba(0,0,0,.45), rgba(0,0,0,.12)),
26
- 0 0 0 1px light-dark(rgba(255,255,255,.06), rgba(0,0,0,.06));
30
+ box-shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
27
31
  opacity: 0;
28
- transition: opacity .1s ease;
32
+ visibility: hidden;
29
33
  }
30
34
 
31
- [data-tooltip]:hover::after { opacity: 1; }
35
+ .tolltop[data-show] {
36
+ opacity: 1;
37
+ visibility: visible;
38
+ }
39
+
40
+ .tolltop::after {
41
+ content: "";
42
+ position: absolute;
43
+ left: var(--tt-arrow-x);
44
+ transform: translateX(-50%);
45
+ width: 0;
46
+ height: 0;
47
+ border: var(--tt-arrow) solid transparent;
48
+ }
49
+
50
+ .tolltop[data-placement="top"]::after {
51
+ top: 100%;
52
+ border-bottom-width: 0;
53
+ border-top-color: var(--tt-bg);
54
+ }
55
+
56
+ .tolltop[data-placement="bottom"]::after {
57
+ bottom: 100%;
58
+ border-top-width: 0;
59
+ border-bottom-color: var(--tt-bg);
60
+ }
package/tolltop.js ADDED
@@ -0,0 +1,188 @@
1
+ (() => {
2
+ 'use strict';
3
+ if (window.__tolltop) return;
4
+ window.__tolltop = true;
5
+
6
+ const VEDGE = 8;
7
+ const TIP_ID = 'tolltop-tip';
8
+
9
+ // Baseline styles, injected only if tolltop.css isn't already on the page.
10
+ // Keep in sync with tolltop.css.
11
+ const CSS = `.tolltop{--tt-bg:#18181b;--tt-color:#e4e4e7;--tt-radius:6px;--tt-font-size:12px;--tt-padding:6px 9px;--tt-arrow:6px;--tt-arrow-x:50%;position:fixed;top:0;left:0;z-index:2147483647;box-sizing:border-box;margin:0;width:max-content;max-width:240px;padding:var(--tt-padding);border-radius:var(--tt-radius);background:var(--tt-bg);color:var(--tt-color);font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:var(--tt-font-size);font-weight:500;line-height:1.4;text-align:center;white-space:normal;overflow-wrap:break-word;pointer-events:none;box-shadow:0 2px 12px rgba(0,0,0,.3);opacity:0;visibility:hidden}.tolltop[data-show]{opacity:1;visibility:visible}.tolltop::after{content:"";position:absolute;left:var(--tt-arrow-x);transform:translateX(-50%);width:0;height:0;border:var(--tt-arrow) solid transparent}.tolltop[data-placement="top"]::after{top:100%;border-bottom-width:0;border-top-color:var(--tt-bg)}.tolltop[data-placement="bottom"]::after{bottom:100%;border-top-width:0;border-bottom-color:var(--tt-bg)}`;
12
+
13
+ const cfg = {
14
+ bg: null,
15
+ color: null,
16
+ radius: null,
17
+ fontSize: null,
18
+ padding: null,
19
+ maxWidth: 240,
20
+ placement: 'auto',
21
+ gap: 8,
22
+ edge: 24,
23
+ };
24
+
25
+ let tip = null;
26
+ let active = null;
27
+ let activePrevAria = null;
28
+
29
+ const clamp = (n, lo, hi) => Math.min(Math.max(n, lo), hi);
30
+ const isNumStr = (v) => /^-?\d*\.?\d+$/.test(String(v).trim());
31
+ const toLen = (v) =>
32
+ v == null || v === '' ? null : typeof v === 'number' ? v + 'px' : isNumStr(v) ? v.trim() + 'px' : v.trim();
33
+ const numOr = (v, fallback) => (isFinite(Number(v)) ? Number(v) : fallback);
34
+
35
+ function setVar(name, value) {
36
+ if (value == null || value === '') tip.style.removeProperty(name);
37
+ else tip.style.setProperty(name, value);
38
+ }
39
+
40
+ function applyConfig() {
41
+ if (!tip) return;
42
+ setVar('--tt-bg', cfg.bg);
43
+ setVar('--tt-color', cfg.color);
44
+ setVar('--tt-radius', toLen(cfg.radius));
45
+ setVar('--tt-font-size', toLen(cfg.fontSize));
46
+ setVar('--tt-padding', cfg.padding);
47
+ }
48
+
49
+ function ensureTip() {
50
+ if (tip) return tip;
51
+ tip = document.createElement('div');
52
+ tip.className = 'tolltop';
53
+ tip.id = TIP_ID;
54
+ tip.setAttribute('role', 'tooltip');
55
+ document.body.appendChild(tip);
56
+ if (getComputedStyle(tip).position !== 'fixed') {
57
+ const style = document.createElement('style');
58
+ style.setAttribute('data-tolltop', '');
59
+ style.textContent = CSS;
60
+ document.head.insertBefore(style, document.head.firstChild);
61
+ }
62
+ applyConfig();
63
+ return tip;
64
+ }
65
+
66
+ function position(el) {
67
+ const rect = el.getBoundingClientRect();
68
+ // clientWidth/Height exclude the scrollbar; innerWidth would not.
69
+ const vw = document.documentElement.clientWidth;
70
+ const vh = document.documentElement.clientHeight;
71
+ const gap = Math.max(0, numOr(cfg.gap, 8));
72
+ const edge = Math.max(0, numOr(cfg.edge, 24));
73
+
74
+ const maxW = Math.min(numOr(cfg.maxWidth, 240), vw - edge * 2);
75
+ tip.style.maxWidth = maxW + 'px';
76
+
77
+ const t = tip.getBoundingClientRect();
78
+
79
+ const pref = cfg.placement;
80
+ const topY = rect.top - t.height - gap;
81
+ const botY = rect.bottom + gap;
82
+ const topFits = topY >= VEDGE;
83
+ const botFits = botY + t.height <= vh - VEDGE;
84
+ let placement;
85
+ if (pref === 'bottom') placement = botFits || !topFits ? 'bottom' : 'top';
86
+ else if (pref === 'top') placement = topFits || !botFits ? 'top' : 'bottom';
87
+ else placement = topFits ? 'top' : 'bottom';
88
+ // When the tip is taller than the viewport, pin to the top edge so its top stays visible.
89
+ const y =
90
+ t.height >= vh - VEDGE * 2 ? VEDGE : clamp(placement === 'top' ? topY : botY, VEDGE, vh - t.height - VEDGE);
91
+
92
+ const centerX = rect.left + rect.width / 2;
93
+ const x = clamp(centerX - t.width / 2, edge, vw - t.width - edge);
94
+
95
+ const radius = parseFloat(getComputedStyle(tip).borderTopLeftRadius) || 6;
96
+ const arrow = parseFloat(getComputedStyle(tip).getPropertyValue('--tt-arrow')) || 6;
97
+ const arrowX = clamp(centerX - x, radius + arrow, t.width - radius - arrow);
98
+
99
+ tip.style.left = x + 'px';
100
+ tip.style.top = y + 'px';
101
+ tip.style.setProperty('--tt-arrow-x', arrowX + 'px');
102
+ tip.setAttribute('data-placement', placement);
103
+ tip.setAttribute('data-show', '');
104
+ }
105
+
106
+ function restoreAria() {
107
+ if (!active) return;
108
+ if (activePrevAria == null) active.removeAttribute('aria-describedby');
109
+ else active.setAttribute('aria-describedby', activePrevAria);
110
+ activePrevAria = null;
111
+ }
112
+
113
+ function show(el) {
114
+ const text = el.getAttribute('data-tooltip');
115
+ if (!text) return;
116
+ ensureTip();
117
+ if (active !== el) {
118
+ if (active) restoreAria();
119
+ active = el;
120
+ activePrevAria = el.getAttribute('aria-describedby');
121
+ const ids = activePrevAria ? activePrevAria.split(/\s+/) : [];
122
+ if (ids.indexOf(TIP_ID) === -1) ids.push(TIP_ID);
123
+ el.setAttribute('aria-describedby', ids.join(' '));
124
+ }
125
+ tip.textContent = text;
126
+ position(el);
127
+ }
128
+
129
+ function hide() {
130
+ if (!active) return;
131
+ restoreAria();
132
+ active = null;
133
+ if (tip) tip.removeAttribute('data-show');
134
+ }
135
+
136
+ function reposition() {
137
+ if (!active) return;
138
+ // Hide if the trigger has been removed or scrolled out of view (a fixed tip
139
+ // isn't clipped by an ancestor's overflow, so it would otherwise float free).
140
+ if (!active.isConnected) {
141
+ hide();
142
+ return;
143
+ }
144
+ const r = active.getBoundingClientRect();
145
+ const vw = document.documentElement.clientWidth;
146
+ const vh = document.documentElement.clientHeight;
147
+ if (r.bottom < 0 || r.top > vh || r.right < 0 || r.left > vw) {
148
+ hide();
149
+ return;
150
+ }
151
+ position(active);
152
+ }
153
+
154
+ function trigger(node) {
155
+ return node && node.closest ? node.closest('[data-tooltip]') : null;
156
+ }
157
+
158
+ document.addEventListener('pointerover', (e) => {
159
+ const el = trigger(e.target);
160
+ if (el && el !== active) show(el);
161
+ });
162
+ document.addEventListener('pointerout', (e) => {
163
+ if (active && (!e.relatedTarget || !active.contains(e.relatedTarget))) hide();
164
+ });
165
+ document.addEventListener('focusin', (e) => {
166
+ const el = trigger(e.target);
167
+ if (el) show(el);
168
+ });
169
+ document.addEventListener('focusout', hide);
170
+ document.addEventListener('keydown', (e) => {
171
+ if (e.key === 'Escape') hide();
172
+ });
173
+ window.addEventListener('scroll', reposition, true);
174
+ window.addEventListener('resize', reposition);
175
+
176
+ window.tolltop = function (opts) {
177
+ if (opts && typeof opts === 'object') {
178
+ for (const k in opts) {
179
+ if (Object.prototype.hasOwnProperty.call(cfg, k)) cfg[k] = opts[k];
180
+ }
181
+ applyConfig();
182
+ if (active) position(active);
183
+ }
184
+ const out = {};
185
+ for (const k in cfg) out[k] = cfg[k];
186
+ return out;
187
+ };
188
+ })();
package/index.d.ts DELETED
@@ -1 +0,0 @@
1
- declare module 'tolltop' {}
package/index.js DELETED
@@ -1 +0,0 @@
1
- (()=>{(()=>{let t=document.createElement("style");t.textContent="[data-tooltip]:hover{anchor-name:--tt}[data-tooltip]:after{content:attr(data-tooltip);position:fixed;position-anchor:--tt;position-area:top;position-try-fallbacks:flip-block;inset:auto;margin:0 0 8px;padding:5px 9px;font-size:var(--tt-font-size, 12px);font-weight:500;line-height:var(--tt-line-height, 1.4);font-family:ui-monospace,monospace;border-radius:var(--tt-radius, 5px);max-width:240px;width:max-content;text-align:center;pointer-events:none;z-index:2147483647;background:var(--tt-bg, light-dark(#18181b, #fafafa));color:var(--tt-color, light-dark(#e4e4e7, #18181b));box-shadow:0 2px 10px light-dark(rgba(0,0,0,.45),rgba(0,0,0,.12)),0 0 0 1px light-dark(rgba(255,255,255,.06),rgba(0,0,0,.06));opacity:0;transition:opacity .1s ease}[data-tooltip]:hover:after{opacity:1}",document.head.appendChild(t)})();})();