shoelace/docs/components/dialog.md

134 wiersze
4.6 KiB
Markdown
Czysty Zwykły widok Historia

2020-07-15 21:30:37 +00:00
# Dialog
[component-header:sl-dialog]
2020-10-13 13:12:31 +00:00
Dialogs, sometimes called "modals", appear above the page and require the user's immediate attention.
2020-07-15 21:30:37 +00:00
```html preview
<sl-dialog label="Dialog" class="dialog-overview">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<sl-button slot="footer" type="primary">Close</sl-button>
</sl-dialog>
<sl-button>Open Dialog</sl-button>
<script>
2021-03-30 12:34:44 +00:00
const dialog = document.querySelector('.dialog-overview');
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector('sl-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
closeButton.addEventListener('click', () => dialog.hide());
2020-07-15 21:30:37 +00:00
</script>
```
## UX Tips
- Use a dialog when you immediately require the user's attention, e.g. confirming a destructive action.
- Always provide an obvious way for the user to dismiss the dialog.
- Don't nest dialogs. It almost always leads to a poor experience for the user.
## Examples
### Custom Width
Use the `--width` custom property to set the dialog's width.
```html preview
<sl-dialog label="Dialog" class="dialog-width" style="--width: 50vw;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<sl-button slot="footer" type="primary">Close</sl-button>
</sl-dialog>
<sl-button>Open Dialog</sl-button>
<script>
2021-03-30 12:34:44 +00:00
const dialog = document.querySelector('.dialog-width');
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector('sl-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
closeButton.addEventListener('click', () => dialog.hide());
2020-07-15 21:30:37 +00:00
</script>
```
### Scrolling
By design, a dialog's height will never exceed that of the viewport. As such, dialogs will not scroll with the page ensuring the header and footer are always accessible to the user.
```html preview
<sl-dialog label="Dialog" class="dialog-scrolling">
2021-08-06 12:32:46 +00:00
<div style="height: 150vh; border: dashed 2px rgb(var(--sl-color-neutral-200)); padding: 0 1rem;">
2020-07-15 21:30:37 +00:00
<p>Scroll down and give it a try! 👇</p>
</div>
<sl-button slot="footer" type="primary">Close</sl-button>
</sl-dialog>
<sl-button>Open Dialog</sl-button>
<script>
2021-03-30 12:34:44 +00:00
const dialog = document.querySelector('.dialog-scrolling');
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector('sl-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
closeButton.addEventListener('click', () => dialog.hide());
2020-07-15 21:30:37 +00:00
</script>
```
2021-06-21 13:40:11 +00:00
### Preventing the Dialog from Closing
2020-07-15 21:30:37 +00:00
2021-06-21 13:40:11 +00:00
By default, dialogs will close when the user clicks the close button, clicks the overlay, or presses the <kbd>Escape</kbd> key. In most cases, the default behavior is the best behavior in terms of UX. However, there are situations where this may be undesirable, such as when data loss will occur.
To keep the dialog open in such cases, you can cancel the `sl-request-close` event. When canceled, the dialog will remain open and pulse briefly to draw the user's attention to it.
2020-07-15 21:30:37 +00:00
```html preview
2021-06-21 13:40:11 +00:00
<sl-dialog label="Dialog" class="dialog-deny-close">
This dialog will not close unless you use the button below.
<sl-button slot="footer" type="primary">Save &amp; Close</sl-button>
2020-07-15 21:30:37 +00:00
</sl-dialog>
<sl-button>Open Dialog</sl-button>
<script>
2021-06-21 13:40:11 +00:00
const dialog = document.querySelector('.dialog-deny-close');
2021-03-30 12:34:44 +00:00
const openButton = dialog.nextElementSibling;
2021-06-21 13:40:11 +00:00
const saveButton = dialog.querySelector('sl-button[slot="footer"]');
2020-07-15 21:30:37 +00:00
2021-03-30 12:34:44 +00:00
openButton.addEventListener('click', () => dialog.show());
2021-06-21 13:40:11 +00:00
saveButton.addEventListener('click', () => dialog.hide());
2020-07-15 21:30:37 +00:00
2021-06-21 13:40:11 +00:00
dialog.addEventListener('sl-request-close', event => event.preventDefault());
2020-07-15 21:30:37 +00:00
</script>
```
### Customizing Initial Focus
2021-07-01 00:03:34 +00:00
By default, the dialog's panel will gain focus when opened. This allows the first tab press to focus on the first tabbable element within the dialog. To set focus on a different element, listen for and cancel the `sl-initial-focus` event.
```html preview
<sl-dialog label="Dialog" class="dialog-focus">
<sl-input placeholder="I will have focus when the dialog is opened"></sl-input>
<sl-button slot="footer" type="primary">Close</sl-button>
</sl-dialog>
<sl-button>Open Dialog</sl-button>
<script>
2021-03-30 12:34:44 +00:00
const dialog = document.querySelector('.dialog-focus');
const input = dialog.querySelector('sl-input');
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector('sl-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
closeButton.addEventListener('click', () => dialog.hide());
dialog.addEventListener('sl-initial-focus', event => {
event.preventDefault();
input.focus({ preventScroll: true });
2021-03-30 12:34:44 +00:00
});
</script>
```
2020-07-15 21:30:37 +00:00
[component-metadata:sl-dialog]