Tired of writing useState(false) and onClick handlers every time you need a modal? The browser already has a declarative API for that.
๐งฉ Overview
Learn how to open and close a <dialog> using only HTML attributes โ no React state, no useState, no event handlers at all. The Invoker Commands API lets buttons control dialogs natively.
๐ https://www.reactchallenges.com/challenges/invoker-modal
โ Requirements
- Wire the "Contact Us" button to open the dialog using
commandandcommandforattributes - Wire the "Close" button to close the dialog
- The dialog must start closed
- Keep the existing form fields and
data-testidattributes intact - Do not remove the TypeScript module augmentation block
๐ก Notes
- A button uses
commandandcommandforto tell the browser to callshowModal()orclose()โ no JavaScript needed. -
useId()from React is the idiomatic way to generate a unique id for linking button to dialog. - The
declare module "react"block already addscommandandcommandforto React's button types.
๐งช Tests
- Renders the app title
- Renders the open button with command attributes
- Matches the button commandfor with the dialog id
- Dialog does not have the open attribute initially
- Close button has command close with matching commandfor
This challenge is perfect for developers who want to rely more on the platform and write less JavaScript. You'll learn a native browser API that makes modals simpler, more accessible, and more resilient.
Top comments (0)