Skip to content

Commit

Permalink
add source to dialog/drawer event
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Feb 10, 2022
1 parent f555a33 commit 3e35b0f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 29 deletions.
30 changes: 22 additions & 8 deletions docs/components/dialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,30 @@ By default, dialogs will close when the user clicks the close button, clicks the

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.

You can use `event.detail.source` to determine what triggered the request to close. This example prevents the dialog from closing when the overlay is clicked, but allows the close button or <kbd>Escape</kbd> to dismiss it.

```html preview
<sl-dialog label="Dialog" class="dialog-deny-close">
This dialog will not close unless you use the button below.
<sl-button slot="footer" variant="primary">Save &amp; Close</sl-button>
This dialog will not close when you click on the overlay.
<sl-button slot="footer" variant="primary">Close</sl-button>
</sl-dialog>

<sl-button>Open Dialog</sl-button>

<script>
const dialog = document.querySelector('.dialog-deny-close');
const openButton = dialog.nextElementSibling;
const saveButton = dialog.querySelector('sl-button[slot="footer"]');
const closeButton = dialog.querySelector('sl-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
saveButton.addEventListener('click', () => dialog.hide());
closeButton.addEventListener('click', () => dialog.hide());
dialog.addEventListener('sl-request-close', event => event.preventDefault());
// Prevent the dialog from closing when the user clicks on the overlay
dialog.addEventListener('sl-request-close', event => {
if (event.detail.source === 'overlay') {
event.preventDefault();
}
});
</script>
```

Expand All @@ -186,17 +193,24 @@ import { SlButton, SlDialog } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);

// Prevent the dialog from closing when the user clicks on the overlay
function handleRequestClose(event) {
if (event.detail.source === 'overlay') {
event.preventDefault();
}
}

