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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@codelook/preview

v0.1.0

Published

A professional code preview component with support for React, Vue, and HTML

Readme

@codelook/preview

一个专业的代码预览组件,支持 React、Vue 和纯 HTML 的实时预览。

特性

  • 🚀 多框架支持:React、Vue、Angular、Svelte 和纯 HTML/CSS/JS
  • 🎨 智能检测:自动检测代码类型和框架
  • 🔥 实时预览:代码修改即时生效
  • 📦 依赖管理:支持 npm 包的自动安装
  • 🛡️ 安全沙箱:隔离的执行环境
  • 🎯 TypeScript:完整的类型支持
  • 🌙 主题支持:内置亮色/暗色主题

安装

npm install @codelook/preview
# 或
yarn add @codelook/preview
# 或
pnpm add @codelook/preview

使用

基础用法

import { CodePreview } from '@codelook/preview';

function App() {
  const code = `
    function App() {
      return <h1>Hello, World!</h1>;
    }
    
    export default App;
  `;

  return <CodePreview code={code} />;
}

多文件项目

import { CodePreview } from '@codelook/preview';

function App() {
  const files = {
    'App.js': `
      import './styles.css';
      
      function App() {
        return (
          <div className="app">
            <h1>Hello, World!</h1>
          </div>
        );
      }
      
      export default App;
    `,
    'styles.css': `
      .app {
        text-align: center;
        padding: 20px;
        font-family: sans-serif;
      }
    `,
    'index.js': `
      import React from 'react';
      import ReactDOM from 'react-dom/client';
      import App from './App';
      
      const root = ReactDOM.createRoot(document.getElementById('root'));
      root.render(<App />);
    `
  };

  return <CodePreview code={files} />;
}

HTML 预览

import { CodePreview } from '@codelook/preview';

function App() {
  const htmlCode = `
    <!DOCTYPE html>
    <html>
    <head>
      <title>示例页面</title>
      <style>
        body {
          font-family: Arial, sans-serif;
          padding: 20px;
        }
        .button {
          background: #007bff;
          color: white;
          padding: 10px 20px;
          border: none;
          border-radius: 4px;
          cursor: pointer;
        }
      </style>
    </head>
    <body>
      <h1>欢迎!</h1>
      <button class="button" onclick="alert('你好!')">
        点击我
      </button>
    </body>
    </html>
  `;

  return <CodePreview code={htmlCode} language="html" />;
}

高级配置

import { CodePreview } from '@codelook/preview';

function App() {
  return (
    <CodePreview
      code={code}
      theme="dark"
      height="500px"
      showToolbar={true}
      showLineNumbers={true}
      showConsole={true}
      autoRun={true}
      sandpackConfig={{
        dependencies: {
          "axios": "^1.6.0",
          "lodash": "^4.17.21"
        },
        template: "react-ts"
      }}
      onReady={() => console.log('预览准备就绪')}
      onError={(error) => console.error('预览错误:', error)}
      onChange={(files) => console.log('文件改变:', files)}
    />
  );
}

API

CodePreview Props

| 属性 | 类型 | 默认值 | 描述 | |------|------|--------|------| | code | string \| CodeFiles | - | 要预览的代码 | | language | Language | 自动检测 | 代码语言 | | framework | Framework | 自动检测 | 使用的框架 | | theme | Theme | 'light' | 主题设置 | | height | string \| number | '400px' | 预览高度 | | showToolbar | boolean | true | 显示工具栏 | | showLineNumbers | boolean | true | 显示行号 | | showConsole | boolean | false | 显示控制台 | | autoRun | boolean | true | 自动运行 | | onReady | () => void | - | 预览准备就绪回调 | | onError | (error: PreviewError) => void | - | 错误回调 | | onChange | (files: CodeFiles) => void | - | 代码改变回调 |

支持的语言

  • javascript
  • typescript
  • jsx
  • tsx
  • html
  • css
  • vue

支持的框架

  • react
  • vue
  • angular
  • vanilla (纯 JavaScript)
  • svelte

进阶用法

使用 PreviewManager

import { PreviewManager } from '@codelook/preview';

const manager = new PreviewManager({
  code: 'console.log("Hello, World!")',
  language: 'javascript'
});

// 初始化
await manager.initialize(containerElement);

// 更新代码
manager.updateCode('console.log("Updated!")');

// 运行
manager.run();

// 销毁
await manager.destroy();

自定义 Provider

import { BaseProvider } from '@codelook/preview';

class CustomProvider extends BaseProvider {
  // 实现自定义预览逻辑
}

开发

# 安装依赖
npm install

# 开发模式
npm run dev

# 构建
npm run build

# 测试
npm test

License

MIT