forked from muxinc/elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux-uploader-retry.ts
73 lines (57 loc) · 1.88 KB
/
mux-uploader-retry.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
import { globalThis, document } from './polyfills';
import { getMuxUploaderEl } from './utils/element-utils';
import type MuxUploaderElement from './mux-uploader';
const template = document.createElement('template');
template.innerHTML = `
<style>
#retry-button {
color: #e22c3e;
text-decoration-line: underline;
cursor: pointer;
position: relative;
display: none;
}
:host([upload-error]) #retry-button {
display: inline-block;
}
</style>
<span id="retry-button" role="button" tabindex="0">Try again</span>
`;
class MuxUploaderRetryElement extends globalThis.HTMLElement {
retryButton: HTMLElement | null | undefined;
#uploaderEl: MuxUploaderElement | null | undefined;
#abortController: AbortController | undefined;
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(template.content.cloneNode(true));
this.retryButton = this.shadowRoot?.getElementById('retry-button');
}
connectedCallback() {
this.#uploaderEl = getMuxUploaderEl(this);
this.#abortController = new AbortController();
if (this.#uploaderEl) {
const opts = { signal: this.#abortController.signal };
this.retryButton?.addEventListener('click', this.triggerReset, opts);
this.retryButton?.addEventListener('keyup', this.handleKeyup, opts);
}
}
disconnectedCallback() {
this.#abortController?.abort();
}
handleKeyup = (e: KeyboardEvent) => {
const ButtonPressedKeys = ['Enter', ' '];
const { key } = e;
if (!ButtonPressedKeys.includes(key)) {
return;
}
this.triggerReset();
};
triggerReset = () => {
this.#uploaderEl?.dispatchEvent(new CustomEvent('reset'));
};
}
if (!globalThis.customElements.get('mux-uploader-retry')) {
globalThis.customElements.define('mux-uploader-retry', MuxUploaderRetryElement);
}
export default MuxUploaderRetryElement;