Skip to content

Commit 2503350

Browse files
authored
fix(dialog): show dialog by default (#37)
If the `open` attribute is initially present on the `twc-dialog` element, we want to show the dialog.
1 parent 74a4c49 commit 2503350

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
### Fixed
1010

11+
- Show dialog by default if `twc-dialog` element has the `open` attribute
1112
- Nested dialog resets `body` padding
1213

1314
## [0.3.0] - 2024-03-08

packages/core/src/elements/dialog/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ button.addEventListener('click', openDialog);
3131

3232
## Examples
3333

34+
### Show dialog by default
35+
36+
You can add the `open` attribute on the `twc-dialog` element, and the dialog will be shown by default.
37+
38+
```html
39+
<twc-dialog open>
40+
<dialog data-target="twc-dialog.dialog">Contents</dialog>
41+
</twc-dialog>
42+
```
43+
3444
### Programmatically toggling the visiblity state
3545

3646
```html

packages/core/src/elements/dialog/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ describe('Dialog', async () => {
4949
expect(document.body).not.to.have.style('overflow', 'hidden');
5050
});
5151

52+
it('opens the dialog by default', async () => {
53+
const el = await fixture<DialogElement>(html`
54+
<twc-dialog open>
55+
<dialog data-target="twc-dialog.dialog">Contents</dialog>
56+
</twc-dialog>
57+
`);
58+
const dialog = el.querySelector('dialog')!;
59+
60+
assertDialogShown(el, dialog);
61+
expect(document.body).to.have.style('overflow', 'hidden');
62+
});
63+
5264
it('closes the dialog when clicked outside', async () => {
5365
const el = await fixture<DialogElement>(html`
5466
<twc-dialog>

packages/core/src/elements/dialog/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export default class DialogElement extends ImpulseElement {
1919
this.handleClose = this.handleClose.bind(this);
2020
}
2121

22+
/**
23+
* Called when the element is connected to the DOM.
24+
*/
25+
connected() {
26+
if (this.open) {
27+
this.showModal();
28+
}
29+
}
30+
2231
/**
2332
* Called when the element is removed from the DOM.
2433
*/

0 commit comments

Comments
 (0)