/
dotlottie.ts
156 lines (141 loc) · 4.16 KB
/
dotlottie.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright 2023 Design Barn Inc.
*/
import { type Config, DotLottie, type Mode } from '@lottiefiles/dotlottie-web';
import {
type VNode,
h,
ref,
onMounted,
type Ref,
watch,
type SetupContext,
onBeforeUnmount,
defineComponent,
toRefs,
} from 'vue';
export { type DotLottie };
export interface DotLottieVueProps extends Omit<Config, 'canvas'> {}
export const DotLottieVue = defineComponent({
props: {
autoplay: { type: Boolean, required: false },
backgroundColor: { type: String, required: false },
data: { type: [String, ArrayBuffer], required: false },
loop: { type: Boolean, required: false },
mode: { type: String as () => Mode, required: false },
renderConfig: { type: Object, required: false },
segment: { type: Array as unknown as () => [number, number], required: false },
speed: { type: Number, required: false },
src: { type: String, required: false },
useFrameInterpolation: { type: Boolean, required: false },
},
setup(props: DotLottieVueProps, { attrs, expose }: SetupContext): () => VNode {
const canvas: Ref<HTMLCanvasElement | undefined> = ref(undefined);
const { backgroundColor, loop, mode, segment, speed, useFrameInterpolation } = toRefs(props);
let dotLottie: DotLottie | null = null;
let intersectionObserver: IntersectionObserver | null = null;
let resizeObserver: ResizeObserver | null = null;
// Prop change
watch(
() => backgroundColor?.value,
(newVal) => {
if (dotLottie && typeof newVal !== 'undefined') {
dotLottie.setBackgroundColor(newVal);
}
},
);
watch(
() => loop?.value,
(newVal) => {
if (dotLottie && typeof newVal !== 'undefined') {
dotLottie.setLoop(newVal);
}
},
);
watch(
() => mode?.value,
(newVal) => {
if (dotLottie && typeof newVal !== 'undefined') {
dotLottie.setMode(newVal);
}
},
);
watch(
() => segment?.value,
(newVal) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (dotLottie && Array.isArray(newVal) && newVal.length === 2) {
dotLottie.setSegment(newVal[0], newVal[1]);
}
},
);
watch(
() => speed?.value,
(newVal) => {
if (dotLottie && typeof newVal !== 'undefined') {
dotLottie.setSpeed(newVal);
}
},
);
watch(
() => useFrameInterpolation?.value,
(newVal) => {
if (dotLottie && typeof newVal !== 'undefined') {
dotLottie.setUseFrameInterpolation(newVal);
}
},
);
function getIntersectionObserver(): IntersectionObserver {
return new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
dotLottie?.unfreeze();
} else {
dotLottie?.freeze();
}
});
},
{
threshold: 0,
},
);
}
function getResizeObserver(): ResizeObserver {
return new ResizeObserver((entries) => {
entries.forEach(() => {
dotLottie?.resize();
});
});
}
function onVisibilityChange(): void {
if (document.hidden) {
dotLottie?.freeze();
} else {
dotLottie?.unfreeze();
}
}
onMounted(() => {
if (!canvas.value) return;
dotLottie = new DotLottie({
canvas: canvas.value,
...props,
});
intersectionObserver = getIntersectionObserver();
resizeObserver = getResizeObserver();
intersectionObserver.observe(canvas.value);
resizeObserver.observe(canvas.value);
document.addEventListener('visibilitychange', onVisibilityChange);
});
onBeforeUnmount(() => {
resizeObserver?.disconnect();
intersectionObserver?.disconnect();
document.removeEventListener('visibilitychange', onVisibilityChange);
dotLottie?.destroy();
});
expose({
getDotLottieInstance: (): DotLottie | null => dotLottie,
});
return () => h('div', { ...attrs }, h('canvas', { style: 'height: 100%; width: 100%', ref: canvas }));
},
});