@uiw/react-textarea-code-editor

A simple code editor with syntax highlighting.

Usage no npm install needed!

<script type="module">
  import uiwReactTextareaCodeEditor from 'https://cdn.skypack.dev/@uiw/react-textarea-code-editor';
</script>

README

React Textarea Code Editor

Build & Deploy Coverage Status Coverage Status Open in unpkg npm bundle size npm version

A simple code editor with syntax highlighting. This library aims to provide a simple code editor with syntax highlighting support without any of the extra features, perfect for simple embeds and forms where users can submit code.

Features:

  • ☕️ Automatic syntax highlighting.
  • 🐲 Automatic indent on new lines.
  • 🩲 Indent line or selected text by pressing tab key, with customizable indentation.
  • 🌸 Wrap selected text in parens, [], (), <>, {}, "", '', "", ``
  • 💡 Support next.js, Use examples in next.js.

Install

$ npm i @uiw/react-textarea-code-editor

Usage

Open in CodeSandbox Open in Github gh-pages

import CodeEditor from '@uiw/react-textarea-code-editor';

function App() {
  const [code, setCode] = React.useState(
    `function add(a, b) {\n  return a + b;\n}`
  );
  return (
    <CodeEditor
      value={code}
      language="js"
      placeholder="Please enter JS code."
      onChange={(evn) => setCode(evn.target.value)}
      padding={15}
      style={{
        fontSize: 12,
        backgroundColor: "#f5f5f5",
        fontFamily: 'ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace',
      }}
    />
  );
}

Support Nextjs

Use examples in nextjs. #31

Open in CodeSandbox

npm install next-remove-imports
npm install @uiw/react-textarea-code-editor@v1.4.4
// next.config.js
const removeImports = require("next-remove-imports")();
module.exports = removeImports({
  experimental: { esmExternals: true }
});
import React from "react";
import dynamic from "next/dynamic";
import "@uiw/react-textarea-code-editor/dist.css";

const CodeEditor = dynamic(
  () => import("@uiw/react-textarea-code-editor").then((mod) => mod.default),
  { ssr: false }
);

function HomePage() {
  const [code, setCode] = React.useState(
    `function add(a, b) {\n  return a + b;\n}`
  );
  return (
    <div>
      <CodeEditor
        value={code}
        language="js"
        placeholder="Please enter JS code."
        onChange={(evn) => setCode(evn.target.value)}
        padding={15}
        style={{
          fontSize: 12,
          backgroundColor: "#f5f5f5",
          fontFamily:
            "ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace"
        }}
      />
    </div>
  );
}

export default HomePage;

Props

interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
  prefixCls?: string;
  /**
   * Set what programming language the code belongs to.
   */
  language?: string;
  /**
   * Optional padding for code. Default: `10`.
   */
  padding?: number;
  /**
   * The minimum height of the editor. Default: `16`.
   */
  minHeight?: number;
}

List of supported languages can be found here

Demo

https://uiwjs.github.io/react-textarea-code-editor/

Development

Runs the project in development mode.

# Step 1, run first, listen to the component compile and output the .js file
# listen for compilation output type .d.ts file
npm run watch
# Step 2, development mode, listen to compile preview website instance
npm run start

production

Builds the app for production to the build folder.

npm run build

The build is minified and the filenames include the hashes. Your app is ready to be deployed!

Related

License

Licensed under the MIT License.