Setting up an Angular library
I'm planning to create an Angular library, starting with generating the workspace and configuring SCSS architecture. I want to include details about asynchronous commands and file edits in my mapping.
First, I'll create the Angular workspace with the appropriate CLI command. Then, I'll generate the library, which will create a directory for it. Next, I will configure SCSS in angular.json, though it seems there are limitations due to changes in Angular 13+. I'll need to verify the build configurations for libraries regarding SCSS options.Configuring SCSS and creating components
I can use stylePreprocessorOptions at the workspace level for schematics, but it's not part of the builder config. A simpler approach would be to use relative imports in SCSS or set up stylePreprocessorOptions in an example app. The library styles are compiled by ng-packagr, which probably supports the path resolution.
To avoid confusion, relative imports make it straightforward. I can propose creating a variables SCSS file under projects/ui/src/lib/styles, and in the button component file, I would import it using a relative path.
Next, I need to create the ButtonComponent with the appropriate command, ensuring that SCSS is set. Finally, I'll remember to export the component in public-api.ts and build the library with ng build ui.Setting up Storybook
To set up Storybook, I’ll start from the root by running npx storybook@latest init --type angular, or I could use --builder webpack5, though Storybook 8 defaults don't require that. Since I only have a library, it might still work, but I might run into errors if it expects a default project.
To avoid this, I can create a small demo app for testing purposes with ng g application demo --style=scss --routing=false. After that, I can run the Storybook setup command in the workspace, linking it to the demo project.Setting up Storybook with the library
The user wants to utilize the library with Storybook, but since Storybook uses its own webpack/builder, it will only rely on Angular during runtime. I just need to import the components from the library into the stories.
First, I'll create a workspace with an initial app using ng new design-system --create-application=true --style=scss. This app can serve as a demo while I build the separate library. Then, I’ll generate the library with ng g library ui and initialize Storybook with npx storybook@latest init. In the main.ts Storybook config, I’ll ensure the stories are aligned with framework: '@storybook/angular' and add a new library story for the button component.Creating a Storybook component story
To create a Storybook component story, I’ll start with the following code structure for the ButtonComponent. I need to ensure Storybook's tsconfig.json includes the paths for the library. Since both the library and Storybook share a root tsconfig.base.json, the path alias @design-system/ui will be auto-set.
I also have to update .storybook/tsconfig.json to extend the base config for path mapping. For SCSS, since the button uses it, Storybook compiles automatically, but I should import any global SCSS tokens in preview.ts or add them in preview-head.html.
Finally, I’ll outline the architecture recommendations for a custom component library, ensuring to mention SCSS support in Storybook by installing the sass package. We’ll define a clear structure in the documentation for a smooth setup process. Below is a concrete, end‑to‑end workflow to build an Angular component library with SCSS and Storybook.
I’ll assume Angular 17+ and NPM, but the steps are similar for recent versions.
1. Create Angular workspace
npm install -g @angular/cli
# Create a workspace with one app (we’ll use it for Storybook integration)
ng new design-system --style=scss --routing=false
cd design-system
This creates:
- App:
src/app
- Global styles:
src/styles.scss
2. Generate the UI library
ng generate library ui --prefix=ui
This creates projects/ui:
projects/ui/src/lib/ui.module.ts
projects/ui/src/public-api.ts
The library will be built and consumed via the name set in projects/ui/ng-package.json and package.json (by default something like design-system-ui).
3. Set up SCSS structure for the library
Create a small SCSS “design system” inside the library:
mkdir -p projects/ui/src/lib/styles
Create these files:
projects/ui/src/lib/styles/_variables.scss
// Design tokens
$color-primary: #2563eb;
$color-primary-hover: #1d4ed8;
$color-danger: #dc2626;
$border-radius-sm: 4px;
$border-radius-md: 6px;
$font-family-base: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
projects/ui/src/lib/styles/_mixins.scss
@mixin focus-ring {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
Optional: a base stylesheet:
projects/ui/src/lib/styles/_base.scss
@use 'variables' as *;
:root {
font-family: $font-family-base;
}
You’ll import these SCSS partials from components with relative @use:
@use '../styles/variables' as *;
@use '../styles/mixins' as *;
No extra config needed if you stick to relative paths.
4. Create your first component (Button)
Generate a button component inside the library and export it:
ng generate component button \
--project=ui \
--export \
--inline-style=false \
--inline-template=false \
--style=scss
You’ll get:
projects/ui/src/lib/button/button.component.ts
projects/ui/src/lib/button/button.component.html
projects/ui/src/lib/button/button.component.scss
4.1. Button component implementation
button.component.ts
import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
type ButtonAppearance = 'primary' | 'secondary' | 'danger' | 'ghost';
@Component({
selector: 'ui-button',
templateUrl: './button.component.html',
styleUrls: ['./button.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ButtonComponent {
@Input() label = '';
@Input() appearance: ButtonAppearance = 'primary';
@Input() disabled = false;
@Input() type: 'button' | 'submit' | 'reset' = 'button';
@Output() clicked = new EventEmitter<MouseEvent>();
onClick(event: MouseEvent) {
if (this.disabled) {
event.preventDefault();
return;
}
this.clicked.emit(event);
}
}
button.component.html
<button
class="ui-button ui-button--{{ appearance }}"
[attr.type]="type"
[disabled]="disabled"
(click)="onClick($event)"
>
<ng-content></ng-content>
<span *ngIf="!$any(label) === ''">{{ label }}</span>
</button>
(Alternatively, choose either label or <ng-content>; I’m showing both for flexibility.)
button.component.scss
@use '../styles/variables' as *;
@use '../styles/mixins' as *;
.ui-button {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.5rem 1rem;
border-radius: $border-radius-md;
border: 1px solid transparent;
font: inherit;
cursor: pointer;
transition: background-color 120ms ease-out, border-color 120ms ease-out, color 120ms ease-out;
background-color: #f9fafb;
color: #111827;
&:focus-visible { @include focus-ring; }
&:disabled {
opacity: 0.6;
cursor: not-allowed;
}
&--primary {
background-color: $color-primary;
color: white;
&:hover:not(:disabled) {
background-color: $color-primary-hover;
}
}
&--secondary {
background-color: white;
border-color: #d1d5db;
color: #111827;
&:hover:not(:disabled) {
background-color: #f3f4f6;
}
}
&--danger {
background-color: $color-danger;
color: white;
&:hover:not(:disabled) {
background-color: darken($color-danger, 5%);
}
}
&--ghost {
background-color: transparent;
border-color: transparent;
color: $color-primary;
&:hover:not(:disabled) {
background-color: rgba(37, 99, 235, 0.08);
}
}
}
4.2. Export from the library entrypoint
projects/ui/src/public-api.ts
export * from './lib/ui.module';
export * from './lib/button/button.component';
Build to confirm everything compiles:
ng build ui
5. Add Storybook to the workspace
Install and initialize Storybook in the Angular workspace:
# From the workspace root
npx storybook@latest init --type angular
This creates a .storybook/ folder and some example stories under src/stories.
Make sure sass is installed so Storybook can handle SCSS:
npm install -D sass
Check .storybook/main.ts (or main.cjs depending on version). It should look roughly like:
import type { StorybookConfig } from '@storybook/angular';
const config: StorybookConfig = {
stories: [
'../src/stories/**/*.stories.@(ts|tsx)',
'../projects/ui/src/lib/**/*.stories.@(ts|tsx)',
],
addons: [
'@storybook/addon-essentials',
'@storybook/addon-interactions',
],
framework: {
name: '@storybook/angular',
options: {},
},
};
export default config;
Also ensure .storybook/tsconfig.json extends the workspace tsconfig so it can resolve the library:
.storybook/tsconfig.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"]
},
"include": [
"../src/**/*.ts",
"../projects/**/*.ts",
"./**/*.ts"
]
}
6. Hook global styles into Storybook (optional)
If you use src/styles.scss for base styles / CSS variables and want those visible in Storybook, import them in Storybook’s preview file:
.storybook/preview.ts (Storybook 7/8)
import '../src/styles.scss';
import type { Preview } from '@storybook/angular';
const preview: Preview = {
parameters: {
controls: { expanded: true },
},
};
export default preview;
7. Create Storybook stories for the Button component
Create a story file next to the component:
projects/ui/src/lib/button/button.stories.ts
import type { Meta, StoryObj } from '@storybook/angular';
import { moduleMetadata } from '@storybook/angular';
import { ButtonComponent } from './button.component';
import { UiModule } from '../ui.module';
// Storybook metadata
const meta: Meta<ButtonComponent> = {
title: 'Components/Button',
component: ButtonComponent,
decorators: [
moduleMetadata({
imports: [UiModule], // or declare ButtonComponent directly if standalone
}),
],
tags: ['autodocs'],
argTypes: {
appearance: {
control: { type: 'select' },
options: ['primary', 'secondary', 'danger', 'ghost'],
},
disabled: { control: 'boolean' },
label: { control: 'text' },
},
};
export default meta;
type Story = StoryObj<ButtonComponent>;
export const Primary: Story = {
args: {
label: 'Primary Button',
appearance: 'primary',
},
};
export const Secondary: Story = {
args: {
label: 'Secondary Button',
appearance: 'secondary',
},
};
export const Danger: Story = {
args: {
label: 'Delete',
appearance: 'danger',
},
};
export const Disabled: Story = {
args: {
label: 'Disabled',
appearance: 'primary',
disabled: true,
},
};
Run Storybook:
npm run storybook
You should see Components/Button in the left sidebar with live controls.
8. Using the library in the Angular app
In your main app (src/app/app.module.ts):
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UiModule } from 'ui'; // or the actual package name, e.g. 'design-system-ui'
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, UiModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Use the component:
src/app/app.component.html
<ui-button appearance="primary" (clicked)="onClick()">Click me</ui-button>
9. Building and publishing the library (optional)
Build:
ng build ui
The compiled library will be output to dist/ui.
To publish to a registry like npm:
-
Ensure dist/ui/package.json has:
- a scoped name (e.g.
"name": "@your-org/ui")
- correct version
-
From workspace root:
cd dist/ui
npm publish --access public
(Configure your scope and registry as needed.)
If you tell me your exact Angular version and whether you prefer standalone components instead of NgModules, I can adapt the component and Storybook setup to that style.