Storybook for Angular: Develop, document, and test UI components in isolation
npx @tessl/cli install tessl/npm-storybook--angular@9.1.0Storybook for Angular is a comprehensive framework integration that enables Angular developers to build, develop, document, and test UI components in isolation. It provides seamless integration with Angular's build system, modern Angular features, and the broader Storybook ecosystem.
npm install -D @storybook/angular storybookimport { Meta, StoryObj, moduleMetadata, applicationConfig } from "@storybook/angular";
import { argsToTemplate } from "@storybook/angular";For decorators and utilities:
import {
moduleMetadata,
componentWrapperDecorator,
applicationConfig
} from "@storybook/angular";import type { Meta, StoryObj } from "@storybook/angular";
import { moduleMetadata } from "@storybook/angular";
import { ButtonComponent } from "./button.component";
import { CommonModule } from "@angular/common";
const meta: Meta<ButtonComponent> = {
title: "Example/Button",
component: ButtonComponent,
decorators: [
moduleMetadata({
imports: [CommonModule],
}),
],
parameters: {
layout: "centered",
},
tags: ["autodocs"],
argTypes: {
backgroundColor: { control: "color" },
},
};
export default meta;
type Story = StoryObj<meta>;
export const Primary: Story = {
args: {
primary: true,
label: "Button",
},
};
export const Secondary: Story = {
args: {
label: "Button",
},
};Storybook for Angular is built around several key components:
Core types and interfaces for defining Angular component stories using Component Story Format (CSF) v2 and v3.
type Meta<TArgs = Args> = ComponentAnnotations<AngularRenderer, TransformComponentType<TArgs>>;
type StoryFn<TArgs = Args> = AnnotatedStoryFn<AngularRenderer, TransformComponentType<TArgs>>;
type StoryObj<TArgs = Args> = StoryAnnotations<AngularRenderer, TransformComponentType<TArgs>>;
type Decorator<TArgs = StrictArgs> = DecoratorFunction<AngularRenderer, TArgs>;
type Preview = ProjectAnnotations<AngularRenderer>;Angular-specific decorators for configuring module metadata, application configuration, and component wrapping in stories.
declare function moduleMetadata<TArgs = any>(
metadata: Partial<NgModuleMetadata>
): DecoratorFunction<AngularRenderer, TArgs>;
declare function applicationConfig<TArgs = any>(
config: ApplicationConfig
): DecoratorFunction<AngularRenderer, TArgs>;
declare function componentWrapperDecorator<TArgs = any>(
element: Type<unknown> | ((story: string) => string),
props?: ICollection | ((storyContext: StoryContext<AngularRenderer, TArgs>) => ICollection)
): DecoratorFunction<AngularRenderer, TArgs>;Utilities for converting story arguments to Angular template bindings and handling dynamic template generation.
declare function argsToTemplate<A extends Record<string, any>>(
args: A,
options?: ArgsToTemplateOptions<keyof A>
): string;
interface ArgsToTemplateOptions<T> {
include?: Array<T>;
exclude?: Array<T>;
}Angular CLI builders for starting and building Storybook applications integrated with Angular's build system.
type StorybookBuilderOptions = JsonObject & {
browserTarget?: string | null;
tsConfig?: string;
compodoc: boolean;
enableProdMode?: boolean;
styles?: StyleElement[];
assets?: AssetPattern[];
} & Pick<CLIOptions, 'port' | 'host' | 'configDir' | 'quiet' | 'disableTelemetry'>;Framework-specific configuration options and Storybook main configuration utilities for Angular projects.
interface StorybookConfig extends StorybookConfigBase, StorybookConfigWebpack {
framework: '@storybook/angular' | {
name: '@storybook/angular';
options: FrameworkOptions;
};
}
interface FrameworkOptions extends AngularOptions {
builder?: BuilderOptions;
}
declare function defineMain(config: StorybookConfig): StorybookConfig;Utilities for using Storybook stories outside of the Storybook environment, such as in testing frameworks.
declare function setProjectAnnotations(
projectAnnotations: NamedOrDefaultProjectAnnotations<any> | NamedOrDefaultProjectAnnotations<any>[]
): NormalizedProjectAnnotations<AngularRenderer>;interface AngularRenderer extends WebRenderer {
component: any;
storyResult: StoryFnAngularReturnType;
}
interface StoryFnAngularReturnType {
props?: ICollection;
moduleMetadata?: NgModuleMetadata;
applicationConfig?: ApplicationConfig;
template?: string;
styles?: string[];
userDefinedTemplate?: boolean;
}
interface NgModuleMetadata {
declarations?: any[];
entryComponents?: any[];
imports?: any[];
schemas?: any[];
providers?: Provider[];
}
interface ICollection {
[p: string]: any;
}interface AngularOptions {
enableIvy?: boolean;
}
type Parameters = DefaultParameters & {
bootstrapModuleOptions?: unknown;
};
type StoryContext = DefaultStoryContext<AngularRenderer> & { parameters: Parameters };