Preview.js Documentation
website·Indexed 33 days ago
https://previewjs.com/docsA 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.
What's inside Preview.js
- 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.
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.Automatic alias detection via tsconfig.json and jsconfig.json
Preview.js can automatically detect and resolve import aliases defined intsconfig.jsonorjsconfig.json. If your aliases are already defined in these files, you may not need to provide additional manual configuration in Vite.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 onexpopackages (e.g.,expo-web-browser) to be previewed. Due to these dependencies, the root App component is unlikely to work.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.Use SASS, SCSS, or LESS in Preview.js
SASS, SCSS, and LESS are supported out of the box. You can import.scssor.lessfiles directly into your components just as you would with standard CSS./* styles.scss */ $color: blue; .button { background: $color; color: white; }import "./styles.scss";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 theuseRouter()hook may crash. To resolve this, install the@previewjs/config-helper-nextjspackage and import it within your__previewjs__/Wrapper.tsxfile.// __previewjs__/Wrapper.tsx import "../styles/global.css"; import "@previewjs/config-helper-nextjs";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".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 theWrapper.tsxcomponent 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>; }Import static assets in Preview.js
Preview.js supports importing static assets (such as images or SVGs) directly within your components using standard JavaScriptimportstatements.import icon from "./icon.svg";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 apostcss.config.jsfile (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: {}, }, };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 installvite-plugin-react-svgras a dev dependency and add it to thevite.pluginsarray in yourpreview.config.jsfile.By setting
exportAs: "ReactComponent", you enable named imports. If you prefer default imports (e.g.,import Logo from "./logo.svg"), setexportAs: "default". If your project uses path aliases for SVG files, you must also provide thealiasconfiguration within thesvgrplugin 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 }), ], }, });