Story for an Angular Directive
Tags: storybook, directives, howto, angular
Published: 2021-09-24 05:14:29

---
title: Story for an Angular Directive
published: true
date: 2021-09-23 20:11:13 UTC
tags: storybook,directives,howto,angular
canonical_url: https://medium.com/@redin.gaetan/story-for-an-angular-directive-656885b44c79
---

#### Storybook and Angular Directive

![](https://cdn-images-1.medium.com/max/564/1*UVhoCifbPsKuiwgKnTMGTA.png)

Hey, I have just started with Storybook for angular, maybe you too. One week ago, I was just trying to write a story for a ButtonDirective and it was not so easy… So I tried and tried and now it’s okay! This article is more for Storybook/Angular beginners, just to save your time.

Context: Angular 12, StoryBook 6.3.8

I presume you already have installed storybook, if you don’t just go [here](https://storybook.js.org/docs/angular/get-started/install).

Here’s my directive:

```
// button.directive.ts
@Directive({
  selector: 'button[adrButton]',
  host: {
    _class_: 'adr-button',
    '[class.adr-small]': 'size === "small"',
    '[class.adr-medium]': 'size === "medium"',
    '[class.adr-large]': 'size === "large"',
    '[class.adr-outlined]': 'outlined',
    '[class.adr-primary]': 'color === "primary"',
    '[class.adr-accent]': 'color === "accent"',
    '[class.adr-warn]': 'color === "warn"'
  }
})
_export class_ ButtonDirective {
  @Input()
  _public_ size: 'small' | 'medium' | 'large' = 'medium';

  @Input()
  _public_ outlined: _boolean_ = _false_;

  @Input()
  _public_ color?: 'primary' | 'accent' | 'warn';
}
```

Basic right?

I didn’t find in the Storybook documentation how to write a story for an Angular directive. Maybe it’s beause it was first created for React, I don’t know. But the tip is to create a DummyComponent. Let me show you:

```
_import_ { CommonModule } _from_ '@angular/common';
_import_ { Component, Input } _from_ '@angular/core';
_import_ { _Meta_, _Story_ } _from_ '@storybook/angular';
_import_ { ButtonDirective } _from_ 'src/components/atoms/button/button.directive';

@Component({
  selector: 'adr-dummy',
  template: `
      <button adrButton [size]="size" [outlined]="outlined" [color]="color">Click</button>`
})
_class_ DummyComponent { // **(1)**
  @Input()
  _public_ size: 'small' | 'medium' | 'large' = 'medium';

  @Input()
  _public_ outlined: _boolean_ = _false_;

  @Input()
  _public_ color?: 'primary' | 'accent' | 'warn';
}

_export default_ {
  title: 'atoms/button',
  component: DummyComponent // **(2)**  **don't forget it**
  }
} _as Meta_;

_const_ Template: _Story_<DummyComponent> = (args) => ({
  props: args, 
  moduleMetadata: { // **(3)**  **don't forget it**
    declarations: [ButtonDirective], 
    imports: [CommonModule]
  }
});

_export const_ Default = Template.bind({});
Default.args = {};
```

And that’s all. What have we do here?

(1) We have create our DummyComponent and set the expected template. We put inside the same inputs like in the ButtonDirective.

(2) In the Meta we set our DummyComponent.

(3) In the Template definition, we have specified the moduleMetadata.

You can try it works perfectly.

Thanks for reading.

### Learn More

- [Install Jest for Angular](https://dev.to/gaetanrdn/jest-and-angular-install-fg3)
- [How to Undo a GIT Commit](https://medium.com/@redin.gaetan/how-to-undo-a-git-commit-7942cdf6c211)
- [TypeScript Function Overloads](https://medium.com/@redin.gaetan/typescript-for-everyone-function-overloads-a08b473926ce)
- [Angular for everyone: All about it](https://medium.com/@redin.gaetan/angular-for-every-one-table-of-contents-d24232940dd4)