6.8 KiB
JSON Package Format in Storybook
This document describes the Storybook integration for loading packages in pure JSON format.
Overview
Storybook supports the JSON-based package format defined in the /schemas/package-schemas/ directory. All packages use pure JSON definitions that are easy to work with, validate, and extend.
Package Structure
Packages are now defined using JSON files with the following structure:
packages/
my_package/
package.json # Package metadata (schema: metadata_schema.json)
components/
ui.json # Component definitions (schema: components_schema.json)
permissions/
roles.json # Permission definitions (schema: permissions_schema.json)
styles/
index.json # Style definitions (schema: styles_schema.json)
scripts/
[script-name].json # Script definitions (schema: script_schema.json, e.g., automation.json)
types/
index.json # Type definitions (schema: types_schema.json)
Key Files
1. Package Discovery (storybook/src/utils/packageDiscovery.ts)
This module handles automatic discovery and loading of JSON packages:
discoverAllPackages()- Discovers all packages by loading their package.json filesloadPackage(packageId)- Loads a single package with all its resourcesdiscoverPackagesWithComponents()- Filters packages with component definitionsdiscoverPackagesWithPermissions()- Filters packages with permission definitionsdiscoverPackagesWithStyles()- Filters packages with style definitionsgetPackagesByCategory()- Groups packages by categorygetPackagesByLevel()- Groups packages by minimum permission level
2. JSON Component Renderer (storybook/src/utils/jsonComponentRenderer.tsx)
This module renders JSON component definitions to React elements:
renderJsonComponent(component, props)- Renders a JSON component with given propscreateComponentStory(component, defaultProps)- Creates a Storybook story for a component- Supports template expressions like
{{props.title}} - Handles conditional rendering
- Maps JSON element types to HTML/React elements
3. Storybook Stories
Auto-Discovered Packages (storybook/src/stories/AutoDiscovered.stories.tsx)
Three main stories for browsing packages:
- Package Browser - Shows all discovered packages with their metadata
- Components Browser - Lists all components from all packages
- Permissions Browser - Displays all permissions in a table format
JSON Components (storybook/src/stories/JSONComponents.stories.tsx)
Interactive component viewer:
- Component Viewer - Interactive tool to browse and test components
- Select package and component
- Adjust props dynamically
- Live preview
- View JSON definition
- Nav Menu Sidebar - Example rendering of a specific component
Legacy Lua Packages (storybook/src/stories/LuaPackages.stories.tsx)
Renamed and updated to clarify this is for legacy Lua-based packages.
Schema Validation
All JSON files reference their schemas using the $schema property:
{
"$schema": "https://metabuilder.dev/schemas/package-metadata.schema.json",
"packageId": "my_package",
"name": "My Package",
...
}
Available schemas:
metadata_schema.json- Package metadata (package.json)components_schema.json- UI component definitionspermissions_schema.json- Permission and role definitionsstyles_schema.json- Style definitionsscript_schema.json- Script/function definitionstypes_schema.json- Type definitionsentities_schema.json- Entity/data model definitionsapi_schema.json- API endpoint definitionsforms_schema.json- Form definitionsstorybook_schema.json- Storybook-specific configuration
Package Metadata Fields
Key fields in package.json:
{
"packageId": "my_package", // Unique identifier (snake_case)
"name": "My Package", // Display name
"version": "1.0.0", // Semver version
"description": "...", // Package description
"category": "ui", // Category for organization
"minLevel": 2, // Minimum permission level
"primary": false, // Featured package flag
"exports": { // Exported items
"components": ["Button", "Card"],
"scripts": ["validate", "transform"]
},
"storybook": { // Storybook configuration
"featured": true,
"excludeFromDiscovery": false,
"stories": [...]
}
}
Component Definitions
Components are defined in components/ui.json:
{
"$schema": "https://metabuilder.dev/schemas/json-script-components.schema.json",
"schemaVersion": "2.0.0",
"package": "my_package",
"components": [
{
"id": "my_button",
"name": "MyButton",
"description": "A customizable button component",
"props": [
{
"name": "label",
"type": "string",
"required": true,
"description": "Button text"
}
],
"render": {
"type": "element",
"template": {
"type": "button",
"className": "btn",
"children": "{{label}}"
}
}
}
]
}
Permission Definitions
Permissions are defined in permissions/roles.json:
{
"$schema": "https://metabuilder.dev/schemas/permissions.schema.json",
"schemaVersion": "1.0.0",
"package": "my_package",
"permissions": [
{
"id": "my_package.view",
"name": "View My Package",
"description": "View package content",
"resource": "my_package",
"action": "read",
"scope": "global",
"minLevel": 2
}
],
"resources": [
{
"id": "my_package",
"name": "My Package",
"type": "component",
"actions": ["read", "create", "update", "delete"]
}
]
}
Running Storybook
cd storybook
npm run storybook
Then navigate to:
- Packages/Auto-Discovered - Browse all packages
- Packages/JSON Components - Interactive component viewer
Package Format
All packages use pure JSON format with schema validation:
package.json- Package metadata with schema referencecomponents/ui.json- Component definitionspermissions/roles.json- Permission and role definitionsstyles/index.json- Style definitions- All files include
$schemareferences for validation
Future Enhancements
Potential improvements:
- Build-time package index - Generate
/packages-index.jsonduring build - Component live editing - Edit component JSON in Storybook
- Schema validation UI - Visual feedback for schema errors
- Component composition - Compose components from other components
- Theme support - Apply different themes to components
- Export to code - Generate React/Vue code from JSON components