Native Interactivity
Build disclosure widgets, dialogs, and commands with elements the browser already understands.
HTML includes interactive elements with keyboard and accessibility behavior built in. Start there before creating a custom widget with JavaScript.
Disclosure with details
faq.html
<details>
<summary>What progress is saved?</summary>
<p>The current lesson and reading position are stored on this device.</p>
</details>
summary provides the control and accessible name. The open attribute represents the expanded state.
Modal dialogs
dialog.html
<button type="button" id="open-settings">Open settings</button>
<dialog id="settings">
<form method="dialog">
<h2>Settings</h2>
<button value="cancel">Close</button>
</form>
</dialog>
dialog.js
const dialog = document.querySelector("#settings");
document.querySelector("#open-settings")?.addEventListener("click", () => dialog?.showModal());
showModal() manages top-layer placement and focus containment. The page still needs a clear opening control, close path, useful heading, and sensible focus return.
Keep this
Prefer native controls, expose state through attributes, and add JavaScript only for the behavior HTML does not provide.
学习位置已保存在此设备上。