@data-slot/tooltip 0.2.4 → 0.2.6
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 +128 -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,84 @@ 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-state` | `"open"` \| `"closed"` |
|
|
124
|
+
| Content | `data-side` | `"top"` \| `"right"` \| `"bottom"` \| `"left"` |
|
|
125
|
+
| Content | `data-align` | `"start"` \| `"center"` \| `"end"` |
|
|
126
|
+
| Content | `role` | `"tooltip"` |
|
|
127
|
+
| Content | `aria-hidden` | `"true"` when closed, `"false"` when open |
|
|
128
|
+
| Trigger | `aria-describedby` | Content ID when open, removed when closed |
|
|
91
129
|
|
|
92
130
|
## Styling
|
|
93
131
|
|
|
94
|
-
|
|
132
|
+
This tooltip uses simple CSS positioning and is not collision-aware. Visibility is controlled entirely via CSS using the `data-state` attribute.
|
|
95
133
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
display: none;
|
|
100
|
-
}
|
|
134
|
+
### Recommended CSS
|
|
135
|
+
|
|
136
|
+
The visibility transition trick keeps the tooltip visible during fade-out, then becomes non-focusable after the transition completes—no JS timers needed.
|
|
101
137
|
|
|
138
|
+
```css
|
|
102
139
|
/* Positioning */
|
|
103
140
|
[data-slot="tooltip"] {
|
|
104
141
|
position: relative;
|
|
@@ -107,67 +144,83 @@ Set position via HTML:
|
|
|
107
144
|
[data-slot="tooltip-content"] {
|
|
108
145
|
position: absolute;
|
|
109
146
|
white-space: nowrap;
|
|
147
|
+
/* Hidden by default */
|
|
148
|
+
opacity: 0;
|
|
149
|
+
visibility: hidden;
|
|
150
|
+
pointer-events: none;
|
|
151
|
+
/* Visibility delays hiding until after opacity fades */
|
|
152
|
+
transition: opacity 0.15s, visibility 0s linear 0.15s;
|
|
110
153
|
}
|
|
111
154
|
|
|
112
|
-
|
|
155
|
+
/* Open state */
|
|
156
|
+
[data-slot="tooltip"][data-state="open"] [data-slot="tooltip-content"] {
|
|
157
|
+
opacity: 1;
|
|
158
|
+
visibility: visible;
|
|
159
|
+
pointer-events: auto;
|
|
160
|
+
transition-delay: 0s;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/* Side positioning */
|
|
164
|
+
[data-slot="tooltip-content"][data-side="top"] {
|
|
113
165
|
bottom: 100%;
|
|
114
166
|
left: 50%;
|
|
115
167
|
transform: translateX(-50%);
|
|
116
168
|
margin-bottom: 8px;
|
|
117
169
|
}
|
|
118
170
|
|
|
119
|
-
[data-slot="tooltip-content"][data-
|
|
171
|
+
[data-slot="tooltip-content"][data-side="bottom"] {
|
|
120
172
|
top: 100%;
|
|
121
173
|
left: 50%;
|
|
122
174
|
transform: translateX(-50%);
|
|
123
175
|
margin-top: 8px;
|
|
124
176
|
}
|
|
125
177
|
|
|
126
|
-
[data-slot="tooltip-content"][data-
|
|
178
|
+
[data-slot="tooltip-content"][data-side="left"] {
|
|
127
179
|
right: 100%;
|
|
128
180
|
top: 50%;
|
|
129
181
|
transform: translateY(-50%);
|
|
130
182
|
margin-right: 8px;
|
|
131
183
|
}
|
|
132
184
|
|
|
133
|
-
[data-slot="tooltip-content"][data-
|
|
185
|
+
[data-slot="tooltip-content"][data-side="right"] {
|
|
134
186
|
left: 100%;
|
|
135
187
|
top: 50%;
|
|
136
188
|
transform: translateY(-50%);
|
|
137
189
|
margin-left: 8px;
|
|
138
190
|
}
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### Animations
|
|
142
191
|
|
|
143
|
-
|
|
144
|
-
[data-slot="tooltip-content"]
|
|
145
|
-
|
|
146
|
-
|
|
192
|
+
/* Alignment (example for top/bottom sides) */
|
|
193
|
+
[data-slot="tooltip-content"][data-side="top"][data-align="start"],
|
|
194
|
+
[data-slot="tooltip-content"][data-side="bottom"][data-align="start"] {
|
|
195
|
+
left: 0;
|
|
196
|
+
transform: none;
|
|
147
197
|
}
|
|
148
198
|
|
|
149
|
-
[data-slot="tooltip"][data-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
[data-slot="tooltip-content"][data-instant] {
|
|
155
|
-
transition: none;
|
|
199
|
+
[data-slot="tooltip-content"][data-side="top"][data-align="end"],
|
|
200
|
+
[data-slot="tooltip-content"][data-side="bottom"][data-align="end"] {
|
|
201
|
+
left: auto;
|
|
202
|
+
right: 0;
|
|
203
|
+
transform: none;
|
|
156
204
|
}
|
|
157
205
|
```
|
|
158
206
|
|
|
159
207
|
### Tailwind Example
|
|
160
208
|
|
|
209
|
+
Use `group` on the root and `group-data-[state=open]:` for open state styles:
|
|
210
|
+
|
|
161
211
|
```html
|
|
162
|
-
<div data-slot="tooltip" class="relative inline-block
|
|
212
|
+
<div data-slot="tooltip" class="group relative inline-block">
|
|
163
213
|
<button data-slot="tooltip-trigger">
|
|
164
214
|
Hover me
|
|
165
215
|
</button>
|
|
166
216
|
<div
|
|
167
217
|
data-slot="tooltip-content"
|
|
168
|
-
data-
|
|
169
|
-
|
|
170
|
-
|
|
218
|
+
data-side="top"
|
|
219
|
+
class="absolute bottom-full left-1/2 -translate-x-1/2 mb-2 px-2 py-1
|
|
220
|
+
bg-gray-900 text-white text-sm rounded
|
|
221
|
+
opacity-0 pointer-events-none transition-opacity duration-150
|
|
222
|
+
group-data-[state=open]:opacity-100
|
|
223
|
+
group-data-[state=open]:pointer-events-auto"
|
|
171
224
|
>
|
|
172
225
|
Tooltip text
|
|
173
226
|
</div>
|
|
@@ -180,25 +233,40 @@ When a user closes one tooltip and quickly hovers another, the second tooltip sh
|
|
|
180
233
|
|
|
181
234
|
- Controlled by `skipDelayDuration` option
|
|
182
235
|
- Set to `0` to disable this behavior
|
|
183
|
-
-
|
|
236
|
+
- Warm-up only skips the delay, not CSS transitions
|
|
237
|
+
- Warm window is set only when a tooltip actually closes (not when a pending open is cancelled)
|
|
184
238
|
|
|
185
239
|
## Accessibility
|
|
186
240
|
|
|
187
241
|
The component automatically handles:
|
|
188
242
|
|
|
189
243
|
- `role="tooltip"` on content
|
|
190
|
-
- `aria-describedby`
|
|
191
|
-
-
|
|
244
|
+
- `aria-describedby` on trigger only when open (prevents stale announcements)
|
|
245
|
+
- `aria-hidden` on content: explicit `"true"`/`"false"` for consistent AT behavior
|
|
246
|
+
- Unique ID generation for content via `ensureId`
|
|
247
|
+
|
|
248
|
+
### Disabled Triggers
|
|
249
|
+
|
|
250
|
+
If the trigger has `disabled` attribute or `aria-disabled="true"`:
|
|
251
|
+
- Pointer and focus events will not open the tooltip
|
|
252
|
+
- Programmatic `.show()` also respects the disabled state
|
|
192
253
|
|
|
193
|
-
##
|
|
254
|
+
## Interaction Model
|
|
194
255
|
|
|
195
|
-
|
|
|
196
|
-
|
|
197
|
-
|
|
|
198
|
-
|
|
|
256
|
+
| Input | Behavior |
|
|
257
|
+
|-------|----------|
|
|
258
|
+
| Pointer enter trigger (mouse/pen) | Show after delay |
|
|
259
|
+
| Pointer leave trigger | Hide immediately (unless entering content) |
|
|
260
|
+
| Pointer enter content | Keep open (hoverable content) |
|
|
261
|
+
| Pointer leave content | Hide immediately (unless entering trigger) |
|
|
262
|
+
| Touch hover | Ignored (focus-only on touch devices) |
|
|
199
263
|
| Focus | Show after delay |
|
|
200
264
|
| Blur | Hide immediately |
|
|
201
|
-
| `Escape` | Hide immediately |
|
|
265
|
+
| `Escape` | Hide immediately (listener only active when open) |
|
|
266
|
+
|
|
267
|
+
**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.
|
|
268
|
+
|
|
269
|
+
**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
270
|
|
|
203
271
|
## Events
|
|
204
272
|
|
|
@@ -206,11 +274,20 @@ Listen for changes via custom events:
|
|
|
206
274
|
|
|
207
275
|
```javascript
|
|
208
276
|
element.addEventListener("tooltip:change", (e) => {
|
|
209
|
-
|
|
277
|
+
const { open, trigger, content, reason } = e.detail;
|
|
278
|
+
console.log(`Tooltip ${open ? 'opened' : 'closed'} via ${reason}`);
|
|
210
279
|
});
|
|
211
280
|
```
|
|
212
281
|
|
|
282
|
+
### Event Detail
|
|
283
|
+
|
|
284
|
+
| Property | Type | Description |
|
|
285
|
+
|----------|------|-------------|
|
|
286
|
+
| `open` | `boolean` | Current visibility state |
|
|
287
|
+
| `trigger` | `HTMLElement` | The trigger element |
|
|
288
|
+
| `content` | `HTMLElement` | The content element |
|
|
289
|
+
| `reason` | `string` | What caused the change: `"pointer"`, `"focus"`, `"blur"`, `"escape"`, `"api"` |
|
|
290
|
+
|
|
213
291
|
## License
|
|
214
292
|
|
|
215
293
|
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)=>{if(v===e)return;v=e;let a=v?`open`:`closed`;t.setAttribute(`data-state`,a),i.setAttribute(`data-state`,a),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`),i.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)=>{if(v===e)return;v=e;let a=v?`open`:`closed`;t.setAttribute(`data-state`,a),i.setAttribute(`data-state`,a),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`),i.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};
|