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

@matthamlin/react-preview-editor

v1.0.1

Published

React-Preview-Editor is a BYO live editor and preview for code. It is built on top of:

Downloads

32

Readme

React Preview Editor

React-Preview-Editor is a BYO live editor and preview for code. It is built on top of:

  • react-simple-code-editor,
  • prism-react-renderer, and
  • React Hooks

Getting Started

Take a look at the CodeSandbox here: https://codesandbox.io/s/wk69q5zv9k. To set this up locally, add the following:

yarn add @matthamlin/react-preview-editor [email protected] [email protected]
# or
npm add @matthamlin/react-preview-editor [email protected] [email protected]

Optionally Install @babel/standalone to transform JSX and future JavaScript features so the code can run within your browser.

Then import the components, and render them:

import { Provider, Editor, Preview } from '@matthamlin/react-preview-editor'
import { transform } from '@babel/standalone'

function transformCode(code) {
  return transform(code, { presets: [['stage-0', { decoratorsLegacy: true }], 'react'] }).code
}

render(
  <Provider
    code={`function App() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>Update count: {count}</button>
  );
}

render(<App />);`}
    transformCode={transformCode}
  >
    <Preview />
    <Editor />
  </Provider>,
)

Advanced Usage

react-preview-editor also exposes useEditor and usePreview hooks to build custom Editor and Preview components.

useEditor

The useEditor hook will provide you the following:

const { value, onValueChange, highlight } = useEditor({ getHighlighterProps })
  • value is the current code that the user is editing
  • onValueChange is a function that handles taking in the raw code as a string and updating the state of the code the user has entered
  • highlight is a function that is called with the code and returns a node that will be rendered to highlight the code

By default, highlight will return the Highlighter component from prism-react-renderer

  • getHighlighterProps is a required argument, that should return an object of props, this will be provided to the Highlighter component returned from highlight

usePreview

usePreview is an effectful hook, that will attempt to render the current code using the provided renderer

function Preview() {
  usePreview({ scope: { customVariableInScope: 5 }, render })
  return <div />
}
  • scope is an object of additional variables exposed within the live preview code (this is shallowly merged with the default values documented below in Features)
  • render is a function that calls ReactDOM.render, you can customize this by using the createRenderer export of the package where you provide a selector for an element that is provided to document.querySelector. Or you can provide a custom renderer here too.

Features

By default, react-preview-editor adds the following to scope when evaluating the code to render in the Preview component:

  • React
  • Component
  • Fragment
  • useState
  • useReducer
  • useEffect
  • useMemo
  • useCallback
  • useContext
  • Other values can be added to scope by providing an object to the scope prop on the Provider component