Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed

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

## [0.3.0] - 2024-03-08
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/elements/dialog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ button.addEventListener('click', openDialog);

## Examples

### Show dialog by default

You can add the `open` attribute on the `twc-dialog` element, and the dialog will be shown by default.

```html
<twc-dialog open>
<dialog data-target="twc-dialog.dialog">Contents</dialog>
</twc-dialog>
```

### Programmatically toggling the visiblity state

```html
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/elements/dialog/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ describe('Dialog', async () => {
expect(document.body).not.to.have.style('overflow', 'hidden');
});

it('opens the dialog by default', async () => {
const el = await fixture<DialogElement>(html`
<twc-dialog open>
<dialog data-target="twc-dialog.dialog">Contents</dialog>
</twc-dialog>
`);
const dialog = el.querySelector('dialog')!;

assertDialogShown(el, dialog);
expect(document.body).to.have.style('overflow', 'hidden');
});

it('closes the dialog when clicked outside', async () => {
const el = await fixture<DialogElement>(html`
<twc-dialog>
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/elements/dialog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export default class DialogElement extends ImpulseElement {
this.handleClose = this.handleClose.bind(this);
}

/**
* Called when the element is connected to the DOM.
*/
connected() {
if (this.open) {
this.showModal();
}
}

/**
* Called when the element is removed from the DOM.
*/
Expand Down