Code Monkey home page Code Monkey logo

code-kitchen's Introduction

Code Kitchen πŸ§‘β€πŸ³

Code Kitchen is a React live-coding playground which allows the developers to embed React component demos into a React UI library’s web documents.

CI

Compared to other solutions, it supports the following features:

  • Private source code dependencies support πŸ”’
  • Multiple files in a single demo
  • CSS (CSS modules) and JSON files
  • A completely static solution without the need for a remote server πŸ”Œ
  • Type hints in the code editor

Demo 🎩

Check out the demo at https://freewheel.github.io/code-kitchen/home

Usage

Install with npm install code-kitchen.

Note: due to the size of esbuild-wasm and monaco-editor, they are not bundled with this library. By default they are loaded via jsDelivr CDN. You can configure this by calling setup in the runtime.

You can checkout a minimum example at this Code Sandbox.

Minimal setup

import * as React from "react";
import { Playground, setup } from "code-kitchen";

import * as privateLib from "my-private-lib";

const dependencies = {
  react: React,
  "my-private-lib": privateLib,
};

const customRequire = (key: string) => {
  const res = (dependencies as any)[key];

  if (res) {
    return res;
  }

  throw new Error("DEP: " + key + " not found");
};

// Two files for the demo playground
const files = [
  {
    code: `
import { Button } from "my-private-lib";
import "./styles.css";

export default function Demo() {
  return <Button>Button</Button>;
}
  `,
    filename: "App.jsx",
  },
  {
    code: `button { width: 200px; }`,
    filename: "styles.css",
  },
];

export default () => {
  return <Playground initialFiles={files} require={customRequire} />;
};

With MDX

Most of the time you may want to use Code Kitchen in a MDX document. You can checkout docs/components/MDXPlayground.mdx as a real-life example.

Project Structure

The project is a monorepo managed by pnpm.

β”œβ”€β”€ docs # Documentation site
β”‚   β”œβ”€β”€ components
β”‚   β”‚   └── MDXPlayground.tsx # configure Code Kitchen for MDX and how to load user-land types
β”‚   └── pages
β”‚       └── index.mdx # A real-life multi-file playground demo in MDX
└── packages
    β”œβ”€β”€ demo-lib # a private React UI library for demo purpose
    └── src # ⭐ the core source code for Code Kitchen
      β”œβ”€β”€ playground.tsx # the main playground component
      β”œβ”€β”€ bundle.ts # the in-browser bundler implemented by [esbuild-wasm](https://esbuild.github.io/api/#running-in-the-browser)
      β”œβ”€β”€ index.ts # the main library entry point
      └── use-monaco.ts # monaco-editor loader that wrapped into a React hook

Local Development

This library is developed with a typical Next.js document site.

Prerequisites

  • Node.js
  • pnpm

Install and start

# Under the root of the repository
pnpm i
pnpm dev

Now you will be able to preview the document in localhost:3000

Q/A

How the in-browser bundler works?

See this explanation

How to Contribution

See Contributing Guide

Acknowledgements

See Acknowledgements

License

Apache-2.0 by FreeWheel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.