Building a Custom Angular Component Library with SCSS & Storybook
Certainly! As Sherlock, I'll deduce the most efficient path to create a reusable, production-ready custom UI component library called sherlock-ui. We'll use:
- Angular 17+ (standalone components for modernity).
- SCSS for modular, themeable styles (BEM methodology).
- Storybook 7+ for isolated component development, docs, and visual testing.
This library will include sample components: Button, Card, and Input. It's publishable to npm.
Prerequisites
# Install globally if needed
npm install -g @angular/cli@17 @storybook/cli@7
# Verify versions
ng version
npx storybook@latest --version # Should be 7.x
node --version # v18+
Step 1: Create Angular Workspace
ng new sherlock-ui-workspace --routing=false --style=scss --ssr=false --package-manager=npm
cd sherlock-ui-workspace
Remove the default app (we'll use the library only):
ng generate application demo-app --routing=false --style=scss --skip-tests
rm -rf src/app # Clean demo-app src for now
Step 2: Generate the Library
ng generate library sherlock-ui --style=scss --skip-tests --standalone
This creates projects/sherlock-ui/ with:
src/lib/ for components.
- SCSS support enabled.
Update projects/sherlock-ui/package.json for better versioning:
{
"name": "sherlock-ui",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^17.0.0",
"@angular/core": "^17.0.0"
}
}
Step 3: Add Sample Components (with SCSS)
We'll create standalone components in projects/sherlock-ui/src/lib/.
3.1 Button Component
ng generate component button --project=sherlock-ui --standalone --style=scss
projects/sherlock-ui/src/lib/button/button.component.ts
import { Component, Input, Output, EventEmitter } from '@angular/core';
import type { CommonModule } from '@angular/common';
export type ButtonVariant = 'primary' | 'secondary' | 'danger';
export type ButtonSize = 'sm' | 'md' | 'lg';
@Component({
selector: 'lib-button',
standalone: true,
imports: [], // No CommonModule needed for simple button
template: `
<button
class="btn"
[class]="variant"
[attr.data-size]="size"
[disabled]="disabled"
(click)="handleClick()">
<ng-content></ng-content>
</button>
`,
styles: [] // Inline for encapsulation; SCSS in separate file
})
export class ButtonComponent {
@Input() variant: ButtonVariant = 'primary';
@Input() size: ButtonSize = 'md';
@Input() disabled = false;
@Output() buttonClick = new EventEmitter<void>();
handleClick() {
if (!this.disabled) this.buttonClick.emit();
}
}
projects/sherlock-ui/src/lib/button/button.component.scss
// BEM-style SCSS with variables for theming
$btn-primary: #3b82f6;
$btn-secondary: #6b7280;
$btn-danger: #ef4444;
$btn-radius: 0.5rem;
.btn {
border: none;
padding: 0;
font-family: inherit;
cursor: pointer;
transition: all 0.2s ease;
border-radius: $btn-radius;
font-weight: 500;
&[data-size="sm"] {
padding: 0.25rem 0.75rem;
font-size: 0.875rem;
}
&[data-size="md"] {
padding: 0.5rem 1rem;
font-size: 1rem;
}
&[data-size="lg"] {
padding: 0.75rem 1.5rem;
font-size: 1.125rem;
}
&.primary {
background: $btn-primary;
color: white;
&:hover:not(:disabled) { background: darken($btn-primary, 10%); }
}
&.secondary {
background: $btn-secondary;
color: white;
&:hover:not(:disabled) { background: darken($btn-secondary, 10%); }
}
&.danger {
background: $btn-danger;
color: white;
&:hover:not(:disabled) { background: darken($btn-danger, 10%); }
}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
}
3.2 Card Component
ng generate component card --project=sherlock-ui --standalone --style=scss
projects/sherlock-ui/src/lib/card/card.component.ts
import { Component, Input } from '@angular/core';
import type { CommonModule } from '@angular/common';
import { ButtonComponent } from '../button/button.component';
@Component({
selector: 'lib-card',
standalone: true,
imports: [CommonModule, ButtonComponent],
template: `
<div class="card">
<div class="card__header" *ngIf="$header">
<ng-content select="[slot='header']"></ng-content>
</div>
<div class="card__body">
<ng-content></ng-content>
</div>
<div class="card__footer" *ngIf="$footer">
<ng-content select="[slot='footer']"></ng-content>
</div>
</div>
`,
styles: []
})
export class CardComponent {
@Input({ alias: 'header', transform: (v: any) => !!v }) $header = false;
@Input({ alias: 'footer', transform: (v: any) => !!v }) $footer = false;
}
projects/sherlock-ui/src/lib/card/card.component.scss
.card {
background: white;
border-radius: 0.75rem;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
overflow: hidden;
&__header {
padding: 1.5rem;
background: #f9fafb;
border-bottom: 1px solid #e5e7eb;
}
&__body {
padding: 1.5rem;
}
&__footer {
padding: 1.5rem;
background: #f9fafb;
border-top: 1px solid #e5e7eb;
}
}
3.3 Input Component (Quick)
ng generate component input --project=sherlock-ui --standalone --style=scss
projects/sherlock-ui/src/lib/input/input.component.ts
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'lib-input',
standalone: true,
template: `
<input
class="input"
[type]="type"
[placeholder]="placeholder"
[(ngModel)]="value"
(ngModelChange)="onChange($event)"
(blur)="onBlur()">
`,
styles: []
})
export class InputComponent {
@Input() type = 'text';
@Input() placeholder = '';
@Input() value = '';
@Output() valueChange = new EventEmitter<string>();
@Output() blur = new EventEmitter<void>();
onChange(v: string) { this.valueChange.emit(v); }
onBlur() { this.blur.emit(); }
}
projects/sherlock-ui/src/lib/input/input.component.scss
.input {
width: 100%;
padding: 0.75rem 1rem;
border: 1px solid #d1d5db;
border-radius: 0.5rem;
font-size: 1rem;
transition: border-color 0.2s;
&:focus {
outline: none;
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}
}
3.4 Public API Barrel
projects/sherlock-ui/src/public-api.ts
export * from './lib/button/button.component';
export * from './lib/card/card.component';
export * from './lib/input/input.component';
3.5 Global Styles (Optional Theme)
projects/sherlock-ui/src/styles.scss
// Exportable theme variables
:root {
--sherlock-primary: #3b82f6;
}
Step 4: Build the Library
ng build sherlock-ui
This generates dist/sherlock-ui/.
Step 5: Install & Configure Storybook
Storybook in the workspace root for library demos.
npx storybook@latest init --type angular --yes
- Choose demo-app when prompted (or root).
- Install Chromatic if desired (skip for now).
Update .storybook/main.ts for library:
import type { StorybookConfig } from '@storybook/angular';
const config: StorybookConfig = {
stories: ['../projects/sherlock-ui/src/**/*.stories.@(js|jsx|ts|tsx|mdx)'],
addons: [
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'@storybook/addon-links',
'@storybook/angular',
],
framework: {
name: '@storybook/angular',
options: {}
},
docs: {
autodocs: true,
},
refs: {
'sherlock-ui': {
title: 'Sherlock UI',
url: './dist/sherlock-ui' // Built library
}
}
};
export default config;
Step 6: Write Stories
Create stories in the library.
projects/sherlock-ui/src/button.button.stories.ts
import type { Meta, StoryObj } from '@storybook/angular';
import { ButtonComponent, type ButtonVariant, type ButtonSize } from './button.component';
const meta: Meta<ButtonComponent> = {
title: 'Components/Button',
component: ButtonComponent,
tags: ['autodocs'],
argTypes: {
variant: {
options: ['primary', 'secondary', 'danger'],
control: 'radio'
},
size: {
options: ['sm', 'md', 'lg'],
control: 'radio'
},
disabled: { control: 'boolean' }
}
};
export default meta;
type Story = StoryObj<ButtonComponent>;
export const Primary: Story = {
args: {
variant: 'primary',
size: 'md'
}
};
export const DangerLgDisabled: Story = {
args: {
variant: 'danger',
size: 'lg',
disabled: true
}
};
Repeat for Card (projects/sherlock-ui/src/card.card.stories.ts) and Input.
Card Story Example:
// Similar structure
export const Default: Story = {
args: {
$header: true,
$footer: true
},
render: (args) => ({
props: args,
template: `
<lib-card [header]="$header" [footer]="$footer">
<ng-container ngProjectAs="header">Header Slot</ng-container>
Card Body Content
<lib-button slot="footer" variant="primary">Action</lib-button>
</lib-card>
`
})
};
Step 7: Run & Develop
# Terminal 1: Watch library builds
ng build sherlock-ui --watch
# Terminal 2: Run Storybook
npm run storybook # http://localhost:6006
# Terminal 3: Run demo app (import library)
ng generate application demo-app --project=demo-app # If needed
# In demo-app/src/app/app.component.ts:
import { ButtonComponent } from 'sherlock-ui';
Step 8: Build, Test & Publish
- Build lib:
ng build sherlock-ui --configuration production
- Storybook static:
npm run build-storybook
- Test: Add
@storybook/addon-a11y, @chromatic-com/storybook.
- Publish:
cd dist/sherlock-ui && npm publish
Customization Tips
- Theming: Use CSS custom properties in SCSS, override in consumers.
- Icons: Integrate
@angular/material/icons or Lucide.
- Advanced: Add Tailwind via
ng-add @ngneat/tailwind, but SCSS keeps it lightweight.
- Nx Workspace: For scaling, migrate to Nx (
npx create-nx-workspace).
Your library is now live in Storybook! Inspect, tweak, and iterate. If you need expansions (e.g., Modal, Table), provide details. 🕵️♂️