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

code-live-sandbox

v1.0.7

Published

A React code sandbox in the browser. Useful for component library documentation, code execution, React playgrounds, and more.

Downloads

7

Readme

中文

code-live-sandbox

React Code Sandbox in the Browser

Ideal for component library documentation, code executors, React playgrounds, and more.

Advantages Over Similar Products

  • Consistency with React Mechanisms: Ensures lifecycle methods and hooks work as expected.
  • Highly Extensible: Supports custom transformations.
  • Inline and Mounting Modes: Flexible integration options.
  • Minimalistic Code: Clean and straightforward implementation.
  • Complex Module Import System: Handles intricate import scenarios seamlessly.
  • Automatic Dependency Analysis And Install: Supports automatic analysis of imported packages in files and downloading npm dependencies.

Installation

pnpm add @swc/wasm-web code-live-sandbox

Basic Usage

import * as react from "react";
import { LivePreview, LiveProvider, useLiveContext } from 'code-live-sandbox';
// Editor component is required, implement an editable container like MirrorCode, vscode, etc.
import Editor from '../Editor';

const Live = useMemo(() => {
  let node;
  try {
    node = !showView ? <Editor /> : <LivePreview />;
  } catch (err) {
    node = <Editor />;
  }
  return (
    <LiveProvider
      code={value}
      scope={{
        import: { // Define third-party package dependencies in import
          react
        }
      }}
      props={props}
    >
      <Error />
      {node}
    </LiveProvider>
  );
}, [props, value, showView]);

Using SWC as the Default Transformer, Supporting Custom Bundlers

import { setDrive } from 'code-live-sandbox';
import { transform as _transform } from 'sucrase';

setDrive((code) => {
  return _transform(code, {
    transforms: ['jsx', 'typescript', 'imports'],
    production: true,
  }).code.substring(13); // remove leading `"use strict";`
});

Default Configuration with TypeScript Support, Customizable Configuration

import { changeTransform } from 'code-live-sandbox';
import { merge } from 'lodash';

// Set custom component compilation rules
changeTransform((config) => {
  return merge(
    {
      jsc: {
        transform: {
          react: { development: process.env.NODE_ENV === 'development' },
        },
      },
    },
    config
  );
});

Inline and Mounting Modes

  • Inline Mode: Perfectly integrates with the current React app.
  • Mounting Mode: Mounts in a separate DOM container, allowing multiple React instances on the same page.
<LiveProvider code={value} alone={true}...

Multi-File Support

import { LivePreview, LiveProvider } from 'code-live-sandbox';
import useEditor from '../useEditor';

const files = {
  './login.tsx': `
    import React from 'react';
    import Content from './Content';
    const Login = () => {
      return (
        <Content/>
      );
    }
    export default Login;
  `,
  'Login.module.css': `
    .button {
      background-color: red;
    }
  `,
  './components/Button.tsx': `
    import React from 'react';
    import '../Login.module.css';
    const Button = () => {
      return (
        <button className="button">
          按钮
        </button>
      );
    }
    export default Button;
  `,
  './Content.tsx': `
    import React from 'react';
    import Button from './components/Button';
    const Content = () => {
      return (
        <Button/>
      );
    }
    export default Content;
  `,
};

const Live = (files, import) => {
  return (
    <LiveProvider
      code={value}
      alone={alone}
      scope={{
        import,
        files // Define files with relative paths, e.g., ./a.tsx, a.css
      }}
      props={props}
    >
      <LivePreview />
    </LiveProvider>
  );
};

const LiveWithEditor = (scope, setScope) => {
  // files contain all relative paths and content, import contains all available modules
  const { files, import: dependencies } = useMemo(() => withMultiFiles(scope), [scope]);

  // Edit file content as needed
  const editor = useEditor(files, setScope);

  return (
    <LiveProvider
      code={value}
      alone={alone}
      scope={{
        // No need for files anymore
        import: dependencies
      }}
      props={props}
    >
      <LivePreview />
      {editor}
    </LiveProvider>
  );
};

Automatically analyze the imported packages in the file and download npm dependencies.

function useAsyncMemo<T>(fn: () => Promise<T>, deps: any[]): T | undefined {
    const [value, setValue] = useState<T>();
    useEffect(() => {
        fn().then(v => {
            setValue(v);
        });
    }, deps);
    return value;
}

function Demo({props,files,dependenciesMap={},code}:{
  props:Record<string,any>,
  files:Record<string,string>,
  dependenciesMap:Record<string,any>,
  code:string;
}){
  const resolveDeps = useAsyncMemo(async () => {
        setInitStatus(false);
        const mods = await fetchPackagesFromFiles({
            files: {
                ...files,
                [ENTRY_FILE_PATH]: code,
                Require: `
                import * as React from 'react';
                import  * as ReactDom from 'react-dom';
                export {React,ReactDom}
            `,
            },
            parseDepsSuccess: deps => {
            },
        });

        return {
            ...mods,
            ...dependenciesMap,
        };
    }, [JSON.stringify(files), code, dependenciesMap]);

    return <LiveProvider
                code={code}
                scope={{
                    import: resolveDeps,
                }}
                props={props}
            >
                <Error />
                {node}
            </LiveProvider>
}

Roadmap

  • Online playground with demos of all features.