@momentum-design/components 0.133.40 → 0.134.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.
@@ -20,6 +20,12 @@ declare class Animation extends Component {
20
20
  * @default undefined
21
21
  */
22
22
  name?: AnimationNames;
23
+ /**
24
+ * URL pointing to a Lottie JSON animation file.
25
+ * When provided, it takes precedence over the `name` property.
26
+ * @default undefined
27
+ */
28
+ src?: string;
23
29
  /**
24
30
  * How many times to loop the animation
25
31
  * - "true" - infinite
@@ -48,6 +54,11 @@ declare class Animation extends Component {
48
54
  * @internal
49
55
  */
50
56
  private lottieInstance?;
57
+ /**
58
+ * Cached animation data from the last successful fetch/import
59
+ * @internal
60
+ */
61
+ private cachedAnimationData?;
51
62
  /**
52
63
  * Container for the animation
53
64
  * @internal
@@ -60,9 +71,13 @@ declare class Animation extends Component {
60
71
  get animation(): AnimationItem | undefined;
61
72
  private getLoopValue;
62
73
  /**
63
- * Create new lotty instance for the loaded data
74
+ * Create new lottie instance for the loaded data
64
75
  */
65
76
  private onLoadSuccessHandler;
77
+ /**
78
+ * Create or re-create the lottie instance with the given animation data
79
+ */
80
+ private createLottieInstance;
66
81
  /**
67
82
  * Error handler for animation loading
68
83
  */
@@ -71,6 +86,10 @@ declare class Animation extends Component {
71
86
  * Import animation data dynamically
72
87
  */
73
88
  private getAnimationData;
89
+ /**
90
+ * Fetch animation data from a URL
91
+ */
92
+ private fetchAnimationFromUrl;
74
93
  updated(changedProperties: PropertyValues): void;
75
94
  disconnectedCallback(): void;
76
95
  /**
@@ -91,9 +91,18 @@ class Animation extends Component {
91
91
  return true;
92
92
  }
93
93
  /**
94
- * Create new lotty instance for the loaded data
94
+ * Create new lottie instance for the loaded data
95
95
  */
96
96
  onLoadSuccessHandler(animationData) {
97
+ this.cachedAnimationData = animationData;
98
+ this.createLottieInstance(animationData);
99
+ // Dispatch load event when animation ready to play
100
+ this.dispatchEvent(new CustomEvent('load', { bubbles: true, cancelable: true, detail: { name: this.name } }));
101
+ }
102
+ /**
103
+ * Create or re-create the lottie instance with the given animation data
104
+ */
105
+ createLottieInstance(animationData) {
97
106
  if (this.lottieInstance) {
98
107
  this.lottieInstance.removeEventListener('complete', this.onCompleteHandler);
99
108
  this.lottieInstance.destroy();
@@ -109,8 +118,6 @@ class Animation extends Component {
109
118
  });
110
119
  this.lottieInstance.addEventListener('complete', this.onCompleteHandler);
111
120
  }
112
- // Dispatch load event when animation ready to play
113
- this.dispatchEvent(new CustomEvent('load', { bubbles: true, cancelable: true, detail: { name: this.name } }));
114
121
  }
115
122
  /**
116
123
  * Error handler for animation loading
@@ -127,7 +134,10 @@ class Animation extends Component {
127
134
  * Import animation data dynamically
128
135
  */
129
136
  getAnimationData() {
130
- if (this.name && animationManifest[this.name]) {
137
+ if (this.src) {
138
+ this.fetchAnimationFromUrl(this.src);
139
+ }
140
+ else if (this.name && animationManifest[this.name]) {
131
141
  // Make sure the path is point to a folder (and its sub-folders) that contains animation data only
132
142
  // otherwise bundlers (eg. webpack) will try to process everything in this folder including the types.d.ts
133
143
  const path = animationManifest[this.name].replace(/^\.\/lottie/, '');
@@ -139,14 +149,32 @@ class Animation extends Component {
139
149
  this.onLoadFailHandler(new Error(`Invalid animation name: ${this.name}`));
140
150
  }
141
151
  }
152
+ /**
153
+ * Fetch animation data from a URL
154
+ */
155
+ fetchAnimationFromUrl(url) {
156
+ fetch(url)
157
+ .then(response => {
158
+ if (!response.ok) {
159
+ throw new Error(`Failed to fetch animation from URL: ${url} (${response.status})`);
160
+ }
161
+ return response.json();
162
+ })
163
+ .then((animationData) => this.onLoadSuccessHandler(animationData))
164
+ .catch((error) => this.onLoadFailHandler(error));
165
+ }
142
166
  updated(changedProperties) {
143
167
  super.updated(changedProperties);
144
- // fetch animation data when new animation needed or any animation parameter changed
145
- // note: we re-create the animation for parameter changes as well, because lottie
146
- // does not API for changing them on the fly
147
- if (changedProperties.has('name') || changedProperties.has('loop') || changedProperties.has('autoplay')) {
168
+ // fetch animation data when the animation source changes
169
+ if (changedProperties.has('name') || changedProperties.has('src')) {
170
+ this.cachedAnimationData = undefined;
148
171
  this.getAnimationData();
149
172
  }
173
+ else if ((changedProperties.has('loop') || changedProperties.has('autoplay')) && this.cachedAnimationData) {
174
+ // re-create the lottie instance from cache for parameter changes,
175
+ // because lottie does not have an API for changing them on the fly
176
+ this.createLottieInstance(this.cachedAnimationData);
177
+ }
150
178
  if (changedProperties.has('ariaLabel') || changedProperties.has('ariaLabelledby')) {
151
179
  this.role = this.ariaLabel || this.ariaLabelledby ? ROLE.IMG : null;
152
180
  }
@@ -169,6 +197,10 @@ __decorate([
169
197
  property({ type: String, reflect: true }),
170
198
  __metadata("design:type", String)
171
199
  ], Animation.prototype, "name", void 0);
200
+ __decorate([
201
+ property({ type: String, reflect: true }),
202
+ __metadata("design:type", String)
203
+ ], Animation.prototype, "src", void 0);
172
204
  __decorate([
173
205
  property({ type: String, reflect: true }),
174
206
  __metadata("design:type", String)
@@ -1846,6 +1846,44 @@
1846
1846
  "attribute": "autoplay",
1847
1847
  "reflects": true
1848
1848
  },
1849
+ {
1850
+ "kind": "method",
1851
+ "name": "createLottieInstance",
1852
+ "privacy": "private",
1853
+ "return": {
1854
+ "type": {
1855
+ "text": "void"
1856
+ }
1857
+ },
1858
+ "parameters": [
1859
+ {
1860
+ "name": "animationData",
1861
+ "type": {
1862
+ "text": "any"
1863
+ }
1864
+ }
1865
+ ],
1866
+ "description": "Create or re-create the lottie instance with the given animation data"
1867
+ },
1868
+ {
1869
+ "kind": "method",
1870
+ "name": "fetchAnimationFromUrl",
1871
+ "privacy": "private",
1872
+ "return": {
1873
+ "type": {
1874
+ "text": "void"
1875
+ }
1876
+ },
1877
+ "parameters": [
1878
+ {
1879
+ "name": "url",
1880
+ "type": {
1881
+ "text": "string"
1882
+ }
1883
+ }
1884
+ ],
1885
+ "description": "Fetch animation data from a URL"
1886
+ },
1849
1887
  {
1850
1888
  "kind": "method",
1851
1889
  "name": "getAnimationData",
@@ -1925,7 +1963,18 @@
1925
1963
  }
1926
1964
  }
1927
1965
  ],
1928
- "description": "Create new lotty instance for the loaded data"
1966
+ "description": "Create new lottie instance for the loaded data"
1967
+ },
1968
+ {
1969
+ "kind": "field",
1970
+ "name": "src",
1971
+ "type": {
1972
+ "text": "string | undefined"
1973
+ },
1974
+ "description": "URL pointing to a Lottie JSON animation file.\nWhen provided, it takes precedence over the `name` property.",
1975
+ "default": "undefined",
1976
+ "attribute": "src",
1977
+ "reflects": true
1929
1978
  }
1930
1979
  ],
1931
1980
  "events": [
@@ -1958,6 +2007,15 @@
1958
2007
  "default": "undefined",
1959
2008
  "fieldName": "name"
1960
2009
  },
2010
+ {
2011
+ "name": "src",
2012
+ "type": {
2013
+ "text": "string | undefined"
2014
+ },
2015
+ "description": "URL pointing to a Lottie JSON animation file.\nWhen provided, it takes precedence over the `name` property.",
2016
+ "default": "undefined",
2017
+ "fieldName": "src"
2018
+ },
1961
2019
  {
1962
2020
  "name": "loop",
1963
2021
  "type": {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@momentum-design/components",
3
3
  "packageManager": "yarn@3.2.4",
4
- "version": "0.133.40",
4
+ "version": "0.134.0",
5
5
  "engines": {
6
6
  "node": ">=20.0.0",
7
7
  "npm": ">=8.0.0"