
Components Book Module for Nuxt

Overview
This module provides a Storybook-like experience for Nuxt components, allowing you to document and test your Vue components using .stories.vue
files. It scans a specified directory for story files, generates dynamic routes, and creates an interactive UI for viewing and testing components.
This module serves as a lightweight alternative to Storybook, designed specifically for Nuxt projects. Unlike Storybook, which can be heavy and complex to configure, this module is built with Vue and seamlessly integrates into Nuxt, making setup and usage much simpler. All stories are written using Vue components, ensuring a more intuitive and consistent development experience within your project.


Features
- Automatically scans and registers
.stories.vue
files as pages. - Generates Vue files dynamically for each story.
- Extracts and displays component props in a structured table.
- Provides hot reloading with file watching.
- Adds a Nuxt DevTools tab for easy component exploration.
- 📌 Supports
CodeBlock.vue
for displaying and copying generated component usage. - 🚀 Provides
useCodeGenerator.ts
for automatic code snippet generation.
Installation
npm install --save-dev nuxt-componentsbook-module
or
yarn add --dev nuxt-componentsbook-module
Usage
1. Register the Module
Add the module to your nuxt.config.ts
:
export default defineNuxtConfig({
modules: [
'nuxt-componentsbook-module',
],
componentsBook: {
componentsDir: 'components',
disabled: false,
},
})
2. Creating a Story
Create a .stories.vue
file in your components directory:
MyInput.stories.vue
<script setup>
import { ref } from 'vue'
import CustomInput from './MyInput.vue'
import { useCodeGenerator } from '#imports'
const modelValue = ref('')
const label = ref('Enter Text')
const type = ref<'text' | 'password' | 'email' | 'number'>('text')
const placeholder = ref('Type something...')
const disabled = ref(false)
const readonly = ref(false)
const helperText = ref('This is a helper text.')
const size = ref<'sm' | 'md' | 'lg'>('md')
const { generatedCode, copyToClipboard } = useCodeGenerator('CustomInput', {
id: 'input',
'v-model': modelValue,
label,
type,
placeholder,
disabled,
readonly,
'helper-text': helperText,
size,
})
</script>
<template>
<h1>🟢 CustomInput Component</h1>
<p>
The <code>CustomInput</code> component is a versatile input field with multiple configurations.
</p>
<h2>🛠 Interactive Controls</h2>
<div class="controls">
<label>
Label:
<input v-model="label" type="text">
</label>
<label>
Type:
<select v-model="type">
<option value="text">Text</option>
<option value="password">Password</option>
<option value="email">Email</option>
<option value="number">Number</option>
</select>
</label>
<label>
Placeholder:
<input v-model="placeholder" type="text">
</label>
<label>
<input v-model="disabled" type="checkbox"> Disabled
</label>
<label>
<input v-model="readonly" type="checkbox"> Readonly
</label>
<label>
Helper Text:
<input v-model="helperText" type="text">
</label>
<label>
Size:
<select v-model="size">
<option value="sm">Small</option>
<option value="md">Medium</option>
<option value="lg">Large</option>
</select>
</label>
</div>
<h2>🔹 Preview</h2>
<CustomInput
id="input"
v-model="modelValue"
:label="label"
:type="type"
:placeholder="placeholder"
:disabled="disabled"
:readonly="readonly"
:helper-text="helperText"
:size="size"
/>
<h2>📋 Generated Code</h2>
<CodeBlock :generated-code="generatedCode" :copy-to-clipboard="copyToClipboard" />
</template>
3. Running the Components Book
Start your Nuxt development server:
npm run dev
Visit /componentsbook
in your browser to see the list of stories.
How It Works
- The module scans the specified
componentsDir
for .stories.vue
files. - It generates dynamic Vue pages for each story and registers them with Nuxt.
- A layout (
componentsbook-layout
) is added to encapsulate the story viewer. - A prop table is generated for each component based on its script setup.
- File watching via
chokidar
enables automatic updates when stories are modified. - A DevTools tab is added for quick access in Nuxt development mode.
🛠 DevTools Integration
When running in development mode, a new DevTools tab called Components Book is available, providing an iframe-based UI to explore component stories.
📡 API Endpoints
GET /__componentsbook_devtools_api__/api/files
: Returns a JSON list of available story files.
🤝 Contributing
Feel free to submit issues and pull requests to improve the module.