# Env Editor

> Names and values such as API keys and URLs, one row each: secrets hidden, a pasted .env file split into rows, mistakes marked.

Page: https://uicontext.com/docs/components/env-editor

Group: Settings.

## When to use

Names and values such as API keys and URLs, one row each: secrets hidden, a pasted .env file split into rows, mistakes marked. Use [Secrets](https://uicontext.com/docs/components/secrets.md) instead when the values are credentials no one should read again after saving.

## Usage

```ts
import { EnvEditor, type EnvVar } from "./ui/uicontext/components/EnvEditor.ts"

export const Settings = <M>(h: Html.H<M>, env: ReadonlyArray<EnvVar>, onChange: (vars: ReadonlyArray<EnvVar>, valid: boolean) => M) =>
	EnvEditor(h, { props: { label: "Environment variables", value: env }, onEmit: { RequestedChange: ({ value, valid }) => onChange(value, valid) } })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### A project's production settings

The example the page opens with: Env Editor in “A project's production settings”.

```ts title="src/demo.ts" source="www/src/examples/env-editor/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { EnvEditor, type EnvVar } from "./ui/uicontext/components/EnvEditor.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 44rem)" } })

const production: ReadonlyArray<EnvVar> = [
	{ key: "DATABASE_URL", value: "postgres://app:hunter2@db.internal:5432/invoices", secret: true },
	{ key: "STRIPE_SECRET_KEY", value: "sk_live_51NbX0aExampleOnly", secret: true },
	{ key: "LOG_LEVEL", value: "info" },
	{ key: "FEATURE_NEW_BILLING", value: "true" },
	{ key: "PUBLIC_APP_URL", value: "https://app.example.com" },
]

// a project's production settings: secrets hidden until their eye is pressed
export const ProductionEnv = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		EnvEditor(h, { key: "prod", props: { label: "Production", value: production } }),
	)
```

### Pasting a .env file

Copy the file and paste it into the name field. The dash in ALLOWED-ORIGINS is marked, and Save waits until it's fixed.

This example also uses Button. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/button
```

```ts title="src/paste.ts" source="www/src/examples/env-editor/paste.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Button } from "./ui/uicontext/components/Button.ts"
import { EnvEditor } from "./ui/uicontext/components/EnvEditor.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { display: "grid", gap: space.lg, inlineSize: "min(100%, 44rem)" },
	file: { margin: 0, padding: space.md, borderRadius: corner.control, backgroundColor: color.app, fontFamily: font.mono, fontSize: font.xs, lineHeight: 1.6, whiteSpace: "pre-wrap", overflowWrap: "anywhere", color: color.fg },
	foot: { display: "flex", flexWrap: "wrap", alignItems: "center", gap: space.md },
	note: { margin: 0, fontSize: font.sm, color: color.muted },
})

// copy this, then paste it into the empty name field below
const dotenv = `# an MCP server's settings
export GITHUB_TOKEN="ghp_exampleOnly000"
SEARCH_INDEX=issues-v2   # the index to query
MAX_RESULTS=25
ALLOWED-ORIGINS=*`

const Message = UiSchema.TaggedEnum({ Edited: { valid: Schema.Boolean, count: Schema.Number }, ClickedSave: {} })

export const ToolSettings = Html.component({
	name: "ToolSettings",
	model: Schema.Struct({ valid: Schema.Boolean, count: Schema.Number, saved: Schema.Boolean }),
	message: Message,
}).make({
	init: () => ({ valid: true, count: 0, saved: false }),
	update: {
		Edited: ($, { valid, count }) => $.evolve({ valid, count, saved: false }),
		ClickedSave: ($) => ($.model.valid ? $.evolve({ saved: true }) : $.model),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			h.pre.attrs({ "aria-label": "A .env file to paste", tabindex: "0", ...sx(styles.file) }).children(dotenv),
			EnvEditor(h, {
				key: "tool",
				props: { id: "tool", label: "Tool settings", value: [] },
				onEmit: { RequestedChange: ({ value, valid }) => Message.Edited({ valid, count: value.length }) },
			}),
			h.div.attrs(sx(styles.foot)).children(
				Button(h, { tone: "primary" }).disabled(!model.valid).on("click", Message.ClickedSave()).children("Save settings"),
				h.p.attrs({ role: "status", ...sx(styles.note) }).children(model.saved ? "Saved. The tool restarts with them." : model.valid ? "" : "Fix the marked rows to save."),
			),
		),
})
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` | — | What the variables are for: "Production environment". |
| `value` (required) | `EnvVar[]` | — | The variables to start with. |
| `id` | `string` | `"env"` | Starts the ids inside it, unique on the page; "env" by default. |
| `locale` | `string` | — | For the counts; the browser's by default. |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedChange` | `{ value: EnvVar[]; valid: boolean }` | The set changed: the named variables, in order, and whether every row is fine to save. |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/env-editor
```

With npm: `npx shadcn@latest add checkfu/uicontext/env-editor`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/env-editor`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/env-editor`.

It writes these files:

- `src/ui/uicontext/components/EnvEditor.ts`
- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/ShowText.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/touch.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/EnvEditor.ts](https://github.com/checkfu/uicontext/blob/main/src/EnvEditor.ts)
- [src/touch.stylex.ts](https://github.com/checkfu/uicontext/blob/main/src/touch.stylex.ts)

## Used in

- [SaaS dashboard](https://uicontext.com/blocks/saas-dashboard.md)
- [Workflow builder](https://uicontext.com/blocks/automation-builder.md)
- [Team chat](https://uicontext.com/blocks/team-chat.md)
- [Whiteboard](https://uicontext.com/blocks/whiteboard.md)

## Related

- [Sign-in Form](https://uicontext.com/docs/components/sign-in-form.md) (`sign-in-form`): Sign in, accept an invitation and manage sessions.
- [Settings](https://uicontext.com/docs/components/settings.md) (`settings`): A settings page with sections, fields and a save bar.
- [Setting List](https://uicontext.com/docs/components/setting-list.md) (`setting-list`): Settings as rows, each with its control.
- [Inherited Settings](https://uicontext.com/docs/components/inherited-settings.md) (`inherited-settings`): Settings that a workspace sets and a team or project can override.
- [Members](https://uicontext.com/docs/components/members.md) (`members`): Workspace members, their roles and pending invitations.
- [Connections](https://uicontext.com/docs/components/connections.md) (`connections`): Connect outside services, and see each connection's status.
