react-native-enriched-html

repository·main·Indexed 23 days ago

https://github.com/software-mansion/react-native-enriched-html

A high-performance HTML-based Rich Text solution for React Native (version 1.1.0) providing native input and display components. It supports Android, iOS, and Web, requiring the React Native New Architecture (Fabric) and supporting React Native versions 0.81 through 0.86. The library includes the EnrichedTextInput component for rich text input with an imperative API for toggling styles and a C++ HTML Normalizer based on gumbo-parser.

Tokens
15.8K
Snippets
27
Records
68
Agent score
76%

What's inside react-native-enriched-html

  1. Manage links in EnrichedTextInput

    main

    Links can be handled in two ways:

    1. Automatic detection: The component automatically detects URLs. You can customize this behavior using the linkRegex prop.
    2. Manual application: Use the setLink method on the input ref. You can derive the start, end, and text arguments from the onChangeSelection event payload to apply a link to the user's current selection.

    Passing a different text argument to setLink than what is currently selected will replace the selected text with the new text before applying the link.

  2. Implement mentions with mentionIndicators and events

    main

    Mentions allow users to type phrases like @someone or #channel.

    • Configuration: Use the mentionIndicators prop to define trigger characters (default is [ @ ]).
    • Starting a mention: Triggered by typing an indicator or calling the startMention ref method.
    • Handling events:
      • onStartMention: Emitted when a mention starts or when the cursor returns to an unfinished mention.
      • onChangeMention: Emitted as the user types or removes characters after an indicator.
      • onEndMention: Emitted when the user finishes the mention (e.g., by typing a space or moving the cursor away).
    • Completing a mention: Call the setMention ref method to finalize the mention (e.g., after a user selects an item from a custom UI list).
  3. Toggle non-parametrized styles

    main

    You can toggle various styles on the current text selection by calling the appropriate toggle function on the component ref. Styles are categorized into two types:

    • Inline styles: Applied to the exact character range selected. If no selection exists, the style is applied to the next characters typed.
      • Supported: bold, italic, underline, strikethrough, inline code.
    • Paragraph styles: Applied to the entire paragraph (text between newlines) containing the selection. If the selection spans multiple paragraphs, multiple paragraphs are affected. If no selection exists, the style is applied to the current paragraph.
      • Supported: H1 through H6 headings, codeblock, blockquote, ordered list, unordered list, checkbox list.
  4. Upgrade Google Test (GTest) version

    main

    Google Test is managed via CMake's FetchContent. To change the version used by the project, modify the GIT_TAG in cpp/CMakeLists.txt.

    After updating the tag, you must perform a clean rebuild by removing the existing build directory.

    # In cpp/CMakeLists.txt
    FetchContent_Declare(
        googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG        v1.17.0   # ← change this to the desired version/tag
    )
    cd cpp
    rm -rf build
    cmake -B build -DCMAKE_BUILD_TYPE=Debug
    cmake --build build
  5. Install react-native-enriched-html in a Bare React Native app

    main

    Follow these steps to install the library in a standard React Native project:

    1. Install the package using yarn:

      yarn add react-native-enriched-html

      Tip: To use the latest features, you can install the nightly build with yarn add react-native-enriched-html@nightly.

    2. Install iOS dependencies: Since the library contains native code, you must re-build the native app. Run the following in your project root:

      cd ios && bundler install && bundler exec pod install
    yarn add react-native-enriched-html
  6. Install react-native-enriched-html in an Expo app

    main

    Follow these steps for Expo projects:

    1. Install the package:

      npx expo install react-native-enriched-html
    2. Run prebuild: Because the library includes native code, you must run prebuild to generate the necessary native files:

      npx expo prebuild

    Important: This library will not work in Expo Go because it requires custom native changes.

    npx expo install react-native-enriched-html
  7. Handle Client-only rendering for SSR (Next.js, Remix, Gatsby)

    main

    Both EnrichedText and EnrichedTextInput are client-only components. They rely on browser-specific APIs such as DOMParser, DOMPurify, and TipTap.

    If you are using a framework with Server-Side Rendering (SSR) like Next.js, Remix, or Gatsby, you must ensure these components are only rendered on the client side to avoid errors.