The user wants to build a custom component library for Angular using SCSS and Storybook. This is a complex request involving setting up an Angular workspace, creating a library, configuring SCSS, and integrating Storybook for component documentation and development.
1. Understand the Goal:
The objective is to create a reusable, modular Angular component library. Key technologies:
Framework: Angular (latest or recent stable version).
Styling: SCSS (Sass).
Documentation/Development Environment: Storybook.
2. Key Steps to Solution:
Step 1: Prerequisites: Node.js, Angular CLI.
Step 2: Workspace Setup: Create a new Angular workspace (usually using ng new). Self-correction: If the user just wants a library, they usually need a workspace first. It's best to create a workspace that isn't just a standard app, or create a standard app and generate the library inside it. The standard Angular CLI approach is ng new my-workspace --create-application=false, then ng generate library my-lib. This is cleaner for libraries.
Step 3: Generate Library: Use Angular CLI to generate the library structure.
Step 4: Create a Component: Create a dummy component (e.g., a Button) inside the library to demonstrate the setup.
Step 5: Styling (SCSS): Ensure the project is configured to use SCSS. Angular CLI usually handles this, but I need to show how to style components using SCSS and global styles if necessary.
Step 6: Integrate Storybook: This is a crucial step. Use npx storybook@latest init (or specific builder @storybook/angular). Self-correction: The storybook init command is smart enough to detect Angular and set things up.
Step 7: Write Stories: Create a .stories.ts file for the component to show it in Storybook.
Step 8: Build and Export: How to build the library (ng build) and how the public-api.ts works.
3. Detailed Walkthrough & Code Structure:
Initialization:
ng new my-ui-lib --style=scss --create-application=false -> This creates a workspace purely for the library. It avoids the default src app folder, which keeps it clean.
cd my-ui-lib
ng generate library custom-components --prefix=custom -> Creates projects/custom-components.
Component Development:
Go into projects/custom-components/src/lib.
Create button/button.component.ts.
Add SCSS styling. Since it's a library, styles are often inline or relative .scss files. I'll use external .scss for better organization.
Update custom-components.module.ts to export the component.
Storybook Integration:
Run npx storybook@latest init.
This adds .storybook folder and a stories folder (usually in the root or inside the project).
Crucial configuration: By default, Storybook might look at the root src. Since this is a library setup, I need to ensure Storybook looks at the projects folder or the compiled library. Usually, for development, we point it to the library source files.
Writing the Story:
button.component.stories.ts.
Use Meta and StoryObj (CSF 3.0 is the standard now).
Building the Library:
ng build custom-components.
4. Refinement and Best Practices:
Global Styles vs. Component Styles: Explain how to handle SCSS variables (e.g., _variables.scss) so they are available to components. This is a common pain point. Using stylePreprocessorOptions in angular.json is the standard way.
Directory Structure: A clear folder structure is vital for a library.
Exports: Emphasize public-api.ts.
5. Drafting the Content:
Introduction: Brief overview.
Step 1: Setup Workspace: CLI commands.
Step 2: Create Library: CLI commands.
Step 3: Create Component (The SCSS part): Actual code for a button. Show how to bind classes.
Step 4: Configure SCSS Imports (Optional but recommended):angular.json config for clean imports (@import 'variables').
Step 5: Add Storybook: The installation command.
Step 6: Write a Story: The story code using CSF 3.
Step 7: Running and Building: Commands to run SB and build the lib.
6. Potential Pitfalls/Troubleshooting:
Storybook finding the component: Storybook needs to know about the Angular module. The story setup usually requires importing the CustomComponentsModule.
SCSS paths: If a component imports a variable file from a parent directory, paths can be tricky in libraries. I will stick to a simple relative path or the preprocessor option.
7. Final Review of the "Prompt":
"Build a custom component library for Angular using SCSS and Storybook."
The output should be a tutorial/guide.
Self-Correction during drafting: I should stick to the latest Angular (17+) syntax where possible (Standalone components), but libraries often still use Modules for compatibility or simplicity in explanation. However, Standalone is the default in new Angular versions. I will use Standalone Components for the library as it is the modern standard and makes Storybook integration easier (no need to import Modules in stories if the component is standalone).
Let's revise the "Standalone vs Module" approach:
If I use Standalone components, I don't strictly need a module in the library, though public-api.ts still handles exports. Let's stick to generating a library module (ng generate library) because it sets up the package.json and build paths correctly, but inside I can make the component standalone. Or, to keep it simple for a general audience, I will use the standard Module approach provided by the CLI, but mention Standalone. Actually, standard library generation still creates a module. I'll stick to the module pattern for the library entry point to ensure compatibility with older apps, but keep the implementation simple.
Actually, let's just use the standard Angular CLI library setup. It creates a module. It's safe and robust.
Plan:
Setup: Workspace + Library.
Component: Button with SCSS.
Storybook: Install + Config.
Story: Create story.
Build: Final output.
Let's write the response.
Building a robust Angular Component Library involves setting up a dedicated workspace, configuring SCSS for styling, and integrating Storybook to develop and document components in isolation.
Here is a step-by-step guide to building a modern Angular library (using Angular 17+ standards).
Prerequisites
Ensure you have Node.js and the Angular CLI installed:
bash
npm install -g @angular/cli
Step 1: Create the Workspace
Since Angular 14+, the recommended way to create a library is to generate a workspace without an initial application.
Generate the workspace:
bash
ng new my-ui-library --no-create-application
cd my-ui-library
Generate the library:
This creates the actual library code in projects/my-ui-lib.
bash
ng generate library my-ui-lib --prefix=mui
(I used mui as a prefix to avoid conflicts with standard HTML tags).
Step 2: Configure Global SCSS Styles
To make your library customizable (e.g., for theming), you should set up a structure for SCSS variables and mixins.
Create a styles folder structure:
Inside your library folder (projects/my-ui-lib), create a styles directory.
Expose styles to the library:
It is best practice to allow users of your library to import your variables. You can do this by adding a style.scss file to src that forwards the partials, or simply by ensuring the files are included in the build.
For now, ensure your ng-package.json (in projects/my-ui-lib) allows SCSS assets if necessary, though usually, standard relative imports work fine.
Step 3: Create a Component (Button)
Let's create a button component that uses the SCSS variables we just defined.
Generate the component:
bash
ng generate component lib/button --project=my-ui-lib
Update the Component TS:
In projects/my-ui-lib/src/lib/button/button.component.ts:
typescript
import { Component, Input } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'mui-button',
standalone: true, // Modern Angular standard
imports: [CommonModule],
templateUrl: './button.component.html',
styleUrls: ['./button.component.scss'] // Using SCSS
})
export class ButtonComponent {
@Input() label = 'Click Me';
@Input() variant: 'primary' | 'secondary' = 'primary';
}
Update the Component HTML:
In projects/my-ui-lib/src/lib/button/button.component.html:
Export the Component:
Ensure the component is exported in projects/my-ui-lib/src/public-api.ts:
typescript
Step 4: Integrate Storybook
Storybook allows you to visualize your components and develop them without needing a main Angular application.
Initialize Storybook:
Run this command in the root of your workspace. It detects Angular automatically.
bash
npx storybook@latest init
Configure SCSS in Storybook:
By default, Storybook doesn't know about your specific SCSS structure (like the _variables file). You need to tell the Storybook Webpack/Vite config to handle these imports globally.
Open .storybook/main.ts:
typescript
import type { StorybookConfig } from "@storybook/angular";
const config: StorybookConfig = {
stories: ["../projects/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
addons: [],
framework: {
name: "@storybook/angular",
options: {},
},
docs: {},
// Add this Vite config to handle global SCSS imports
async viteFinal(config) {
return {
...config,
css: {
preprocessorOptions: {
scss: {
additionalData: `@import "./projects/my-ui-lib/src/styles/variables.scss";`
}
}
}
};
}
};
export default config;
Create a Story:
Delete the example stories generated in the stories folder. Create a new file inside your button folder (or a dedicated stories folder).