Env Editor
Environment variables as editable rows: values masked until revealed, paste a .env to import, duplicates flagged.
$
View as Markdownnpx shadcn@latest add https://sundaeui.dev/r/env-editor.json'use client'
import { useId, useRef, useState } from 'react'
import './env-editor.css'
type Row = { id: number; key: string; value: string }
const SECRET = /(SECRET|TOKEN|KEY|PASSWORD|PRIVATE)/
export interface EnvEditorProps {
defaultVars?: { key: string; value: string }[]
masked?: boolean
onSave?: (v: { key: string; value: string }[]) => void
fill?: 'dark' | 'light'
className?: string
}
/** Key/value env editor with masking, paste import and validation. */
export function EnvEditor({ defaultVars = [], masked = true, onSave, fill = 'dark', className }: EnvEditorProps) {
const seq = useRef(1)
const mk = (key = '', value = ''): Row => ({ id: seq.current++, key, value })
const [rows, setRows] = useState<Row[]>(() => defaultVars.map((v) => mk(v.key, v.value)))
const [saved, setSaved] = useState(() => JSON.stringify(defaultVars))
const [shown, setShown] = useState<number[]>([])
const box = useRef<HTMLDivElement>(null)
const uid = useId()
const counts = rows.reduce<Record<string, number>>((a, r) => ({ ...a, [r.key]: (a[r.key] ?? 0) + 1 }), {})
const bad = (r: Row) => (r.key && !/^[A-Z_][A-Z0-9_]*$/.test(r.key) ? 'Use A–Z, 0–9 and _' : counts[r.key] > 1 && r.key ? 'Duplicate key' : '')
const errs = rows.map((r) => bad(r)).filter(Boolean)
const data = rows.filter((r) => r.key).map(({ key, value }) => ({ key, value }))
const dirty = JSON.stringify(data) !== saved
const edit = (id: number, p: Partial<Row>) => setRows((rs) => rs.map((r) => (r.id === id ? { ...r, ...p } : r)))
const paste = (id: number) => (e: React.ClipboardEvent) => {
const t = e.clipboardData.getData('text'); if (!t.includes('=') || !t.includes('\n') && t.split('=').length < 2) return
const parsed = t.split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !l.startsWith('#') && l.includes('=')).map((l) => { const i = l.indexOf('='); return mk(l.slice(0, i).trim().toUpperCase(), l.slice(i + 1).trim().replace(/^["']|["']$/g, '')) })
if (!parsed.length) return
e.preventDefault()
setRows((rs) => { const i = rs.findIndex((r) => r.id === id); const n = rs.slice(); n.splice(i, rs[i].key || rs[i].value ? 0 : 1, ...parsed); return n })
}
const addRow = () => { const r = mk(); setRows((rs) => [...rs, r]); requestAnimationFrame(() => box.current?.querySelector<HTMLInputElement>(`[data-k="${r.id}"]`)?.focus()) }
return (
<div ref={box} className={['sd-env', className].filter(Boolean).join(' ')} data-fill={fill}>
<div className="sd-env__h"><b>Environment variables</b><span>{data.length} set{dirty ? ' · unsaved' : ''}</span></div>
{rows.map((r, i) => {
const e = bad(r), hide = masked && SECRET.test(r.key) && !shown.includes(r.id)
return (
<div key={r.id} className="sd-env__row">
<input className="sd-env__in" data-k={r.id} aria-label={`Key ${i + 1}`} placeholder="KEY" spellCheck={false} value={r.key} aria-invalid={!!e} aria-describedby={e ? `${uid}-err` : undefined}
onChange={(ev) => edit(r.id, { key: ev.target.value.toUpperCase().replace(/\s/g, '_') })} onPaste={paste(r.id)} />
<span className="sd-env__val">
<input className="sd-env__in" type={hide ? 'password' : 'text'} aria-label={`Value for ${r.key || `row ${i + 1}`}`} placeholder="value" spellCheck={false} autoComplete="off" value={r.value}
onChange={(ev) => edit(r.id, { value: ev.target.value })} onKeyDown={(ev) => { if (ev.key === 'Enter' && i === rows.length - 1) addRow() }} />
{masked && SECRET.test(r.key) && (
<button type="button" className="sd-env__eye" aria-pressed={!hide} aria-label={`${hide ? 'Reveal' : 'Hide'} ${r.key}`} onClick={() => setShown((s) => (s.includes(r.id) ? s.filter((x) => x !== r.id) : [...s, r.id]))}>
<svg viewBox="0 0 16 16" aria-hidden><path d="M1.5 8S4 3.5 8 3.5 14.5 8 14.5 8 12 12.5 8 12.5 1.5 8 1.5 8z" /><circle cx="8" cy="8" r="2" />{hide && <path d="m2.5 13.5 11-11" />}</svg>
</button>
)}
</span>
<button type="button" className="sd-env__rm" aria-label={`Remove ${r.key || `row ${i + 1}`}`} onClick={() => setRows((rs) => rs.filter((x) => x.id !== r.id))}><svg viewBox="0 0 12 12" aria-hidden><path d="m3 3 6 6M9 3 3 9" /></svg></button>
</div>
)
})}
<button type="button" className="sd-env__add" onClick={addRow}>+ Add variable · or paste a .env</button>
<p id={`${uid}-err`} className="sd-env__err" role="alert">{errs[0] ?? ''}</p>
<div className="sd-env__foot">
<button type="button" disabled={!dirty} onClick={() => { const v = JSON.parse(saved) as { key: string; value: string }[]; setRows(v.map((x) => mk(x.key, x.value))) }}>Discard</button>
<button type="button" className="pri" disabled={!dirty || errs.length > 0} onClick={() => { setSaved(JSON.stringify(data)); onSave?.(data) }}>Save</button>
</div>
</div>
)
}.sd-env { --bg: #1c1c1c; --fg: #f6f6f6; --mute: #8e8e8e; --line: rgb(255 255 255 / 0.1); --soft: rgb(255 255 255 / 0.07); --strong: #fafafa; --on: #111;
color: var(--fg); font-size: 14px; line-height: 1.4; -webkit-tap-highlight-color: transparent; }
.sd-env[data-fill='light'] { --bg: #fff; --fg: #121212; --mute: #6f6f6f; --line: rgb(0 0 0 / 0.09); --soft: rgb(0 0 0 / 0.05); --strong: #111; --on: #fff; }
.sd-env :where(button) { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
.sd-env :focus-visible { outline: 2px solid var(--fg); outline-offset: 2px; }
.sd-env .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.sd-env { width: min(100%, 360px); padding: 14px; border-radius: 18px; background: var(--bg); box-shadow: inset 0 0 0 1px var(--line); }
.sd-env__h { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }
.sd-env__h b { font-size: 14px; }
.sd-env__h span { font-size: 12px; color: var(--mute); }
.sd-env__row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr) 28px; gap: 6px; margin-bottom: 6px; animation: sd-env-in 200ms ease-out; }
.sd-env__in { width: 100%; min-width: 0; height: 34px; padding: 0 9px; border: 0; border-radius: 8px; background: var(--soft); color: var(--fg); font: 12.5px/1 ui-monospace, SFMono-Regular, monospace; outline: 0; }
.sd-env__in:focus { box-shadow: inset 0 0 0 1.5px var(--fg); }
.sd-env__in[aria-invalid='true'] { box-shadow: inset 0 0 0 1.5px var(--fg); text-decoration: wavy underline; text-decoration-color: var(--mute); }
.sd-env__val { position: relative; }
.sd-env__val .sd-env__in { padding-right: 32px; }
.sd-env__eye { position: absolute; right: 3px; top: 3px; display: grid; place-items: center; width: 28px; height: 28px; border-radius: 7px; color: var(--mute) !important; }
.sd-env__eye svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; }
.sd-env__rm { display: grid; place-items: center; width: 28px; height: 34px; border-radius: 8px; color: var(--mute) !important; }
.sd-env__rm:hover { background: var(--soft); color: var(--fg) !important; }
.sd-env__rm svg { width: 12px; height: 12px; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.sd-env__add { margin: 4px 0 10px; padding: 7px 10px; border-radius: 8px; font-size: 12.5px; color: var(--mute) !important; }
.sd-env__add:hover { background: var(--soft); color: var(--fg) !important; }
.sd-env__err { margin: 0 0 10px; font-size: 12px; min-height: 16px; }
.sd-env__foot { display: flex; justify-content: flex-end; gap: 6px; padding-top: 10px; border-top: 1px solid var(--line); }
.sd-env__foot button { padding: 8px 14px; border-radius: 10px; font-size: 13px; font-weight: 600; box-shadow: inset 0 0 0 1px var(--line); }
.sd-env__foot .pri { background: var(--strong); color: var(--on); box-shadow: none; }
.sd-env__foot button:disabled { opacity: 0.35; }
@keyframes sd-env-in { from { opacity: 0; transform: translateY(-3px); } }
@media (prefers-reduced-motion: reduce) { .sd-env *, .sd-env *::before, .sd-env *::after { animation: none !important; transition: none !important; } }Overview
Each row is a key and a value; secret-looking values are masked with a reveal toggle. Keys are upper-cased and validated as you type.
Paste a whole .env file into the key field and it splits into rows. Duplicate keys and empty values are flagged before save.
When to use
- Deploy settings, CI secrets, app configuration.
When not to use
- Large secret stores with versions: use a vault UI.
Installation
$
pnpm dlx shadcn@latest add https://sundaeui.dev/r/env-editor.json$
npx shadcn@latest add https://sundaeui.dev/r/env-editor.json$
yarn dlx shadcn@latest add https://sundaeui.dev/r/env-editor.json$
bunx --bun shadcn@latest add https://sundaeui.dev/r/env-editor.jsonWorks in any React project. Files are written to components/sundae/. Read the source in the stage’s Code view.
- No dependencies beyond React 18 or later.
- Copy env-editor.tsx and env-editor.css into components/sundae/.
- The CSS file is imported by the component, so no extra configuration is needed.
- Import the component and use it as shown below.
Usage
import { EnvEditor } from '@/components/sundae/env-editor'
<EnvEditor />Props
| Prop | Type | Default |
|---|---|---|
defaultVarsStarting rows. | { key: string; value: string }[] | — |
onSaveSave callback. | (vars) => void | — |
fillSurface: dark or light. | 'dark' | 'light' | 'dark' |
Accessibility
- Inputs are labelled per row; reveal buttons say which key they reveal.
- Validation errors are announced and tied to their inputs.
- Reduced motion removes the movement and keeps every state change.
Customization
- Colours are CSS custom properties on .sd-env; override them in your own stylesheet.
- fill="light" switches the built-in light palette.
Keyboard
- Paste a .env
- Import many rows at once
- Enter on last row
- Add a row
FAQ
- Is Env Editor free?
- Yes. Env Editor is free for personal and commercial projects, with full source on this page.
- How do I install Env Editor?
- Run npx shadcn@latest add https://sundaeui.dev/r/env-editor.json. The files are written to components/sundae/. It has no dependencies beyond React 18 or later.
- Does Env Editor support keyboard and screen readers?
- Inputs are labelled per row; reveal buttons say which key they reveal. Validation errors are announced and tied to their inputs.
- Does Env Editor respect reduced motion?
- Reduced motion removes the movement and keeps every state change.
More like this
All forms →- Pro
Feature Flag
Forms · Refined Notification Matrix
Forms · Refined- Pro
Shortcut Recorder
Forms · Refined Settings List
Forms · RefinedAsync Switch
Forms · RefinedWi-Fi Networks
Forms · Refined