@momentum-design/components 0.71.2 → 0.73.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,77 @@
1
+ import { CSSResult } from 'lit';
2
+ import { AnimationItem } from 'lottie-web/build/player/lottie_light';
3
+ import { Component } from '../../models';
4
+ import { AnimationNames, LoopType } from './animation.types';
5
+ /**
6
+ * The `mdc-animation` component is a wrapper around the Lottie animation library.
7
+ * It fetches the animation data dynamically based on the provided name and renders it.
8
+ * This is a display only component that does not have any interactive functionality.
9
+ * From accessibility perspective, (by default) it is a decorative image component.
10
+ *
11
+ * @tagname mdc-animation
12
+ *
13
+ * @event load - (React: onLoad) This event is dispatched when the animation is loaded
14
+ * @event complete - (React: onComplete) This event is dispatched when all animation loops completed
15
+ * @event error - (React: onError) This event is dispatched when animation loading failed
16
+ */
17
+ declare class Animation extends Component {
18
+ /**
19
+ * Name of the animation (= filename)
20
+ */
21
+ name?: AnimationNames;
22
+ /**
23
+ * How many times to loop the animation
24
+ * - "true" - infinite
25
+ * - "false" - no loop
26
+ * - number - number of times to loop
27
+ */
28
+ loop?: LoopType;
29
+ /**
30
+ * Weather start the animation automatically
31
+ */
32
+ autoplay?: boolean;
33
+ /**
34
+ * Aria-label attribute to be set for accessibility
35
+ */
36
+ ariaLabel: string | null;
37
+ /**
38
+ * Aria-labelledby attribute to be set for accessibility
39
+ */
40
+ ariaLabelledBy: string | null;
41
+ /**
42
+ * Lottie animation instance
43
+ */
44
+ private lottieInstance?;
45
+ /**
46
+ * Container for the animation
47
+ */
48
+ private containerRef;
49
+ /**
50
+ * Exposed API of the animation library (lottie)
51
+ */
52
+ get animation(): AnimationItem | undefined;
53
+ private getLoopValue;
54
+ /**
55
+ * Create new lotty instance for the loaded data
56
+ */
57
+ private onLoadSuccessHandler;
58
+ /**
59
+ * Error handler for animation loading
60
+ */
61
+ private onLoadFailHandler;
62
+ /**
63
+ * Import animation data dynamically
64
+ */
65
+ private getAnimationData;
66
+ updated(changedProperties: Map<string, any>): void;
67
+ /**
68
+ * Re-dispatch the complete event from the animation library
69
+ *
70
+ * This handler called with the animation instance instead of the component instance
71
+ * so we need to bind it to the component instance. The arrow function just does that.
72
+ */
73
+ onCompleteHandler: () => void;
74
+ render(): import("lit-html").TemplateResult<1>;
75
+ static styles: Array<CSSResult>;
76
+ }
77
+ export default Animation;
@@ -0,0 +1,171 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { html } from 'lit';
11
+ import { property } from 'lit/decorators.js';
12
+ import { createRef, ref } from 'lit/directives/ref.js';
13
+ import lottie from 'lottie-web/build/player/lottie_light';
14
+ import * as animationManifest from '@momentum-design/animations/dist/manifest.json';
15
+ import styles from './animation.styles';
16
+ import { Component } from '../../models';
17
+ import { DEFAULTS } from './animation.constants';
18
+ /**
19
+ * The `mdc-animation` component is a wrapper around the Lottie animation library.
20
+ * It fetches the animation data dynamically based on the provided name and renders it.
21
+ * This is a display only component that does not have any interactive functionality.
22
+ * From accessibility perspective, (by default) it is a decorative image component.
23
+ *
24
+ * @tagname mdc-animation
25
+ *
26
+ * @event load - (React: onLoad) This event is dispatched when the animation is loaded
27
+ * @event complete - (React: onComplete) This event is dispatched when all animation loops completed
28
+ * @event error - (React: onError) This event is dispatched when animation loading failed
29
+ */
30
+ class Animation extends Component {
31
+ constructor() {
32
+ super(...arguments);
33
+ /**
34
+ * Name of the animation (= filename)
35
+ */
36
+ this.name = DEFAULTS.NAME;
37
+ /**
38
+ * How many times to loop the animation
39
+ * - "true" - infinite
40
+ * - "false" - no loop
41
+ * - number - number of times to loop
42
+ */
43
+ this.loop = DEFAULTS.LOOP;
44
+ /**
45
+ * Weather start the animation automatically
46
+ */
47
+ this.autoplay = DEFAULTS.AUTO_PLAY;
48
+ /**
49
+ * Aria-label attribute to be set for accessibility
50
+ */
51
+ this.ariaLabel = null;
52
+ /**
53
+ * Aria-labelledby attribute to be set for accessibility
54
+ */
55
+ this.ariaLabelledBy = null;
56
+ /**
57
+ * Container for the animation
58
+ */
59
+ this.containerRef = createRef();
60
+ /**
61
+ * Re-dispatch the complete event from the animation library
62
+ *
63
+ * This handler called with the animation instance instead of the component instance
64
+ * so we need to bind it to the component instance. The arrow function just does that.
65
+ */
66
+ this.onCompleteHandler = () => {
67
+ const event = new CustomEvent('complete', {
68
+ detail: { name: this.name },
69
+ bubbles: true,
70
+ });
71
+ this.dispatchEvent(event);
72
+ };
73
+ }
74
+ /**
75
+ * Exposed API of the animation library (lottie)
76
+ */
77
+ get animation() {
78
+ return this.lottieInstance;
79
+ }
80
+ getLoopValue() {
81
+ if (this.loop === 'true')
82
+ return true;
83
+ if (this.loop === 'false')
84
+ return false;
85
+ if (this.loop)
86
+ return Number(this.loop);
87
+ return true;
88
+ }
89
+ /**
90
+ * Create new lotty instance for the loaded data
91
+ */
92
+ onLoadSuccessHandler(animationData) {
93
+ if (this.lottieInstance) {
94
+ this.lottieInstance.removeEventListener('complete', this.onCompleteHandler);
95
+ this.lottieInstance.destroy();
96
+ }
97
+ if (this.containerRef.value) {
98
+ this.lottieInstance = lottie.loadAnimation({
99
+ container: this.containerRef.value,
100
+ renderer: 'svg',
101
+ loop: this.getLoopValue(),
102
+ autoplay: this.autoplay,
103
+ animationData,
104
+ name: this.name,
105
+ });
106
+ this.lottieInstance.addEventListener('complete', this.onCompleteHandler);
107
+ }
108
+ // Dispatch load event when animation ready to play
109
+ this.dispatchEvent(new CustomEvent('load', { bubbles: true, cancelable: true, detail: { name: this.name } }));
110
+ }
111
+ /**
112
+ * Error handler for animation loading
113
+ */
114
+ onLoadFailHandler(error) {
115
+ const errorEvent = new CustomEvent('error', {
116
+ bubbles: true,
117
+ cancelable: true,
118
+ detail: { error },
119
+ });
120
+ this.dispatchEvent(errorEvent);
121
+ }
122
+ /**
123
+ * Import animation data dynamically
124
+ */
125
+ getAnimationData() {
126
+ if (!(this.name && animationManifest[this.name])) {
127
+ return Promise.reject(new Error(`Invalid animation name: ${this.name}`));
128
+ }
129
+ const path = animationManifest[this.name].replace(/^\./, '');
130
+ return import(`@momentum-design/animations/dist${path}`)
131
+ .then((result) => this.onLoadSuccessHandler(result.default))
132
+ .catch((error) => this.onLoadFailHandler(error));
133
+ }
134
+ updated(changedProperties) {
135
+ super.updated(changedProperties);
136
+ // fetch animation data when new animation needed or any animation parameter changed
137
+ // note: we re-create the animation for parameter changes as well, because lottie
138
+ // does not API for changing them on the fly
139
+ if (changedProperties.has('name') || changedProperties.has('loop') || changedProperties.has('autoplay')) {
140
+ this.getAnimationData().catch((err) => { var _a; return (_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, err.message); });
141
+ }
142
+ if (changedProperties.has('ariaLabel') || changedProperties.has('ariaLabelledBy')) {
143
+ this.role = this.ariaLabel || this.ariaLabelledBy ? 'img' : null;
144
+ }
145
+ }
146
+ render() {
147
+ return html `<div aria-hidden="true" ${ref(this.containerRef)}></div>`;
148
+ }
149
+ }
150
+ Animation.styles = [...Component.styles, ...styles];
151
+ __decorate([
152
+ property({ type: String, reflect: true }),
153
+ __metadata("design:type", String)
154
+ ], Animation.prototype, "name", void 0);
155
+ __decorate([
156
+ property({ type: String, reflect: true }),
157
+ __metadata("design:type", String)
158
+ ], Animation.prototype, "loop", void 0);
159
+ __decorate([
160
+ property({ type: Boolean, reflect: true }),
161
+ __metadata("design:type", Boolean)
162
+ ], Animation.prototype, "autoplay", void 0);
163
+ __decorate([
164
+ property({ type: String, attribute: 'aria-label' }),
165
+ __metadata("design:type", Object)
166
+ ], Animation.prototype, "ariaLabel", void 0);
167
+ __decorate([
168
+ property({ type: String, attribute: 'aria-labelledby' }),
169
+ __metadata("design:type", Object)
170
+ ], Animation.prototype, "ariaLabelledBy", void 0);
171
+ export default Animation;
@@ -0,0 +1,7 @@
1
+ declare const TAG_NAME: "mdc-animation";
2
+ declare const DEFAULTS: {
3
+ readonly NAME: undefined;
4
+ readonly AUTO_PLAY: true;
5
+ readonly LOOP: "true";
6
+ };
7
+ export { TAG_NAME, DEFAULTS };
@@ -0,0 +1,8 @@
1
+ import utils from '../../utils/tag-name';
2
+ const TAG_NAME = utils.constructTagName('animation');
3
+ const DEFAULTS = {
4
+ NAME: undefined,
5
+ AUTO_PLAY: true,
6
+ LOOP: 'true',
7
+ };
8
+ export { TAG_NAME, DEFAULTS };
@@ -0,0 +1,2 @@
1
+ declare const styles: import("lit").CSSResult[];
2
+ export default styles;
@@ -0,0 +1,3 @@
1
+ import { hostFitContentStyles } from '../../utils/styles';
2
+ const styles = [hostFitContentStyles];
3
+ export default styles;
@@ -0,0 +1,12 @@
1
+ import type AnimationNames from '@momentum-design/animations/dist/types/types';
2
+ /** Event mapping for React */
3
+ interface Events {
4
+ /** This event is dispatched when the animation is loaded */
5
+ onLoadEvent: Event;
6
+ /** This event is dispatched when all animation loops completed */
7
+ onCompleteEvent: Event;
8
+ /** This event is dispatched when animation loading failed */
9
+ onErrorEvent: Event;
10
+ }
11
+ type LoopType = `${boolean | number}`;
12
+ export type { Events, AnimationNames, LoopType };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import Animation from './animation.component';
2
+ declare global {
3
+ interface HTMLElementTagNameMap {
4
+ ['mdc-animation']: Animation;
5
+ }
6
+ }
7
+ export default Animation;
@@ -0,0 +1,4 @@
1
+ import Animation from './animation.component';
2
+ import { TAG_NAME } from './animation.constants';
3
+ Animation.register(TAG_NAME);
4
+ export default Animation;