CodeEditor
A few lines of code with line numbers. Tab indents, Shift+Tab outdents, Ctrl/⌘+Enter runs. Light on purpose: no highlighting, so it loads at once and works with every screen reader.
import { CodeEditor, CodeEditorSkeleton } from '@sypnia/ui/react'
On this page
A function
Code
import { useState } from 'react';
import { CodeEditor, CodeEditorSkeleton, Field } from '@sypnia/ui/react';
// Tab indents and Shift+Tab outdents; the line numbers follow. The skeleton is under it.
export default function Script() {
const [code, setCode] = useState("export default async (req) => {\n const user = await req.user();\n return { hello: user.name };\n};");
return <div className="sy-stack"><Field label="Function"><CodeEditor value={code} onChange={setCode} language="JavaScript" rows={6} /></Field><CodeEditorSkeleton rows={4} /></div>;
} When to use it
Use it when
- Short functions, scripts, templates.
Do not use it when
- Whole files: link to the repository or an editor.
For everyone
- It is a text area where Tab indents; press Escape, then Tab, to move on, so the keyboard is never trapped.
Props
| CodeEditor | Type | Default |
|---|---|---|
onChangerequired | (v: string) => void | |
valuerequired | string | |
aria-describedby | string | |
aria-labelThe name for screen readers when there is no Field around it. | string | |
aria-labelledby | string | |
className | string | |
disabled | boolean | |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
languageThe language, said to screen readers and used by SqlEditor and JsonEditor. | string | |
onRunRuns with Ctrl/⌘ + Enter. | (() => void) | |
placeholder | string | |
readOnly | boolean | |
rowsVisible lines before it scrolls. | number | 10 |
| CodeEditorSkeleton | Type | Default |
|---|---|---|
rows | number | 10 |