Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Saulo Dias
Saulo Dias

Posted on

     

Working with ng-content on Storybook

Very often when creating a new story for an Angular component onStorybook you might need to insert content into components which have anng-content area inside them.

To do that you need to create atemplate for your story.

Here is a simple component, which has adiv with and anng-content area inside it. The component has two inputs,width andheight.

// paper.component.tsimport{Component,Input}from'@angular/core';@Component({selector:'cx-paper',template:`    <div [ngStyle]="{ width: width, height: height }">      <ng-content></ng-content>    </div>  `,styles:[`      .paper {        border: navy solid 2px;        padding: 10px;      }    `,],})exportclassPaperComponent{@Input()width:string;@Input()height:string;}
Enter fullscreen modeExit fullscreen mode

The story for this component

// paper.stories.tsimport{Story,Meta}from'@storybook/angular';import{PaperComponent}from'./paper.component';exportdefault{title:'Example/Paper',component:PaperComponent,}asMeta;constTemplate:Story<PaperComponent>=(args:PaperComponent)=>({props:args,template:`  <cx-paper [height]="height" [width]="width">  This is a template test.  </cx-paper>`,});exportconstSimpleExample=Template.bind({});SimpleExample.args={height:'50px',width:'300px',}asPartial<PaperComponent>;
Enter fullscreen modeExit fullscreen mode

Which should render like this:
image

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
sarcevicantonio profile image
Antonio Sarcevic
  • Joined

Now this works for writing stories but it doesn't work right with the ArgsTable and doesn't show any output actions for me at least :z

CollapseExpand
 
saulodias profile image
Saulo Dias
Software Developer graduated in Automation and Control Engineering trying way too hard to become a brogrammer.
  • Location
    Rio de Janeiro, Brazil
  • Education
    Automation and Control Engineering at CEFET/RJ
  • Work
    Solutions Developer
  • Joined
• Edited on• Edited

If by ArgsTable you mean the component inputs, it's working fine for me. I change the inputs and can see the component respond to those changes. I haven't been able to figure out how to make Actions work with a template like that yet.

CollapseExpand
 
sarcevicantonio profile image
Antonio Sarcevic
  • Joined

ArgTable works in the story view but doesn’t in the docs view / docs page.
If this actually works someday that would be very nice but right now storybook for angular is such a pain :z

CollapseExpand
 
caraie profile image
Carlos Aiello
  • Joined
• Edited on• Edited

Why the show code part is not showing the content?
This text is missing in the code: "This is a template test."

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer graduated in Automation and Control Engineering trying way too hard to become a brogrammer.
  • Location
    Rio de Janeiro, Brazil
  • Education
    Automation and Control Engineering at CEFET/RJ
  • Work
    Solutions Developer
  • Joined

More fromSaulo Dias

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp