Editor & Code

npm install @sugar-high/react react

<Code />

Present code with optional line numbers and marked lines.

Taffy
example.ts
1import { Code } from '@sugar-high/react'2import { taffy } from '@sugar-high/react/themes'34export default function CodePreview({ source }: { source: string }) {5  return (6    <section>7      <Code8        title="example.js"9        lang="javascript"10        theme={taffy}11        controls12        lineNumbers13        highlightLines={[1, [5, 6]]}14      >15        {source}16      </Code>17    </section>18  )19}

Themes

Import a JavaScript theme or pass your own token colors.

Taffy
import { Editor } from '@sugar-high/react'import { taffy } from '@sugar-high/react/themes'<Editor theme={taffy} value={source} onChange={setSource} />
All themes

Taffy, Gruvbox, Minimal, Monokai, Nord Light, One Dark Pro, Soft Minimal, Tokyo Night, Vercel, VS Code.

<Editor />

A controlled, highlighted editor with optional line numbers.

Taffy
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

<FileTree />

Compose with Editor to edit files, or Code for read-only viewing.

Taffy
src/index.tsx
src
components
button.tsx
index.tsx
styles.css
package.json
1import { Button } from './components/button'2import './styles.css'34export default function App() {5  return <Button>Say hello</Button>6}
6 lines · 137 characters
API details

Props at a glance. All three components also accept standard div attributes.

<Code />

PropType / defaultPurpose
childrenstring · requiredSource or generated markup to render.
langLanguageNameCanonical language; overrides the title.
themeThemeJavaScript token and component colors.
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.

<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.
themeThemeJavaScript token and component colors.
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.

<FileTree />

PropType / defaultPurpose
pathsreadonly string[] · requiredRelative file paths. Folders are inferred and initially expanded.
activeFilestring | null · requiredSelected file path, or null for no selection.
onActiveFileChange(path: string) => void · requiredRuns when a file is selected. Use the path to update Editor or Code.
themeTheme · optionalPass the same theme as the adjacent Editor or Code.
aria-labelstring · 'Files'Accessible name for the tree.

The tree manages folder expansion and keyboard focus. Your component owns the files and selected path.

CSS variables

VariableScope / defaultPurpose
--sh-editor-text-colorEditor
transparent
Textarea text; transparent over highlighted code.
--sh-editor-background-colorEditor
transparent
Textarea background.
--sh-caret-colorBoth
CanvasText
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.

A single palette stays consistent in every color scheme. Paired themes inherit the standard CSS color-scheme. Set it to light dark for the system preference, or light / dark on an ancestor to force a mode. The variables above remain available as low-level overrides.

Read the full React API on GitHub ↗

WebGPU experimental

Language-agnostic highlighting for large code blocks and editors.

npm install @sugar-high/react react gpu-lexer
Taffy
gpu-editor.tsx
1import { Editor } from '@sugar-high/react/gpu'23export function LargeEditor({ source }) {4  return (5    <Editor6      value={source}7      onChange={saveSource}8      lineNumbers9    />10  )11}
11 lines · 192 charactersWebGPU · language agnostic