npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-coderenderer

v1.1.4

Published

A lightweight and customizable code rendering component for React, supporting syntax highlighting, dark mode, and copy functionality.

Downloads

127

Readme

react-coderenderer

A flexible and customizable code rendering component for React applications, supporting syntax highlighting, dark mode, and copy functionality. This package allows developers to easily embed code blocks into their React applications with a clean interface and minimal setup.

See the Examples

Table of Contents

Features

  • Syntax Highlighting: Automatically highlights code using react-syntax-highlighter.
  • Copy to Clipboard: Provides an integrated copy button to copy code snippets.
  • Dark Mode Support: Built-in support for light and dark modes.
  • Customizable: Opt-out of default Tailwind CSS styles and apply your own custom styles.
  • Smooth Transitions: Animate between code previews and actual code with smooth transitions.
  • Optimized for React: Designed specifically for React applications with full TypeScript support.

Prerequisites

Make sure you have a working React environment. This package assumes you are using:

  • React 18 or later
  • React-DOM 18 or later

Installation

Install the package via npm:

npm install react-coderenderer

Or via yarn:

yarn add react-coderenderer

Usage

Import the CodeRenderer into your React component:

import React from "react";
import { CodeRenderer } from "react-coderenderer";
import "react-coderenderer/dist/index.css"; // Import default styles

Pass the required props to the CodeRenderer component:

<CodeRenderer code={code}>
  <MyComponent />
</CodeRenderer>

Basic Example

Here's a basic example of how to use the CodeRenderer component:

import React from "react";
import { CodeRenderer } from "react-coderenderer";
import "react-coderenderer/dist/index.css"; // Import default styles

const code = `
import React from 'react';

function MyComponent() {
  return (
    <div className="p-4 border">
        <h2 className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">My Custom Component</h2>
    </div>
  );
}

export default MyComponent;
`;

const BasicCodeRenderer: React.FC = () => {
  return (
    <CodeRenderer code={code}>
      <div className="p-4 border">
        <h2 className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
          My Custom Component
        </h2>
      </div>
    </CodeRenderer>
  );
};

export default BasicCodeRenderer;

Props

The CodeRenderer component accepts the following props:

Props

| Prop | Type | Required | Description | | ---------------------- | ----------- | -------- | ------------------------------------------------------------------------------ | | code | string | Yes | The code to display inside the code block. | | children | ReactNode | Yes | The content to display when the "Preview" button is selected. | | disableDefaultStyles | boolean | No | Disables default Tailwind CSS styles if set to true. | | customClassNames | object | No | Custom class names for various elements (e.g., container, button, code block). | | enableDarkMode | boolean | No | Enables dark mode for the code block. |

Opting Out of Default Styles

If you prefer to use your own styling or are not using Tailwind CSS in your project, you can opt-out of the default styles provided by the package. Here's how:

  1. Do Not Import the Default CSS
// import "react-coderenderer/dist/index.css"; // Do not import this
  1. Set disableDefaultStyles to true

Pass the disableDefaultStyles prop to the CodeRenderer:

<CodeRenderer
  code={code}
  disableDefaultStyles={true}
  customClassNames={{
    container: "my-custom-container",
    buttonGroup: "my-custom-button-group",
    button: "my-custom-button",
    copyButton: "my-custom-copy-button",
    codeBlock: "my-custom-code-block",
  }}
>
  <MyComponent />
</CodeRenderer>
  1. Provide Custom Class Names (Optional)

If you want to apply your own styles, you can pass custom class names via the customClassNames prop. This allows you to fully customize the appearance of the component.

import React from "react";
import { CodeRenderer } from "react-coderenderer";
import "./my-custom-styles.css"; // Your custom styles

const exampleCode = `
import React from 'react';
const HelloWorld = () => <div>Hello, World!</div>;
`;

const MyComponent = () => <div>Hello, World!</div>;

const Example = () => (
  <CodeRenderer
    code={exampleCode}
    disableDefaultStyles={true}
    customClassNames={{
      container: "my-custom-container",
      buttonGroup: "my-custom-button-group",
      button: "my-custom-button",
      copyButton: "my-custom-copy-button",
      codeBlock: "my-custom-code-block",
    }}
  >
    <MyComponent />
  </CodeRenderer>
);

export default Example;

Contributing

Contributions are welcome! If you'd like to contribute to this project, please follow these steps:

  1. Fork the repository.
  2. Create a new feature branch (git checkout -b feature/your-feature-name).
  3. Commit your changes (git commit -m "Add your message").
  4. Push to the branch (git push origin feature/your-feature-name).
  5. Create a pull request detailing your changes.

Please ensure your code adheres to the project's coding standards and includes relevant tests if necessary. For more information, see CONTRIBUTING.md.

Versioning

We use Semantic Versioning for versioning. For the versions available, see the tags on this repository.

To bump the version, update the version field in package.json and follow the guidelines in the CONTRIBUTING.md file.

License

This project is licensed under the ISC License - see the LICENSE file for details.

Code of Conduct

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to [INSERT EMAIL HERE].

Acknowledgments

  • Inspired by common code rendering and highlighting patterns in React applications.
  • Thanks to all contributors and users for their support.
  • A special shout-out to the open-source community for providing tools and inspiration for this project.