Editor & Code

npm install @sugar-high/react sugar-high

<Editor />

A controlled, highlighted editor with optional line numbers.

editor-example.tsx
1import { useState } from 'react'2import { Editor } from '@sugar-high/react'34const defaultText = 'console.log("hello world")'56export default function Page() {7  const [code, setCode] = useState(defaultText)8  const [title, setTitle] = useState('index.js')910  return (11    <Editor12      value={code}13      title={title}14      onChange={(text) => setCode(text)}15      onChangeTitle={(title) => setTitle(title)}16    />17  )18}
18 lines · 422 characters

<Code />

Present code with optional line numbers and marked lines.

code-example.tsx
1import { Code } from '@sugar-high/react'2import { highlight } from 'sugar-high'34function renderMarkup() {5  const code = "return 'long live sugar-high'"6  return highlight(code)7}89const markup = renderMarkup()10console.log(markup)1112render(13  <div>14    <Code15      controls16      title="app/index.js"17      lineNumbers18      highlightLines={[1, [14, 19]]}19    >20      {'<div>Hello World</div>'}21    </Code>22  </div>23)

Styling

Compose frame variables with Sugar High token themes.

const style = {  backgroundColor: '#f6f8fa',  '--sh-editor-background-color': 'transparent',  '--sh-caret-color': '#24292f',  '--sh-title-color': '#57606a',  '--sh-control-color': '#afb8c1',  '--sh-line-number-color': '#8c959f',  '--sh-line-highlight-color': '#fff8c5',  '--sh-keyword': '#cf222e',  '--sh-string': '#0a3069',} as React.CSSProperties<Editor style={style} value={source} onChange={setSource} />
VariableScope / defaultPurpose
--sh-editor-text-colorEditor
transparent
Textarea text; transparent over highlighted code.
--sh-editor-background-colorEditor
transparent
Textarea background.
--sh-caret-colorBoth
inherit
Editor and editable-title caret.
--sh-font-familyBoth
Editor monospace; Code inherited
Code, textarea, and title font.
--sh-font-sizeBoth
inherit
Code and textarea font size.
--sh-paddingBoth
1rem
Content and header spacing.
--sh-line-number-widthBoth
2.5rem / auto
Line-number gutter width.
--sh-control-colorBoth
unset
Header control-dot color.
--sh-title-colorBoth
unset
Header filename color.
--sh-line-number-colorBoth
unset
Line-number color.
--sh-line-highlight-colorBoth
unset
Highlighted-line background.

Set these inline or on the component root. Use --sh-* variables for token colors; the theme guide lists copyable presets. Keep the editor textarea colors transparent to preserve its highlighted overlay.

API

Props at a glance. Both components also accept standard div attributes.

<Editor />

PropType / defaultPurpose
valuestring · ''Controlled source text.
onChange(code) => voidRuns when the source changes.
titlestring | nullEditable filename and language hint.
onChangeTitle(title) => voidMakes the title editable.
langLanguageNameCanonical language; overrides the title.
extensionstringLegacy language hint when no lang is set.
controlsboolean · trueShows the header controls.
lineNumbersboolean · trueShows one-based line numbers.
lineNumbersWidthstringSets the line-number gutter width.
cxHighlightOptions['cx']Maps token types to classes.
markHighlightOptions['mark']Mutates generated token properties.
paddingstringSets editor content padding.
fontSizestring | numberSets the editor font size.
fontFamilystringSets the editor font family.
textareaRefRef<HTMLTextAreaElement>Accesses the underlying textarea.
refRef<HTMLDivElement>Accesses the editor root.

<Code />

PropType / defaultPurpose
childrenstring · requiredSource or generated markup to render.
langLanguageNameCanonical language; overrides the title.
titlestringFilename shown in the header and used as a language hint.
extensionstringLegacy language hint when no lang is set.
controlsboolean · falseShows header controls.
lineNumbersboolean · falseShows one-based line numbers.
highlightLines(number | [number, number])[]Highlights lines and inclusive ranges.
cxHighlightOptions['cx']Maps token types to classes.
markHighlightOptions['mark']Mutates generated token properties.
markLineHighlightOptions['markLine']Mutates generated line properties.
preformattedboolean · trueUses a pre and code wrapper.
asMarkupboolean · falseTreats children as highlighted HTML.
lineNumbersWidthstringSets the line-number gutter width.
paddingstringSets code content padding.
fontSizestring | numberSets the code font size.
Read the full React API on GitHub ↗