react-textarea-code-editor

uiwjsMIT活跃维护

基于 textarea 的轻量 React 代码编辑器组件,带语法高亮,适合在表单或评论框里快速嵌入代码输入能力。

A simple code editor with syntax highlighting.

v3.1.3发布于 2026-11-04 01:52 UTC579star +2 / 7 天26fork同步于 2026-11-10 11:58 UTC

怎么上手

从 README 代码块原样提取,可能是其中一种安装方式
npm i @uiw/react-textarea-code-editor
npm install next-remove-imports
npm install @uiw/[email protected]
npm run watch
npm run start
硬件要求

README 没有说明硬件要求。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

这个项目的说明还在生成中,先参考下面「最近发版」和 README。

最近发版

5 个版本v3.1.3
  1. v3.1.3最新正式版2026-11-04 · 2026-11-04 01:52 UTC

    Buy me a coffee npm bundle size

    Documentation v3.1.3: https://raw.githack.com/uiwjs/react-textarea-code-editor/042ed7a/index.html
    Comparing Changes: https://github.com/uiwjs/react-textarea-code-editor/compare/v3.1.2...v3.1.3

    npm i @uiw/[email protected]
    
    • 💢 ci: update .github/workflows/ci.yml. e101c7f @jaywcjlove
    • 🐞 fix: Fix double escaping in fenced code previews (#188) cefb9cc @feritcemkahraman
    • ⛑ test: cover remaining editor branches. ba0dce7 @jaywcjlove
    • ⛑ test: expect fenced code to be escaped once. ac6b7f8 @jaywcjlove
  2. v3.1.22026-10-06 · 2026-10-06 10:03 UTC
    查看更新说明
  3. v3.1.12025-05-01 · 2025-05-01 17:49 UTC
    查看更新说明
  4. v3.1.02024-10-16 · 2024-10-16 17:22 UTC
    查看更新说明

    Buy me a coffee npm bundle size

    Documentation v3.1.0: https://raw.githack.com/uiwjs/react-textarea-code-editor/a4b373a/index.html
    Comparing Changes: https://github.com/uiwjs/react-textarea-code-editor/compare/v3.0.2...v3.1.0

    npm i @uiw/[email protected]
    
    • 🌍 website: update website nav menu. 5ee5695 @jaywcjlove
    • 🐞 fix(deps): update dependency rehype-prism-plus to v2 (#172) 8d15407 @renovate-bot
    • 💢 ci: update workflows config. c3d2efa @jaywcjlove
    • 🌍 website: add example. 624e0f1 @jaywcjlove
    • 🌟 feat: add optional indentWidth (#175) 30a6878 @l9c
  5. v3.0.22023-11-30 · 2023-11-30 02:18 UTC
    查看更新说明

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    React Textarea Code Editor

    Buy me a coffee Follow On X Build & Deploy Coverage Status NPM Download jsDelivr CDN 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:

    • 🌒 Support dark-mode/night-mode @v2.
    • ☕️ 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
    

    Demo & Document

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

    Usage

    Open in CodeSandbox Open in Github gh-pages

    import React, { useState } from "react";
    import CodeEditor from '@uiw/react-textarea-code-editor';
    
    export default function App() {
      const [code, setCode] = 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={{
            backgroundColor: "#f5f5f5",
            fontFamily: 'ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace',
          }}
        />
      );
    }
    

    Highlight line or character

    Open in CodeSandbox

    List of rehype plugins to use.

    import CodeEditor from '@uiw/react-textarea-code-editor';
    import rehypePrism from "rehype-prism-plus";
    import rehypeRewrite from "rehype-rewrite";
    import "./styles.css";
    
    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}
          rehypePlugins={[
            [rehypePrism, { ignoreMissing: true }],
            [
              rehypeRewrite,
              {
                rewrite: (node, index, parent) => {
                  if (node.properties?.className?.includes("code-line")) {
                    if (index === 0 && node.properties?.className) {
                      node.properties.className.push("demo01");
                      // console.log("~~~", index, node.properties?.className);
                    }
                  }
                  if (node.type === "text" && node.value === "return" && parent.children.length === 1) {
                    parent.properties.className.push("demo123");
                  }
                }
              }
            ]
          ]}
          style={{
            fontSize: 12,
            backgroundColor: "#f5f5f5",
            fontFamily: 'ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace',
          }}
        />
      );
    }
    

    Remove Code Highlight

    The following example can help you exclude code highlighting code from being included in the bundle. @uiw/react-textarea-code-editor/nohighlight component does not contain the rehype-prism-plus code highlighting package.

    import React, { useState } from "react";
    import CodeEditor from '@uiw/react-textarea-code-editor/nohighlight';
    
    export default function App() {
      const [code, setCode] = 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={{
            backgroundColor: "#f5f5f5",
            fontFamily: 'ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace',
          }}
        />
      );
    }
    

    Show LineNumbers

    import rehypePrism from 'rehype-prism-plus';
    import React, { useState } from "react";
    import CodeEditor from '@uiw/react-textarea-code-editor';
    
    export default function App() {
      const [code, setCode] = 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)}
          rehypePlugins={[
            [rehypePrism, { ignoreMissing: true, showLineNumbers: true }]
          ]}
          style={{
            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/[email protected]
    
    // 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;
    

    Support dark-mode/night-mode

    By default, the dark-mode is automatically switched according to the system. If you need to switch manually, just set the data-color-mode="dark" parameter for html Element.

    <html data-color-mode="dark">
    
    document.documentElement.setAttribute('data-color-mode', 'dark')
    document.documentElement.setAttribute('data-color-mode', 'light')
    

    Inherit custom color variables by adding .w-tc-editor-var selector.

    const Demo = () => {
      return (
        <div>
          <div className="w-tc-editor-var"> </div>
          <CodeEditor value={code} />
        </div>
      )
    }
    

    Set (data-color-mode="dark") dark theme.

    import CodeEditor from '@uiw/react-textarea-code-editor';
    
    function App() {
      return (
        <CodeEditor
          value="function add(a, b) {\n  return a + b;\n}"
          data-color-mode="dark"
        />
      );
    }
    

    Props

    interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
      prefixCls?: string;
      /**
       * Support dark-mode/night-mode
       */
      ['data-color-mode']?: 'dark' | 'light';
      /**
       * Set what programming language the code belongs to.
       */
      language?: string;
      /**
       * Optional padding for code. Default: `10`.
       */
      padding?: number;
      /**
       * rehypePlugins (Array.<Plugin>, default: `[[rehypePrism, { ignoreMissing: true }]]`)  
       * List of [rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md#list-of-plugins) to use. See the next section for examples on how to pass options
       */
      rehypePlugins?: PluggableList;
      /**
       * The minimum height of the editor. Default: `16`.
       */
      minHeight?: number;
      onKeyDown?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void | boolean;
      /**
       * The number of spaces for indentation when pressing tab key. Default: `2`.
       */
      indentWidth?: number
    }
    

    List of supported languages can be found here

    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!

    See Also

    Contributors

    As always, thanks to our amazing contributors!

    Made with github-action-contributors.

    License

    Licensed under the MIT License.