shadcn.io is an independent project built on shadcn/ui
On this page10 sections
Environment Variables is a card of the variables a project needs, one row each: the name, a Required badge where the app won't start without it, and the value masked as dots until the switch in the header shows them all. Reach for it when an agent hands over configuration, such as the keys a setup step created or the variables a deploy is missing.
For a whole .env file that people read or copy in one go, use a Code Block.
Installation
Anatomy

Usage
import {
EnvironmentVariable, EnvironmentVariableCopyButton,
EnvironmentVariableGroup, EnvironmentVariableName,
EnvironmentVariableRequired, EnvironmentVariables,
EnvironmentVariablesContent, EnvironmentVariablesHeader,
EnvironmentVariablesTitle, EnvironmentVariablesToggle,
EnvironmentVariableValue,
} from "@/components/ui/environment-variables";Each row takes its name and value on EnvironmentVariable, and the parts inside it read them from there. The toggle in the header shows or hides every value in the card.
<EnvironmentVariables>
<EnvironmentVariablesHeader>
<EnvironmentVariablesTitle />
<EnvironmentVariablesToggle />
</EnvironmentVariablesHeader>
<EnvironmentVariablesContent>
<EnvironmentVariable name="OPENAI_API_KEY" value="sk-proj-abc123xyz789">
<EnvironmentVariableGroup className="shrink-0">
<EnvironmentVariableName />
<EnvironmentVariableRequired />
</EnvironmentVariableGroup>
<EnvironmentVariableGroup>
<EnvironmentVariableValue />
<EnvironmentVariableCopyButton />
</EnvironmentVariableGroup>
</EnvironmentVariable>
</EnvironmentVariablesContent>
</EnvironmentVariables>Accessibility
Each copy button is labelled with what it copies, such as Copy the value of DATABASE_URL, and after a copy a polite live region says Copied. A masked value reads as Hidden, because the dots are aria-hidden and a visually hidden word stands in for them. The title is an h3, so change it in the component's file if your page needs another heading level.
The toggle is a switch named Show values by its aria-label, so a screen reader announces it as on or off.
- Tab
- Moves focus to the toggle, then to each copy button in turn.
- Shift+Tab
- Moves focus back through the same stops.
- Space
- On the toggle, shows or hides every value. On a copy button, copies.
- Enter
- On a copy button, copies. It doesn't flip the toggle.
Demos
Copy format
A .env.local card with one row per copyFormat: the Anthropic key copies its value, the site URL its name, and the Redis URL a shell line, export REDIS_URL='redis://localhost:6379'. The line under the card shows the text each copy put on the clipboard. Pick export when people paste into a terminal and name when they paste into a host's settings form.
Controlled
A Production card of Stripe and Resend keys whose toggle runs on the demo's own state, through showValues and onShowValuesChange, with an effect that hides the keys again five seconds after they're shown. Hold the state yourself when something outside the card decides, such as a timer, a permission check or a second card that follows the first.
Default row
A Development card of four rows that pass only name and value, so EnvironmentVariable lays out the name, then the value, on its own. defaultShowValues starts them shown, since a port number and a log level have nothing to hide. Compose the row from EnvironmentVariableGroup once it needs a Required badge or a copy button.
Migrating from AI SDK Elements
If you added Environment Variables from AI SDK Elementscomponents/ai-elements/environment-variables.tsx. Run the command in the Installation section, then change the import from @/components/ai-elements/environment-variables to @/components/ui/environment-variables. Every part keeps its name and its props: showValues, defaultShowValues and onShowValuesChange on the root, name and value on a row, copyFormat, onCopy, onError and timeout on the copy button.
What changes is how they read. The title says "Environment variables" when you give it no words, a hidden value is "Hidden" to a screen reader instead of a run of bullets, and the copy button has a label that names the variable, "Copy the value of DATABASE_URL".
The copy button is a React Aria Button, so a handler of your own goes in onPress, not onClick, and the toggle passes its props to the Switch, which takes isDisabled.
FAQ
Is it safe to show API keys in the Environment Variables component?
Only keys the reader is allowed to have. The mask is drawn on screen, while the full value still reaches the browser in the component's props, where anyone can read it in the page's code or copy it with the button. A value shorter than 20 characters also shows its length, one dot per character.
What the mask does well is keep a key off a screen share or a recording. For a secret the reader shouldn't see, send its name with an empty value or a placeholder, and keep the real value on the server.
How do I control whether Environment Variables shows its values?
Set defaultShowValues on EnvironmentVariables to start with every value shown, as the Default row demo does for harmless development settings. Without it, the values start masked.
To decide from outside the card, pass showValues and update it from onShowValuesChange, which receives true when someone shows the values and false when they hide them. The Controlled demo does this to hide production keys again five seconds after they're shown.
Can I reveal one environment variable without showing the others?
Not with the toggle, which shows or hides every value in its card at once. Each value reads the same state from the card around it, and a row has no state of its own.
For one secret that people reveal by itself, give it a card of its own with its own toggle. Or pass text as the children of EnvironmentVariableValue: children replace both the value and the dots, so one row can always show its value, or a placeholder such as Set on the server, while its neighbors stay masked.
What does the Environment Variables copy button copy?
The value by default, even while it is masked, so nobody has to show a key to copy it. Set copyFormat to name for the name alone, or to export for a line a shell runs, export NAME='value', and the button's label changes to say which.
The value goes between single quotes, with any single quote inside it escaped, so bash, zsh and sh keep a dollar sign, a backtick or a double quote in it as written and run nothing from it. After a copy the icon turns to a check for timeout milliseconds, 2000 by default, and onCopy runs.
Why does the Environment Variables copy button not copy anything?
The clipboard is only there in a secure context. navigator.clipboard exists on HTTPS and on localhost, so on a plain HTTP address, such as a dev server opened from a phone by its network address, the button calls onError and nothing is copied.
The browser can also refuse the write, for example inside an iframe that isn't allowed to write to the clipboard, and that error reaches onError as well. Handle it to tell people the copy failed, with a toast or a line of text. The Copy format demo only clears its line, so a failed copy never shows text that isn't on the clipboard.
How do I mark an environment variable as required or optional?
Put an EnvironmentVariableRequired beside its name, in the same EnvironmentVariableGroup as EnvironmentVariableName. It is a secondary Badge that says Required.
For another mark, give it other words, such as Optional or Missing, and a variant to change its color: destructive suits a variable a deploy is missing. A row without the badge reads as optional, as DEBUG does in the demo at the top of the page.
What does a screen reader hear for a hidden environment variable value?
The word Hidden. While the card masks its values, the dots are aria-hidden and a visually hidden span reads Hidden in their place, so a screen reader never spells out a row of bullets. Once the values show, it reads the value itself.
Each copy button names its variable, so a list of them isn't a list of Copy, and after a copy a polite live region says Copied. The toggle is a switch named Show values.
Does Environment Variables work on a right-to-left page?
Yes. Each row lays out from the inline start, so on a right-to-left page the name sits on the right and the value and its copy button on the left. The names and values keep a left-to-right direction of their own, through dir set to ltr, since DATABASE_URL and a connection string read that way in any language.
API Reference
EnvironmentVariables
showValues?boolean
Whether the values show (controlled).
defaultShowValues?boolean
Whether the values show at first, when uncontrolled.
- Default
false
onShowValuesChange?(show: boolean) → void
Called with true when the values are shown and false when hidden.
- Parameters
show– boolean- Returns
void
className?string
Merged with the part's classes.
EnvironmentVariable
namestring
The variable's name, as code reads it: DATABASE_URL.
valuestring
Its value, masked until the card shows its values.
className?string
Merged with the part's classes.
EnvironmentVariableCopyButton
data-slot="environment-variable-copy-button"
children?ReactNode
The content to display in the button.
copyFormat?"export" | "name" | "value"
What it copies: the value, the name, or both as a shell line,
export NAME='value'.
- Type
"export" | "name" | "value"- Default
"value"
isDisabled?boolean
Whether the button is disabled.
onCopy?() → void
Called once the text is on the clipboard.
- Returns
void
onError?(error: Error) → void
Called when the clipboard refuses the text, or there is none (a page not served over HTTPS).
- Parameters
error– Error- Returns
void
timeout?number
How long the check stays before the copy icon comes back, in milliseconds.
- Default
2000
className?string
Merged with the part's classes.
data-copied?string
Present when copied is set.
EnvironmentVariableGroup
<div> data-slot="environment-variable-group"
className?string
Merged with the part's classes.
EnvironmentVariableName
<span> data-slot="environment-variable-name"
className?string
Merged with the part's classes.
EnvironmentVariableRequired
data-slot="environment-variable-required"
pill?boolean
Fully rounded corners.
size?"lg" | "md" | "sm"
The size.
- Type
"lg" | "md" | "sm"
variant?"default" | "destructive" | "ghost" | "info" | "link" | "outline" | "secondary" | "success" | "warning"
Ark default: default.
- Type
"default" | "destructive" | "ghost" | "info" | "link" | "outline" | "secondary" | "success" | "warning"- Default
"secondary"
render?(props: React.HTMLAttributes<HTMLElement>) → React.ReactNode
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: React.HTMLAttributes<HTMLElement>) → React.ReactNode- Parameters
props– React.HTMLAttributes<HTMLElement>- Returns
React.ReactNode
className?string
Merged with the part's classes.
EnvironmentVariablesContent
<div> data-slot="environment-variables-content"
className?string
Merged with the part's classes.
EnvironmentVariablesHeader
<div> data-slot="environment-variables-header"
className?string
Merged with the part's classes.
EnvironmentVariablesTitle
<h3> data-slot="environment-variables-title"
className?string
Merged with the part's classes.
EnvironmentVariablesToggle
<div> data-slot="environment-variables-toggle"
children?ReactNode
The label for the element.
defaultSelected?boolean
Whether the element should be selected (uncontrolled).
isSelected?boolean
Whether the element should be selected (controlled).
onChange?(isSelected: boolean) → void
Handler that is called when the element's selection state changes.
- Parameters
isSelected– boolean- Returns
void
size?"sm" | "default"
The size.
- Type
"sm" | "default"
value?string
The value of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefvalue).
className?string
Merged with the part's classes.
EnvironmentVariableValue
<span> data-slot="environment-variable-value"
className?string
Merged with the part's classes.
data-masked?string











