Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Environment Variables for Svelte UI SDK

A card of environment variables with values masked until a switch shows them, and a copy button per row. For Svelte on UI SDK, for keys a setup needs.

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.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Environment Variables anatomy: root, toggle, variable, copy button, required, value

Usage

<script lang="ts">
import {
  EnvironmentVariable, EnvironmentVariableCopyButton,
  EnvironmentVariableGroup, EnvironmentVariableName,
  EnvironmentVariableRequired, EnvironmentVariables,
  EnvironmentVariablesContent, EnvironmentVariablesHeader,
  EnvironmentVariablesTitle, EnvironmentVariablesToggle,
  EnvironmentVariableValue,
} from "@/components/ui/environment-variables";
</script>

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 class="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.

Preview width

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.

Preview width

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.

Preview width

Migrating from AI SDK Elements

AI SDK Elements is for React, so there is nothing to migrate in Svelte. The parts here have the same names as its React ones, and the demos on this page show them in Svelte.

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

PropType
showValues?boolean

Whether the values show (controlled). Two-way with bind:showValues.

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
class?string

Merged with the part's classes.

EnvironmentVariable

PropType
namestring

The variable's name, as code reads it: DATABASE_URL.

valuestring

Its value, masked until the card shows its values.

class?string

Merged with the part's classes.

EnvironmentVariableCopyButton

data-slot="environment-variable-copy-button"

PropType
clickEffect?boolean

Apply a click effect to the button.

Default
true
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"
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled button in the tab order with aria-disabled instead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing.

Default
false
isLoading?boolean

Show a loading indicator.

Default
false
onclick?( event: MouseEvent, details?: ButtonActionDetails ) → unknown

Runs when the button is pressed. Return a Promise to keep it pending (data-pending) until it settles. details.source says how it was pressed; a wrapper passing the click on may leave it out.

Type
( event: MouseEvent, details?: ButtonActionDetails ) → unknown
Parameters

event – React.MouseEvent<HTMLButtonElement>

details? – ButtonActionDetails

Returns
unknown
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
pendingAnnouncement?string

What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Default
"Loading"
timeout?number

How long the check stays before the copy icon comes back, in milliseconds.

Default
2000
type?"button" | "reset" | "submit"

What a native button does in a form: "submit" submits it and "reset" resets it. Ignored when the button renders as something else.

Type
"button" | "reset" | "submit"
Default
"button"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-copied?string

Present when copied is set.

EnvironmentVariableGroup

<div> data-slot="environment-variable-group"

PropType
class?string

Merged with the part's classes.

EnvironmentVariableName

<span> data-slot="environment-variable-name"

PropType
class?string

Merged with the part's classes.

EnvironmentVariableRequired

data-slot="environment-variable-required"

PropType
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"

Badge's default: default.

Type
"default" | "destructive" | "ghost" | "info" | "link" | "outline" | "secondary" | "success" | "warning"
Default
"secondary"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

EnvironmentVariablesContent

<div> data-slot="environment-variables-content"

PropType
class?string

Merged with the part's classes.

EnvironmentVariablesHeader

<div> data-slot="environment-variables-header"

PropType
class?string

Merged with the part's classes.

EnvironmentVariablesTitle

<h3> data-slot="environment-variables-title"

PropType
class?string

Merged with the part's classes.

EnvironmentVariablesToggle

<div> data-slot="environment-variables-toggle"

PropType
checked?boolean

Whether it's checked. Pass it to control the checkbox. Two-way with bind:checked.

defaultChecked?boolean

Whether it's checked at first, when uncontrolled. A form reset returns to it.

Default
false
onCheckedChange?(checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) → void

Runs when a user or a form reset wants to change it. details.reason says why, and details.cancel() stops it.

Type
(checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) → void
Parameters

checked – boolean

details – ChangeDetails<CheckboxCheckedChangeReason>

Returns
void
controller?{ toggle: (details?: Details) → boolean; getChecked: () → boolean; setChecked: (checked: boolean, details?: Details) → boolean; focus: () → void; }

Toggles it from anywhere. Create one with createSwitchController().

Type
{ toggle: (details?: Details) → boolean; getChecked: () → boolean; setChecked: (checked: boolean, details?: Details) → boolean; focus: () → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Disables it like a native control: it can't be focused or changed, and isn't submitted.

form?string

The id of a form to belong to, when it isn't inside that form.

name?string

The name it submits under. Without one, it submits nothing.

readOnly?boolean

Keeps it focusable and submitted, but stops changes.

required?boolean

Blocks form submission until it's checked, with the browser's own message.

size?"sm" | "default"

The size.

Type
"sm" | "default"
value?string

What it submits while checked.

Default
"on"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

EnvironmentVariableValue

<span> data-slot="environment-variable-value"

PropType
class?string

Merged with the part's classes.

AttributeType
data-masked?string