shadcn.io is not affiliated with official shadcn/ui
API Key Dialog Block for React
A keys page with its create-key dialog shown open: a name, scopes and an expiry in the first step, then the secret shown once, masked behind an eye button, with a Copy button and a box that has to be ticked before Done.
Installation
Overview
Let developers make a key and keep its secret safe, with this dialog for React with Base UI. It opens from the API keys page of Tillbridge, a point-of-sale API: the header holds an outline Create API key Button, and the keys table, with a row of bars for each key it already has, is drawn under a soft scrim inside a hairline frame with a plus mark on each corner. The Dialog makes the key in two steps without leaving the frame. First a Field with an Input names it, four Checkboxes pick its scopes with a count beside them, and a Native Select says when it stops working, with the date spelled out under it and a warning for never. Then the secret appears in a Password Input that masks it until the eye is pressed, beside a Copy Button, with a line saying it is shown only once and a box, "I have stored this key", that has to be ticked before Done. The new key's row, its name, prefix, scope count and expiry in words, appears at the top of the drawn table. Create key, the only Shine Button, becomes Done in the second step. For preview the dialog is drawn open in place when the page loads; the header button opens the real Dialog, with the same fields and the same keys. For developer consoles, tokens, webhooks and any secret that is shown once.
FAQ
Why is the dialog already open, and what do I keep?
So you can see it without a click. On load the dialog is a copy drawn in place over the page: a native dialog element that isn't modal, so nothing is trapped, hidden or focused, and the page around the preview keeps working. The Create API key button opens the real Dialog, modal, with its focus trap, Escape and focus return, and the copy stays closed until a reload. Both show the same KeyFields component and share the block's state, so a name typed in the copy is there in the real one. In your app keep the Dialog and drop the copy: the "in-place" state, the scrim and the dialog element inside the frame.
Is the secret really generated?
No. SECRETS holds four typed placeholders and each key you make takes the next, so a reload repeats the same sequence. Nothing is random and nothing is stored: the secret is held in the page and gone once the dialog closes, which is what the dialog tells the visitor. In your app have your server make the secret when you press Create key, store only a hash of it, return it in that one response, and show it in RevealStep. Never show it again after that, in the list or anywhere else.
How does shown once work, and what if I close early?
The key exists from the moment you press Create key: its row is in the table and its prefix is all the page keeps. The second step shows the secret masked, with an eye button to reveal it and a Copy button that copies the whole secret either way. Done stays quiet until you tick that you have stored it, and pressing it early says so and moves focus to the box. Closing in any other way, with Escape, Cancel or the close button, is allowed and says in a polite message that the secret is gone and a new key is needed, since a key nobody has is no use and the list lets people revoke it.
What does Create key do here, and how do I wire it up?
Here it adds a row to the drawn table in local state and shows a placeholder secret; nothing is created or sent. Name, scopes and expiry are kept in the row, and the expiry date is worked out from AS_OF, a fixed day, in UTC. In your app post the form to your server in create, show a failure in the message above the buttons, keep the first step's values if it fails, and let your server pick the real prefix. Copy uses the browser's clipboard, which needs a page served over HTTPS and a user press, and says when the clipboard refuses.
Where does focus go?
The real dialog is named by its title, which changes with the step, and described by its sentence. Focus starts in the name field, moves to the secret when it appears, and goes to the first thing missing when a press is refused. Escape and Cancel close it and return focus to the Create API key button. The trigger is passed to DialogTrigger through render, and the dialog is controlled with open and onOpenChange. The copy shown on load moves no focus; Escape inside it, Cancel and its close button close it and put focus on the button.
How does it look on a phone?
The copy shown on load docks to the foot of the frame as a bottom sheet, its actions pinned at the bottom, and the form scrolls above them; the scopes are one column. The drawn table keeps its name and prefix columns and drops the rest. The real dialog keeps the Dialog's own phone layout, a sheet from the bottom that scrolls inside itself.
Related
- Pro
A stock spreadsheet with its copy-as dialog shown open: the same four selected rows as Markdown, CSV, JSON or plain text, a preview that shows exactly what lands on the clipboard, a header-row option and a button that writes it.
- Pro
Confirm Delete Dialog
Destructive confirmsA planner's project list with its delete confirmation shown open: an alert dialog that lists what goes with the project, asks for its name to be typed exactly, and keeps Delete off until it matches.
- Pro
A usage-report tool's Exports page with its export dialog shown open: a CSV or JSON format, a range with row counts, six fields behind a mixed Select all box and a size estimate, and a Download button that saves a real file.
- Pro
A site-builder page with its embed dialog shown open: a pasted YouTube or Vimeo link detected by pattern, options for autoplay, controls, loop and start time, a drawn poster that reflects them, and the embed code to copy or insert.
- Pro
Deactivate 2FA Dialog
Destructive confirmsA security page with its turn-off confirmation shown open: an alert dialog that lists what goes with two-step verification, asks for the password and a ticked acknowledgement, and keeps its destructive action quiet until both are done.
- Pro
Add Profile Dialog
Account and securityA newsletter's writers page with its byline composer shown open: a name, what they write, a 120 character bio, a mark from six drawn glyphs or initials and a switch, beside a card that shows the byline as it will print.