return (
<>
<SlDialog
label="Dialog"
open={open}
onSlRequestClose={event => event.preventDefault()}
onSlRequestClose={handleRequestClose}
onSlAfterHide={() => setOpen(false)}
>
This dialog will not close unless you use the button below.
This dialog will not close when you click on the overlay.
<SlButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
Save &amp; Close
Close
</SlButton>
</SlDialog>

Expand Down
25 changes: 20 additions & 5 deletions docs/components/drawer.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,12 @@ By default, drawers will close when the user clicks the close button, clicks the

To keep the drawer open in such cases, you can cancel the `sl-request-close` event. When canceled, the drawer will remain open and pulse briefly to draw the user's attention to it.
You can use `event.detail.source` to determine what triggered the request to close. This example prevents the drawer from closing when the overlay is clicked, but allows the close button or <kbd>Escape</kbd> to dismiss it.
```html preview
<sl-drawer label="Drawer" class="drawer-deny-close">
This drawer will not close unless you use the button below.
<sl-button slot="footer" variant="primary">Save &amp; Close</sl-button>
This drawer will not close when you click on the overlay.
<sl-button slot="footer" variant="primary">Close</sl-button>
</sl-drawer>
<sl-button>Open Drawer</sl-button>
Expand All @@ -362,7 +364,13 @@ To keep the drawer open in such cases, you can cancel the `sl-request-close` eve
openButton.addEventListener('click', () => drawer.show());
closeButton.addEventListener('click', () => drawer.hide());
drawer.addEventListener('sl-request-close', event => event.preventDefault());
// Prevent the drawer from closing when the user clicks on the overlay
drawer.addEventListener('sl-request-close', event => {
if (event.detail.source === 'overlay') {
event.preventDefault();
}
});
</script>
```
Expand All @@ -373,15 +381,22 @@ import { SlButton, SlDrawer } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
// Prevent the drawer from closing when the user clicks on the overlay
function handleRequestClose(event) {
if (event.detail.source === 'overlay') {
event.preventDefault();
}
}
return (
<>
<SlDrawer
label="Drawer"
open={open}
onSlRequestClose={event => event.preventDefault()}
onSlRequestClose={handleRequestClose}
onSlAfterHide={() => setOpen(false)}
>
This drawer will not close unless you use the button below.
This drawer will not close when you click on the overlay.
<SlButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
Save &amp; Close
</SlButton>
Expand Down
1 change: 1 addition & 0 deletions docs/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ _During the beta period, these restrictions may be relaxed in the event of a mis
- 馃毃 BREAKING: the `unit` property of `<sl-format-bytes>` has changed to `byte | bit` instead of `bytes | bits`
- Added `display-label` part to `<sl-select>` [#650](https://github.com/shoelace-style/shoelace/issues/650)
- Added `--spacing` CSS custom property to `<sl-divider>` [#664](https://github.com/shoelace-style/shoelace/pull/664)
- Added `event.detail.source` to the `sl-request-close` event in `<sl-dialog>` and `<sl-drawer>`
- Fixed a bug that caused `<sl-progress-ring>` to render the wrong size when `--track-width` was increased [#656](https://github.com/shoelace-style/shoelace/issues/656)
- Fixed a bug that allowed `<sl-details>` to open and close when disabled using a screen reader [#658](https://github.com/shoelace-style/shoelace/issues/658)
- Fixed a bug in the FormData event polyfill that threw an error in some environments [#666](https://github.com/shoelace-style/shoelace/issues/666)
Expand Down
21 changes: 13 additions & 8 deletions src/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ const hasPreventScroll = isPreventScrollSupported();
* @event sl-after-hide - Emitted after the dialog closes and all animations are complete.
* @event sl-initial-focus - Emitted when the dialog opens and the panel gains focus. Calling `event.preventDefault()`
* will prevent focus and allow you to set it on a different element in the dialog, such as an input or button.
* @event sl-request-close - Emitted when the user attempts to close the dialog by clicking the close button, clicking the
* overlay, or pressing the escape key. Calling `event.preventDefault()` will prevent the dialog from closing. Avoid
* using this unless closing the dialog will result in destructive behavior such as data loss.
* @event {{ source: 'close-button' | 'keyboard' | 'overlay' }} sl-request-close - Emitted when the user attempts to
* close the dialog by clicking the close button, clicking the overlay, or pressing escape. Calling
* `event.preventDefault()` will keep the dialog open. Avoid using this unless closing the dialog will result in
* destructive behavior such as data loss.
*
* @csspart base - The component's base wrapper.
* @csspart overlay - The overlay.
Expand Down Expand Up @@ -123,8 +124,12 @@ export default class SlDialog extends LitElement {
return waitForEvent(this, 'sl-after-hide');
}

private requestClose() {
const slRequestClose = emit(this, 'sl-request-close', { cancelable: true });
private requestClose(source: 'close-button' | 'keyboard' | 'overlay') {
const slRequestClose = emit(this, 'sl-request-close', {
cancelable: true,
detail: { source }
});

if (slRequestClose.defaultPrevented) {
const animation = getAnimation(this, 'dialog.denyClose');
animateTo(this.panel, animation.keyframes, animation.options);
Expand All @@ -137,7 +142,7 @@ export default class SlDialog extends LitElement {
handleKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
event.stopPropagation();
this.requestClose();
this.requestClose('keyboard');
}
}

Expand Down Expand Up @@ -217,7 +222,7 @@ export default class SlDialog extends LitElement {
})}
@keydown=${this.handleKeyDown}
>
<div part="overlay" class="dialog__overlay" @click=${this.requestClose} tabindex="-1"></div>
<div part="overlay" class="dialog__overlay" @click=${() => this.requestClose('overlay')} tabindex="-1"></div>
<div
part="panel"
Expand All @@ -241,7 +246,7 @@ export default class SlDialog extends LitElement {
name="x"
label=${this.localize.term('close')}
library="system"
@click="${this.requestClose}"
@click="${() => this.requestClose('close-button')}"
></sl-icon-button>
</header>
`
Expand Down
21 changes: 13 additions & 8 deletions src/components/drawer/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ const hasPreventScroll = isPreventScrollSupported();
* @event sl-after-hide - Emitted after the drawer closes and all animations are complete.
* @event sl-initial-focus - Emitted when the drawer opens and the panel gains focus. Calling `event.preventDefault()` will
* prevent focus and allow you to set it on a different element in the drawer, such as an input or button.
* @event sl-request-close - Emitted when the user attempts to close the drawer by clicking the close button, clicking the
* overlay, or pressing the escape key. Calling `event.preventDefault()` will prevent the drawer from closing. Avoid
* using this unless closing the drawer will result in destructive behavior such as data loss.
* @event {{ source: 'close-button' | 'keyboard' | 'overlay' }} sl-request-close - Emitted when the user attempts to
* close the drawer by clicking the close button, clicking the overlay, or pressing escape. Calling
* `event.preventDefault()` will keep the drawer open. Avoid using this unless closing the drawer will result in
* destructive behavior such as data loss.
*
* @csspart base - The component's base wrapper.
* @csspart overlay - The overlay.
Expand Down Expand Up @@ -140,8 +141,12 @@ export default class SlDrawer extends LitElement {
return waitForEvent(this, 'sl-after-hide');
}

private requestClose() {
const slRequestClose = emit(this, 'sl-request-close', { cancelable: true });
private requestClose(source: 'close-button' | 'keyboard' | 'overlay') {
const slRequestClose = emit(this, 'sl-request-close', {
cancelable: true,
detail: { source }
});

if (slRequestClose.defaultPrevented) {
const animation = getAnimation(this, 'drawer.denyClose');
animateTo(this.panel, animation.keyframes, animation.options);
Expand All @@ -154,7 +159,7 @@ export default class SlDrawer extends LitElement {
handleKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
event.stopPropagation();
this.requestClose();
this.requestClose('keyboard');
}
}

Expand Down Expand Up @@ -243,7 +248,7 @@ export default class SlDrawer extends LitElement {
})}
@keydown=${this.handleKeyDown}
>
<div part="overlay" class="drawer__overlay" @click=${this.requestClose} tabindex="-1"></div>
<div part="overlay" class="drawer__overlay" @click=${() => this.requestClose('overlay')} tabindex="-1"></div>
<div
part="panel"
Expand All @@ -268,7 +273,7 @@ export default class SlDrawer extends LitElement {
name="x"
label=${this.localize.term('close')}
library="system"
@click=${this.requestClose}
@click=${() => this.requestClose('close-button')}
></sl-icon-button>
</header>
`
Expand Down

0 comments on commit 3e35b0f

Please sign in to comment.