Build a custom component library for Angular using SCSS and Storybook.
- User
Build a custom component library for Angular using SCSS and Storybook.
Assistant
Framing the Component Library
I'm now zeroing in on the core objective: crafting a component library. My focus is on the user's intent to build something truly reusable and configurable. It's becoming clearer that SCSS and Storybook are essential tools to achieve this goal. I'm starting to visualize the structure that best supports this vision.
Planning the Foundation
I've decided on Angular (TypeScript), SCSS, and Storybook as the core technologies. The foundation begins with an Angular workspace to keep the library and test application separate. Now, I'm generating the library project within the workspace and setting up Storybook for it. My next step is to create a Button component to demonstrate Input/Output and later, address SCSS architecture.
Visualizing Component State
I'm presently building the
distfolder, which is nearly complete, and considering how the library will be consumed by other applications. I'm focusing on providing practical, digestible examples within Storybook to show the component visually.Implementing the Button Component
I'm now diving into the actual implementation of the Button component. I'm defining inputs for label and variant, and an output for click events, with the HTML containing a simple button structure that renders the label. SCSS is being defined to include BEM-like classes. I've begun integrating this with the SCSS architecture. I'm focusing on exporting styles or variables from the library.
Reviewing the Implementation
I'm now running the
npm run storybookcommand to ensure the Button component renders as expected. I'm also building the library for distribution withng build ui-kit. I've revisited the styling encapsulation in Angular libraries and how we can use it to support global themes. I'm making sure to use code blocks consistently and bolding key terms. I'm verifying the versions assumed (Angular 17+ and Storybook 7/8). Finally, I'm making the Button component a standalone component, which is the current best practice.Refining the Global Styles
I've realized that Storybook can be finicky if the Angular project isn't perfectly set up. So, I need to remember to advise the user to run
npm installif things seem off. Also, I'm thinking about how to handle global styles for the library. I'll need to add a section specifically on where to put resets and global variables.This is a complete guide to building a scalable Angular Component Library using SCSS for styling and Storybook for documentation and development.
We will create a workspace structure that allows you to build the library in isolation and visualize components without needing a full host application.
Prerequisites
- Node.js (LTS)
- Angular CLI installed (
npm install -g @angular/cli)
Step 1: Create the Workspace
We will create an empty workspace. This is a best practice for libraries, separating the "library" code from potential "documentation" or "test" apps.
# 1. Create a workspace without a default app ng new my-ui-lib --no-create-application # 2. Navigate into the folder cd my-ui-libStep 2: Generate the Library Project
Now we generate the actual library functionality within the workspace.
ng generate library ui-kitThis creates a
projects/ui-kitfolder.Step 3: Initialize Storybook
We will initialize Storybook specifically for the
ui-kitproject.npx storybook@latest initThe CLI will ask you to detect the project type. It should auto-detect Angular. If asked which project to add Storybook to, select ui-kit.
Step 4: Build a Reusable Component (Button)
Let's build a classic Button component to demonstrate inputs, outputs, and SCSS architecture.
ng generate component components/button --project=ui-kit --inline-template=false --inline-style=falseA. The Logic (
button.component.ts)We will use Standalone Components (standard in modern Angular).
// projects/ui-kit/src/lib/components/button/button.component.ts import { Component, Input, Output, EventEmitter } from '@angular/core'; import { CommonModule } from '@angular/common'; @Component({ selector: 'lib-button', standalone: true, imports: [CommonModule], templateUrl: './button.component.html', styleUrls: ['./button.component.scss'] }) export class ButtonComponent { /** * Button contents */ @Input() label = 'Button'; /** * Primary or Secondary UI style */ @Input() primary = true; /** * Optional click handler */ @Output() onClick = new EventEmitter<Event>(); public get classes(): string[] { const mode = this.primary ? 'storybook-button--primary' : 'storybook-button--secondary'; return ['storybook-button', mode]; } }B. The Template (
button.component.html)<button type="button" [ngClass]="classes" (click)="onClick.emit($event)"> {{ label }} </button>C. The Styling (
button.component.scss)Here we define the styles. In a real library, you might import variables from a core file.
// projects/ui-kit/src/lib/components/button/button.component.scss // Define some variables (In a real app, move these to a _variables.scss file) $primary: #1ea7fd; $secondary: #333333; $text-light: #ffffff; .storybook-button { font-family: 'Nunito Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 700; border: 0; border-radius: 3em; cursor: pointer; display: inline-block; line-height: 1; padding: 10px 20px; font-size: 14px; &--primary { color: $text-light; background-color: $primary; } &--secondary { color: $secondary; background-color: transparent; box-shadow: rgba(0, 0, 0, 0.15) 0px 0px 0px 1px inset; } }Step 5: Export via the Public API
Crucial Step: If you don't export the component here, consumers of your library cannot import it.
Open
projects/ui-kit/src/public-api.tsand update:/* * Public API Surface of ui-kit */ export * from './lib/components/button/button.component'; // Export services or modules here if you have themStep 6: Create the Story
Storybook needs a
.stories.tsfile to know how to render your component.Create
projects/ui-kit/src/lib/components/button/button.stories.ts:import { Meta, StoryObj } from '@storybook/angular'; import { fn } from '@storybook/test'; import { ButtonComponent } from './button.component'; const meta: Meta<ButtonComponent> = { title: 'Example/Button', component: ButtonComponent, tags: ['autodocs'], // Generates auto-documentation page argTypes: { // Controls allows you to edit inputs dynamically in the UI label: { control: 'text' }, primary: { control: 'boolean' }, }, args: { onClick: fn() // Mocks the event emitter for the Actions panel }, }; export default meta; type Story = StoryObj<ButtonComponent>; // Story 1: Primary State export const Primary: Story = { args: { primary: true, label: 'Primary Button', }, }; // Story 2: Secondary State export const Secondary: Story = { args: { primary: false, label: 'Secondary Button', }, };Step 7: Run Storybook
Now, let's see it in action.
npm run storybookThis will spin up a local server (usually port 6006). You will see your
Buttoncomponent in the sidebar. You can toggle theprimarycheckbox and change thelabeltext in real-time.
Architecture Tips for Scaling
1. Handling Global SCSS
Libraries often need shared CSS variables (colors, spacing, typography).
- Create a folder:
projects/ui-kit/src/lib/styles. - Create
_variables.scssinside it. - In your component SCSS files, import it:
@import '../../styles/variables';
2. Asset Handling
If your library uses images or fonts, you need to configure the
ng-package.json(located inprojects/ui-kit/ng-package.json) to include assets in the build output.{ "$schema": "../../node_modules/ng-packagr/ng-package.schema.json", "dest": "../../dist/ui-kit", "lib": { "entryFile": "src/public-api.ts" }, "assets": [ "./src/lib/assets" ] }3. Building for Distribution
When you are ready to publish your library to NPM or use it in another app:
ng build ui-kitThis creates a
dist/ui-kitfolder. This is the folder you publish to NPM command (npm publish dist/ui-kit).Summary of Project Structure
Your workspace should look like this:
my-ui-lib/ ├── .storybook/ # Global Storybook config ├── projects/ │ └── ui-kit/ # Your Library │ ├── .storybook/ # Project-level Storybook config │ ├── src/ │ │ ├── lib/ │ │ │ ├── components/ │ │ │ │ └── button/ │ │ │ │ ├── button.component.ts │ │ │ │ ├── button.component.scss │ │ │ │ ├── button.component.html │ │ │ │ └── button.stories.ts │ │ │ └── styles/ # Shared SCSS │ │ └── public-api.ts # Exports │ └── ng-package.json ├── package.json └── angular.json
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.