Skip to content

Conversation

dletorey
Copy link
Contributor

@dletorey dletorey commented Oct 7, 2025

Description

  • Added working example for custom values of command attribute on:
    • API commandEvent() page
    • HTMLButtonElement: command property page
  • Added working examle of request-close value for command attribute

Motivation

Related issues and pull requests

@dletorey dletorey requested review from a team as code owners October 7, 2025 15:49
@dletorey dletorey requested review from estelle and sideshowbarker and removed request for a team October 7, 2025 15:49
@github-actions github-actions bot added Content:HTML Hypertext Markup Language docs Content:WebAPI Web API docs labels Oct 7, 2025
@github-actions github-actions bot added the size/m [PR only] 51-500 LoC changed label Oct 7, 2025
@dletorey dletorey requested review from dipikabh and pepelsbey and removed request for estelle and sideshowbarker October 7, 2025 15:50
Copy link
Contributor

github-actions bot commented Oct 7, 2025

dletorey and others added 6 commits October 7, 2025 16:53
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
dletorey and others added 5 commits October 7, 2025 16:55
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
dletorey and others added 8 commits October 7, 2025 16:57
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@dipikabh
Copy link
Contributor

dipikabh commented Oct 8, 2025

Would these also need to be updated in BCD (https://github.com/mdn/browser-compat-data/blob/main/html/elements/button.json):

  • command
  • commandfor
  • request-close

They all link to the same bug: ⁨bug 1983523⁩

Copy link
Contributor

@dipikabh dipikabh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review for the <button> page


## Examples

### Basic example
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Basic example
### Creating a basic button

```

{{ EmbedLiveSample('Example', 200, 64) }}
{{ EmbedLiveSample('basic_example', 200, 64) }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the macro will need updating if you accept the title change

Comment on lines 229 to 231
```html
<button name="button">Press me</button>
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize that this example was already on the page and not one you've added. Since we're touching it here anyway, sharing some thoughts I have for improving it slightly. LMK what you think.

If we don't mention the type, its default value is submit but that's true when the button is inside a form. In this case, since we're not showing a form or explaining any other context for the button, let's explicitly specify the type as a best practice. Also suggesting a different name for the button to hint that it doesn't do anything.

Suggested change
```html
<button name="button">Press me</button>
```
This example creates a clickable button.
The `type="button"` attribute ensures the button has no default behavior.
You can make this button interactive using JavaScript or attributes such as `command` and `commandfor`.
```html
<button type="button" name="button">I'm a button</button>

{{ EmbedLiveSample('Example', 200, 64) }}
{{ EmbedLiveSample('basic_example', 200, 64) }}

### Command request-close
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Command request-close
### Using the `request-close` value for the `command` attribute


### Command request-close

This example uses the `request-close` value for the command attribute to check if a dialog is [`cancelable`](/en-US/docs/Web/API/Event/cancelable) when clicking a close button. When clicking the close button inside the dialog it checks to see if the radio button is set to yes, if not the dialog will not close.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the UI description before the code and move all explanations about what's happening behind the scenes to after the Result frame.

So here, we can keep it as:

Suggested change
This example uses the `request-close` value for the command attribute to check if a dialog is [`cancelable`](/en-US/docs/Web/API/Event/cancelable) when clicking a close button. When clicking the close button inside the dialog it checks to see if the radio button is set to yes, if not the dialog will not close.
The dialog in this example has two radio buttons that control whether or not the dialog can be closed.
Select **Yes** or **No**, and then click **Request to Close** to try to close the dialog.
If **Yes** is selected, the dialog closes; if **No** is selected, the dialog stays open and shows a message instead.

<dialog id="mydialog">
<div class="wrapper">
<form>
<legend>Would you like to be able to close this dialog:</legend>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

Suggested change
<legend>Would you like to be able to close this dialog:</legend>
<legend>Allow this dialog to close when requested?</legend>

Comment on lines +243 to +244
<form>
<legend>Would you like to be able to close this dialog:</legend>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a fieldset to the form?

Suggested change
<form>
<legend>Would you like to be able to close this dialog:</legend>
<form>
<fieldset>
<legend>Would you like to be able to close this dialog:</legend>

Request to Close
</button>
<p class="warning" hidden>
You have not selected yes so can not close this dialog.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You have not selected yes so can not close this dialog.
You must choose "Yes" to close this dialog.

});
```

{{ EmbedLiveSample('command_request-close', 200) }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macro will need updating if changing the section title

```

{{ EmbedLiveSample('command_request-close', 200) }}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's explain here (after the result frame) what's happening behind the scenes. Just jotting down some rough points to outline the flow:

  • The Open Dialog button opens the dialog using command="show-modal".
  • The Request to Close button has command="request-close" and commandfor="mydialog". So when it's clicked, it asks the dialog if it can be closed (unlike the close command, which would close the dialog immediately).
  • This triggers a cancelable cancel event on the dialog.
  • The script listening for this event calls preventDefault() when No is selected. in turn keeping dialog open and showing message.
  • If event is not canceled (when Yes is selected), the browser closes the dialog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:HTML Hypertext Markup Language docs Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants