-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathremove-button.js
More file actions
95 lines (83 loc) · 2.56 KB
/
remove-button.js
File metadata and controls
95 lines (83 loc) · 2.56 KB
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
import { LitElement, html } from 'da-lit';
const { default: getStyle } = await import('../../../../../utils/styles.js');
const style = await getStyle(import.meta.url);
/**
* A two-stage confirmation remove button.
* First click: Shows checkmark (confirmation state)
* Second click: Dispatches 'remove-item' event
* Auto-reverts to trash icon after 3 seconds if not confirmed
*/
class RemoveButton extends LitElement {
static properties = {
path: { type: String },
index: { type: Number },
confirmState: { state: true },
};
constructor() {
super();
this.path = '';
this.index = null;
this.confirmState = false;
this.timeoutId = null;
}
connectedCallback() {
super.connectedCallback();
this.shadowRoot.adoptedStyleSheets = [style];
}
disconnectedCallback() {
this.clearConfirmTimeout();
super.disconnectedCallback();
}
clearConfirmTimeout() {
if (this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId = null;
}
}
handleClick(e) {
e.stopPropagation();
if (this.confirmState) {
this.clearConfirmTimeout();
this.confirmState = false;
this.dispatchEvent(new CustomEvent('remove-item', {
detail: { path: this.path },
bubbles: true,
composed: true,
}));
return;
}
this.confirmState = true;
this.timeoutId = setTimeout(() => {
this.confirmState = false;
this.timeoutId = null;
}, 3000);
}
render() {
const indexLabel = this.index != null ? `Remove item ${this.index}` : 'Remove item';
const ariaLabel = this.confirmState ? 'Confirm removal' : indexLabel;
const title = this.confirmState
? 'Click to confirm removal'
: 'Remove this item';
return html`
<button
class="remove-btn ${this.confirmState ? 'confirm-state' : ''}"
@click=${this.handleClick}
title="${title}"
aria-label="${ariaLabel}"
>
${this.confirmState
? html`<span class="check-icon">✓</span>`
: html`
<svg class="trash-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<polyline points="3 6 5 6 21 6"/>
<path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/>
<path d="M10 11v6"/>
<path d="M14 11v6"/>
<path d="M9 6V4a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v2"/>
</svg>
`}
</button>
`;
}
}
customElements.define('remove-button', RemoveButton);