Skip to content

Commit 3774688

Browse files
committed
Fix confirm and alert dialogs.
1 parent 878697a commit 3774688

13 files changed

Lines changed: 199 additions & 28 deletions

File tree

element.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default {
1010
}, {
1111
label: 'Modals',
1212
extends: ['PgModal'],
13-
include: ['PgModal', 'PgModalAlert'],
13+
include: ['PgModal', 'PgModalAlert', 'PgModalConfirm'],
1414
}],
1515
themes: [{
1616
label: 'UI3',

src/components/pg/modal/__examples__/basic/basic.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import template from './basic.html';
99
})
1010
export class XMyModal extends PgModal {
1111

12+
@Prop() header: string = '';
13+
@Prop() message: string = '';
14+
15+
1216
@Part() $close: HTMLButtonElement;
1317

1418
connectedCallback() {
@@ -34,7 +38,7 @@ export default class XPgModalBasic extends HTMLElement {
3438
async handleClick() {
3539
const result = await XMyModal.open({
3640
header: 'Delete Item',
37-
message: 'Are you sure you want to delete the item?'
41+
message: 'Are you sure you want to delete the item?',
3842
});
3943
this.$result.textContent = `${result}`;
4044
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
# `PgModalAlert`
22

3-
The `PgModalAlert` creates a alert box above everything.
3+
The `PgModalAlert` creates a alert box dialog. For options like okay/cancel use `PgModalConfirm`.
44

55
```typescript
6-
import PgModalAlert from '@pictogrammers/components/pgModalAlert';
6+
import PgModalConfirm from '@pictogrammers/components/pg/modalAlert';
77
```
88

99
```typescript
1010
const result = await PgModalAlert.open({
11-
header: 'Delete Item',
12-
message: 'Are you sure you want to delete the item?'
11+
header: 'Something Went Wrong',
12+
message: 'Some information to alert about.',
1313
});
14-
if (result) {
15-
console.log('Item has been deleted.');
16-
}
14+
// we don't care about result
15+
console.log('Okay was clicked');
1716
```

src/components/pg/modalAlert/__examples__/basic/basic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export default class XPgModalAlertBasic extends HTMLElement {
1818

1919
async handleClick() {
2020
const result = await PgModalAlert.open({
21-
header: 'Delete Item',
22-
message: 'Are you sure you want to delete the item?'
21+
header: 'Something Went Wrong',
22+
message: 'Some information to alert about.',
2323
});
2424
this.$result.textContent = `${result}`;
2525
}

src/components/pg/modalAlert/modalAlert.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
<h2 id="dialog_label"
99
class="dialog_label"
1010
part="headerText">
11-
Add Delivery Address
11+
Alert
1212
</h2>
1313
</header>
1414
<main>
1515
<p part="message"></p>
1616
</main>
1717
<footer>
18-
<pg-button part="no">No</pg-button>
19-
<pg-button part="yes" variant="brand">Yes</pg-button>
18+
<pg-button part="okay" variant="brand">Okay</pg-button>
2019
</footer>
2120
</div>
2221
</div>

src/components/pg/modalAlert/modalAlert.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ export default class PgModalAlert extends PgOverlay {
1818
@Part() $header: HTMLDivElement;
1919
@Part() $headerText: HTMLHeadingElement;
2020
@Part() $message: HTMLDivElement;
21-
@Part() $yes: PgButton;
22-
@Part() $no: PgButton;
21+
@Part() $okay: PgButton;
2322

2423
#cacheKeydownHandler: any;
2524

2625
connectedCallback() {
27-
this.$yes.addEventListener('click', this.#handleYes.bind(this));
28-
this.$no.addEventListener('click', this.#handleNo.bind(this));
26+
this.$okay.addEventListener('click', this.#handleOkay.bind(this));
2927
this.#cacheKeydownHandler ??= this.#handleKeyDown.bind(this);
3028
document.addEventListener('keydown', this.#cacheKeydownHandler);
3129
}
@@ -40,20 +38,16 @@ export default class PgModalAlert extends PgOverlay {
4038
}
4139
}
4240

43-
#handleYes() {
44-
this.close(true);
45-
}
46-
47-
#handleNo() {
48-
this.close(false);
41+
#handleOkay() {
42+
this.close();
4943
}
5044

5145
render(changes) {
5246
if (changes.header) {
53-
this.$headerText.innerText = this.header;
47+
this.$headerText.textContent = this.header;
5448
}
5549
if (changes.message) {
56-
this.$message.innerText = this.message;
50+
this.$message.textContent = this.message;
5751
}
5852
}
5953
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# `PgModalConfirm`
2+
3+
The `PgModalConfirm` creates a confirm box dialog.
4+
5+
```typescript
6+
import PgModalConfirm from '@pictogrammers/components/pg/modalConfirm';
7+
```
8+
9+
```typescript
10+
const result = await PgModalConfirm.open({
11+
header: 'Delete Item',
12+
message: 'Are you sure you want to delete the item?',
13+
okay: 'Delete',
14+
cancel: 'Keep Item',
15+
});
16+
if (result) {
17+
console.log('Item has been deleted.');
18+
}
19+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="example">
2+
<button part="button">Launch Confirm Modal</button>
3+
<div><code>Result: </code><code part="result"></code></div>
4+
</div>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Component, Part, Prop } from '@pictogrammers/element';
2+
import PgModalConfirm from '../../modalConfirm';
3+
4+
import template from './basic.html';
5+
6+
@Component({
7+
selector: 'x-pg-modal-confirm-basic',
8+
template
9+
})
10+
export default class XPgModalConfirmBasic extends HTMLElement {
11+
12+
@Part() $button: HTMLButtonElement;
13+
@Part() $result: HTMLSpanElement;
14+
15+
connectedCallback() {
16+
this.$button.addEventListener('click', this.handleClick.bind(this));
17+
}
18+
19+
async handleClick() {
20+
const result = await PgModalConfirm.open({
21+
header: 'Delete Item',
22+
message: 'Are you sure you want to delete the item?',
23+
okay: 'Delete',
24+
cancel: 'Keep Item',
25+
});
26+
this.$result.textContent = `${result}`;
27+
}
28+
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
.backdrop {
2+
display: flex;
3+
position: fixed;
4+
top: 0;
5+
left: 0;
6+
right: 0;
7+
bottom: 0;
8+
background: rgba(0, 0, 0, 0.6);
9+
justify-content: center;
10+
align-items: center;
11+
}
12+
.dialog {
13+
background: #fff;
14+
border-radius: 0.5rem;
15+
box-shadow: 0 1px 1rem rgba(0, 0, 0, 0.5);
16+
overflow: hidden;
17+
min-width: 15rem;
18+
}
19+
header {
20+
border-bottom: 1px solid #ccc;
21+
background: #f1f1f1;
22+
padding: 0.75rem 1rem;
23+
}
24+
header h2 {
25+
font-size: 1.25rem;
26+
margin: 0;
27+
font-weight: normal;
28+
}
29+
main {
30+
padding: 0.5rem 1rem;
31+
}
32+
footer {
33+
display: flex;
34+
flex-direction: row;
35+
padding: 0.75rem 1rem;
36+
border-top: 1px solid #ccc;
37+
background: #f1f1f1;
38+
justify-content: flex-end;
39+
gap: 0.5rem;
40+
}

0 commit comments

Comments
 (0)