@mkmonkeycat/dom-utils 0.1.0 → 1.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 ADDED
@@ -0,0 +1,18 @@
1
+ # dom-utils
2
+
3
+ A TypeScript utility library for DOM manipulation and common web development tasks.
4
+
5
+ [![npm](https://img.shields.io/npm/v/@mkmonkeycat/dom-utils)](https://www.npmjs.com/package/@mkmonkeycat/dom-utils)
6
+ [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @mkmonkeycat/dom-utils
12
+ # or
13
+ yarn add @mkmonkeycat/dom-utils
14
+ # or
15
+ pnpm add @mkmonkeycat/dom-utils
16
+ ```
17
+
18
+ ## Usage
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export declare const VERSION = "0.0.1";
1
2
  export * from './dom';
2
3
  export * from './utils';
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=(...e)=>e.flatMap(r=>typeof r=="string"?r.trim().split(/\s+/).filter(Boolean):r||[]),L=(e,r,...n)=>{const o=document.createElement(e);return r?(Object.entries(r).forEach(([s,t])=>{if(t!=null){if(s==="className"||s==="class"){const i=Array.isArray(t)?h(...t):h(t);o.className=i.join(" ");return}if(s==="style"){typeof t=="string"?o.style.cssText=t:typeof t=="object"&&Object.assign(o.style,t);return}if(s==="dataset"&&typeof t=="object"){Object.assign(o.dataset,t);return}if(s.startsWith("on")){const i=s.slice(2).toLowerCase();typeof t=="function"?o.addEventListener(i,t):Array.isArray(t)&&t.forEach(c=>{typeof c=="function"&&o.addEventListener(i,c)});return}s in o?o[s]=t:o.setAttribute(s,String(t))}}),b(o,n),o):(b(o,n),o)},A=(...e)=>{const r=document.createDocumentFragment();return e.forEach(n=>{typeof n=="string"?r.appendChild(document.createTextNode(n)):r.appendChild(n)}),r},b=(e,r)=>{r.forEach(n=>{typeof n=="string"?e.appendChild(document.createTextNode(n)):e.appendChild(n)})},F=e=>({width:e.offsetWidth,height:e.offsetHeight}),O=e=>({top:e.offsetTop,left:e.offsetLeft}),P=(e,r)=>window.getComputedStyle(e).getPropertyValue(r).trim(),I=(e,r)=>{e&&(e===document.documentElement||e===document.body?window.scrollTo(r):e.scrollTo?.(r))},M=(e,r)=>{e.scrollIntoView?.(r)},D=e=>({width:e.clientWidth,height:e.clientHeight}),j=(e,r=!1)=>{let n=e.offsetWidth,o=e.offsetHeight;if(r){const s=window.getComputedStyle(e),t=parseFloat(s.marginLeft||"0"),i=parseFloat(s.marginRight||"0"),c=parseFloat(s.marginTop||"0"),a=parseFloat(s.marginBottom||"0");n+=t+i,o+=c+a}return{width:n,height:o}},R=e=>e.getBoundingClientRect(),V=e=>{const n=new DOMParser().parseFromString(e,"image/svg+xml");if(n.documentElement.nodeName==="parsererror")throw new Error("Invalid SVG string: Failed to parse SVG");return n.documentElement},y=(e,r)=>(Object.defineProperty(e,r,{value:!0}),e),W=(e,r)=>!!e[r],p=e=>y(e,"__mkEvent"),x=(e,r,n={})=>{const{event:o="mousedown",capture:s=!0,exclude:t=[]}=n,i=p(c=>{const a=c.target;e.contains(a)||t.some(l=>l?.contains(a))||r(c)});return document.addEventListener(o,i,s),()=>document.removeEventListener(o,i,s)},g=(e,r,n={})=>{const{preventDefault:o=!1,stopPropagation:s=!1,event:t="keydown",target:i=document}=n,c=Array.isArray(e)?e:[e],a=p(l=>{c.includes(l.key)&&(o&&l.preventDefault(),s&&l.stopPropagation(),r(l))});return i.addEventListener(t,a),()=>i.removeEventListener(t,a)},z=(e,r={})=>g("Escape",e,r),$=(e,r={})=>g("Enter",e,r),K=(e,r,n={})=>{const{duration:o=500,threshold:s=10}=n;let t=null,i=0,c=0;const a=d=>"touches"in d?d.touches[0]:d,l=p(d=>{const f=a(d);i=f.clientX,c=f.clientY,t=setTimeout(()=>{r(d)},o)}),w=p(d=>{if(!t)return;const f=a(d),S=Math.abs(f.clientX-i),C=Math.abs(f.clientY-c);(S>s||C>s)&&u()}),u=p(()=>{t&&(clearTimeout(t),t=null)}),m=v(e,{mousedown:l,mousemove:w,mouseup:u,mouseleave:u,touchstart:l,touchmove:w,touchend:u,touchcancel:u});return()=>{m(),t&&clearTimeout(t)}},v=(e,r,n)=>{const o=[];return Object.entries(r).forEach(([s,t])=>{if(!t)return;const i=s;Array.isArray(t)?t.forEach(c=>{e.addEventListener(i,c,n),o.push([i,c])}):(e.addEventListener(i,t,n),o.push([i,t]))}),()=>{o.forEach(([s,t])=>{e.removeEventListener(s,t,n)})}},N=(e,r={})=>{const{timeout:n=5e3,root:o=document.body,interval:s=100}=r;return new Promise((t,i)=>{const c=o.querySelector(e);if(c){t(c);return}const a=new MutationObserver(()=>{const m=o.querySelector(e);m&&(u(),t(m))});a.observe(o,{childList:!0,subtree:!0});const l=setInterval(()=>{const m=o.querySelector(e);m&&(u(),t(m))},s),w=setTimeout(()=>{u(),i(new Error(`Timeout: Element "${e}" not found within ${n}ms`))},n),u=()=>{a.disconnect(),clearTimeout(w),clearInterval(l)}})},G=(e,r={})=>{const{onRemove:n,root:o=document.body}=r,s=new MutationObserver(()=>{o.contains(e)||(n?.(),s.disconnect())});return s.observe(o,{childList:!0,subtree:!0}),()=>s.disconnect()},X=(e,r={})=>{const{root:n=document.body,timeout:o}=r;return new Promise((s,t)=>{if(!n.contains(e)){s();return}let i;const c=new MutationObserver(()=>{n.contains(e)||(a(),s())});c.observe(n,{childList:!0,subtree:!0}),o&&(i=setTimeout(()=>{a(),t(new Error(`Timeout: Element not removed within ${o}ms`))},o));const a=()=>{c.disconnect(),i&&clearTimeout(i)}})},Y=(e,r={})=>{const{attributes:n=!0,childList:o=!0,characterData:s=!1,subtree:t=!1,onMutation:i}=r,c=new MutationObserver(a=>i?.(a));return c.observe(e,{attributes:n,childList:o,characterData:s,subtree:t}),()=>c.disconnect()},Z=(e,r={})=>{const{id:n,nonce:o,target:s=document.head}=r,t=document.createElement("style");return n&&(t.id=n),o&&t.setAttribute("nonce",o),t.textContent=e,s.appendChild(t),{style:t,remove:()=>{t.parentNode?.removeChild(t)}}},E=typeof unsafeWindow<"u"?unsafeWindow:window,q=E.document;function B(e){return e?e instanceof Window?e:e instanceof UIEvent&&e.view?e.view:e instanceof Document?e.defaultView:e instanceof ShadowRoot?e.host.ownerDocument?.defaultView??null:e instanceof Node?e.ownerDocument?.defaultView??null:null:E}const H=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),_=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),U=(e,r)=>{let n=null,o=null;const s=(...t)=>{o=t,n&&clearTimeout(n),n=setTimeout(()=>{e(...o),n=null,o=null},r)};return s.cancel=()=>{n&&clearTimeout(n),n=null,o=null},s.flush=()=>{n&&(e(...o),clearTimeout(n),n=null,o=null)},s},J=(e,r)=>{let n=null,o=null,s=0;const t=()=>{n&&(clearTimeout(n),n=null)},i=(...c)=>{const a=Date.now();if(o=c,s===0)e(...c),s=a;else{const l=a-s;l>=r?(e(...c),s=a,t()):(t(),n=setTimeout(()=>{e(...o),s=Date.now(),n=null},r-l))}};return i.cancel=()=>{t(),s=0,o=null},i.flush=()=>{s>0&&o!==null&&e(...o),t(),s=0,o=null},i},T=e=>new Promise(r=>setTimeout(r,e)),Q=e=>T(e),k=(e,r,n=`Operation timed out after ${r}ms`)=>Promise.race([e,new Promise((o,s)=>setTimeout(()=>s(new Error(n)),r))]),ee=()=>{},te=async(e,r={})=>{const{maxAttempts:n=3,delay:o=1e3,exponential:s=!1}=r;let t;for(let i=1;i<=n;i++)try{return await e()}catch(c){if(t=c instanceof Error?c:new Error(String(c)),i<n){const a=s?o*Math.pow(2,i-1):o;await new Promise(l=>setTimeout(l,a))}}throw t||new Error("Retry failed")};exports.addEventsListener=v;exports.camelToKebab=H;exports.createElement=L;exports.createFragment=A;exports.createSVGFromString=V;exports.debounce=U;exports.delay=T;exports.getComputedStyle=P;exports.getInnerSize=D;exports.getOffset=O;exports.getOuterSize=j;exports.getRect=R;exports.getSize=F;exports.getWin=B;exports.globalDoc=q;exports.globalWin=E;exports.injectStyle=Z;exports.isMarkedAs=W;exports.markFunc=y;exports.noop=ee;exports.onClickOutside=x;exports.onEnter=$;exports.onEscape=z;exports.onKeyPress=g;exports.onLongPress=K;exports.parseClass=h;exports.pascalToKebab=_;exports.retry=te;exports.scrollIntoView=M;exports.scrollTo=I;exports.sleep=Q;exports.throttle=J;exports.timeout=k;exports.waitForElement=N;exports.waitForElementRemoval=X;exports.watchElementChanges=Y;exports.watchElementRemoval=G;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=(...e)=>e.flatMap(r=>typeof r=="string"?r.trim().split(/\s+/).filter(Boolean):r||[]),L=(e,r,...n)=>{const o=document.createElement(e);return r?(Object.entries(r).forEach(([s,t])=>{if(t!=null){if(s==="className"||s==="class"){const i=Array.isArray(t)?h(...t):h(t);o.className=i.join(" ");return}if(s==="style"){typeof t=="string"?o.style.cssText=t:typeof t=="object"&&Object.assign(o.style,t);return}if(s==="dataset"&&typeof t=="object"){Object.assign(o.dataset,t);return}if(s.startsWith("on")){const i=s.slice(2).toLowerCase();typeof t=="function"?o.addEventListener(i,t):Array.isArray(t)&&t.forEach(c=>{typeof c=="function"&&o.addEventListener(i,c)});return}s in o?o[s]=t:o.setAttribute(s,String(t))}}),b(o,n),o):(b(o,n),o)},A=(...e)=>{const r=document.createDocumentFragment();return e.forEach(n=>{typeof n=="string"?r.appendChild(document.createTextNode(n)):r.appendChild(n)}),r},b=(e,r)=>{r.forEach(n=>{typeof n=="string"?e.appendChild(document.createTextNode(n)):e.appendChild(n)})},O=e=>({width:e.offsetWidth,height:e.offsetHeight}),F=e=>({top:e.offsetTop,left:e.offsetLeft}),P=(e,r)=>window.getComputedStyle(e).getPropertyValue(r).trim(),I=(e,r)=>{e&&(e===document.documentElement||e===document.body?window.scrollTo(r):e.scrollTo?.(r))},M=(e,r)=>{e.scrollIntoView?.(r)},R=e=>({width:e.clientWidth,height:e.clientHeight}),V=(e,r=!1)=>{let n=e.offsetWidth,o=e.offsetHeight;if(r){const s=window.getComputedStyle(e),t=parseFloat(s.marginLeft||"0"),i=parseFloat(s.marginRight||"0"),c=parseFloat(s.marginTop||"0"),a=parseFloat(s.marginBottom||"0");n+=t+i,o+=c+a}return{width:n,height:o}},D=e=>e.getBoundingClientRect(),j=e=>{const n=new DOMParser().parseFromString(e,"image/svg+xml");if(n.documentElement.nodeName==="parsererror")throw new Error("Invalid SVG string: Failed to parse SVG");return n.documentElement},y=(e,r)=>(Object.defineProperty(e,r,{value:!0}),e),W=(e,r)=>!!e[r],p=e=>y(e,"__mkEvent"),x=(e,r,n={})=>{const{event:o="mousedown",capture:s=!0,exclude:t=[]}=n,i=p(c=>{const a=c.target;e.contains(a)||t.some(l=>l?.contains(a))||r(c)});return document.addEventListener(o,i,s),()=>document.removeEventListener(o,i,s)},g=(e,r,n={})=>{const{preventDefault:o=!1,stopPropagation:s=!1,event:t="keydown",target:i=document}=n,c=Array.isArray(e)?e:[e],a=p(l=>{c.includes(l.key)&&(o&&l.preventDefault(),s&&l.stopPropagation(),r(l))});return i.addEventListener(t,a),()=>i.removeEventListener(t,a)},z=(e,r={})=>g("Escape",e,r),N=(e,r={})=>g("Enter",e,r),$=(e,r,n={})=>{const{duration:o=500,threshold:s=10}=n;let t=null,i=0,c=0;const a=d=>"touches"in d?d.touches[0]:d,l=p(d=>{const f=a(d);i=f.clientX,c=f.clientY,t=setTimeout(()=>{r(d)},o)}),w=p(d=>{if(!t)return;const f=a(d),S=Math.abs(f.clientX-i),C=Math.abs(f.clientY-c);(S>s||C>s)&&u()}),u=p(()=>{t&&(clearTimeout(t),t=null)}),m=v(e,{mousedown:l,mousemove:w,mouseup:u,mouseleave:u,touchstart:l,touchmove:w,touchend:u,touchcancel:u});return()=>{m(),t&&clearTimeout(t)}},v=(e,r,n)=>{const o=[];return Object.entries(r).forEach(([s,t])=>{if(!t)return;const i=s;Array.isArray(t)?t.forEach(c=>{e.addEventListener(i,c,n),o.push([i,c])}):(e.addEventListener(i,t,n),o.push([i,t]))}),()=>{o.forEach(([s,t])=>{e.removeEventListener(s,t,n)})}},K=(e,r={})=>{const{timeout:n=5e3,root:o=document.body,interval:s=100}=r;return new Promise((t,i)=>{const c=o.querySelector(e);if(c){t(c);return}const a=new MutationObserver(()=>{const m=o.querySelector(e);m&&(u(),t(m))});a.observe(o,{childList:!0,subtree:!0});const l=setInterval(()=>{const m=o.querySelector(e);m&&(u(),t(m))},s),w=setTimeout(()=>{u(),i(new Error(`Timeout: Element "${e}" not found within ${n}ms`))},n),u=()=>{a.disconnect(),clearTimeout(w),clearInterval(l)}})},G=(e,r={})=>{const{onRemove:n,root:o=document.body}=r,s=new MutationObserver(()=>{o.contains(e)||(n?.(),s.disconnect())});return s.observe(o,{childList:!0,subtree:!0}),()=>s.disconnect()},X=(e,r={})=>{const{root:n=document.body,timeout:o}=r;return new Promise((s,t)=>{if(!n.contains(e)){s();return}let i;const c=new MutationObserver(()=>{n.contains(e)||(a(),s())});c.observe(n,{childList:!0,subtree:!0}),o&&(i=setTimeout(()=>{a(),t(new Error(`Timeout: Element not removed within ${o}ms`))},o));const a=()=>{c.disconnect(),i&&clearTimeout(i)}})},Y=(e,r={})=>{const{attributes:n=!0,childList:o=!0,characterData:s=!1,subtree:t=!1,onMutation:i}=r,c=new MutationObserver(a=>i?.(a));return c.observe(e,{attributes:n,childList:o,characterData:s,subtree:t}),()=>c.disconnect()},Z=(e,r={})=>{const{id:n,nonce:o,target:s=document.head}=r,t=document.createElement("style");return n&&(t.id=n),o&&t.setAttribute("nonce",o),t.textContent=e,s.appendChild(t),{style:t,remove:()=>{t.parentNode?.removeChild(t)}}},E=typeof unsafeWindow<"u"?unsafeWindow:window,q=E.document;function B(e){return e?e instanceof Window?e:e instanceof UIEvent&&e.view?e.view:e instanceof Document?e.defaultView:e instanceof ShadowRoot?e.host.ownerDocument?.defaultView??null:e instanceof Node?e.ownerDocument?.defaultView??null:null:E}const H=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),_=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),U=(e,r)=>{let n=null,o=null;const s=(...t)=>{o=t,n&&clearTimeout(n),n=setTimeout(()=>{e(...o),n=null,o=null},r)};return s.cancel=()=>{n&&clearTimeout(n),n=null,o=null},s.flush=()=>{n&&(e(...o),clearTimeout(n),n=null,o=null)},s},J=(e,r)=>{let n=null,o=null,s=0;const t=()=>{n&&(clearTimeout(n),n=null)},i=(...c)=>{const a=Date.now();if(o=c,s===0)e(...c),s=a;else{const l=a-s;l>=r?(e(...c),s=a,t()):(t(),n=setTimeout(()=>{e(...o),s=Date.now(),n=null},r-l))}};return i.cancel=()=>{t(),s=0,o=null},i.flush=()=>{s>0&&o!==null&&e(...o),t(),s=0,o=null},i},T=e=>new Promise(r=>setTimeout(r,e)),Q=e=>T(e),k=(e,r,n=`Operation timed out after ${r}ms`)=>Promise.race([e,new Promise((o,s)=>setTimeout(()=>s(new Error(n)),r))]),ee=()=>{},te=async(e,r={})=>{const{maxAttempts:n=3,delay:o=1e3,exponential:s=!1}=r;let t;for(let i=1;i<=n;i++)try{return await e()}catch(c){if(t=c instanceof Error?c:new Error(String(c)),i<n){const a=s?o*Math.pow(2,i-1):o;await new Promise(l=>setTimeout(l,a))}}throw t||new Error("Retry failed")},ne="0.0.1";exports.VERSION=ne;exports.addEventsListener=v;exports.camelToKebab=H;exports.createElement=L;exports.createFragment=A;exports.createSVGFromString=j;exports.debounce=U;exports.delay=T;exports.getComputedStyle=P;exports.getInnerSize=R;exports.getOffset=F;exports.getOuterSize=V;exports.getRect=D;exports.getSize=O;exports.getWin=B;exports.globalDoc=q;exports.globalWin=E;exports.injectStyle=Z;exports.isMarkedAs=W;exports.markFunc=y;exports.noop=ee;exports.onClickOutside=x;exports.onEnter=N;exports.onEscape=z;exports.onKeyPress=g;exports.onLongPress=$;exports.parseClass=h;exports.pascalToKebab=_;exports.retry=te;exports.scrollIntoView=M;exports.scrollTo=I;exports.sleep=Q;exports.throttle=J;exports.timeout=k;exports.waitForElement=K;exports.waitForElementRemoval=X;exports.watchElementChanges=Y;exports.watchElementRemoval=G;
@@ -1 +1 @@
1
- var MKDomUtils=(function(i){"use strict";const E=(...e)=>e.flatMap(r=>typeof r=="string"?r.trim().split(/\s+/).filter(Boolean):r||[]),C=(e,r,...n)=>{const o=document.createElement(e);return r?(Object.entries(r).forEach(([s,t])=>{if(t!=null){if(s==="className"||s==="class"){const c=Array.isArray(t)?E(...t):E(t);o.className=c.join(" ");return}if(s==="style"){typeof t=="string"?o.style.cssText=t:typeof t=="object"&&Object.assign(o.style,t);return}if(s==="dataset"&&typeof t=="object"){Object.assign(o.dataset,t);return}if(s.startsWith("on")){const c=s.slice(2).toLowerCase();typeof t=="function"?o.addEventListener(c,t):Array.isArray(t)&&t.forEach(a=>{typeof a=="function"&&o.addEventListener(c,a)});return}s in o?o[s]=t:o.setAttribute(s,String(t))}}),v(o,n),o):(v(o,n),o)},L=(...e)=>{const r=document.createDocumentFragment();return e.forEach(n=>{typeof n=="string"?r.appendChild(document.createTextNode(n)):r.appendChild(n)}),r},v=(e,r)=>{r.forEach(n=>{typeof n=="string"?e.appendChild(document.createTextNode(n)):e.appendChild(n)})},A=e=>({width:e.offsetWidth,height:e.offsetHeight}),F=e=>({top:e.offsetTop,left:e.offsetLeft}),O=(e,r)=>window.getComputedStyle(e).getPropertyValue(r).trim(),P=(e,r)=>{e&&(e===document.documentElement||e===document.body?window.scrollTo(r):e.scrollTo?.(r))},I=(e,r)=>{e.scrollIntoView?.(r)},M=e=>({width:e.clientWidth,height:e.clientHeight}),D=(e,r=!1)=>{let n=e.offsetWidth,o=e.offsetHeight;if(r){const s=window.getComputedStyle(e),t=parseFloat(s.marginLeft||"0"),c=parseFloat(s.marginRight||"0"),a=parseFloat(s.marginTop||"0"),l=parseFloat(s.marginBottom||"0");n+=t+c,o+=a+l}return{width:n,height:o}},j=e=>e.getBoundingClientRect(),R=e=>{const n=new DOMParser().parseFromString(e,"image/svg+xml");if(n.documentElement.nodeName==="parsererror")throw new Error("Invalid SVG string: Failed to parse SVG");return n.documentElement},p=(e,r)=>(Object.defineProperty(e,r,{value:!0}),e),V=(e,r)=>!!e[r],w=e=>p(e,"__mkEvent"),W=(e,r,n={})=>{const{event:o="mousedown",capture:s=!0,exclude:t=[]}=n,c=w(a=>{const l=a.target;e.contains(l)||t.some(u=>u?.contains(l))||r(a)});return document.addEventListener(o,c,s),()=>document.removeEventListener(o,c,s)},b=(e,r,n={})=>{const{preventDefault:o=!1,stopPropagation:s=!1,event:t="keydown",target:c=document}=n,a=Array.isArray(e)?e:[e],l=w(u=>{a.includes(u.key)&&(o&&u.preventDefault(),s&&u.stopPropagation(),r(u))});return c.addEventListener(t,l),()=>c.removeEventListener(t,l)},z=(e,r={})=>b("Escape",e,r),K=(e,r={})=>b("Enter",e,r),$=(e,r,n={})=>{const{duration:o=500,threshold:s=10}=n;let t=null,c=0,a=0;const l=m=>"touches"in m?m.touches[0]:m,u=w(m=>{const h=l(m);c=h.clientX,a=h.clientY,t=setTimeout(()=>{r(m)},o)}),g=w(m=>{if(!t)return;const h=l(m),te=Math.abs(h.clientX-c),ne=Math.abs(h.clientY-a);(te>s||ne>s)&&d()}),d=w(()=>{t&&(clearTimeout(t),t=null)}),f=T(e,{mousedown:u,mousemove:g,mouseup:d,mouseleave:d,touchstart:u,touchmove:g,touchend:d,touchcancel:d});return()=>{f(),t&&clearTimeout(t)}},T=(e,r,n)=>{const o=[];return Object.entries(r).forEach(([s,t])=>{if(!t)return;const c=s;Array.isArray(t)?t.forEach(a=>{e.addEventListener(c,a,n),o.push([c,a])}):(e.addEventListener(c,t,n),o.push([c,t]))}),()=>{o.forEach(([s,t])=>{e.removeEventListener(s,t,n)})}},N=(e,r={})=>{const{timeout:n=5e3,root:o=document.body,interval:s=100}=r;return new Promise((t,c)=>{const a=o.querySelector(e);if(a){t(a);return}const l=new MutationObserver(()=>{const f=o.querySelector(e);f&&(d(),t(f))});l.observe(o,{childList:!0,subtree:!0});const u=setInterval(()=>{const f=o.querySelector(e);f&&(d(),t(f))},s),g=setTimeout(()=>{d(),c(new Error(`Timeout: Element "${e}" not found within ${n}ms`))},n),d=()=>{l.disconnect(),clearTimeout(g),clearInterval(u)}})},G=(e,r={})=>{const{onRemove:n,root:o=document.body}=r,s=new MutationObserver(()=>{o.contains(e)||(n?.(),s.disconnect())});return s.observe(o,{childList:!0,subtree:!0}),()=>s.disconnect()},X=(e,r={})=>{const{root:n=document.body,timeout:o}=r;return new Promise((s,t)=>{if(!n.contains(e)){s();return}let c;const a=new MutationObserver(()=>{n.contains(e)||(l(),s())});a.observe(n,{childList:!0,subtree:!0}),o&&(c=setTimeout(()=>{l(),t(new Error(`Timeout: Element not removed within ${o}ms`))},o));const l=()=>{a.disconnect(),c&&clearTimeout(c)}})},Y=(e,r={})=>{const{attributes:n=!0,childList:o=!0,characterData:s=!1,subtree:t=!1,onMutation:c}=r,a=new MutationObserver(l=>c?.(l));return a.observe(e,{attributes:n,childList:o,characterData:s,subtree:t}),()=>a.disconnect()},Z=(e,r={})=>{const{id:n,nonce:o,target:s=document.head}=r,t=document.createElement("style");return n&&(t.id=n),o&&t.setAttribute("nonce",o),t.textContent=e,s.appendChild(t),{style:t,remove:()=>{t.parentNode?.removeChild(t)}}},y=typeof unsafeWindow<"u"?unsafeWindow:window,q=y.document;function B(e){return e?e instanceof Window?e:e instanceof UIEvent&&e.view?e.view:e instanceof Document?e.defaultView:e instanceof ShadowRoot?e.host.ownerDocument?.defaultView??null:e instanceof Node?e.ownerDocument?.defaultView??null:null:y}const H=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),_=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),U=(e,r)=>{let n=null,o=null;const s=(...t)=>{o=t,n&&clearTimeout(n),n=setTimeout(()=>{e(...o),n=null,o=null},r)};return s.cancel=()=>{n&&clearTimeout(n),n=null,o=null},s.flush=()=>{n&&(e(...o),clearTimeout(n),n=null,o=null)},s},J=(e,r)=>{let n=null,o=null,s=0;const t=()=>{n&&(clearTimeout(n),n=null)},c=(...a)=>{const l=Date.now();if(o=a,s===0)e(...a),s=l;else{const u=l-s;u>=r?(e(...a),s=l,t()):(t(),n=setTimeout(()=>{e(...o),s=Date.now(),n=null},r-u))}};return c.cancel=()=>{t(),s=0,o=null},c.flush=()=>{s>0&&o!==null&&e(...o),t(),s=0,o=null},c},S=e=>new Promise(r=>setTimeout(r,e)),Q=e=>S(e),k=(e,r,n=`Operation timed out after ${r}ms`)=>Promise.race([e,new Promise((o,s)=>setTimeout(()=>s(new Error(n)),r))]),x=()=>{},ee=async(e,r={})=>{const{maxAttempts:n=3,delay:o=1e3,exponential:s=!1}=r;let t;for(let c=1;c<=n;c++)try{return await e()}catch(a){if(t=a instanceof Error?a:new Error(String(a)),c<n){const l=s?o*Math.pow(2,c-1):o;await new Promise(u=>setTimeout(u,l))}}throw t||new Error("Retry failed")};return i.addEventsListener=T,i.camelToKebab=H,i.createElement=C,i.createFragment=L,i.createSVGFromString=R,i.debounce=U,i.delay=S,i.getComputedStyle=O,i.getInnerSize=M,i.getOffset=F,i.getOuterSize=D,i.getRect=j,i.getSize=A,i.getWin=B,i.globalDoc=q,i.globalWin=y,i.injectStyle=Z,i.isMarkedAs=V,i.markFunc=p,i.noop=x,i.onClickOutside=W,i.onEnter=K,i.onEscape=z,i.onKeyPress=b,i.onLongPress=$,i.parseClass=E,i.pascalToKebab=_,i.retry=ee,i.scrollIntoView=I,i.scrollTo=P,i.sleep=Q,i.throttle=J,i.timeout=k,i.waitForElement=N,i.waitForElementRemoval=X,i.watchElementChanges=Y,i.watchElementRemoval=G,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"}),i})({});
1
+ var MKDomUtils=(function(i){"use strict";const E=(...e)=>e.flatMap(r=>typeof r=="string"?r.trim().split(/\s+/).filter(Boolean):r||[]),C=(e,r,...n)=>{const o=document.createElement(e);return r?(Object.entries(r).forEach(([s,t])=>{if(t!=null){if(s==="className"||s==="class"){const c=Array.isArray(t)?E(...t):E(t);o.className=c.join(" ");return}if(s==="style"){typeof t=="string"?o.style.cssText=t:typeof t=="object"&&Object.assign(o.style,t);return}if(s==="dataset"&&typeof t=="object"){Object.assign(o.dataset,t);return}if(s.startsWith("on")){const c=s.slice(2).toLowerCase();typeof t=="function"?o.addEventListener(c,t):Array.isArray(t)&&t.forEach(a=>{typeof a=="function"&&o.addEventListener(c,a)});return}s in o?o[s]=t:o.setAttribute(s,String(t))}}),v(o,n),o):(v(o,n),o)},L=(...e)=>{const r=document.createDocumentFragment();return e.forEach(n=>{typeof n=="string"?r.appendChild(document.createTextNode(n)):r.appendChild(n)}),r},v=(e,r)=>{r.forEach(n=>{typeof n=="string"?e.appendChild(document.createTextNode(n)):e.appendChild(n)})},A=e=>({width:e.offsetWidth,height:e.offsetHeight}),O=e=>({top:e.offsetTop,left:e.offsetLeft}),F=(e,r)=>window.getComputedStyle(e).getPropertyValue(r).trim(),P=(e,r)=>{e&&(e===document.documentElement||e===document.body?window.scrollTo(r):e.scrollTo?.(r))},I=(e,r)=>{e.scrollIntoView?.(r)},M=e=>({width:e.clientWidth,height:e.clientHeight}),D=(e,r=!1)=>{let n=e.offsetWidth,o=e.offsetHeight;if(r){const s=window.getComputedStyle(e),t=parseFloat(s.marginLeft||"0"),c=parseFloat(s.marginRight||"0"),a=parseFloat(s.marginTop||"0"),l=parseFloat(s.marginBottom||"0");n+=t+c,o+=a+l}return{width:n,height:o}},R=e=>e.getBoundingClientRect(),V=e=>{const n=new DOMParser().parseFromString(e,"image/svg+xml");if(n.documentElement.nodeName==="parsererror")throw new Error("Invalid SVG string: Failed to parse SVG");return n.documentElement},T=(e,r)=>(Object.defineProperty(e,r,{value:!0}),e),j=(e,r)=>!!e[r],w=e=>T(e,"__mkEvent"),W=(e,r,n={})=>{const{event:o="mousedown",capture:s=!0,exclude:t=[]}=n,c=w(a=>{const l=a.target;e.contains(l)||t.some(u=>u?.contains(l))||r(a)});return document.addEventListener(o,c,s),()=>document.removeEventListener(o,c,s)},b=(e,r,n={})=>{const{preventDefault:o=!1,stopPropagation:s=!1,event:t="keydown",target:c=document}=n,a=Array.isArray(e)?e:[e],l=w(u=>{a.includes(u.key)&&(o&&u.preventDefault(),s&&u.stopPropagation(),r(u))});return c.addEventListener(t,l),()=>c.removeEventListener(t,l)},z=(e,r={})=>b("Escape",e,r),K=(e,r={})=>b("Enter",e,r),N=(e,r,n={})=>{const{duration:o=500,threshold:s=10}=n;let t=null,c=0,a=0;const l=m=>"touches"in m?m.touches[0]:m,u=w(m=>{const h=l(m);c=h.clientX,a=h.clientY,t=setTimeout(()=>{r(m)},o)}),g=w(m=>{if(!t)return;const h=l(m),ne=Math.abs(h.clientX-c),oe=Math.abs(h.clientY-a);(ne>s||oe>s)&&d()}),d=w(()=>{t&&(clearTimeout(t),t=null)}),f=p(e,{mousedown:u,mousemove:g,mouseup:d,mouseleave:d,touchstart:u,touchmove:g,touchend:d,touchcancel:d});return()=>{f(),t&&clearTimeout(t)}},p=(e,r,n)=>{const o=[];return Object.entries(r).forEach(([s,t])=>{if(!t)return;const c=s;Array.isArray(t)?t.forEach(a=>{e.addEventListener(c,a,n),o.push([c,a])}):(e.addEventListener(c,t,n),o.push([c,t]))}),()=>{o.forEach(([s,t])=>{e.removeEventListener(s,t,n)})}},$=(e,r={})=>{const{timeout:n=5e3,root:o=document.body,interval:s=100}=r;return new Promise((t,c)=>{const a=o.querySelector(e);if(a){t(a);return}const l=new MutationObserver(()=>{const f=o.querySelector(e);f&&(d(),t(f))});l.observe(o,{childList:!0,subtree:!0});const u=setInterval(()=>{const f=o.querySelector(e);f&&(d(),t(f))},s),g=setTimeout(()=>{d(),c(new Error(`Timeout: Element "${e}" not found within ${n}ms`))},n),d=()=>{l.disconnect(),clearTimeout(g),clearInterval(u)}})},G=(e,r={})=>{const{onRemove:n,root:o=document.body}=r,s=new MutationObserver(()=>{o.contains(e)||(n?.(),s.disconnect())});return s.observe(o,{childList:!0,subtree:!0}),()=>s.disconnect()},X=(e,r={})=>{const{root:n=document.body,timeout:o}=r;return new Promise((s,t)=>{if(!n.contains(e)){s();return}let c;const a=new MutationObserver(()=>{n.contains(e)||(l(),s())});a.observe(n,{childList:!0,subtree:!0}),o&&(c=setTimeout(()=>{l(),t(new Error(`Timeout: Element not removed within ${o}ms`))},o));const l=()=>{a.disconnect(),c&&clearTimeout(c)}})},Y=(e,r={})=>{const{attributes:n=!0,childList:o=!0,characterData:s=!1,subtree:t=!1,onMutation:c}=r,a=new MutationObserver(l=>c?.(l));return a.observe(e,{attributes:n,childList:o,characterData:s,subtree:t}),()=>a.disconnect()},Z=(e,r={})=>{const{id:n,nonce:o,target:s=document.head}=r,t=document.createElement("style");return n&&(t.id=n),o&&t.setAttribute("nonce",o),t.textContent=e,s.appendChild(t),{style:t,remove:()=>{t.parentNode?.removeChild(t)}}},y=typeof unsafeWindow<"u"?unsafeWindow:window,q=y.document;function B(e){return e?e instanceof Window?e:e instanceof UIEvent&&e.view?e.view:e instanceof Document?e.defaultView:e instanceof ShadowRoot?e.host.ownerDocument?.defaultView??null:e instanceof Node?e.ownerDocument?.defaultView??null:null:y}const H=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),_=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),U=(e,r)=>{let n=null,o=null;const s=(...t)=>{o=t,n&&clearTimeout(n),n=setTimeout(()=>{e(...o),n=null,o=null},r)};return s.cancel=()=>{n&&clearTimeout(n),n=null,o=null},s.flush=()=>{n&&(e(...o),clearTimeout(n),n=null,o=null)},s},J=(e,r)=>{let n=null,o=null,s=0;const t=()=>{n&&(clearTimeout(n),n=null)},c=(...a)=>{const l=Date.now();if(o=a,s===0)e(...a),s=l;else{const u=l-s;u>=r?(e(...a),s=l,t()):(t(),n=setTimeout(()=>{e(...o),s=Date.now(),n=null},r-u))}};return c.cancel=()=>{t(),s=0,o=null},c.flush=()=>{s>0&&o!==null&&e(...o),t(),s=0,o=null},c},S=e=>new Promise(r=>setTimeout(r,e)),Q=e=>S(e),k=(e,r,n=`Operation timed out after ${r}ms`)=>Promise.race([e,new Promise((o,s)=>setTimeout(()=>s(new Error(n)),r))]),x=()=>{},ee=async(e,r={})=>{const{maxAttempts:n=3,delay:o=1e3,exponential:s=!1}=r;let t;for(let c=1;c<=n;c++)try{return await e()}catch(a){if(t=a instanceof Error?a:new Error(String(a)),c<n){const l=s?o*Math.pow(2,c-1):o;await new Promise(u=>setTimeout(u,l))}}throw t||new Error("Retry failed")},te="0.0.1";return i.VERSION=te,i.addEventsListener=p,i.camelToKebab=H,i.createElement=C,i.createFragment=L,i.createSVGFromString=V,i.debounce=U,i.delay=S,i.getComputedStyle=F,i.getInnerSize=M,i.getOffset=O,i.getOuterSize=D,i.getRect=R,i.getSize=A,i.getWin=B,i.globalDoc=q,i.globalWin=y,i.injectStyle=Z,i.isMarkedAs=j,i.markFunc=T,i.noop=x,i.onClickOutside=W,i.onEnter=K,i.onEscape=z,i.onKeyPress=b,i.onLongPress=N,i.parseClass=E,i.pascalToKebab=_,i.retry=ee,i.scrollIntoView=I,i.scrollTo=P,i.sleep=Q,i.throttle=J,i.timeout=k,i.waitForElement=$,i.waitForElementRemoval=X,i.watchElementChanges=Y,i.watchElementRemoval=G,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"}),i})({});
@@ -1,4 +1,4 @@
1
- const h = (...t) => t.flatMap((r) => typeof r == "string" ? r.trim().split(/\s+/).filter(Boolean) : r || []), A = (t, r, ...n) => {
1
+ const h = (...t) => t.flatMap((r) => typeof r == "string" ? r.trim().split(/\s+/).filter(Boolean) : r || []), S = (t, r, ...n) => {
2
2
  const o = document.createElement(t);
3
3
  return r ? (Object.entries(r).forEach(([s, e]) => {
4
4
  if (e != null) {
@@ -25,7 +25,7 @@ const h = (...t) => t.flatMap((r) => typeof r == "string" ? r.trim().split(/\s+/
25
25
  s in o ? o[s] = e : o.setAttribute(s, String(e));
26
26
  }
27
27
  }), E(o, n), o) : (E(o, n), o);
28
- }, S = (...t) => {
28
+ }, A = (...t) => {
29
29
  const r = document.createDocumentFragment();
30
30
  return t.forEach((n) => {
31
31
  typeof n == "string" ? r.appendChild(document.createTextNode(n)) : r.appendChild(n);
@@ -34,13 +34,13 @@ const h = (...t) => t.flatMap((r) => typeof r == "string" ? r.trim().split(/\s+/
34
34
  r.forEach((n) => {
35
35
  typeof n == "string" ? t.appendChild(document.createTextNode(n)) : t.appendChild(n);
36
36
  });
37
- }, P = (t) => ({
37
+ }, O = (t) => ({
38
38
  width: t.offsetWidth,
39
39
  height: t.offsetHeight
40
- }), O = (t) => ({
40
+ }), P = (t) => ({
41
41
  top: t.offsetTop,
42
42
  left: t.offsetLeft
43
- }), F = (t, r) => window.getComputedStyle(t).getPropertyValue(r).trim(), I = (t, r) => {
43
+ }), I = (t, r) => window.getComputedStyle(t).getPropertyValue(r).trim(), F = (t, r) => {
44
44
  t && (t === document.documentElement || t === document.body ? window.scrollTo(r) : t.scrollTo?.(r));
45
45
  }, M = (t, r) => {
46
46
  t.scrollIntoView?.(r);
@@ -54,12 +54,12 @@ const h = (...t) => t.flatMap((r) => typeof r == "string" ? r.trim().split(/\s+/
54
54
  n += e + i, o += c + u;
55
55
  }
56
56
  return { width: n, height: o };
57
- }, j = (t) => t.getBoundingClientRect(), V = (t) => {
57
+ }, V = (t) => t.getBoundingClientRect(), j = (t) => {
58
58
  const n = new DOMParser().parseFromString(t, "image/svg+xml");
59
59
  if (n.documentElement.nodeName === "parsererror")
60
60
  throw new Error("Invalid SVG string: Failed to parse SVG");
61
61
  return n.documentElement;
62
- }, T = (t, r) => (Object.defineProperty(t, r, { value: !0 }), t), W = (t, r) => !!t[r], p = (t) => T(t, "__mkEvent"), R = (t, r, n = {}) => {
62
+ }, T = (t, r) => (Object.defineProperty(t, r, { value: !0 }), t), R = (t, r) => !!t[r], p = (t) => T(t, "__mkEvent"), W = (t, r, n = {}) => {
63
63
  const { event: o = "mousedown", capture: s = !0, exclude: e = [] } = n, i = p((c) => {
64
64
  const u = c.target;
65
65
  t.contains(u) || e.some((a) => a?.contains(u)) || r(c);
@@ -241,29 +241,30 @@ const H = (t) => t.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase(
241
241
  }
242
242
  }
243
243
  throw e || new Error("Retry failed");
244
- };
244
+ }, nt = "0.0.1";
245
245
  export {
246
+ nt as VERSION,
246
247
  L as addEventsListener,
247
248
  H as camelToKebab,
248
- A as createElement,
249
- S as createFragment,
250
- V as createSVGFromString,
249
+ S as createElement,
250
+ A as createFragment,
251
+ j as createSVGFromString,
251
252
  U as debounce,
252
253
  C as delay,
253
- F as getComputedStyle,
254
+ I as getComputedStyle,
254
255
  D as getInnerSize,
255
- O as getOffset,
256
+ P as getOffset,
256
257
  x as getOuterSize,
257
- j as getRect,
258
- P as getSize,
258
+ V as getRect,
259
+ O as getSize,
259
260
  G as getWin,
260
261
  B as globalDoc,
261
262
  y as globalWin,
262
263
  q as injectStyle,
263
- W as isMarkedAs,
264
+ R as isMarkedAs,
264
265
  T as markFunc,
265
266
  tt as noop,
266
- R as onClickOutside,
267
+ W as onClickOutside,
267
268
  N as onEnter,
268
269
  $ as onEscape,
269
270
  g as onKeyPress,
@@ -272,7 +273,7 @@ export {
272
273
  _ as pascalToKebab,
273
274
  et as retry,
274
275
  M as scrollIntoView,
275
- I as scrollTo,
276
+ F as scrollTo,
276
277
  Q as sleep,
277
278
  J as throttle,
278
279
  k as timeout,
@@ -1 +1 @@
1
- (function(i,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(i=typeof globalThis<"u"?globalThis:i||self,m(i.MKDomUtils={}))})(this,(function(i){"use strict";const m=(...e)=>e.flatMap(r=>typeof r=="string"?r.trim().split(/\s+/).filter(Boolean):r||[]),C=(e,r,...n)=>{const o=document.createElement(e);return r?(Object.entries(r).forEach(([s,t])=>{if(t!=null){if(s==="className"||s==="class"){const c=Array.isArray(t)?m(...t):m(t);o.className=c.join(" ");return}if(s==="style"){typeof t=="string"?o.style.cssText=t:typeof t=="object"&&Object.assign(o.style,t);return}if(s==="dataset"&&typeof t=="object"){Object.assign(o.dataset,t);return}if(s.startsWith("on")){const c=s.slice(2).toLowerCase();typeof t=="function"?o.addEventListener(c,t):Array.isArray(t)&&t.forEach(a=>{typeof a=="function"&&o.addEventListener(c,a)});return}s in o?o[s]=t:o.setAttribute(s,String(t))}}),p(o,n),o):(p(o,n),o)},L=(...e)=>{const r=document.createDocumentFragment();return e.forEach(n=>{typeof n=="string"?r.appendChild(document.createTextNode(n)):r.appendChild(n)}),r},p=(e,r)=>{r.forEach(n=>{typeof n=="string"?e.appendChild(document.createTextNode(n)):e.appendChild(n)})},A=e=>({width:e.offsetWidth,height:e.offsetHeight}),F=e=>({top:e.offsetTop,left:e.offsetLeft}),O=(e,r)=>window.getComputedStyle(e).getPropertyValue(r).trim(),P=(e,r)=>{e&&(e===document.documentElement||e===document.body?window.scrollTo(r):e.scrollTo?.(r))},I=(e,r)=>{e.scrollIntoView?.(r)},M=e=>({width:e.clientWidth,height:e.clientHeight}),D=(e,r=!1)=>{let n=e.offsetWidth,o=e.offsetHeight;if(r){const s=window.getComputedStyle(e),t=parseFloat(s.marginLeft||"0"),c=parseFloat(s.marginRight||"0"),a=parseFloat(s.marginTop||"0"),u=parseFloat(s.marginBottom||"0");n+=t+c,o+=a+u}return{width:n,height:o}},j=e=>e.getBoundingClientRect(),R=e=>{const n=new DOMParser().parseFromString(e,"image/svg+xml");if(n.documentElement.nodeName==="parsererror")throw new Error("Invalid SVG string: Failed to parse SVG");return n.documentElement},v=(e,r)=>(Object.defineProperty(e,r,{value:!0}),e),V=(e,r)=>!!e[r],w=e=>v(e,"__mkEvent"),W=(e,r,n={})=>{const{event:o="mousedown",capture:s=!0,exclude:t=[]}=n,c=w(a=>{const u=a.target;e.contains(u)||t.some(l=>l?.contains(u))||r(a)});return document.addEventListener(o,c,s),()=>document.removeEventListener(o,c,s)},y=(e,r,n={})=>{const{preventDefault:o=!1,stopPropagation:s=!1,event:t="keydown",target:c=document}=n,a=Array.isArray(e)?e:[e],u=w(l=>{a.includes(l.key)&&(o&&l.preventDefault(),s&&l.stopPropagation(),r(l))});return c.addEventListener(t,u),()=>c.removeEventListener(t,u)},z=(e,r={})=>y("Escape",e,r),K=(e,r={})=>y("Enter",e,r),$=(e,r,n={})=>{const{duration:o=500,threshold:s=10}=n;let t=null,c=0,a=0;const u=f=>"touches"in f?f.touches[0]:f,l=w(f=>{const g=u(f);c=g.clientX,a=g.clientY,t=setTimeout(()=>{r(f)},o)}),E=w(f=>{if(!t)return;const g=u(f),te=Math.abs(g.clientX-c),ne=Math.abs(g.clientY-a);(te>s||ne>s)&&d()}),d=w(()=>{t&&(clearTimeout(t),t=null)}),h=T(e,{mousedown:l,mousemove:E,mouseup:d,mouseleave:d,touchstart:l,touchmove:E,touchend:d,touchcancel:d});return()=>{h(),t&&clearTimeout(t)}},T=(e,r,n)=>{const o=[];return Object.entries(r).forEach(([s,t])=>{if(!t)return;const c=s;Array.isArray(t)?t.forEach(a=>{e.addEventListener(c,a,n),o.push([c,a])}):(e.addEventListener(c,t,n),o.push([c,t]))}),()=>{o.forEach(([s,t])=>{e.removeEventListener(s,t,n)})}},N=(e,r={})=>{const{timeout:n=5e3,root:o=document.body,interval:s=100}=r;return new Promise((t,c)=>{const a=o.querySelector(e);if(a){t(a);return}const u=new MutationObserver(()=>{const h=o.querySelector(e);h&&(d(),t(h))});u.observe(o,{childList:!0,subtree:!0});const l=setInterval(()=>{const h=o.querySelector(e);h&&(d(),t(h))},s),E=setTimeout(()=>{d(),c(new Error(`Timeout: Element "${e}" not found within ${n}ms`))},n),d=()=>{u.disconnect(),clearTimeout(E),clearInterval(l)}})},G=(e,r={})=>{const{onRemove:n,root:o=document.body}=r,s=new MutationObserver(()=>{o.contains(e)||(n?.(),s.disconnect())});return s.observe(o,{childList:!0,subtree:!0}),()=>s.disconnect()},X=(e,r={})=>{const{root:n=document.body,timeout:o}=r;return new Promise((s,t)=>{if(!n.contains(e)){s();return}let c;const a=new MutationObserver(()=>{n.contains(e)||(u(),s())});a.observe(n,{childList:!0,subtree:!0}),o&&(c=setTimeout(()=>{u(),t(new Error(`Timeout: Element not removed within ${o}ms`))},o));const u=()=>{a.disconnect(),c&&clearTimeout(c)}})},Y=(e,r={})=>{const{attributes:n=!0,childList:o=!0,characterData:s=!1,subtree:t=!1,onMutation:c}=r,a=new MutationObserver(u=>c?.(u));return a.observe(e,{attributes:n,childList:o,characterData:s,subtree:t}),()=>a.disconnect()},Z=(e,r={})=>{const{id:n,nonce:o,target:s=document.head}=r,t=document.createElement("style");return n&&(t.id=n),o&&t.setAttribute("nonce",o),t.textContent=e,s.appendChild(t),{style:t,remove:()=>{t.parentNode?.removeChild(t)}}},b=typeof unsafeWindow<"u"?unsafeWindow:window,q=b.document;function B(e){return e?e instanceof Window?e:e instanceof UIEvent&&e.view?e.view:e instanceof Document?e.defaultView:e instanceof ShadowRoot?e.host.ownerDocument?.defaultView??null:e instanceof Node?e.ownerDocument?.defaultView??null:null:b}const H=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),_=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),U=(e,r)=>{let n=null,o=null;const s=(...t)=>{o=t,n&&clearTimeout(n),n=setTimeout(()=>{e(...o),n=null,o=null},r)};return s.cancel=()=>{n&&clearTimeout(n),n=null,o=null},s.flush=()=>{n&&(e(...o),clearTimeout(n),n=null,o=null)},s},J=(e,r)=>{let n=null,o=null,s=0;const t=()=>{n&&(clearTimeout(n),n=null)},c=(...a)=>{const u=Date.now();if(o=a,s===0)e(...a),s=u;else{const l=u-s;l>=r?(e(...a),s=u,t()):(t(),n=setTimeout(()=>{e(...o),s=Date.now(),n=null},r-l))}};return c.cancel=()=>{t(),s=0,o=null},c.flush=()=>{s>0&&o!==null&&e(...o),t(),s=0,o=null},c},S=e=>new Promise(r=>setTimeout(r,e)),Q=e=>S(e),k=(e,r,n=`Operation timed out after ${r}ms`)=>Promise.race([e,new Promise((o,s)=>setTimeout(()=>s(new Error(n)),r))]),x=()=>{},ee=async(e,r={})=>{const{maxAttempts:n=3,delay:o=1e3,exponential:s=!1}=r;let t;for(let c=1;c<=n;c++)try{return await e()}catch(a){if(t=a instanceof Error?a:new Error(String(a)),c<n){const u=s?o*Math.pow(2,c-1):o;await new Promise(l=>setTimeout(l,u))}}throw t||new Error("Retry failed")};i.addEventsListener=T,i.camelToKebab=H,i.createElement=C,i.createFragment=L,i.createSVGFromString=R,i.debounce=U,i.delay=S,i.getComputedStyle=O,i.getInnerSize=M,i.getOffset=F,i.getOuterSize=D,i.getRect=j,i.getSize=A,i.getWin=B,i.globalDoc=q,i.globalWin=b,i.injectStyle=Z,i.isMarkedAs=V,i.markFunc=v,i.noop=x,i.onClickOutside=W,i.onEnter=K,i.onEscape=z,i.onKeyPress=y,i.onLongPress=$,i.parseClass=m,i.pascalToKebab=_,i.retry=ee,i.scrollIntoView=I,i.scrollTo=P,i.sleep=Q,i.throttle=J,i.timeout=k,i.waitForElement=N,i.waitForElementRemoval=X,i.watchElementChanges=Y,i.watchElementRemoval=G,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(i,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(i=typeof globalThis<"u"?globalThis:i||self,m(i.MKDomUtils={}))})(this,(function(i){"use strict";const m=(...e)=>e.flatMap(r=>typeof r=="string"?r.trim().split(/\s+/).filter(Boolean):r||[]),C=(e,r,...n)=>{const o=document.createElement(e);return r?(Object.entries(r).forEach(([s,t])=>{if(t!=null){if(s==="className"||s==="class"){const c=Array.isArray(t)?m(...t):m(t);o.className=c.join(" ");return}if(s==="style"){typeof t=="string"?o.style.cssText=t:typeof t=="object"&&Object.assign(o.style,t);return}if(s==="dataset"&&typeof t=="object"){Object.assign(o.dataset,t);return}if(s.startsWith("on")){const c=s.slice(2).toLowerCase();typeof t=="function"?o.addEventListener(c,t):Array.isArray(t)&&t.forEach(a=>{typeof a=="function"&&o.addEventListener(c,a)});return}s in o?o[s]=t:o.setAttribute(s,String(t))}}),p(o,n),o):(p(o,n),o)},L=(...e)=>{const r=document.createDocumentFragment();return e.forEach(n=>{typeof n=="string"?r.appendChild(document.createTextNode(n)):r.appendChild(n)}),r},p=(e,r)=>{r.forEach(n=>{typeof n=="string"?e.appendChild(document.createTextNode(n)):e.appendChild(n)})},A=e=>({width:e.offsetWidth,height:e.offsetHeight}),O=e=>({top:e.offsetTop,left:e.offsetLeft}),F=(e,r)=>window.getComputedStyle(e).getPropertyValue(r).trim(),P=(e,r)=>{e&&(e===document.documentElement||e===document.body?window.scrollTo(r):e.scrollTo?.(r))},I=(e,r)=>{e.scrollIntoView?.(r)},M=e=>({width:e.clientWidth,height:e.clientHeight}),D=(e,r=!1)=>{let n=e.offsetWidth,o=e.offsetHeight;if(r){const s=window.getComputedStyle(e),t=parseFloat(s.marginLeft||"0"),c=parseFloat(s.marginRight||"0"),a=parseFloat(s.marginTop||"0"),u=parseFloat(s.marginBottom||"0");n+=t+c,o+=a+u}return{width:n,height:o}},R=e=>e.getBoundingClientRect(),V=e=>{const n=new DOMParser().parseFromString(e,"image/svg+xml");if(n.documentElement.nodeName==="parsererror")throw new Error("Invalid SVG string: Failed to parse SVG");return n.documentElement},v=(e,r)=>(Object.defineProperty(e,r,{value:!0}),e),j=(e,r)=>!!e[r],w=e=>v(e,"__mkEvent"),W=(e,r,n={})=>{const{event:o="mousedown",capture:s=!0,exclude:t=[]}=n,c=w(a=>{const u=a.target;e.contains(u)||t.some(l=>l?.contains(u))||r(a)});return document.addEventListener(o,c,s),()=>document.removeEventListener(o,c,s)},y=(e,r,n={})=>{const{preventDefault:o=!1,stopPropagation:s=!1,event:t="keydown",target:c=document}=n,a=Array.isArray(e)?e:[e],u=w(l=>{a.includes(l.key)&&(o&&l.preventDefault(),s&&l.stopPropagation(),r(l))});return c.addEventListener(t,u),()=>c.removeEventListener(t,u)},z=(e,r={})=>y("Escape",e,r),K=(e,r={})=>y("Enter",e,r),N=(e,r,n={})=>{const{duration:o=500,threshold:s=10}=n;let t=null,c=0,a=0;const u=f=>"touches"in f?f.touches[0]:f,l=w(f=>{const g=u(f);c=g.clientX,a=g.clientY,t=setTimeout(()=>{r(f)},o)}),E=w(f=>{if(!t)return;const g=u(f),ne=Math.abs(g.clientX-c),oe=Math.abs(g.clientY-a);(ne>s||oe>s)&&d()}),d=w(()=>{t&&(clearTimeout(t),t=null)}),h=T(e,{mousedown:l,mousemove:E,mouseup:d,mouseleave:d,touchstart:l,touchmove:E,touchend:d,touchcancel:d});return()=>{h(),t&&clearTimeout(t)}},T=(e,r,n)=>{const o=[];return Object.entries(r).forEach(([s,t])=>{if(!t)return;const c=s;Array.isArray(t)?t.forEach(a=>{e.addEventListener(c,a,n),o.push([c,a])}):(e.addEventListener(c,t,n),o.push([c,t]))}),()=>{o.forEach(([s,t])=>{e.removeEventListener(s,t,n)})}},$=(e,r={})=>{const{timeout:n=5e3,root:o=document.body,interval:s=100}=r;return new Promise((t,c)=>{const a=o.querySelector(e);if(a){t(a);return}const u=new MutationObserver(()=>{const h=o.querySelector(e);h&&(d(),t(h))});u.observe(o,{childList:!0,subtree:!0});const l=setInterval(()=>{const h=o.querySelector(e);h&&(d(),t(h))},s),E=setTimeout(()=>{d(),c(new Error(`Timeout: Element "${e}" not found within ${n}ms`))},n),d=()=>{u.disconnect(),clearTimeout(E),clearInterval(l)}})},G=(e,r={})=>{const{onRemove:n,root:o=document.body}=r,s=new MutationObserver(()=>{o.contains(e)||(n?.(),s.disconnect())});return s.observe(o,{childList:!0,subtree:!0}),()=>s.disconnect()},X=(e,r={})=>{const{root:n=document.body,timeout:o}=r;return new Promise((s,t)=>{if(!n.contains(e)){s();return}let c;const a=new MutationObserver(()=>{n.contains(e)||(u(),s())});a.observe(n,{childList:!0,subtree:!0}),o&&(c=setTimeout(()=>{u(),t(new Error(`Timeout: Element not removed within ${o}ms`))},o));const u=()=>{a.disconnect(),c&&clearTimeout(c)}})},Y=(e,r={})=>{const{attributes:n=!0,childList:o=!0,characterData:s=!1,subtree:t=!1,onMutation:c}=r,a=new MutationObserver(u=>c?.(u));return a.observe(e,{attributes:n,childList:o,characterData:s,subtree:t}),()=>a.disconnect()},Z=(e,r={})=>{const{id:n,nonce:o,target:s=document.head}=r,t=document.createElement("style");return n&&(t.id=n),o&&t.setAttribute("nonce",o),t.textContent=e,s.appendChild(t),{style:t,remove:()=>{t.parentNode?.removeChild(t)}}},b=typeof unsafeWindow<"u"?unsafeWindow:window,q=b.document;function B(e){return e?e instanceof Window?e:e instanceof UIEvent&&e.view?e.view:e instanceof Document?e.defaultView:e instanceof ShadowRoot?e.host.ownerDocument?.defaultView??null:e instanceof Node?e.ownerDocument?.defaultView??null:null:b}const H=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),_=e=>e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),U=(e,r)=>{let n=null,o=null;const s=(...t)=>{o=t,n&&clearTimeout(n),n=setTimeout(()=>{e(...o),n=null,o=null},r)};return s.cancel=()=>{n&&clearTimeout(n),n=null,o=null},s.flush=()=>{n&&(e(...o),clearTimeout(n),n=null,o=null)},s},J=(e,r)=>{let n=null,o=null,s=0;const t=()=>{n&&(clearTimeout(n),n=null)},c=(...a)=>{const u=Date.now();if(o=a,s===0)e(...a),s=u;else{const l=u-s;l>=r?(e(...a),s=u,t()):(t(),n=setTimeout(()=>{e(...o),s=Date.now(),n=null},r-l))}};return c.cancel=()=>{t(),s=0,o=null},c.flush=()=>{s>0&&o!==null&&e(...o),t(),s=0,o=null},c},S=e=>new Promise(r=>setTimeout(r,e)),Q=e=>S(e),k=(e,r,n=`Operation timed out after ${r}ms`)=>Promise.race([e,new Promise((o,s)=>setTimeout(()=>s(new Error(n)),r))]),x=()=>{},ee=async(e,r={})=>{const{maxAttempts:n=3,delay:o=1e3,exponential:s=!1}=r;let t;for(let c=1;c<=n;c++)try{return await e()}catch(a){if(t=a instanceof Error?a:new Error(String(a)),c<n){const u=s?o*Math.pow(2,c-1):o;await new Promise(l=>setTimeout(l,u))}}throw t||new Error("Retry failed")},te="0.0.1";i.VERSION=te,i.addEventsListener=T,i.camelToKebab=H,i.createElement=C,i.createFragment=L,i.createSVGFromString=V,i.debounce=U,i.delay=S,i.getComputedStyle=F,i.getInnerSize=M,i.getOffset=O,i.getOuterSize=D,i.getRect=R,i.getSize=A,i.getWin=B,i.globalDoc=q,i.globalWin=b,i.injectStyle=Z,i.isMarkedAs=j,i.markFunc=v,i.noop=x,i.onClickOutside=W,i.onEnter=K,i.onEscape=z,i.onKeyPress=y,i.onLongPress=N,i.parseClass=m,i.pascalToKebab=_,i.retry=ee,i.scrollIntoView=I,i.scrollTo=P,i.sleep=Q,i.throttle=J,i.timeout=k,i.waitForElement=$,i.waitForElementRemoval=X,i.watchElementChanges=Y,i.watchElementRemoval=G,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,12 +1,26 @@
1
1
  {
2
2
  "name": "@mkmonkeycat/dom-utils",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "A TypeScript utility library for DOM manipulation and common web development tasks.",
5
+ "homepage": "https://github.com/MKMonkeyCat/dom-utils#readme",
5
6
  "type": "module",
6
7
  "main": "dist/index.cjs",
7
8
  "module": "dist/index.js",
8
9
  "types": "dist/index.d.ts",
9
- "repository": "https://github.com/MKMonkeyCat/dom-utils.git",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/MKMonkeyCat/dom-utils.git"
23
+ },
10
24
  "bugs": {
11
25
  "url": "https://github.com/MKMonkeyCat/dom-utils/issues"
12
26
  },
package/.eslintignore DELETED
@@ -1,2 +0,0 @@
1
- dist/
2
- node_modules/
@@ -1,39 +0,0 @@
1
- name: Release
2
- on:
3
- push:
4
- branches:
5
- - main
6
-
7
- permissions:
8
- contents: read
9
-
10
- jobs:
11
- release:
12
- name: Release
13
- runs-on: ubuntu-latest
14
- permissions:
15
- contents: write
16
- issues: write
17
- pull-requests: write
18
- id-token: write
19
- steps:
20
- - name: Checkout
21
- uses: actions/checkout@v6
22
- with:
23
- fetch-depth: 0
24
-
25
- - name: Setup Node.js
26
- uses: actions/setup-node@v6
27
- with:
28
- node-version: lts/*
29
-
30
- - name: Install dependencies
31
- run: npm clean-install
32
-
33
- - name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
34
- run: npm audit signatures
35
-
36
- - name: Release
37
- env:
38
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39
- run: npx semantic-release
package/.prettierignore DELETED
@@ -1 +0,0 @@
1
- node_modules/
package/.prettierrc.yml DELETED
@@ -1,9 +0,0 @@
1
- semi: true
2
- singleQuote: true
3
- trailingComma: all
4
- printWidth: 100
5
- tabWidth: 2
6
- bracketSpacing: true
7
- arrowParens: always
8
- endOfLine: auto
9
- quoteProps: as-needed
package/eslint.config.mjs DELETED
@@ -1,33 +0,0 @@
1
- import { defineConfig } from 'eslint/config';
2
- import prettier from 'eslint-plugin-prettier';
3
- import simpleImportSort from 'eslint-plugin-simple-import-sort';
4
- import tseslint from 'typescript-eslint';
5
-
6
- export default defineConfig([
7
- { ignores: ['dist/**', 'node_modules/**', 'example-dist/**'] },
8
- ...tseslint.configs.recommended,
9
- {
10
- name: 'globals-and-rules',
11
- languageOptions: {
12
- ecmaVersion: 2024,
13
- sourceType: 'module',
14
- globals: {
15
- window: 'readonly',
16
- document: 'readonly',
17
- navigator: 'readonly',
18
- },
19
- },
20
- plugins: {
21
- 'simple-import-sort': simpleImportSort,
22
- prettier,
23
- },
24
- rules: {
25
- '@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
26
- 'simple-import-sort/imports': 'warn',
27
- 'simple-import-sort/exports': 'warn',
28
- 'prettier/prettier': ['warn', { endOfLine: 'auto' }],
29
- 'no-console': 'warn',
30
- 'no-debugger': 'warn',
31
- },
32
- },
33
- ]);
package/index.html DELETED
@@ -1,13 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
- <title>tt</title>
8
- </head>
9
- <body>
10
- <div id="app"></div>
11
- <script type="module" src="/src/main.ts"></script>
12
- </body>
13
- </html>
@@ -1,252 +0,0 @@
1
- /**
2
- * Parses class names from various input formats into a flat array of class names.
3
- * @param classNames - Class names as strings, arrays of strings, or undefined.
4
- * @returns An array of individual class names.
5
- */
6
- export const parseClass = (...classNames: (string | string[] | undefined)[]): string[] => {
7
- return classNames.flatMap((className) => {
8
- if (typeof className === 'string') {
9
- return className.trim().split(/\s+/).filter(Boolean);
10
- }
11
- return className || [];
12
- });
13
- };
14
-
15
- /**
16
- * Creates an HTML element with specified attributes and children.
17
- * @param tag - The tag name of the HTML element to create.
18
- * @param attributes - An object containing attributes, styles, event handlers, and dataset entries to set on the element.
19
- * @param children - Child nodes or strings to append to the created element.
20
- * @returns The created HTML element.
21
- */
22
- export const createElement = <K extends keyof HTMLElementTagNameMap>(
23
- tag: K,
24
- attributes?: ElementAttributes<HTMLElementTagNameMap[K]>,
25
- ...children: (Node | string)[]
26
- ): HTMLElementTagNameMap[K] => {
27
- const element = document.createElement(tag);
28
-
29
- if (!attributes) {
30
- addChildren(element, children);
31
- return element;
32
- }
33
-
34
- Object.entries(attributes).forEach(([key, value]) => {
35
- if (value == null) return;
36
-
37
- if (key === 'className' || key === 'class') {
38
- const classes = Array.isArray(value) ? parseClass(...value) : parseClass(value as string);
39
- element.className = classes.join(' ');
40
- return;
41
- }
42
-
43
- if (key === 'style') {
44
- if (typeof value === 'string') {
45
- element.style.cssText = value;
46
- } else if (typeof value === 'object') {
47
- Object.assign(element.style, value);
48
- }
49
- return;
50
- }
51
-
52
- if (key === 'dataset' && typeof value === 'object') {
53
- Object.assign(element.dataset, value as Record<string, string>);
54
- return;
55
- }
56
-
57
- if (key.startsWith('on')) {
58
- const eventName = key.slice(2).toLowerCase();
59
- if (typeof value === 'function') {
60
- element.addEventListener(eventName, value as EventListener);
61
- } else if (Array.isArray(value)) {
62
- value.forEach((handler) => {
63
- if (typeof handler === 'function') {
64
- element.addEventListener(eventName, handler);
65
- }
66
- });
67
- }
68
- return;
69
- }
70
-
71
- if (key in element) {
72
- (element as Record<string, unknown>)[key] = value;
73
- } else {
74
- element.setAttribute(key, String(value));
75
- }
76
- });
77
-
78
- addChildren(element, children);
79
- return element;
80
- };
81
-
82
- /**
83
- * Creates a DocumentFragment for batch DOM operations.
84
- * @param children - Child nodes or strings to add to the fragment.
85
- * @returns A DocumentFragment containing the children.
86
- */
87
- export const createFragment = (...children: (Node | string)[]): DocumentFragment => {
88
- const frag = document.createDocumentFragment();
89
- children.forEach((child) => {
90
- if (typeof child === 'string') {
91
- frag.appendChild(document.createTextNode(child));
92
- } else {
93
- frag.appendChild(child);
94
- }
95
- });
96
- return frag;
97
- };
98
-
99
- const addChildren = (element: HTMLElement, children: (Node | string)[]) => {
100
- children.forEach((child) => {
101
- if (typeof child === 'string') {
102
- element.appendChild(document.createTextNode(child));
103
- } else {
104
- element.appendChild(child);
105
- }
106
- });
107
- };
108
-
109
- export type CSSProperties = Partial<CSSStyleDeclaration> | string;
110
-
111
- export type EventHandlers = {
112
- [K in keyof HTMLElementEventMap as `on${Capitalize<K>}`]?: (
113
- event: HTMLElementEventMap[K],
114
- ) => void;
115
- };
116
-
117
- export type ElementAttributes<T extends HTMLElement = HTMLElement> = {
118
- className?: string | string[];
119
- class?: string | string[];
120
- style?: CSSProperties;
121
- dataset?: Record<string, string>;
122
- } & EventHandlers &
123
- Partial<Omit<T, 'style' | 'children' | 'className' | 'classList' | 'dataset'>> & {
124
- [key: string]: unknown;
125
- };
126
-
127
- /**
128
- * Gets the width and height of an element (including padding and border).
129
- * @param element - The element to measure.
130
- * @returns An object with width and height properties.
131
- */
132
- export const getSize = (element: HTMLElement): { width: number; height: number } => {
133
- return {
134
- width: element.offsetWidth,
135
- height: element.offsetHeight,
136
- };
137
- };
138
-
139
- /**
140
- * Gets the offset position of an element relative to the document.
141
- * @param element - The element to measure.
142
- * @returns An object with top and left properties.
143
- */
144
- export const getOffset = (element: HTMLElement): { top: number; left: number } => {
145
- return {
146
- top: element.offsetTop,
147
- left: element.offsetLeft,
148
- };
149
- };
150
-
151
- /**
152
- * Gets a computed style value for an element.
153
- * @param element - The element to query.
154
- * @param property - The CSS property name.
155
- * @returns The computed style value.
156
- */
157
- export const getComputedStyle = (element: HTMLElement, property: string): string => {
158
- return window.getComputedStyle(element).getPropertyValue(property).trim();
159
- };
160
-
161
- /**
162
- * Scrolls the element to a specific position (or to view if no position specified).
163
- * @param element - The element to scroll.
164
- * @param options - Scroll behavior options.
165
- */
166
- export const scrollTo = (
167
- element: HTMLElement,
168
- options?: ScrollToOptions | { top?: number; left?: number },
169
- ): void => {
170
- if (!element) return;
171
- if (element === document.documentElement || element === document.body) {
172
- window.scrollTo(options as ScrollToOptions);
173
- } else {
174
- element.scrollTo?.(options as ScrollToOptions);
175
- }
176
- };
177
-
178
- /**
179
- * Scrolls the element into the viewport.
180
- * @param element - The element to scroll into view.
181
- * @param options - Scroll behavior options.
182
- */
183
- export const scrollIntoView = (
184
- element: HTMLElement,
185
- options?: ScrollIntoViewOptions | boolean,
186
- ): void => {
187
- element.scrollIntoView?.(options);
188
- };
189
-
190
- /**
191
- * Gets the inner size of an element (client width/height: includes padding, excludes border and scrollbar).
192
- * @param element - The element to measure.
193
- * @returns An object with width and height properties.
194
- */
195
- export const getInnerSize = (element: HTMLElement): { width: number; height: number } => {
196
- return {
197
- width: element.clientWidth,
198
- height: element.clientHeight,
199
- };
200
- };
201
-
202
- /**
203
- * Gets the outer size of an element (offset size). Optionally includes margins.
204
- * @param element - The element to measure.
205
- * @param includeMargin - Whether to include margins in the calculation.
206
- * @returns An object with width and height properties.
207
- */
208
- export const getOuterSize = (
209
- element: HTMLElement,
210
- includeMargin = false,
211
- ): { width: number; height: number } => {
212
- let width = element.offsetWidth;
213
- let height = element.offsetHeight;
214
-
215
- if (includeMargin) {
216
- const styles = window.getComputedStyle(element);
217
- const ml = parseFloat(styles.marginLeft || '0');
218
- const mr = parseFloat(styles.marginRight || '0');
219
- const mt = parseFloat(styles.marginTop || '0');
220
- const mb = parseFloat(styles.marginBottom || '0');
221
- width += ml + mr;
222
- height += mt + mb;
223
- }
224
-
225
- return { width, height };
226
- };
227
-
228
- /**
229
- * Gets the bounding client rect of an element.
230
- * @param element - The element to measure.
231
- * @returns The DOMRect representing the element's bounding box.
232
- */
233
- export const getRect = (element: HTMLElement): DOMRect => {
234
- return element.getBoundingClientRect();
235
- };
236
-
237
- /**
238
- * Creates an SVG element from an SVG string.
239
- * @param svgString - The SVG markup as a string.
240
- * @returns The parsed SVGElement.
241
- * @throws Error if the SVG string is invalid or contains multiple root elements.
242
- */
243
- export const createSVGFromString = (svgString: string): SVGElement => {
244
- const parser = new DOMParser();
245
- const doc = parser.parseFromString(svgString, 'image/svg+xml');
246
-
247
- if (doc.documentElement.nodeName === 'parsererror') {
248
- throw new Error('Invalid SVG string: Failed to parse SVG');
249
- }
250
-
251
- return doc.documentElement as unknown as SVGElement;
252
- };