Skip to content

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
  1. A function
  2. When to use it
  3. For everyone
  4. Props

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

CodeEditorTypeDefault
onChangerequired (v: string) => void
valuerequired string
aria-describedby string
aria-label

The name for screen readers when there is no Field around it.

string
aria-labelledby string
className string
disabled boolean
id

Inside a Field it comes from the Field.

string
invalid

Marks the value as wrong. Inside a `<Field error>` it is set for you.

boolean
language

The language, said to screen readers and used by SqlEditor and JsonEditor.

string
onRun

Runs with Ctrl/⌘ + Enter.

(() => void)
placeholder string
readOnly boolean
rows

Visible lines before it scrolls.

number 10
CodeEditorSkeletonTypeDefault
rows number 10
  1. A function
  2. When to use it
  3. For everyone
  4. Props

    Search pages, sections and terms