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