Preview.js Documentation

website·Indexed 33 days ago

https://previewjs.com/docs

A component preview tool available as an extension for Visual Studio Code, Cursor, and Windsurf. It provides first-class support for React (16.14+), including integrations with Next.js, Tailwind CSS, and React Router. The tool supports custom aliases, SVGR, static assets, and wrapper components for global styles and providers.

Tokens
3K
Snippets
19
Records
29
Agent score
66%

What's inside Preview.js

  1. Overview of Preview.js for React

    Preview.js is a tool that allows developers to preview React components and Storybook stories directly within their IDE (supporting VS Code, Cursor, and Windsurf). It uses Vite under the hood to provide instant refreshes as you type, eliminating the need for manual browser refreshes or build waits. Key features include zero-config setup, automatic detection of components and stories, and auto-generation of properties for TypeScript components.
  2. Understand the difference between Preview.js and Storybook

    While Storybook is designed for documenting components and writing stories (especially for engineering teams), Preview.js is designed for quickly visualizing components without writing boilerplate code. Preview.js Pro allows users to save previews as Storybook stories, meaning the two tools can be used together.
  3. Automatic alias detection via tsconfig.json and jsconfig.json

    Preview.js can automatically detect and resolve import aliases defined in tsconfig.json or jsconfig.json. If your aliases are already defined in these files, you may not need to provide additional manual configuration in Vite.
  4. Use Preview.js with React Native (Experimental)

    React Native support in Preview.js is currently experimental and only works for a subset of components. Components must not depend on expo packages (e.g., expo-web-browser) to be previewed. Due to these dependencies, the root App component is unlikely to work.
  5. Use Preview.js with React projects

    Preview.js works out of the box with React projects created using Create React App or Vite, including those using CSS-in-JS libraries like Styled Components or Emotion. Because Preview.js renders components in isolation, components depending on React context or global state will crash unless a <Wrapper /> component is configured to provide the necessary context providers and mocks.
  6. Use SASS, SCSS, or LESS in Preview.js

    SASS, SCSS, and LESS are supported out of the box. You can import .scss or .less files directly into your components just as you would with standard CSS.
    /* styles.scss */
    $color: blue;
    
    .button {
      background: $color;
      color: white;
    }
    import "./styles.scss";
  7. Configure Preview.js for Next.js using the config helper

    Because Preview.js renders components in isolation outside of the Next.js environment, components using <Link />, <Image />, or the useRouter() hook may crash. To resolve this, install the @previewjs/config-helper-nextjs package and import it within your __previewjs__/Wrapper.tsx file.
    // __previewjs__/Wrapper.tsx
    
    import "../styles/global.css";
    import "@previewjs/config-helper-nextjs";
  8. Install Preview.js extension for VS Code, Cursor, or Windsurf

    Preview.js is available as an extension for Visual Studio Code, Cursor, and Windsurf. To install it, open the Extensions panel in your editor and search for "Preview.js".
  9. Import CSS and global styles in Preview.js

    Preview.js supports CSS imports out of the box. You can import CSS files directly within a component for local styling, or import them within the Wrapper.tsx component to apply global styles across all previews.
    // Local import in a component
    import "./styles.css";
    
    // Global import in the wrapper
    // __previewjs__/Wrapper.tsx
    import "../src/styles.css";
    
    export function Wrapper({ children }) {
      return <div>{children}</div>;
    }
  10. Import static assets in Preview.js

    Preview.js supports importing static assets (such as images or SVGs) directly within your components using standard JavaScript import statements.
    import icon from "./icon.svg";
  11. Use Tailwind CSS with Preview.js

    Preview.js automatically loads your PostCSS configuration, allowing Tailwind CSS to work without additional setup. If Tailwind is not detected automatically, ensure a postcss.config.js file (or equivalent) exists in your project. If it still fails to load, you may need to configure a wrapper that imports your global CSS.
    module.exports = {
      plugins: {
        tailwindcss: {},
        autoprefixer: {},
      },
    };
  12. Configure SVGR support for SVG imports as React components

    To support importing SVG files as React components (e.g., import { ReactComponent as Logo } from "./logo.svg"), you must install vite-plugin-react-svgr as a dev dependency and add it to the vite.plugins array in your preview.config.js file.

    By setting exportAs: "ReactComponent", you enable named imports. If you prefer default imports (e.g., import Logo from "./logo.svg"), set exportAs: "default". If your project uses path aliases for SVG files, you must also provide the alias configuration within the svgr plugin options.

    // preview.config.js
    import { defineConfig } from "@previewjs/config";
    import { svgr } from "vite-plugin-react-svgr";
    
    export default defineConfig({
      vite: {
        plugins: [
          svgr({
            exportAs: "ReactComponent",
            // Use exportAs: "default" for default imports
            // alias: { ... } // Add if using SVG aliases
          }),
        ],
      },
    });