CodeInput
A code field with colours, line numbers and Tab that indents.
SourcePackage @weft-js/warp
Preview
tsx
import { CodeInput } from '@weft-js/warp'export function Basic() { return ( <CodeInput label="Post-deploy hook" language="ts" defaultValue={SCRIPT} rows={8} /> )}const SCRIPT = `export async function onDeploy(ctx) { const health = await ctx.fetch('/health') if (health.status !== 200) { return ctx.rollback('Health check failed') } await ctx.notify('#deploys', ctx.commit)}`Installation
npm install @weft-js/warpUsage
tsx
import { CodeInput } from '@weft-js/warp'<CodeInput label="Post-deploy hook" language="ts" defaultValue={SCRIPT} rows={8} />Wrapped lines
wrap soft-wraps long lines, and line numbers step aside.
tsx
import { CodeInput } from '@weft-js/warp'export function Wrap() { return ( <CodeInput label="Run command" language="sh" wrap rows={3} defaultValue="node --max-old-space-size=4096 server.js --port $PORT --region $REGION --log-level info" /> )}Mark a line
errorLine marks it in the gutter.
tsx
import { CodeInput } from '@weft-js/warp'export function ErrorLine() { return ( <CodeInput label="Post-deploy hook" language="ts" defaultValue={SCRIPT} rows={8} errorLine={3} error="Line 3: health.status is a string here." /> )}const SCRIPT = `export async function onDeploy(ctx) { const health = await ctx.fetch('/health') if (health.status !== 200) { return ctx.rollback('Health check failed') } await ctx.notify('#deploys', ctx.commit)}`States
A code input can show a hint or an error, and can be disabled or readonly.
tsx
import { CodeInput, Grid, Stack, Text } from '@weft-js/warp'export function States() { return ( <Grid cols={3} gap="group"> <Cell name="Empty"><CodeInput label="Run command" language="sh" rows={3} /></Cell> <Cell name="Filled"><CodeInput label="Run command" language="sh" rows={3} defaultValue="node server.js" /></Cell> <Cell name="Hint"><CodeInput label="Run command" language="sh" rows={3} hint="Runs once in every copy." /></Cell> <Cell name="Invalid"><CodeInput label="Run command" language="sh" rows={3} defaultValue="node" error="Name the file to run." /></Cell> <Cell name="Disabled"><CodeInput label="Run command" language="sh" rows={3} defaultValue="node server.js" disabled /></Cell> <Cell name="Read-only"><CodeInput label="Run command" language="sh" rows={3} defaultValue="node server.js" readonly /></Cell> </Grid> )}const Cell = (p: { name: string; children?: unknown }) => ( <Stack gap="tight"> <Text role="caption" subtle>{p.name}</Text> {p.children} </Stack>)When to use
Use it for
- A script, a command or a config people edit by hand
Reach for something else
- JsonEditor, when it's JSON and should be checked
- CodeBlock, when it's only for reading
Accessibility
- Tab
- Indents
- ⇧Tab
- Outdents
- EscthenTab
- Leaves the field
- A real
<textarea>, so screen readers, undo and paste work as usual. - The hint tells keyboard users how to leave the field, since Tab is taken.
API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | The visible name, and what screen readers announce. |
hint | unknown | — | A short line of help under the field. |
error | unknown | — | Replaces the hint and marks the field invalid. |
icon | IconRef | — | Leading icon: it sweeps with the border. |
prefix | unknown | — | Text or an element inside the field before the value, such as $. |
suffix | unknown | — | Text or an element inside the field after the value, such as kg. |
variant | "floating" | "classic" | — | floating (default, or the app's warp({ fields })) · classic: the label above. |
appearance | "line" | — | line: only a bottom line under the value, no box or fill. The boxed field otherwise. |
size | "sm" | "md" | "lg" | 'md' | Control size. md by default. |
clearable | boolean | false | A clear button (shown once there is a value). |
counter | number | — | Maximum length, shown as a counter. |
id | string | — | The element id. One is made for you if you leave it out. |
disabled | boolean | false | Can't be focused or changed. |
readonly | boolean | false | Shows the value and lets it be selected and copied, but not changed. |
value | string | State<string> | — | The current value, when you own the state. |
defaultValue | string | — | The starting value, when the component owns the state. |
language | string | — | What to colour it as. |
lineNumbers | boolean | true | Draws the line numbers. |
tabSize | 2 | 4 | 2 | 2 or 4 spaces. |
wrap | boolean | false | Soft-wraps long lines. Line numbers then step aside. |
rows | number | 8 | The height in lines. |
errorLine | number | — | The line to mark in the gutter. |
name | string | — | Posts the value with a form. |
| Event | Type | Default | Description |
|---|---|---|---|
onInput | ((e: InputEvent & { currentTarget: HTMLInputElement & HTMLTextAreaElemen… | — | Called on each keystroke. |
onValueChange | ((v: string, reason: ChangeReason) => void) | — | Called with each new value, and whether a pointer, a key or code made the change. |
onReset | (() => void) | — | Called after a form's reset. |
Parts
Every element carries a stable name, so you can style it from your own CSS: [data-scope="code-input"] [data-part="body"]
bodylinessurfacelayerinput
RelatedJson editorCode block