@carbon/utilities 0.7.0 → 0.8.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.
@@ -0,0 +1 @@
1
+ import{registerSwipeEvents as b}from"./swipeEvents";const Y=(l,x)=>{const r="carousel";let s=[0],c=[0];const a={},p=10,{onViewChangeStart:S,onViewChangeEnd:M,excludeSwipeSupport:g}=x||{},v=(e,t)=>{a[e]=t},C=(e,t)=>{if(e.querySelector(`.${t}`))return;const n=document.createElement("div");for(n.classList.add(`${t}`);e.firstChild;)n.appendChild(e.firstChild);e.appendChild(n)},k=()=>s.map(e=>({id:e,elem:a[e]})),h=()=>{const e=Object.keys(a).length,t=a[e-1],n=k();return{currentIndex:s[0],lastIndex:parseInt(t?.dataset.index||s[0].toString(),10),totalViews:e,historyStack:n}},E=()=>{c=[...s];const e=h();S?.(e)},L=e=>{if(!e)return;const t=e.dataset.index;if(t&&s[0]===parseInt(t,10)){const n=h();M?.(n)}},$=e=>{const n=Object.keys(a).length-1;return Math.max(0,Math.min(e,n))},y=e=>{const t=$(e);s[0]!==t&&(E(),s=[t,...s],d(!1))},V=e=>{L(e)},w=(e,t,n,o,m)=>{e.classList.add(`${r}__view`),e.classList.toggle(`${r}__view-in-stack`,t&&!n),e.classList.toggle(`${r}__view-active`,t&&n),m&&!o&&e.classList.add(`${r}__view-recycle-in`),!m&&o&&e.classList.add(`${r}__view-recycle-out`)},R=e=>{e.classList.remove(`${r}__view-recycle-in`,`${r}__view-recycle-out`)},H=e=>e*parseFloat(getComputedStyle(document.documentElement).fontSize),A=e=>{const t=H(p);if(l.clientHeight<t){e<t&&(e=t);const o=l.querySelector(`.${r}__itemsWrapper`);o&&(o.style.blockSize=`${e}px`)}},d=e=>{let t=0;Array.from(_).forEach((n,o)=>{const m=s.findIndex(i=>i===o),T=c.filter(i=>i===o).length,O=c.length>s.length&&c[0]===o&&T>0,F=c.length<s.length&&s[0]===o&&T>0,N=m>-1,P=o===s[0];if(w(n,N,P,O,F),e){v(o,n),setTimeout(()=>{(!t||n.offsetHeight<t&&t>H(p))&&(t=n.offsetHeight),n.style.position="absolute",A(t)});const i=B=>{R(n),B.target===a[s[0]]&&V(n)};n._carouselListener=i,n.addEventListener("animationend",i),n.addEventListener("transitionend",i),n.setAttribute("data-index",o.toString())}}),e&&L(Array.from(_)[0])},u=()=>{const e=s[0]+1;y(e)},f=()=>{s.length-1>=1&&(E(),s=s.slice(1),d(!1))},I=e=>{y(e)},W=()=>({index:s[0],item:a[s[0]]}),q=()=>{s=[0],d(!1)},z=()=>{Object.values(a).forEach(e=>{e&&e._carouselListener&&(e.removeEventListener("animationend",e._carouselListener),e.removeEventListener("transitionend",e._carouselListener))}),g||b(l,u,f,!0)},j=e=>{const t=e.querySelector("slot");return t?t.assignedElements({flatten:!0}):Array.from(e.children)};C(l,`${r}__itemsWrapper`);const D=l.querySelector(`.${r}__itemsWrapper`),_=j(D);return l.classList.add(`${r}__view-stack`),d(!0),g||b(l,u,f,!1),{next:u,prev:f,reset:q,goToIndex:I,getActiveItem:W,destroyEvents:z,allViews:a}};export{Y as initCarousel};
@@ -0,0 +1 @@
1
+ export*from"./carousel";export*from"./types";
@@ -0,0 +1 @@
1
+ const b=(t,d,v,w)=>{let o=null,l=null,r=0;const M=400;let s=!1,i=null,u=null;const a=e=>{o=e.touches[0].clientX},m=e=>{l=e.touches[0].clientX},c=e=>{if(o!==null&&l!==null){const n=o-l;Math.abs(n)>50&&(n>0?d():v())}o=null,l=null},h=e=>{s=!0,i=e.clientX},E=e=>{s&&(u=e.clientX)},L=()=>{if(s&&i!==null&&u!==null){const e=i-u;Math.abs(e)>50&&(e>0?d():v())}s=!1,i=null,u=null},f=e=>{const n=Date.now();if(Math.abs(e.deltaX)>Math.abs(e.deltaY)&&Math.abs(e.deltaX)>20){if(e.preventDefault(),n-r<M)return;e.deltaX>0?d():v(),r=n}};w&&(t.removeEventListener("touchstart",a),t.removeEventListener("touchmove",m),t.removeEventListener("touchend",c),t.removeEventListener("mousedown",h),t.removeEventListener("mousemove",E),t.removeEventListener("mouseup",L),t.removeEventListener("wheel",f)),t.addEventListener("touchstart",a),t.addEventListener("touchmove",m),t.addEventListener("touchend",c),t.addEventListener("mousedown",h),t.addEventListener("mousemove",E),t.addEventListener("mouseup",L),t.addEventListener("wheel",f)};export{b as registerSwipeEvents};
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @license
3
+ *
4
+ * Copyright IBM Corp. 2025
5
+ *
6
+ * This source code is licensed under the Apache-2.0 license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
package/es/index.js CHANGED
@@ -1 +1 @@
1
- export*from"./dateTimeFormat";export*from"./overflowHandler";export*from"@internationalized/number";export*from"./documentLang";
1
+ export*from"./dateTimeFormat";export*from"./overflowHandler";export*from"@internationalized/number";export*from"./documentLang";export*from"./carousel";
@@ -0,0 +1 @@
1
+ "use strict";var g=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var U=(r,l)=>{for(var o in l)g(r,o,{get:l[o],enumerable:!0})},X=(r,l,o,s)=>{if(l&&typeof l=="object"||typeof l=="function")for(let i of K(l))!Q.call(r,i)&&i!==o&&g(r,i,{get:()=>l[i],enumerable:!(s=J(l,i))||s.enumerable});return r};var Y=r=>X(g({},"__esModule",{value:!0}),r);var ee={};U(ee,{initCarousel:()=>Z});module.exports=Y(ee);var h=require("./swipeEvents");const Z=(r,l)=>{const o="carousel";let s=[0],i=[0];const c={},E=10,{onViewChangeStart:M,onViewChangeEnd:v,excludeSwipeSupport:L}=l||{},C=(e,t)=>{c[e]=t},k=(e,t)=>{if(e.querySelector(`.${t}`))return;const n=document.createElement("div");for(n.classList.add(`${t}`);e.firstChild;)n.appendChild(e.firstChild);e.appendChild(n)},$=()=>s.map(e=>({id:e,elem:c[e]})),y=()=>{const e=Object.keys(c).length,t=c[e-1],n=$();return{currentIndex:s[0],lastIndex:parseInt(t?.dataset.index||s[0].toString(),10),totalViews:e,historyStack:n}},H=()=>{i=[...s];const e=y();M?.(e)},_=e=>{if(!e)return;const t=e.dataset.index;if(t&&s[0]===parseInt(t,10)){const n=y();v?.(n)}},V=e=>{const n=Object.keys(c).length-1;return Math.max(0,Math.min(e,n))},T=e=>{const t=V(e);s[0]!==t&&(H(),s=[t,...s],m(!1))},w=e=>{_(e)},R=(e,t,n,a,u)=>{e.classList.add(`${o}__view`),e.classList.toggle(`${o}__view-in-stack`,t&&!n),e.classList.toggle(`${o}__view-active`,t&&n),u&&!a&&e.classList.add(`${o}__view-recycle-in`),!u&&a&&e.classList.add(`${o}__view-recycle-out`)},A=e=>{e.classList.remove(`${o}__view-recycle-in`,`${o}__view-recycle-out`)},b=e=>e*parseFloat(getComputedStyle(document.documentElement).fontSize),I=e=>{const t=b(E);if(r.clientHeight<t){e<t&&(e=t);const a=r.querySelector(`.${o}__itemsWrapper`);a&&(a.style.blockSize=`${e}px`)}},m=e=>{let t=0;Array.from(x).forEach((n,a)=>{const u=s.findIndex(d=>d===a),S=i.filter(d=>d===a).length,F=i.length>s.length&&i[0]===a&&S>0,N=i.length<s.length&&s[0]===a&&S>0,P=u>-1,B=a===s[0];if(R(n,P,B,F,N),e){C(a,n),setTimeout(()=>{(!t||n.offsetHeight<t&&t>b(E))&&(t=n.offsetHeight),n.style.position="absolute",I(t)});const d=G=>{A(n),G.target===c[s[0]]&&w(n)};n._carouselListener=d,n.addEventListener("animationend",d),n.addEventListener("transitionend",d),n.setAttribute("data-index",a.toString())}}),e&&_(Array.from(x)[0])},f=()=>{const e=s[0]+1;T(e)},p=()=>{s.length-1>=1&&(H(),s=s.slice(1),m(!1))},W=e=>{T(e)},q=()=>({index:s[0],item:c[s[0]]}),z=()=>{s=[0],m(!1)},j=()=>{Object.values(c).forEach(e=>{e&&e._carouselListener&&(e.removeEventListener("animationend",e._carouselListener),e.removeEventListener("transitionend",e._carouselListener))}),L||(0,h.registerSwipeEvents)(r,f,p,!0)},D=e=>{const t=e.querySelector("slot");return t?t.assignedElements({flatten:!0}):Array.from(e.children)};k(r,`${o}__itemsWrapper`);const O=r.querySelector(`.${o}__itemsWrapper`),x=D(O);return r.classList.add(`${o}__view-stack`),m(!0),L||(0,h.registerSwipeEvents)(r,f,p,!1),{next:f,prev:p,reset:z,goToIndex:W,getActiveItem:q,destroyEvents:j,allViews:c}};
@@ -0,0 +1 @@
1
+ "use strict";var a=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var t=(r,o,p,x)=>{if(o&&typeof o=="object"||typeof o=="function")for(let m of c(o))!d.call(r,m)&&m!==p&&a(r,m,{get:()=>o[m],enumerable:!(x=b(o,m))||x.enumerable});return r},f=(r,o,p)=>(t(r,o,"default"),p&&t(p,o,"default"));var g=r=>t(a({},"__esModule",{value:!0}),r);var e={};module.exports=g(e);f(e,require("./carousel"),module.exports);f(e,require("./types"),module.exports);
@@ -0,0 +1 @@
1
+ "use strict";var m=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var D=(e,n)=>{for(var o in n)m(e,o,{get:n[o],enumerable:!0})},T=(e,n,o,i)=>{if(n&&typeof n=="object"||typeof n=="function")for(let l of S(n))!p.call(e,l)&&l!==o&&m(e,l,{get:()=>n[l],enumerable:!(i=H(n,l))||i.enumerable});return e};var g=e=>T(m({},"__esModule",{value:!0}),e);var U={};D(U,{registerSwipeEvents:()=>C});module.exports=g(U);const C=(e,n,o,i)=>{let u=null,d=null,c=0;const b=400;let v=!1,r=null,a=null;const h=t=>{u=t.touches[0].clientX},E=t=>{d=t.touches[0].clientX},L=t=>{if(u!==null&&d!==null){const s=u-d;Math.abs(s)>50&&(s>0?n():o())}u=null,d=null},f=t=>{v=!0,r=t.clientX},w=t=>{v&&(a=t.clientX)},M=()=>{if(v&&r!==null&&a!==null){const t=r-a;Math.abs(t)>50&&(t>0?n():o())}v=!1,r=null,a=null},X=t=>{const s=Date.now();if(Math.abs(t.deltaX)>Math.abs(t.deltaY)&&Math.abs(t.deltaX)>20){if(t.preventDefault(),s-c<b)return;t.deltaX>0?n():o(),c=s}};i&&(e.removeEventListener("touchstart",h),e.removeEventListener("touchmove",E),e.removeEventListener("touchend",L),e.removeEventListener("mousedown",f),e.removeEventListener("mousemove",w),e.removeEventListener("mouseup",M),e.removeEventListener("wheel",X)),e.addEventListener("touchstart",h),e.addEventListener("touchmove",E),e.addEventListener("touchend",L),e.addEventListener("mousedown",f),e.addEventListener("mousemove",w),e.addEventListener("mouseup",M),e.addEventListener("wheel",X)};
@@ -0,0 +1,8 @@
1
+ "use strict";var r=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var a=Object.prototype.hasOwnProperty;var u=(t,e,s,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of l(e))!a.call(t,o)&&o!==s&&r(t,o,{get:()=>e[o],enumerable:!(n=i(e,o))||n.enumerable});return t};var d=t=>u(r({},"__esModule",{value:!0}),t);var m={};module.exports=d(m);/**
2
+ * @license
3
+ *
4
+ * Copyright IBM Corp. 2025
5
+ *
6
+ * This source code is licensed under the Apache-2.0 license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var n=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var f=(t,e,p,x)=>{if(e&&typeof e=="object"||typeof e=="function")for(let m of a(e))!b.call(t,m)&&m!==p&&n(t,m,{get:()=>e[m],enumerable:!(x=i(e,m))||x.enumerable});return t},o=(t,e,p)=>(f(t,e,"default"),p&&f(p,e,"default"));var d=t=>f(n({},"__esModule",{value:!0}),t);var r={};module.exports=d(r);o(r,require("./dateTimeFormat"),module.exports);o(r,require("./overflowHandler"),module.exports);o(r,require("@internationalized/number"),module.exports);o(r,require("./documentLang"),module.exports);
1
+ "use strict";var n=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var f=(t,e,p,x)=>{if(e&&typeof e=="object"||typeof e=="function")for(let m of a(e))!b.call(t,m)&&m!==p&&n(t,m,{get:()=>e[m],enumerable:!(x=i(e,m))||x.enumerable});return t},o=(t,e,p)=>(f(t,e,"default"),p&&f(p,e,"default"));var d=t=>f(n({},"__esModule",{value:!0}),t);var r={};module.exports=d(r);o(r,require("./dateTimeFormat"),module.exports);o(r,require("./overflowHandler"),module.exports);o(r,require("@internationalized/number"),module.exports);o(r,require("./documentLang"),module.exports);o(r,require("./carousel"),module.exports);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@carbon/utilities",
3
3
  "description": "Utilities and helpers to drive consistency across software products using the Carbon Design System",
4
- "version": "0.7.0",
4
+ "version": "0.8.0",
5
5
  "license": "Apache-2.0",
6
6
  "main": "lib/index.js",
7
7
  "module": "es/index.js",
@@ -39,13 +39,13 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "esbuild": "^0.25.0",
42
- "rimraf": "^6.0.0",
42
+ "rimraf": "^6.0.1",
43
43
  "typescript": "^5.7.3",
44
- "typescript-config-carbon": "^0.5.0"
44
+ "typescript-config-carbon": "^0.6.0"
45
45
  },
46
46
  "dependencies": {
47
47
  "@ibm/telemetry-js": "^1.6.1",
48
48
  "@internationalized/number": "^3.6.1"
49
49
  },
50
- "gitHead": "f6debacbfe06fa5c31f70809e15762dc30096d70"
50
+ "gitHead": "d66cc6afbdf6802cf8b14042f20f7c350beef90b"
51
51
  }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Copyright IBM Corp. 2025
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { InitCarousel, Config } from './types';
8
+ /**
9
+ * Initializes a carousel with the given configuration.
10
+ * @param carouselContainer - The HTMLElement representing the carousel container.
11
+ * @param config - Optional configuration object.
12
+ * @returns An object containing methods to control the carousel.
13
+ */
14
+ export declare const initCarousel: (carouselContainer: HTMLElement, config?: Config) => InitCarousel;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright IBM Corp. 2025
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ export * from './carousel';
8
+ export * from './types';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Registers swipe event handlers for a carousel element.
3
+ * Handles touch, mouse, and wheel events for navigation.
4
+ * @param {HTMLElement} carousel - The carousel element to attach event listeners to.
5
+ * @param {() => void} next - Callback function to execute when swiping right.
6
+ * @param {() => void} prev - Callback function to execute when swiping left.
7
+ * @param {boolean} destroy - If true, removes existing event listeners before adding new ones.
8
+ */
9
+ export declare const registerSwipeEvents: (carousel: HTMLElement, next: () => void, prev: () => void, destroy: boolean) => void;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @license
3
+ *
4
+ * Copyright IBM Corp. 2025
5
+ *
6
+ * This source code is licensed under the Apache-2.0 license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+ interface CarouselStackHistory {
10
+ id: number;
11
+ elem: HTMLLIElement;
12
+ }
13
+ type CarouselResponse = {
14
+ currentIndex: number;
15
+ lastIndex: number;
16
+ totalViews: number;
17
+ historyStack: CarouselStackHistory[];
18
+ };
19
+ type ActiveItem = {
20
+ index: number;
21
+ item: HTMLElement | null;
22
+ };
23
+ export type Config = {
24
+ onViewChangeStart?: (args: CarouselResponse) => void;
25
+ onViewChangeEnd?: (args: CarouselResponse) => void;
26
+ excludeSwipeSupport?: boolean;
27
+ };
28
+ interface InitCarousel {
29
+ next: () => void;
30
+ prev: () => void;
31
+ reset: () => void;
32
+ goToIndex: (index: number) => void;
33
+ getActiveItem: () => ActiveItem;
34
+ destroyEvents: (() => void) | null;
35
+ allViews: Record<number, HTMLElement | null>;
36
+ }
37
+ export type { CarouselStackHistory, CarouselResponse, InitCarousel };
package/types/index.d.ts CHANGED
@@ -9,3 +9,4 @@ export * from './overflowHandler';
9
9
  export * from '@internationalized/number';
10
10
  export type * from '@internationalized/number';
11
11
  export * from './documentLang';
12
+ export * from './carousel';