Skip to content

Commit 7d8c6e4

Browse files
authored
Merge pull request #71 from bakura10/my-request-close-changes
2 parents a92dbd8 + 64f4cf5 commit 7d8c6e4

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ With the module imported, you can add `commandfor` and `command` attributes to y
6060
<dialog id="my-dialog">I'm a dialog!</dialog>
6161
```
6262

63+
## Supported commands
64+
65+
The following built-in commands (aligned with current spec) are supported:
66+
67+
* `toggle-popover`/`open-popover`/`close-popover`
68+
* `show-modal`: open a `<dialog>` element in modal mode
69+
* `close`: close a `<dialog>` open (either in modal or non-modal mode)
70+
* `request-close`: close a `<dialog>` but emit a `cancel` event first, allowing a user to eventually prevent it. `requestClose` is only available from Safari 18.4, the `requestClose` will be polyfilled on older browsers.
71+
6372
## Limitations
6473

6574
This polyfill does not handle the aria (e.g. `aria-expanded`) of the command button the way browsers do. You are _strongly_ encouraged to handle this state yourself, to ensure your site is accessible.

invoker.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export function apply() {
205205
const valueLower = value.toLowerCase();
206206
switch (valueLower) {
207207
case "show-modal":
208+
case "request-close":
208209
case "close":
209210
case "toggle-popover":
210211
case "hide-popover":
@@ -338,6 +339,7 @@ export function apply() {
338339
source.command !== "hide-popover" &&
339340
source.command !== "toggle-popover" &&
340341
source.command !== "show-modal" &&
342+
source.command !== "request-close" &&
341343
source.command !== "close" &&
342344
!source.command.startsWith("--")
343345
) {
@@ -373,13 +375,25 @@ export function apply() {
373375
}
374376
} else if (invokee.localName === "dialog") {
375377
const canShow = !invokee.hasAttribute("open");
376-
const shouldShow = canShow && command === "show-modal";
377-
const shouldHide = !canShow && command === "close";
378378

379-
if (shouldShow) {
379+
if (canShow && command == "show-modal") {
380380
invokee.showModal();
381-
} else if (shouldHide) {
382-
invokee.close();
381+
} else if (!canShow && command == "close") {
382+
invokee.close(source.value ? source.value : undefined);
383+
} else if (!canShow && command == "request-close") {
384+
// requestClose is only supported from Safari 18.4, so we polyfill it on older browsers
385+
if (!HTMLDialogElement.prototype.requestClose) {
386+
HTMLDialogElement.prototype.requestClose = function() {
387+
const cancelEvent = new Event('cancel', { cancelable: true });
388+
this.dispatchEvent(cancelEvent);
389+
390+
if (!cancelEvent.defaultPrevented) {
391+
this.close();
392+
}
393+
};
394+
}
395+
396+
invokee.requestClose(source.value ? source.value : undefined);
383397
}
384398
}
385399
}

0 commit comments

Comments
 (0)