Socket
Book a DemoInstallSign in
Socket

copy-container-react

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

copy-container-react

Use this component to display code with syntax highlighting and a clipboard users can click to copy the code to their clipboard. The design is based on OpenAI's component in ChatGPT, and can be styled further.

1.0.11
latest
npmnpm
Version published
Weekly downloads
19
-59.57%
Maintainers
1
Weekly downloads
 
Created
Source

Introduction

Use this component to display code with syntax highlighting and a clipboard users can click to copy the code to their clipboard. The design is based on OpenAI's component in ChatGPT, and can be styled further.

The component can also be used to display text without syntax highlighting.

How it looks

basic difftheme text

Usage

import { CopyContainer } from 'copy-container-react'

function App(){
  return(
    <CopyContainer lang='python'>
{`import requests
from bs4 import BeautifulSoup

def crawl(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')

    for title in soup.find_all('h2'):  # assuming titles are within h2 tags
        print(title.get_text())

crawl('http://www.blogsite.com')
`}
  </CopyContainer>
  ) 
} 

Your text needs to be within braces (to embed JS in JSX) within grave accent marks (to start a multiline string) and needs to be a child of CopyContainer, as shown above. Spacing (tabs, index, new lines, etc.) in the multi-line string is preserved. It would be nice if the pasted text could start on the line after the grave accent so it would look nice in the code, but an extra new line would appear in your UI. The existing solution is likely the best we can do :)

All props can be omitted. Here's an example which uses all the props:

import { CopyContainer } from 'copy-container-react'

function App(){
  return(
    <CopyContainer lang='python' highlight={true} copyText='Copy :)' copiedText='Yay :)'>
{`import requests
from bs4 import BeautifulSoup

def crawl(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')

    for title in soup.find_all('h2'):  # assuming titles are within h2 tags
        print(title.get_text())

crawl('http://www.blogsite.com')
`}
  </CopyContainer>
  ) 
} 

Props

NameTypeDefaultRequiredDescription
langString''NoDisplay the a string in the top left corner and pass the language to highlight.js so it doesn't need to guess.
highlightBooleantrueNoEnable syntax highlighting.
copyTextStringCopy CodeNoText beside the clipboard before it is clicked.
copiedTextStringCopied!NoText beside the clipboard after it is clicked.

Choose a syntax highlighting style

To use a different style from the default, import one from this link: https://github.com/highlightjs/highlight.js/tree/main/src/styles like so:

import 'highlight.js/styles/a11y-light.css';

Syntax highlighting styles and overriding CSS will likely interfere with each other so be careful if you choose to do both.

Override CSS

Use the following classes in your own css to style parts of the UI.

.text-lang{}, .text-copy{}, .top-bar{}, .code-body{}, .check{}, .clipboard{}

Let me know if anything should be added. If you want to style an element yourself you could use devtools to figure out a css selector.

Here's an example of the CSS used for the screenshot with the green top bar in the 'How it looks' section:

.clipboard{
  stroke: #C71585;
}
.top-bar{
  background-color: #3CB371;
}
.text-body{
  background-color: whitesmoke;
}

Additional information

The syntax highlighting library used by this component is highlight.js and the style is stackoverflow-dark.css. Highlight.js tries to automatically detect the programming language of the code, but it hasn't been correct in my experience, though I could be doing something wrong and appreciate their library very much. See their docs for more information (you can examine the highlightjs object to see confidence scores the library gives for its predictions).

Contact

Please open an issue on github or email adam.p.zakaria at gmail dot com with any questions or comments :)

Keywords

code-display

FAQs

Package last updated on 27 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.