Skip to content
weftGitHub

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/warp

Usage

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

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

PropTypeDefaultDescription
labelstring—The visible name, and what screen readers announce.
hintunknown—A short line of help under the field.
errorunknown—Replaces the hint and marks the field invalid.
iconIconRef—Leading icon: it sweeps with the border.
prefixunknown—Text or an element inside the field before the value, such as $.
suffixunknown—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.
clearablebooleanfalseA clear button (shown once there is a value).
counternumber—Maximum length, shown as a counter.
idstring—The element id. One is made for you if you leave it out.
disabledbooleanfalseCan't be focused or changed.
readonlybooleanfalseShows the value and lets it be selected and copied, but not changed.
valuestring | State<string>—The current value, when you own the state.
defaultValuestring—The starting value, when the component owns the state.
languagestring—What to colour it as.
lineNumbersbooleantrueDraws the line numbers.
tabSize2 | 422 or 4 spaces.
wrapbooleanfalseSoft-wraps long lines. Line numbers then step aside.
rowsnumber8The height in lines.
errorLinenumber—The line to mark in the gutter.
namestring—Posts the value with a form.
EventTypeDefaultDescription
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