diff --git a/files/en-us/web/api/htmldialogelement/closedby/index.md b/files/en-us/web/api/htmldialogelement/closedby/index.md new file mode 100644 index 000000000000000..2b5bff0ae1a6a72 --- /dev/null +++ b/files/en-us/web/api/htmldialogelement/closedby/index.md @@ -0,0 +1,56 @@ +--- +title: "HTMLDialogElement: closedBy property" +short-title: closedBy +slug: Web/API/HTMLDialogElement/closedBy +page-type: web-api-instance-property +browser-compat: api.HTMLDialogElement.closedBy +--- + +{{ APIRef("HTML DOM") }} + +The **`closedBy`** property of the +{{domxref("HTMLDialogElement")}} interface indicates the types of user actions that can be used to close the associated {{htmlelement("dialog")}} element. It sets or returns the dialog's [`closedby`](/en-US/docs/Web/HTML/Reference/Elements/dialog#closedby) attribute value. + +## Value + +A string; possible values are: + +- `any` + - : The dialog can be dismissed with a light dismiss user action, a platform-specific user action, or a developer-specified mechanism. +- `closerequest` + - : The dialog can be dismissed with a platform-specific user action or a developer-specified mechanism. +- `none` + - : The dialog can only be dismissed with a developer-specified mechanism. + +## Examples + +### Basic `closedBy` usage + +```html + +

My dialog

+

+ Closable using the Esc key, or by clicking outside the dialog. "Light + dismiss" behavior. +

+
+``` + +```js +const dialogElem = document.querySelector("dialog"); + +// Logs "any" to the console +console.log(dialogElem.closedBy); +``` + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- {{ HTMLElement("dialog") }} diff --git a/files/en-us/web/api/htmldialogelement/index.md b/files/en-us/web/api/htmldialogelement/index.md index 5901a0045e47a5c..6649133329bad0b 100644 --- a/files/en-us/web/api/htmldialogelement/index.md +++ b/files/en-us/web/api/htmldialogelement/index.md @@ -18,6 +18,8 @@ The **`HTMLDialogElement`** interface provides methods to manipulate {{HTMLEleme _Also inherits properties from its parent interface, {{domxref("HTMLElement")}}._ +- {{domxref("HTMLDialogElement.closedBy")}} + - : A string that sets or returns the [`closedby`](/en-US/docs/Web/HTML/Reference/Elements/dialog#closedby) attribute value of the `` element, which indicates the types of user actions that can be used to close the dialog. - {{domxref("HTMLDialogElement.open")}} - : A boolean value reflecting the [`open`](/en-US/docs/Web/HTML/Reference/Elements/dialog#open) HTML attribute, indicating whether the dialog is available for interaction. - {{domxref("HTMLDialogElement.returnValue")}} diff --git a/files/en-us/web/html/reference/elements/dialog/index.md b/files/en-us/web/html/reference/elements/dialog/index.md index 022293f45ddf283..a8bd410e055a5cf 100644 --- a/files/en-us/web/html/reference/elements/dialog/index.md +++ b/files/en-us/web/html/reference/elements/dialog/index.md @@ -20,6 +20,28 @@ This element includes the [global attributes](/en-US/docs/Web/HTML/Reference/Glo > [!WARNING] > The `tabindex` attribute must not be used on the `` element. See [usage notes](#usage_notes). +- `closedby` + + - : Specifies the types of user actions that can be used to close the `` element. This attribute distinguishes three methods by which a dialog might be closed: + + - A _light dismiss user action_, in which the `` is closed when the user clicks or taps outside it. This is equivalent to the ["light dismiss" behavior of "auto" state popovers](/en-US/docs/Web/API/Popover_API/Using#auto_state_and_light_dismiss). + - A _platform-specific user action_, such as pressing the Esc key on desktop platforms, or a "back" or "dismiss" gesture on mobile platforms. + - A developer-specified mechanism such as a {{htmlelement("button")}} with a [`click`](/en-US/docs/Web/API/Element/click_event) handler that invokes {{domxref("HTMLDialogElement.close()")}} or a {{htmlelement("form")}} submission. + + Possible values are: + + - `any` + - : The dialog can be dismissed using any of the three methods. + - `closerequest` + - : The dialog can be dismissed with a platform-specific user action or a developer-specified mechanism. + - `none` + - : The dialog can only be dismissed with a developer-specified mechanism. + + If the `` element does not have a valid `closedby` value specified, then + + - if it was opened using {{domxref("HTMLDialogElement.showModal()", "showModal()")}}, it behaves as if the value was `"closerequest"` + - otherwise, it behaves as if the value was `"none"`. + - `open` - : Indicates that the dialog box is active and is available for interaction. If the `open` attribute is not set, the dialog box will not be visible to the user. @@ -272,6 +294,116 @@ jsCloseBtn.addEventListener("click", (e) => { From the output, we see it is impossible to close the dialog using the _Normal close_ button. But the dialog can be closed if we bypass the form validation using the `formnovalidate` attribute on the _Cancel_ button. Programmatically, `dialog.close()` will also close such dialog. +### Comparison of different closedby behaviors + +This example demonstrates the difference in behavior between different values of the [`closedby`](#closedby) attribute. + +#### HTML + +We provide three {{htmlelement("button")}} elements and three `` elements. Each button will be programmed to open a different dialog that demonstrates the behavior of one of the three values of the `closedby` attribute — `none`, `closerequest`, and `any`. Note that each `` element contains a ` + + + + + +

closedby="none"

+

+ Only closable using a specific provided mechanism, which in this case is + pressing the "Close" button below. +

+ +
+ + +

closedby="closerequest"

+

Closable using the "Close" button or the Esc key.

+ +
+ + +

closedby="any"

+

+ Closable using the "Close" button, the Esc key, or by clicking outside the + dialog. "Light dismiss" behavior. +

+ +
+``` + +```css hidden live-sample___closedbyvalues +body { + font-family: sans-serif; +} + +#controls { + display: flex; + justify-content: space-around; +} + +dialog { + width: 480px; + border-radius: 5px; + border-color: rgb(0 0 0 / 0.3); +} + +dialog h2 { + margin: 0; +} + +dialog p { + line-height: 1.4; +} +``` + +#### JavaScript + +Here we assign different variables to reference the main control `