-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Angular] How to render a component that has <ng-content> in the template #2817
Comments
Can you please try v3.4.0-alpha.5, it contains a "template" prop that you can use instead of the "component" if your ButtonComponent selector is named storiesOf('Button', module)
.add('Simple', () => ({
moduleMetadata: {
declarations: [ButtonComponent],
},
template: `<button-component> Put your content here </button-component>`,
})) |
@igor-dv Just the ticket. Thanks! |
@igor-dv How do you add v3.4.0-alpha.5 to try it? Can you explain it please? |
@omt66 , just install all the |
@igor-dv sorry I tried that but I am getting some errors. |
What are the errors ? |
Using the I got some errors because I had a custom |
Is this (should this be) in the docs? |
That couldn't be the solution. Why I should write the HTML manually again? It already in the component. We need a additional |
This is really cumbersome. Now we have to declare the component three(!) redundant times in a MDX story. Once in the Meta, once in the Props, and once in the moduleMetadata.... And then add the html with the selector too. There has to be a better way... |
is this the "official way" to put some html inside a component? because it's not really compatible with args therefore not with the whole controls addon, right? |
You can use see exemples here : Line 7 in d7f0e0f
pseudo code for this exemple : export default {
title: 'Button',
component: ButtonComponent,
decorators: [
componentWrapperDecorator(ButtonComponent, (args) => ({ iconSrc: args.iconSrc })),
],
args: { iconSrc: '/icon.png', buttonValue: 'some value' },
} as Meta;
export const WithTemplate = (args) => ({
template: `Button text {{ buttonValue }}`,
props: {
...args
},
}); |
while the approach of @ThibaudAV works it feels like a hack and there are some issues
There needs to be an actual solution, please! I'd love to use all these cool new features like the Docs Tab and Controls Tab but it feels like its usage with Angular was an afterthought. |
@SarcevicAntonio You can have args per story. Just remove them from the default export and add them to the story export default {
title: 'Button',
component: ButtonComponent,
decorators: [
componentWrapperDecorator(ButtonComponent)
} as Meta;
export const WithTemplate = (args) => ({
template: `Button text {{ buttonValue }}`,
props: {
...args
},
});
WithTemplate.args = {
buttonValue: 'Click me'
} You can also move the componentWrapperDecorator out the default export to the story as well. WithTemplate.decorators = [
componentWrapperDecorator(ButtonComponent)
]; I am seeing my args in "Show code". Now if I could just see my ng-content there as well then this solution would be great |
@dexster The solution is not working for
Now using your code example (without any modification), it does not render the text If I remove the |
I am using typescript for stories and had to adjust @ThibaudAV and @dexster solution otherwise the component would not reflect the changes to angular component //component file, not full code
@Component({
selector: 'a[myLink]',
template: `<ng-content></ng-content>`,
styleUrls: ['./my-link.component.scss'],
})
export class MyLinkComponent {
@Input() set myLink(variant: LinkVariant | '') {
if (variant !== '') {
this.variant = variant;
}
}
private variant: LinkVariant = 'primary';
@Input() size: LinkTextSize = '100';
@Input() bold = false;
@HostBinding('class')
private get classes() {
return [
`text${this.bold ? '-bold' : ''}${'-' + this.size}`,
`link-class-${this.variant}`,
].join(' ');
}
} //story file, not full code
export default {
title: 'ng-Components/Link',
component: MyLinkComponent,
decorators: [
moduleMetadata({
imports: [MyLinkModule], //component depends on some other components, so it has it's own module
}),
componentWrapperDecorator(MyLinkComponent, ({ args }) => { //object destructuring was needed here '{args}'
return args;
}),
],
} as Meta;
const Template: Story<MyLinkComponent | { ngContent: unknown }> = (args) => ({
template: `Hello {{ ngContent }}`, // whole content of this string with replaced ngContent will be correctly inserted, e.g. 'Hello World!'
props: {
...args,
},
});
export const Default = Template.bind({});
Default.args = {
bold: true,
myLink: 'secondary',
ngContent: 'World!',
}; Usage example of the component: <a href="google.com" myLink="secondary" [bold]="true" > Hello World! </a> This might help to some people, although there is a limitation to this approach - it is not possible to set attributes native to the So for a component that is using an attribute selector you cannot modify its native attribute in stroybook using this approach, for that you must use template @igor-dv . I wonder whether there is a nice solution that supports |
@dexster Did you find any solution to make it work with ng-content and select ? I'm facing the same issue as you. Thanks ! |
Any news? |
Oh my.. by using the template solution all the control inputs don't do ANYTHING. At least showing them works as @dexster suggested... but these args/controls are useless since the inline template html code is now the "control" but outsiders that can't programm won't be able to quickly change the input properties for a component... which makes using storybook as a whole completely unnecessary There needs to be a better solution, please! I need to know if there is at least some hope or plans to this problem or if I have to shut down storybook in my company immediately before we waste too much time on it... very sad Edit: Ok, I did miss something... it can work if you write the template code like this |
Thanks @BigPackie! Your solution is the cleanest out there. Used like this:
|
For Decorator code
Usage
You don't need a render anymore :-) |
Issue details
I'm pretty new to Angular so this is probably a noob question. I have a button component that uses the tags to project content passed into it when it is used:
Component template:
but I can't figure out how to pass some content in via my story book story:
Are there any docs describing the properties that can be used in the object that you return to describe a component? Have tried 'content', 'text' etc but to no avail.
The text was updated successfully, but these errors were encountered: