import "./App.css"; import { useState } from "react"; function App() { const [closeReturnValue, setCloseReturnValue] = useState(""); const [requestCloseReturnValue, setRequestCloseReturnValue] = useState(""); const [blockRequestClose, setBlockRequestClose] = useState(false); const [requestCloseLog, setRequestCloseLog] = useState([]); const [customCommand, setCustomCommand] = useState(""); function logRequestClose(message) { setRequestCloseLog((prev) => [...prev.slice(-4), message]); } return (

Command attribute examples

Buttons use commandFor to target an element by id and{" "} command to run a built-in or custom action.

Popover

Target: #demo-popover (popover="auto")

Popover content

Dialog: show-modal

Opens #dialog-show-modal as a modal ( HTMLDialogElement.showModal()).

Modal dialog opened with show-modal.

Dialog: close

Closes immediately (HTMLDialogElement.close()). With{" "} value on the button, that string becomes the dialog's{" "} returnValue.

setCloseReturnValue(e.currentTarget.returnValue)} >

Choose how to close:

Last returnValue:{" "} {closeReturnValue || "(none yet)"}

Dialog: request-close

Fires cancel then close ( HTMLDialogElement.requestClose()). Call{" "} preventDefault() on cancel to keep the dialog open.

{ logRequestClose( blockRequestClose ? "cancel (default prevented — dialog stays open)" : "cancel (allowed — dialog will close)", ); if (blockRequestClose) { e.preventDefault(); } }} onClose={(e) => { setRequestCloseReturnValue(e.currentTarget.returnValue); logRequestClose( `close (returnValue: ${e.currentTarget.returnValue || "empty"})`, ); }} >

Try request-close with or without blocking:

Last returnValue:{" "} {requestCloseReturnValue || "(none yet)"}

{requestCloseLog.length > 0 && ( )}

Custom invoker command

Names starting with -- are author-defined. The target receives a command event; handle it in{" "} onCommand (no browser default behavior).

setCustomCommand(e.command)} >

Command target

Last event.command:{" "} {customCommand || "(none yet)"}

); } export default App;