@data-slot/tooltip 0.2.11 → 0.2.13
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 +38 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -270,6 +270,8 @@ If the trigger has `disabled` attribute or `aria-disabled="true"`:
|
|
|
270
270
|
|
|
271
271
|
## Events
|
|
272
272
|
|
|
273
|
+
### Outbound Events
|
|
274
|
+
|
|
273
275
|
Listen for changes via custom events:
|
|
274
276
|
|
|
275
277
|
```javascript
|
|
@@ -279,7 +281,7 @@ element.addEventListener("tooltip:change", (e) => {
|
|
|
279
281
|
});
|
|
280
282
|
```
|
|
281
283
|
|
|
282
|
-
|
|
284
|
+
#### Event Detail
|
|
283
285
|
|
|
284
286
|
| Property | Type | Description |
|
|
285
287
|
|----------|------|-------------|
|
|
@@ -288,6 +290,41 @@ element.addEventListener("tooltip:change", (e) => {
|
|
|
288
290
|
| `content` | `HTMLElement` | The content element |
|
|
289
291
|
| `reason` | `string` | What caused the change: `"pointer"`, `"focus"`, `"blur"`, `"escape"`, `"api"` |
|
|
290
292
|
|
|
293
|
+
### Inbound Events
|
|
294
|
+
|
|
295
|
+
Control the tooltip via events:
|
|
296
|
+
|
|
297
|
+
| Event | Detail | Description |
|
|
298
|
+
|-------|--------|-------------|
|
|
299
|
+
| `tooltip:set` | `{ open: boolean }` | Set visibility programmatically |
|
|
300
|
+
|
|
301
|
+
```javascript
|
|
302
|
+
// Show the tooltip
|
|
303
|
+
element.dispatchEvent(
|
|
304
|
+
new CustomEvent("tooltip:set", { detail: { open: true } })
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
// Hide the tooltip
|
|
308
|
+
element.dispatchEvent(
|
|
309
|
+
new CustomEvent("tooltip:set", { detail: { open: false } })
|
|
310
|
+
);
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
**Note:** Opening respects disabled state (trigger has `disabled` attribute or `aria-disabled="true"`). Closing is always allowed.
|
|
314
|
+
|
|
315
|
+
#### Deprecated Shapes
|
|
316
|
+
|
|
317
|
+
The following shape is deprecated and will be removed in v1.0:
|
|
318
|
+
|
|
319
|
+
```javascript
|
|
320
|
+
// Deprecated: { value: boolean }
|
|
321
|
+
element.dispatchEvent(
|
|
322
|
+
new CustomEvent("tooltip:set", { detail: { value: true } })
|
|
323
|
+
);
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
Use `{ open: boolean }` instead.
|
|
327
|
+
|
|
291
328
|
## License
|
|
292
329
|
|
|
293
330
|
MIT
|
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}"]`)],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;
|
|
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`)})),S.push(u(t,`tooltip:set`,e=>{let t=e.detail,n;if(t?.open===void 0?t?.value!==void 0&&(n=t.value):n=t.open,typeof n==`boolean`)if(n){if(w())return;b&&(clearTimeout(b),b=null),D(!0,`api`)}else k(`api`)}));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.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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};
|
|
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`)})),S.push(u(t,`tooltip:set`,e=>{let t=e.detail,n;if(t?.open===void 0?t?.value!==void 0&&(n=t.value):n=t.open,typeof n==`boolean`)if(n){if(w())return;b&&(clearTimeout(b),b=null),D(!0,`api`)}else k(`api`)}));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};
|