Code Editor
import { CodeEditor } from '@hanzo/ui'
Default
A language menu, a copy button and a short JavaScript sample.
export function Default() {
return (
<CodeEditor
language="javascript"
height={220}
defaultValue={`function fibonacci(n) {\n if (n <= 1) return n\n return fibonacci(n - 1) + fibonacci(n - 2)\n}\n\nconsole.log(fibonacci(10))`}
/>
)
}Controlled
The field's value lives in the parent's own state, reset and cleared from outside it.
23 chars
export function Controlled() {
const [code, setCode] = useState("// type some code here\n")
return (
<YStack gap="$3">
<CodeEditor value={code} onChange={setCode} language="typescript" height={180} />
<XStack gap="$2" items="center">
<Button variant="outline" size="sm" onClick={() => setCode("// reset\n")}>
Reset
</Button>
<Button variant="outline" size="sm" onClick={() => setCode("")}>
Clear
</Button>
<Text fontFamily="$mono" fontSize="$1" color="$soft">
{code.length} chars
</Text>
</XStack>
</YStack>
)
}Read only
No toolbar, no gutter, and the field refuses every keystroke.
export function ReadOnly() {
return (
<CodeEditor
readOnly
showLanguageSelector={false}
showCopyButton={false}
lineNumbers={false}
language="sql"
height={140}
defaultValue={`SELECT id, email\nFROM users\nWHERE created_at >= NOW() - INTERVAL '30 days'`}
/>
)
}Fixed language
The menu is hidden and word wrap is off, so a long Go line scrolls sideways instead of breaking.
Go
export function FixedLanguage() {
return (
<CodeEditor
showLanguageSelector={false}
wordWrap="off"
language="go"
height={160}
defaultValue={`func worker(id int, jobs <-chan int, results chan<- int) {\n\tfor job := range jobs {\n\t\tresults <- job * 2\n\t}\n}`}
/>
)
}JSON
Highlighted, checked on every keystroke, re-indented by Format; the footer names a broken line and resizes the editor.
JSON
export function Json() {
return (
<CodeEditor
language="json"
showLanguageSelector={false}
defaultValue={`{\n "customer": "Acme Corp",\n "plan": "Enterprise"\n "seats": 40\n}`}
/>
)
}JSON or text
`allowText` lets prose through and still holds anything that opens an object or array to JSON.
JSON
export function JsonOrText() {
return (
<CodeEditor
language="json"
allowText
showLanguageSelector={false}
placeholder="JSON object, array, or text"
defaultValue="We are receiving 502 Bad Gateway errors since 14:00 UTC."
/>
)
}Types
export type CodeEditorWordWrap = 'on' | 'off' | 'wordWrapColumn' | 'bounded'export interface CodeEditorProps extends Omit<YStackProps, 'children' | 'height' | 'minHeight' | 'maxHeight' | 'onChange'> {
/** A controlled value; omit it and set `defaultValue` to let the editor own its text. */
value?: string
defaultValue?: string
language?: string
/** A fixed height. Omit it and the editor follows its text between `minHeight` and `maxHeight`. */
height?: string | number
/** The shortest the editor gets when it follows its text, in px. */
minHeight?: number
/** The tallest it grows on its own, in px; past it the editor scrolls. */
maxHeight?: number
/** Whether the editor's bottom edge resizes it. Defaults to true unless `height` is set. */
resizable?: boolean
/** JSON only: text that does not open an object or an array is plain text, not an error. */
allowText?: boolean
onChange?: (value: string) => void
/** JSON only: the verdict on the text, after mount and after every edit. */
onCheck?: (check: JsonCheck) => void
/** Called once, after mount, with the CodeMirror view. */
onMount?: (view: EditorView) => void
readOnly?: boolean
lineNumbers?: boolean
wordWrap?: CodeEditorWordWrap
fontSize?: number
/** Shown while the editor is empty. */
placeholder?: string
showCopyButton?: boolean
/** Defaults to true for editable JSON. */
showFormatButton?: boolean
showLanguageSelector?: boolean
availableLanguages?: readonly string[]
/**
* The editor's accessible name. An editing surface with none is announced
* with nothing to say what it holds; a file's path is the usual answer.
* Defaults to "Code".
*/
label?: string
/** `default`, `borderless` or `with-header`; a `title` or `actions` implies the last. */
variant?: 'default' | 'borderless' | 'with-header'
/** The header's title. */
title?: string
/** Controls at the header's end. */
actions?: React.ReactNode
/** Follow the text with no ceiling (`maxHeight` is ignored). */
autoHeight?: boolean
}Exports
CodeEditortype CodeEditorPropstype CodeEditorWordWrap