@data-slot/tooltip 0.2.3 → 0.2.5
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 +127 -51
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +28 -8
- package/dist/index.d.ts +28 -8
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @data-slot/tooltip
|
|
|
13
13
|
```html
|
|
14
14
|
<div data-slot="tooltip">
|
|
15
15
|
<button data-slot="tooltip-trigger">Hover me</button>
|
|
16
|
-
<div data-slot="tooltip-content"
|
|
16
|
+
<div data-slot="tooltip-content" hidden>
|
|
17
17
|
Helpful tooltip text
|
|
18
18
|
</div>
|
|
19
19
|
</div>
|
|
@@ -47,7 +47,8 @@ import { createTooltip } from "@data-slot/tooltip";
|
|
|
47
47
|
const tooltip = createTooltip(element, {
|
|
48
48
|
delay: 300,
|
|
49
49
|
skipDelayDuration: 300,
|
|
50
|
-
|
|
50
|
+
side: "top",
|
|
51
|
+
align: "center",
|
|
51
52
|
onOpenChange: (open) => console.log(open),
|
|
52
53
|
});
|
|
53
54
|
```
|
|
@@ -57,48 +58,83 @@ const tooltip = createTooltip(element, {
|
|
|
57
58
|
| Option | Type | Default | Description |
|
|
58
59
|
|--------|------|---------|-------------|
|
|
59
60
|
| `delay` | `number` | `300` | Delay before showing tooltip (ms) |
|
|
60
|
-
| `skipDelayDuration` | `number` | `300` | Duration to skip delay after closing (ms). Set to `0` to disable. |
|
|
61
|
-
| `
|
|
61
|
+
| `skipDelayDuration` | `number` | `300` | Duration to skip delay after closing (ms). Set to `0` to disable warm-up. |
|
|
62
|
+
| `side` | `"top" \| "right" \| "bottom" \| "left"` | `"top"` | Side of tooltip relative to trigger (bind-time only) |
|
|
63
|
+
| `align` | `"start" \| "center" \| "end"` | `"center"` | Alignment along the side (bind-time only) |
|
|
62
64
|
| `onOpenChange` | `(open: boolean) => void` | `undefined` | Callback when visibility changes |
|
|
63
65
|
|
|
66
|
+
**Note:** `side` and `align` are resolved once at bind time. To change placement dynamically, destroy and recreate the tooltip with new options.
|
|
67
|
+
|
|
68
|
+
### Data Attributes
|
|
69
|
+
|
|
70
|
+
Options can also be set via data attributes. JS options take precedence.
|
|
71
|
+
|
|
72
|
+
| Attribute | Type | Default | Description |
|
|
73
|
+
|-----------|------|---------|-------------|
|
|
74
|
+
| `data-delay` | number | `300` | Delay before showing tooltip (ms) |
|
|
75
|
+
| `data-skip-delay-duration` | number | `300` | Duration to skip delay after closing (ms) |
|
|
76
|
+
| `data-side` | string | `"top"` | Side relative to trigger (checked on content first, then root) |
|
|
77
|
+
| `data-align` | string | `"center"` | Alignment along the side (checked on content first, then root) |
|
|
78
|
+
|
|
79
|
+
```html
|
|
80
|
+
<!-- Tooltip with faster response -->
|
|
81
|
+
<div data-slot="tooltip" data-delay="100">
|
|
82
|
+
...
|
|
83
|
+
</div>
|
|
84
|
+
|
|
85
|
+
<!-- Disable warm-up behavior -->
|
|
86
|
+
<div data-slot="tooltip" data-skip-delay-duration="0">
|
|
87
|
+
...
|
|
88
|
+
</div>
|
|
89
|
+
|
|
90
|
+
<!-- Side/align on content element -->
|
|
91
|
+
<div data-slot="tooltip-content" data-side="bottom" data-align="start">
|
|
92
|
+
...
|
|
93
|
+
</div>
|
|
94
|
+
```
|
|
95
|
+
|
|
64
96
|
### Controller
|
|
65
97
|
|
|
66
98
|
| Method/Property | Description |
|
|
67
99
|
|-----------------|-------------|
|
|
68
|
-
| `show()` | Show the tooltip immediately |
|
|
100
|
+
| `show()` | Show the tooltip immediately. Respects disabled state. |
|
|
69
101
|
| `hide()` | Hide the tooltip |
|
|
70
102
|
| `isOpen` | Current visibility state (readonly `boolean`) |
|
|
71
|
-
| `destroy()` | Cleanup all event listeners |
|
|
103
|
+
| `destroy()` | Cleanup all event listeners and timers |
|
|
72
104
|
|
|
73
105
|
## Markup Structure
|
|
74
106
|
|
|
75
107
|
```html
|
|
76
108
|
<div data-slot="tooltip">
|
|
77
109
|
<button data-slot="tooltip-trigger">Trigger</button>
|
|
78
|
-
<div data-slot="tooltip-content"
|
|
110
|
+
<div data-slot="tooltip-content">Content</div>
|
|
79
111
|
</div>
|
|
80
112
|
```
|
|
81
113
|
|
|
82
114
|
Both `tooltip-trigger` and `tooltip-content` are required.
|
|
83
115
|
|
|
84
|
-
###
|
|
116
|
+
### Output Attributes
|
|
85
117
|
|
|
86
|
-
|
|
118
|
+
The component sets these attributes automatically:
|
|
87
119
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
120
|
+
| Element | Attribute | Values |
|
|
121
|
+
|---------|-----------|--------|
|
|
122
|
+
| Root | `data-state` | `"open"` \| `"closed"` |
|
|
123
|
+
| Content | `data-side` | `"top"` \| `"right"` \| `"bottom"` \| `"left"` |
|
|
124
|
+
| Content | `data-align` | `"start"` \| `"center"` \| `"end"` |
|
|
125
|
+
| Content | `role` | `"tooltip"` |
|
|
126
|
+
| Content | `aria-hidden` | `"true"` when closed, `"false"` when open |
|
|
127
|
+
| Trigger | `aria-describedby` | Content ID when open, removed when closed |
|
|
91
128
|
|
|
92
129
|
## Styling
|
|
93
130
|
|
|
94
|
-
|
|
131
|
+
This tooltip uses simple CSS positioning and is not collision-aware. Visibility is controlled entirely via CSS using the `data-state` attribute.
|
|
95
132
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
display: none;
|
|
100
|
-
}
|
|
133
|
+
### Recommended CSS
|
|
134
|
+
|
|
135
|
+
The visibility transition trick keeps the tooltip visible during fade-out, then becomes non-focusable after the transition completes—no JS timers needed.
|
|
101
136
|
|
|
137
|
+
```css
|
|
102
138
|
/* Positioning */
|
|
103
139
|
[data-slot="tooltip"] {
|
|
104
140
|
position: relative;
|
|
@@ -107,67 +143,83 @@ Set position via HTML:
|
|
|
107
143
|
[data-slot="tooltip-content"] {
|
|
108
144
|
position: absolute;
|
|
109
145
|
white-space: nowrap;
|
|
146
|
+
/* Hidden by default */
|
|
147
|
+
opacity: 0;
|
|
148
|
+
visibility: hidden;
|
|
149
|
+
pointer-events: none;
|
|
150
|
+
/* Visibility delays hiding until after opacity fades */
|
|
151
|
+
transition: opacity 0.15s, visibility 0s linear 0.15s;
|
|
110
152
|
}
|
|
111
153
|
|
|
112
|
-
|
|
154
|
+
/* Open state */
|
|
155
|
+
[data-slot="tooltip"][data-state="open"] [data-slot="tooltip-content"] {
|
|
156
|
+
opacity: 1;
|
|
157
|
+
visibility: visible;
|
|
158
|
+
pointer-events: auto;
|
|
159
|
+
transition-delay: 0s;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/* Side positioning */
|
|
163
|
+
[data-slot="tooltip-content"][data-side="top"] {
|
|
113
164
|
bottom: 100%;
|
|
114
165
|
left: 50%;
|
|
115
166
|
transform: translateX(-50%);
|
|
116
167
|
margin-bottom: 8px;
|
|
117
168
|
}
|
|
118
169
|
|
|
119
|
-
[data-slot="tooltip-content"][data-
|
|
170
|
+
[data-slot="tooltip-content"][data-side="bottom"] {
|
|
120
171
|
top: 100%;
|
|
121
172
|
left: 50%;
|
|
122
173
|
transform: translateX(-50%);
|
|
123
174
|
margin-top: 8px;
|
|
124
175
|
}
|
|
125
176
|
|
|
126
|
-
[data-slot="tooltip-content"][data-
|
|
177
|
+
[data-slot="tooltip-content"][data-side="left"] {
|
|
127
178
|
right: 100%;
|
|
128
179
|
top: 50%;
|
|
129
180
|
transform: translateY(-50%);
|
|
130
181
|
margin-right: 8px;
|
|
131
182
|
}
|
|
132
183
|
|
|
133
|
-
[data-slot="tooltip-content"][data-
|
|
184
|
+
[data-slot="tooltip-content"][data-side="right"] {
|
|
134
185
|
left: 100%;
|
|
135
186
|
top: 50%;
|
|
136
187
|
transform: translateY(-50%);
|
|
137
188
|
margin-left: 8px;
|
|
138
189
|
}
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### Animations
|
|
142
190
|
|
|
143
|
-
|
|
144
|
-
[data-slot="tooltip-content"]
|
|
145
|
-
|
|
146
|
-
|
|
191
|
+
/* Alignment (example for top/bottom sides) */
|
|
192
|
+
[data-slot="tooltip-content"][data-side="top"][data-align="start"],
|
|
193
|
+
[data-slot="tooltip-content"][data-side="bottom"][data-align="start"] {
|
|
194
|
+
left: 0;
|
|
195
|
+
transform: none;
|
|
147
196
|
}
|
|
148
197
|
|
|
149
|
-
[data-slot="tooltip"][data-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
[data-slot="tooltip-content"][data-instant] {
|
|
155
|
-
transition: none;
|
|
198
|
+
[data-slot="tooltip-content"][data-side="top"][data-align="end"],
|
|
199
|
+
[data-slot="tooltip-content"][data-side="bottom"][data-align="end"] {
|
|
200
|
+
left: auto;
|
|
201
|
+
right: 0;
|
|
202
|
+
transform: none;
|
|
156
203
|
}
|
|
157
204
|
```
|
|
158
205
|
|
|
159
206
|
### Tailwind Example
|
|
160
207
|
|
|
208
|
+
Use `group` on the root and `group-data-[state=open]:` for open state styles:
|
|
209
|
+
|
|
161
210
|
```html
|
|
162
|
-
<div data-slot="tooltip" class="relative inline-block
|
|
211
|
+
<div data-slot="tooltip" class="group relative inline-block">
|
|
163
212
|
<button data-slot="tooltip-trigger">
|
|
164
213
|
Hover me
|
|
165
214
|
</button>
|
|
166
215
|
<div
|
|
167
216
|
data-slot="tooltip-content"
|
|
168
|
-
data-
|
|
169
|
-
|
|
170
|
-
|
|
217
|
+
data-side="top"
|
|
218
|
+
class="absolute bottom-full left-1/2 -translate-x-1/2 mb-2 px-2 py-1
|
|
219
|
+
bg-gray-900 text-white text-sm rounded
|
|
220
|
+
opacity-0 pointer-events-none transition-opacity duration-150
|
|
221
|
+
group-data-[state=open]:opacity-100
|
|
222
|
+
group-data-[state=open]:pointer-events-auto"
|
|
171
223
|
>
|
|
172
224
|
Tooltip text
|
|
173
225
|
</div>
|
|
@@ -180,25 +232,40 @@ When a user closes one tooltip and quickly hovers another, the second tooltip sh
|
|
|
180
232
|
|
|
181
233
|
- Controlled by `skipDelayDuration` option
|
|
182
234
|
- Set to `0` to disable this behavior
|
|
183
|
-
-
|
|
235
|
+
- Warm-up only skips the delay, not CSS transitions
|
|
236
|
+
- Warm window is set only when a tooltip actually closes (not when a pending open is cancelled)
|
|
184
237
|
|
|
185
238
|
## Accessibility
|
|
186
239
|
|
|
187
240
|
The component automatically handles:
|
|
188
241
|
|
|
189
242
|
- `role="tooltip"` on content
|
|
190
|
-
- `aria-describedby`
|
|
191
|
-
-
|
|
243
|
+
- `aria-describedby` on trigger only when open (prevents stale announcements)
|
|
244
|
+
- `aria-hidden` on content: explicit `"true"`/`"false"` for consistent AT behavior
|
|
245
|
+
- Unique ID generation for content via `ensureId`
|
|
246
|
+
|
|
247
|
+
### Disabled Triggers
|
|
248
|
+
|
|
249
|
+
If the trigger has `disabled` attribute or `aria-disabled="true"`:
|
|
250
|
+
- Pointer and focus events will not open the tooltip
|
|
251
|
+
- Programmatic `.show()` also respects the disabled state
|
|
192
252
|
|
|
193
|
-
##
|
|
253
|
+
## Interaction Model
|
|
194
254
|
|
|
195
|
-
|
|
|
196
|
-
|
|
197
|
-
|
|
|
198
|
-
|
|
|
255
|
+
| Input | Behavior |
|
|
256
|
+
|-------|----------|
|
|
257
|
+
| Pointer enter trigger (mouse/pen) | Show after delay |
|
|
258
|
+
| Pointer leave trigger | Hide immediately (unless entering content) |
|
|
259
|
+
| Pointer enter content | Keep open (hoverable content) |
|
|
260
|
+
| Pointer leave content | Hide immediately (unless entering trigger) |
|
|
261
|
+
| Touch hover | Ignored (focus-only on touch devices) |
|
|
199
262
|
| Focus | Show after delay |
|
|
200
263
|
| Blur | Hide immediately |
|
|
201
|
-
| `Escape` | Hide immediately |
|
|
264
|
+
| `Escape` | Hide immediately (listener only active when open) |
|
|
265
|
+
|
|
266
|
+
**Hoverable Content:** Moving the pointer from trigger to content (or vice versa) keeps the tooltip open. This allows users to interact with links or selectable text in tooltips.
|
|
267
|
+
|
|
268
|
+
**Focus Priority:** While the trigger has keyboard focus, the tooltip stays open even if the pointer leaves. This prevents jarring closures during keyboard navigation.
|
|
202
269
|
|
|
203
270
|
## Events
|
|
204
271
|
|
|
@@ -206,11 +273,20 @@ Listen for changes via custom events:
|
|
|
206
273
|
|
|
207
274
|
```javascript
|
|
208
275
|
element.addEventListener("tooltip:change", (e) => {
|
|
209
|
-
|
|
276
|
+
const { open, trigger, content, reason } = e.detail;
|
|
277
|
+
console.log(`Tooltip ${open ? 'opened' : 'closed'} via ${reason}`);
|
|
210
278
|
});
|
|
211
279
|
```
|
|
212
280
|
|
|
281
|
+
### Event Detail
|
|
282
|
+
|
|
283
|
+
| Property | Type | Description |
|
|
284
|
+
|----------|------|-------------|
|
|
285
|
+
| `open` | `boolean` | Current visibility state |
|
|
286
|
+
| `trigger` | `HTMLElement` | The trigger element |
|
|
287
|
+
| `content` | `HTMLElement` | The content element |
|
|
288
|
+
| `reason` | `string` | What caused the change: `"pointer"`, `"focus"`, `"blur"`, `"escape"`, `"api"` |
|
|
289
|
+
|
|
213
290
|
## License
|
|
214
291
|
|
|
215
292
|
MIT
|
|
216
|
-
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,t)=>e.querySelector(`[data-slot="${t}"]`),t=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)];let n=0;const
|
|
1
|
+
const e=(e,t)=>e.querySelector(`[data-slot="${t}"]`),t=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)],n=new WeakMap;function r(e,t,r){if(typeof process<`u`&&process.env?.NODE_ENV===`production`)return;let i=n.get(e);i||(i=new Set,n.set(e,i)),!i.has(t)&&(i.add(t),console.warn(`[@data-slot] ${r}`))}function i(e){let t=`data-${e.replace(/([A-Z])/g,`-$1`).toLowerCase()}`,n=`data-${e}`;return t===n?[t]:[t,n]}function a(e,t){for(let n of i(t))if(e.hasAttribute(n))return e.getAttribute(n);return null}function o(e,t){let n=a(e,t);if(n===null||n===``)return;let i=Number(n);if(Number.isNaN(i)||!Number.isFinite(i)){r(e,t,`Invalid number value "${n}" for data-${t}.`);return}return i}function s(e,t,n){let i=a(e,t);if(i!==null){if(n.includes(i))return i;r(e,t,`Invalid value "${i}" for data-${t}. Expected one of: ${n.join(`, `)}.`)}}let c=0;const l=(e,t)=>e.id||=`${t}-${++c}`;function u(e,t,n,r){return e.addEventListener(t,n,r),()=>e.removeEventListener(t,n,r)}const d=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));let f=0;const p=[`top`,`right`,`bottom`,`left`],m=[`start`,`center`,`end`];function h(t,n={}){let r=e(t,`tooltip-trigger`),i=e(t,`tooltip-content`);if(!r||!i)throw Error(`Tooltip requires trigger and content slots`);let a=n.delay??o(t,`delay`)??300,c=n.skipDelayDuration??o(t,`skipDelayDuration`)??300,h=n.onOpenChange,g=n.side??s(i,`side`,p)??s(t,`side`,p)??`top`,_=n.align??s(i,`align`,m)??s(t,`align`,m)??`center`,v=!1,y=!1,b=null,x=null,S=[],C=l(i,`tooltip-content`);i.setAttribute(`role`,`tooltip`),i.setAttribute(`data-side`,g),i.setAttribute(`data-align`,_);let w=()=>r.hasAttribute(`disabled`)||r.getAttribute(`aria-disabled`)===`true`,T=()=>{x||=u(document,`keydown`,e=>{e.key===`Escape`&&v&&k(`escape`)})},E=()=>{x?.(),x=null},D=(e,n)=>{v!==e&&(v=e,t.setAttribute(`data-state`,v?`open`:`closed`),v?(r.setAttribute(`aria-describedby`,C),i.setAttribute(`aria-hidden`,`false`),T()):(r.removeAttribute(`aria-describedby`),i.setAttribute(`aria-hidden`,`true`),E()),d(t,`tooltip:change`,{open:v,trigger:r,content:i,reason:n}),h?.(v))},O=e=>{if(b&&(clearTimeout(b),b=null),Date.now()<f){D(!0,e);return}b=setTimeout(()=>{D(!0,e),b=null},a)},k=e=>{b&&(clearTimeout(b),b=null),v&&c>0&&(f=Date.now()+c),D(!1,e)};i.setAttribute(`aria-hidden`,`true`),t.setAttribute(`data-state`,`closed`),S.push(u(r,`pointerenter`,e=>{e.pointerType!==`touch`&&(w()||O(`pointer`))}),u(r,`pointerleave`,e=>{if(e.pointerType===`touch`||y)return;let t=e.relatedTarget;t&&i.contains(t)||k(`pointer`)}),u(r,`focus`,()=>{y=!0,!w()&&O(`focus`)}),u(r,`blur`,()=>{y=!1,k(`blur`)})),S.push(u(i,`pointerleave`,e=>{if(e.pointerType===`touch`||y)return;let t=e.relatedTarget;t&&r.contains(t)||k(`pointer`)}));let A={show:()=>{w()||(b&&(clearTimeout(b),b=null),D(!0,`api`))},hide:()=>k(`api`),get isOpen(){return v},destroy:()=>{b&&clearTimeout(b),E(),S.forEach(e=>e()),S.length=0}};return A}const g=new WeakSet;function _(e=document){let n=[];for(let r of t(e,`tooltip`)){if(g.has(r))continue;g.add(r),n.push(h(r))}return n}exports.create=_,exports.createTooltip=h;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
//#region src/index.d.ts
|
|
2
|
-
type
|
|
2
|
+
type TooltipSide = "top" | "right" | "bottom" | "left";
|
|
3
|
+
type TooltipAlign = "start" | "center" | "end";
|
|
4
|
+
type TooltipReason = "pointer" | "focus" | "blur" | "escape" | "api";
|
|
3
5
|
interface TooltipOptions {
|
|
4
|
-
/** Delay before showing tooltip (ms) */
|
|
6
|
+
/** Delay before showing tooltip (ms). Default: 300 */
|
|
5
7
|
delay?: number;
|
|
6
|
-
/** Duration to skip delay after closing (ms). Set to 0 to disable warm-up
|
|
8
|
+
/** Duration to skip delay after closing (ms). Set to 0 to disable warm-up. Default: 300 */
|
|
7
9
|
skipDelayDuration?: number;
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
+
/** Side of tooltip relative to trigger. Default: 'top'. Set at bind time only. */
|
|
11
|
+
side?: TooltipSide;
|
|
12
|
+
/** Alignment along the side. Default: 'center'. Set at bind time only. */
|
|
13
|
+
align?: TooltipAlign;
|
|
10
14
|
/** Callback when visibility changes */
|
|
11
15
|
onOpenChange?: (open: boolean) => void;
|
|
12
16
|
}
|
|
13
17
|
interface TooltipController {
|
|
14
|
-
/** Show the tooltip */
|
|
18
|
+
/** Show the tooltip immediately. Respects disabled state. */
|
|
15
19
|
show(): void;
|
|
16
20
|
/** Hide the tooltip */
|
|
17
21
|
hide(): void;
|
|
@@ -27,9 +31,25 @@ interface TooltipController {
|
|
|
27
31
|
* ```html
|
|
28
32
|
* <div data-slot="tooltip">
|
|
29
33
|
* <button data-slot="tooltip-trigger">Hover me</button>
|
|
30
|
-
* <div data-slot="tooltip-content"
|
|
34
|
+
* <div data-slot="tooltip-content" data-side="top" data-align="center">
|
|
35
|
+
* Tooltip text
|
|
36
|
+
* </div>
|
|
31
37
|
* </div>
|
|
32
38
|
* ```
|
|
39
|
+
*
|
|
40
|
+
* Data attributes (checked on content first, then root):
|
|
41
|
+
* - `data-side`: 'top' | 'right' | 'bottom' | 'left' (bind-time only)
|
|
42
|
+
* - `data-align`: 'start' | 'center' | 'end' (bind-time only)
|
|
43
|
+
* - `data-delay`: number (ms)
|
|
44
|
+
* - `data-skip-delay-duration`: number (ms)
|
|
45
|
+
*
|
|
46
|
+
* Note: side and align are resolved once at bind time. To change placement,
|
|
47
|
+
* destroy and recreate the tooltip with new options.
|
|
48
|
+
*
|
|
49
|
+
* This tooltip uses simple CSS positioning (not collision-aware).
|
|
50
|
+
* Opens on hover (non-touch) and focus. Touch devices: focus-only.
|
|
51
|
+
* Content is hoverable: moving pointer from trigger to content keeps it open.
|
|
52
|
+
* Tooltip stays open while trigger has focus, even if pointer leaves.
|
|
33
53
|
*/
|
|
34
54
|
declare function createTooltip(root: Element, options?: TooltipOptions): TooltipController;
|
|
35
55
|
/**
|
|
@@ -38,4 +58,4 @@ declare function createTooltip(root: Element, options?: TooltipOptions): Tooltip
|
|
|
38
58
|
*/
|
|
39
59
|
declare function create(scope?: ParentNode): TooltipController[];
|
|
40
60
|
//#endregion
|
|
41
|
-
export { TooltipController, TooltipOptions,
|
|
61
|
+
export { TooltipAlign, TooltipController, TooltipOptions, TooltipReason, TooltipSide, create, createTooltip };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
//#region src/index.d.ts
|
|
2
|
-
type
|
|
2
|
+
type TooltipSide = "top" | "right" | "bottom" | "left";
|
|
3
|
+
type TooltipAlign = "start" | "center" | "end";
|
|
4
|
+
type TooltipReason = "pointer" | "focus" | "blur" | "escape" | "api";
|
|
3
5
|
interface TooltipOptions {
|
|
4
|
-
/** Delay before showing tooltip (ms) */
|
|
6
|
+
/** Delay before showing tooltip (ms). Default: 300 */
|
|
5
7
|
delay?: number;
|
|
6
|
-
/** Duration to skip delay after closing (ms). Set to 0 to disable warm-up
|
|
8
|
+
/** Duration to skip delay after closing (ms). Set to 0 to disable warm-up. Default: 300 */
|
|
7
9
|
skipDelayDuration?: number;
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
+
/** Side of tooltip relative to trigger. Default: 'top'. Set at bind time only. */
|
|
11
|
+
side?: TooltipSide;
|
|
12
|
+
/** Alignment along the side. Default: 'center'. Set at bind time only. */
|
|
13
|
+
align?: TooltipAlign;
|
|
10
14
|
/** Callback when visibility changes */
|
|
11
15
|
onOpenChange?: (open: boolean) => void;
|
|
12
16
|
}
|
|
13
17
|
interface TooltipController {
|
|
14
|
-
/** Show the tooltip */
|
|
18
|
+
/** Show the tooltip immediately. Respects disabled state. */
|
|
15
19
|
show(): void;
|
|
16
20
|
/** Hide the tooltip */
|
|
17
21
|
hide(): void;
|
|
@@ -27,9 +31,25 @@ interface TooltipController {
|
|
|
27
31
|
* ```html
|
|
28
32
|
* <div data-slot="tooltip">
|
|
29
33
|
* <button data-slot="tooltip-trigger">Hover me</button>
|
|
30
|
-
* <div data-slot="tooltip-content"
|
|
34
|
+
* <div data-slot="tooltip-content" data-side="top" data-align="center">
|
|
35
|
+
* Tooltip text
|
|
36
|
+
* </div>
|
|
31
37
|
* </div>
|
|
32
38
|
* ```
|
|
39
|
+
*
|
|
40
|
+
* Data attributes (checked on content first, then root):
|
|
41
|
+
* - `data-side`: 'top' | 'right' | 'bottom' | 'left' (bind-time only)
|
|
42
|
+
* - `data-align`: 'start' | 'center' | 'end' (bind-time only)
|
|
43
|
+
* - `data-delay`: number (ms)
|
|
44
|
+
* - `data-skip-delay-duration`: number (ms)
|
|
45
|
+
*
|
|
46
|
+
* Note: side and align are resolved once at bind time. To change placement,
|
|
47
|
+
* destroy and recreate the tooltip with new options.
|
|
48
|
+
*
|
|
49
|
+
* This tooltip uses simple CSS positioning (not collision-aware).
|
|
50
|
+
* Opens on hover (non-touch) and focus. Touch devices: focus-only.
|
|
51
|
+
* Content is hoverable: moving pointer from trigger to content keeps it open.
|
|
52
|
+
* Tooltip stays open while trigger has focus, even if pointer leaves.
|
|
33
53
|
*/
|
|
34
54
|
declare function createTooltip(root: Element, options?: TooltipOptions): TooltipController;
|
|
35
55
|
/**
|
|
@@ -38,4 +58,4 @@ declare function createTooltip(root: Element, options?: TooltipOptions): Tooltip
|
|
|
38
58
|
*/
|
|
39
59
|
declare function create(scope?: ParentNode): TooltipController[];
|
|
40
60
|
//#endregion
|
|
41
|
-
export { TooltipController, TooltipOptions,
|
|
61
|
+
export { TooltipAlign, TooltipController, TooltipOptions, TooltipReason, TooltipSide, create, createTooltip };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,t)=>e.querySelector(`[data-slot="${t}"]`),t=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)];let n=0;const
|
|
1
|
+
const e=(e,t)=>e.querySelector(`[data-slot="${t}"]`),t=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)],n=new WeakMap;function r(e,t,r){if(typeof process<`u`&&process.env?.NODE_ENV===`production`)return;let i=n.get(e);i||(i=new Set,n.set(e,i)),!i.has(t)&&(i.add(t),console.warn(`[@data-slot] ${r}`))}function i(e){let t=`data-${e.replace(/([A-Z])/g,`-$1`).toLowerCase()}`,n=`data-${e}`;return t===n?[t]:[t,n]}function a(e,t){for(let n of i(t))if(e.hasAttribute(n))return e.getAttribute(n);return null}function o(e,t){let n=a(e,t);if(n===null||n===``)return;let i=Number(n);if(Number.isNaN(i)||!Number.isFinite(i)){r(e,t,`Invalid number value "${n}" for data-${t}.`);return}return i}function s(e,t,n){let i=a(e,t);if(i!==null){if(n.includes(i))return i;r(e,t,`Invalid value "${i}" for data-${t}. Expected one of: ${n.join(`, `)}.`)}}let c=0;const l=(e,t)=>e.id||=`${t}-${++c}`;function u(e,t,n,r){return e.addEventListener(t,n,r),()=>e.removeEventListener(t,n,r)}const d=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));let f=0;const p=[`top`,`right`,`bottom`,`left`],m=[`start`,`center`,`end`];function h(t,n={}){let r=e(t,`tooltip-trigger`),i=e(t,`tooltip-content`);if(!r||!i)throw Error(`Tooltip requires trigger and content slots`);let a=n.delay??o(t,`delay`)??300,c=n.skipDelayDuration??o(t,`skipDelayDuration`)??300,h=n.onOpenChange,g=n.side??s(i,`side`,p)??s(t,`side`,p)??`top`,_=n.align??s(i,`align`,m)??s(t,`align`,m)??`center`,v=!1,y=!1,b=null,x=null,S=[],C=l(i,`tooltip-content`);i.setAttribute(`role`,`tooltip`),i.setAttribute(`data-side`,g),i.setAttribute(`data-align`,_);let w=()=>r.hasAttribute(`disabled`)||r.getAttribute(`aria-disabled`)===`true`,T=()=>{x||=u(document,`keydown`,e=>{e.key===`Escape`&&v&&k(`escape`)})},E=()=>{x?.(),x=null},D=(e,n)=>{v!==e&&(v=e,t.setAttribute(`data-state`,v?`open`:`closed`),v?(r.setAttribute(`aria-describedby`,C),i.setAttribute(`aria-hidden`,`false`),T()):(r.removeAttribute(`aria-describedby`),i.setAttribute(`aria-hidden`,`true`),E()),d(t,`tooltip:change`,{open:v,trigger:r,content:i,reason:n}),h?.(v))},O=e=>{if(b&&(clearTimeout(b),b=null),Date.now()<f){D(!0,e);return}b=setTimeout(()=>{D(!0,e),b=null},a)},k=e=>{b&&(clearTimeout(b),b=null),v&&c>0&&(f=Date.now()+c),D(!1,e)};i.setAttribute(`aria-hidden`,`true`),t.setAttribute(`data-state`,`closed`),S.push(u(r,`pointerenter`,e=>{e.pointerType!==`touch`&&(w()||O(`pointer`))}),u(r,`pointerleave`,e=>{if(e.pointerType===`touch`||y)return;let t=e.relatedTarget;t&&i.contains(t)||k(`pointer`)}),u(r,`focus`,()=>{y=!0,!w()&&O(`focus`)}),u(r,`blur`,()=>{y=!1,k(`blur`)})),S.push(u(i,`pointerleave`,e=>{if(e.pointerType===`touch`||y)return;let t=e.relatedTarget;t&&r.contains(t)||k(`pointer`)}));let A={show:()=>{w()||(b&&(clearTimeout(b),b=null),D(!0,`api`))},hide:()=>k(`api`),get isOpen(){return v},destroy:()=>{b&&clearTimeout(b),E(),S.forEach(e=>e()),S.length=0}};return A}const g=new WeakSet;function _(e=document){let n=[];for(let r of t(e,`tooltip`)){if(g.has(r))continue;g.add(r),n.push(h(r))}return n}export{_ as create,h as createTooltip};
|