Backup Code
FreeOne recovery code as a button. Pressing it copies the code, and a used code is disabled and struck through.
npx wunderui-cli add backup-codePlayground
Installation
npx wunderui-cli add backup-codeWrites the source into your project with its dependencies and, on first use, the styles - see Installation.
Then import what you need:
import { BackupCode } from "@/components/blocks/auth"Then use it:
import { BackupCode } from "@wunderui/react"
export function Code({ onCopy }: { onCopy: (code: string) => void }) {
return <BackupCode code="4F7K-9QX2" onCopy={onCopy} />
}Expected result: You should see a small bordered button with the code in it. Hover it to see a copy icon; press it to copy the code.
When to use
Use Backup Code for
- Recovery codes after two-factor setup, one code per button
- A short code that is copied with one press
- A list that shows which codes were already used
Use something else for
- Input Group for one long value, such as a link, with a visible copy button
- OTP Field for typing a verification code instead of copying one
- Code Block for a block of code or a command with a copy action
- Input for a value that people edit
State
A code is either ready or used. A used code is disabled, struck through and grey, and there is no loading, error or copied state.
Clipboard
The copy callback runs after the clipboard took the code. If the page may not write to the clipboard, nothing happens.
Long text
A long code makes the button grow. In a narrow box it wraps at the hyphen, so give each code a cell of at least 9 rem.
Text
Layout
A code has no width of its own, so a grid sets it. Two columns suit a card, one column a narrow screen.
Keyboard interactions
A code is a native button. Used codes are disabled and are skipped by the Tab key.
- TaborShift+Tab
- Moves focus to the next or previous code that is not used.
- EnterorSpace
- Presses the focused code: copies it and, once the clipboard took it, calls .
Behavior and motion
What a backup code does with state, content, the DOM and motion.
- Copy
- A press writes the code to the clipboard and calls the copy handler with it, but only after the write succeeded. If the page may not write, nothing happens: no callback, no message, no change.
- States
- A code is ready or used: hover shows the copy icon, focus draws the indigo ring, and a used code is struck through, disabled and out of the Tab order. Feedback after a copy is yours.
- Long text
- The button grows with its code and has no ellipsis: in a box narrower than the code the text wraps at the hyphen onto two lines. Give the code a wide enough grid cell.
- Responsive
- A code has no width of its own, so the grid you put it in decides. Two columns fit down to a 320 px screen; use one column for longer codes.
- Names for assistive technology
- The accessible name is the code, and the button has a native tooltip, Copy code. A used code is announced as unavailable but not as used, so state the count in text, for example 5 codes left.
- Motion and reduced motion
- The fill changes on hover and the copy icon fades in, but nothing moves. With reduced motion the colours and fade still apply, because they are not movement.
Anatomy
The parts of a backup code, numbered in the figure. It is one component whose code and copy icon are parts of the button, and the figure keeps the icon visible so it can be numbered.
- 1data-slot="backup-code"
A button with an 8 px radius, a 1 px border and the card fill. It is the only element: pressing it copies the code, and a used code is disabled.
- 2
The code in 14 px medium text with tabular figures, on one line that wraps at a hyphen when the button is too narrow. A used code is struck through and tertiary.
- 3
A 14 px copy icon after the code. It is transparent until hover so it never shifts the code, and it is decorative because the code names the button.
Best practices
- Do
Mark codes that were already redeemed as used, so the list shows which ones are left. Say how many are left in text next to the grid, because the strike-through is visual only.
- Do
Confirm the copy in the copy handler, for example with a status line or a toast. It runs only after the clipboard took the code.
- Do
Offer a second way next to the list, such as Download as a text file, for the moment the clipboard is blocked.
- Do
Give each code a cell of at least 9 rem. A narrower box wraps the code at its hyphen.
- Don’t
Don't show your own Copied message on press. When the clipboard is blocked (an iframe, an http page, a denied permission) the copy handler does not run, and a message of your own would be wrong.
- Don’t
Don't rely on the copy icon to find the button. It shows on hover only, so on a touch screen the code itself is the target.
- Don’t
Don't use real codes in docs or tests. The demos here are made up and are not held anywhere.
API reference
Backup Code is one component with the props below. It takes no other attributes, so an id or aria-label cannot be passed to the button.
A button with an 8 px radius, a 1 px border and the card fill. It is the only element: pressing it copies the code, and a used code is disabled.
| Prop | Type | Default | Description |
|---|---|---|---|
| code required | string | — | The recovery code. It is the text of the button, the accessible name and the text that is copied. Required. |
| used | boolean | — | Disables the button, strikes the code through and turns it tertiary. A used code cannot be pressed or focused. |
| onCopy | (code) => void | — | Called with the code after the clipboard accepted it. It is not called when the clipboard refuses, so use it for the confirmation. |
| className | string | — | Added to the button. Use it for the width in a grid. |
Notes for AI
Give your coding assistant the Markdown reference instead of screenshots.
get_component("backup-code")npx wunderui-cli add backup-codeThe full library index for assistants is at /llms.txt.