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

copy-container-react

v1.0.11

Published

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.

Downloads

157

Readme

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

| Name | Type | Default | Required | Description | |-------|-------|---------|----------|-------------| | lang | String | '' | No | Display the a string in the top left corner and pass the language to highlight.js so it doesn't need to guess. | | highlight | Boolean | true | No | Enable syntax highlighting. | | copyText | String | Copy Code | No | Text beside the clipboard before it is clicked. | | copiedText | String | Copied! | No | Text 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 :)