Codice 1.6.0
repository·main·Indexed 41 days ago
https://github.com/huozhi/codiceA React package for displaying and editing syntax-highlighted code blocks. It provides a Code component for rendering with support for line highlighting, line numbers, and automatic language detection via file extensions (e.g., py, rs, css), as well as an Editor component that synchronizes a styled display with a textarea. Note: This project has moved to Sugar High.
What's inside Codice
- Codice is no longer being maintained at this repository. The project has moved to Sugar High. If you are looking for the current version of this library, please visit https://sugar-high.vercel.app/react.
Use the Editor component
mainThe
Editorcomponent is the primary entrypoint for thecodiceeditor package. It is exported from the package root and can be imported to integrate the editor into your application.import { Editor } from './editor'; // Usage of the Editor component function MyComponent() { return <Editor />; }Use the Editor component
mainThe
Editorcomponent is a React component designed for code editing. It provides a synchronized view between a styledCodedisplay and a functionaltextarea. It supports features like line numbers, title management, and language extension detection based on the title.Key Props
title: The title displayed in the header. If provided, the component attempts to infer the file extension usinggetExtension(title)ifextensionis not explicitly set.value: The current code content.onChange: Callback function triggered when the code content changes.onChangeTitle: Callback function triggered when the title is edited via the header controls.controls: Boolean to show/hide the header controls (defaults totrue).lineNumbers: Boolean to enable/disable line numbers (defaults totrue).extension: Explicitly set the language extension (e.g., 'ts', 'js').fontSize/fontFamily: Controls the typography for both the display and the input area.textareaRef: A ref to access the underlyingHTMLTextAreaElement.lineNumbersWidth: Custom width for the line numbers gutter.padding: Custom padding for the editor content.
import { Editor } from './editor' function MyComponent() { return ( <Editor title="example.ts" value="console.log('hello world');" onChange={(newCode) => console.log(newCode)} onChangeTitle={(newTitle) => console.log(newTitle)} fontSize={14} lineNumbersWidth="40px" /> ) }Use the Code component
mainThe
Codecomponent is the primary entrypoint for thecodicepackage. It is used to render code blocks within your application. Import it from the package root to access its functionality.import { Code } from 'codice'; // Usage of the Code componentUse the CodeHeader component
mainThe
CodeHeadercomponent renders a header for a code block, which can include a title and control placeholders. It is typically used as a child or companion to theCodecomponent.- If
controlsistrue, it renders placeholder spans for UI controls (e.g., copy, expand). - If
onChangeTitleis provided, the title becomes an editable input field.
import { CodeHeader } from 'codice/code'; <CodeHeader title="example.ts" controls={true} onChangeTitle={(newTitle) => console.log(newTitle)} />- If
Use the Code component to display syntax-highlighted code
mainThe
Codecomponent is the primary way to render syntax-highlighted code blocks in React. It supports line highlighting, line numbers, and custom styling via props. It usessugar-highfor tokenization and automatically detects language presets based on the file extension provided in thetitleor theextensionprop.Supported language extensions for automatic preset selection include:
- CSS:
sass,scss,less,css - Python:
py - Rust:
rs
If the extension is not recognized, it falls back to default tokenization.
import { Code } from 'codice/code'; function MyComponent() { return ( <Code title="script.py" highlightLines={[2, 4, [6, 8]]} lineNumbers fontSize="14px" > def hello():\n print("world")\n # This is line 3\n return True\n\n # Lines 6 to 8\n x = 1\n y = 2\n z = x + y </Code> ); }- CSS:
Access the Editor DOM via data attributes
mainThe
Editorcomponent renders with specificdata-*attributes to facilitate easy selection and state inspection in the DOM or via testing libraries.data-codice="editor": Identifies the main editor container.data-codice-editor: A secondary identifier for the editor.data-codice-title="[title]": The current title of the editor.data-codice-controls="true|false": Indicates if controls are visible.data-codice-line-numbers="true|false": Indicates if line numbers are enabled.data-codice-content: Identifies the container holding theCodedisplay and thetextarea.
<!-- Example DOM structure --> <div data-codice="editor" data-codice-title="script.js" data-codice-controls="true" data-codice-line-numbers="true"> <div data-codice-content> <!-- Code display and textarea reside here --> </div> </div>Configure Code component options
mainThe
Codecomponent accepts the following props to customize the display:Prop Type Description childrenstringThe raw code text to be displayed. titlestringThe title of the code block (used for file extension detection). extensionstringExplicitly set the language extension (e.g., py,rs,css).highlightLines([number, number] | number)[]An array of line numbers or line ranges [start, end]to highlight.lineNumbersbooleanWhether to display line numbers. Defaults to false.lineNumbersWidthstringThe width of the line number gutter. fontSizestring | numberThe font size of the code text. paddingstringCSS padding for the code container. preformattedbooleanIf true, wraps the code in<pre><code>. Defaults totrue.asMarkupbooleanIf true, treatschildrenas raw HTML/markup instead of processing it through the tokenizer. Defaults tofalse.controlsbooleanWhether to show the CodeHeadercontrols.Note:
Codealso accepts all standardReact.HTMLAttributes<HTMLDivElement>props.Get the file extension from a title
mainThe
getExtensionutility function extracts the file extension from a given title string. If the title is undefined or has no extension, it returns an empty string.import { getExtension } from 'codice/code'; const ext = getExtension('main.py'); // returns 'py' const none = getExtension('README'); // returns ''Use the Code class
mainThe
Codeclass is exported from the main package entrypoint and is used to represent or manage code segments within the Codice ecosystem.import { Code } from 'codice';Use the Editor class
mainThe
Editorclass is the primary entrypoint for interacting with the Codice editor. It is exported from the main package entrypoint.import { Editor } from 'codice';