Skip to content

Commit

Permalink
Merge pull request #3573 from klimentru1986/angular_centered
Browse files Browse the repository at this point in the history
addon-centered for Angular
  • Loading branch information
Hypnosphi authored May 13, 2018
2 parents 44bee5a + 55ac4d1 commit 05d8797
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ADDONS_SUPPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
|[a11y](addons/a11y) |+| | | | | |+| |
|[actions](addons/actions) |+|+|+|+|+|+|+|+|
|[backgrounds](addons/backgrounds) |+| | | | |+|+| |
|[centered](addons/centered) |+| |+| | |+|+| |
|[centered](addons/centered) |+| |+|+| |+|+| |
|[events](addons/events) |+| | | | | |+| |
|[graphql](addons/graphql) |+| | | | | | | |
|[info](addons/info) |+| | | | | | | |
Expand Down
44 changes: 44 additions & 0 deletions addons/centered/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,50 @@ storiesOf('MyComponent', module)
}));
```

example for Angular with component:

```ts
import { centered } from '@storybook/addon-centered/angular';
import { storiesOf } from '@storybook/angular';
import { AppComponent } from '../app/app.component';

storiesOf('Addon|Centered', module)
.addDecorator(centered)
.add('centered component', () => ({
component: AppComponent,
props: {},
}));

```

example for Angular with template:

```ts
import { centered } from '@storybook/addon-centered/angular';
import { moduleMetadata, storiesOf } from '@storybook/angular';
import { AppComponent } from '../app/app.component';

storiesOf('Addon|Centered', module)
.addDecorator(
moduleMetadata({
declarations: [Button],
})
)
.addDecorator(centered)
.add('centered template', () => ({
template: `<storybook-button-component
[text]="text" (onClick)="onClick($event)">
</storybook-button-component>`,
props: {
text: 'Hello Button',
onClick: event => {
console.log('some bindings work');
console.log(event);
},
},
}));
```

Also, you can also add this decorator globally

example for React:
Expand Down
5 changes: 5 additions & 0 deletions addons/centered/angular.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IStory } from '@storybook/angular';

declare module '@storybook/addon-centered/angular' {
export function centered(story: IStory): IStory;
}
3 changes: 3 additions & 0 deletions addons/centered/angular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fromCentered from './dist/angular';

export const centered = fromCentered;
58 changes: 58 additions & 0 deletions addons/centered/src/angular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import styles from './styles';

function getComponentSelector(component) {
// eslint-disable-next-line no-underscore-dangle
return component.__annotations__[0].selector;
}

function getTemplate(metadata) {
let tpl = '';
if (metadata.component) {
const selector = getComponentSelector(metadata.component);
tpl = `<${selector}></${selector}>`;
}

if (metadata.template) {
tpl = metadata.template;
}

return `
<div [ngStyle]="styles.style">
<div [ngStyle]="styles.innerStyle">
${tpl}
</div>
</div>`;
}

function getModuleMetadata(metadata) {
const { moduleMetadata, component } = metadata;

if (component && !moduleMetadata) {
return {
declarations: [metadata.component],
};
}

if (component && moduleMetadata) {
return {
...moduleMetadata,
declarations: [...moduleMetadata.declarations, metadata.component],
};
}

return moduleMetadata;
}

export default function(metadataFn) {
const metadata = metadataFn();

return {
...metadata,
template: getTemplate(metadata),
moduleMetadata: getModuleMetadata(metadata),
props: {
...metadata.props,
styles,
},
};
}
1 change: 0 additions & 1 deletion addons/centered/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import ReactCentered from './react';
import VueCentered from './vue';

const Centered = window.STORYBOOK_ENV === 'vue' ? VueCentered : ReactCentered;

export default Centered;
1 change: 1 addition & 0 deletions examples/angular-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@storybook/addon-links": "4.0.0-alpha.6",
"@storybook/addon-notes": "4.0.0-alpha.6",
"@storybook/addon-options": "4.0.0-alpha.6",
"@storybook/addon-centered": "4.0.0-alpha.6",
"@storybook/addon-storyshots": "4.0.0-alpha.6",
"@storybook/addon-storysource": "4.0.0-alpha.6",
"@storybook/addons": "4.0.0-alpha.6",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Addon|Centered centered component 1`] = `
<storybook-dynamic-app-root
cfr={[Function CodegenComponentFactoryResolver]}
data={[Function Object]}
target={[Function ViewContainerRef_]}
>
<ng-component>


<div
ng-reflect-ng-style="[object Object]"
style="position: fixed; top: 0px; left: 0px; bottom: 0px; right: 0px; display: flex; overflow: auto;"
>


<div
ng-reflect-ng-style="[object Object]"
style="margin: auto;"
>


<storybook-app-root>


<div
class="hide"
style="color: red; font-size: 30px; text-align: center;"
>

This should be hidden, if not - scss is not loaded as needed.

</div>


<div
style="text-align:center"
>


<h1>

Welcome to app!

</h1>


<img
src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOS4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHZpZXdCb3g9IjAgMCAyNTAgMjUwIiBzdHlsZT0iZW5hYmxlLWJhY2tncm91bmQ6bmV3IDAgMCAyNTAgMjUwOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+DQo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPg0KCS5zdDB7ZmlsbDojREQwMDMxO30NCgkuc3Qxe2ZpbGw6I0MzMDAyRjt9DQoJLnN0MntmaWxsOiNGRkZGRkY7fQ0KPC9zdHlsZT4NCjxnPg0KCTxwb2x5Z29uIGNsYXNzPSJzdDAiIHBvaW50cz0iMTI1LDMwIDEyNSwzMCAxMjUsMzAgMzEuOSw2My4yIDQ2LjEsMTg2LjMgMTI1LDIzMCAxMjUsMjMwIDEyNSwyMzAgMjAzLjksMTg2LjMgMjE4LjEsNjMuMiAJIi8+DQoJPHBvbHlnb24gY2xhc3M9InN0MSIgcG9pbnRzPSIxMjUsMzAgMTI1LDUyLjIgMTI1LDUyLjEgMTI1LDE1My40IDEyNSwxNTMuNCAxMjUsMjMwIDEyNSwyMzAgMjAzLjksMTg2LjMgMjE4LjEsNjMuMiAxMjUsMzAgCSIvPg0KCTxwYXRoIGNsYXNzPSJzdDIiIGQ9Ik0xMjUsNTIuMUw2Ni44LDE4Mi42aDBoMjEuN2gwbDExLjctMjkuMmg0OS40bDExLjcsMjkuMmgwaDIxLjdoMEwxMjUsNTIuMUwxMjUsNTIuMUwxMjUsNTIuMUwxMjUsNTIuMQ0KCQlMMTI1LDUyLjF6IE0xNDIsMTM1LjRIMTA4bDE3LTQwLjlMMTQyLDEzNS40eiIvPg0KPC9nPg0KPC9zdmc+DQo="
width="300"
/>


</div>


<h2>
Here are some links to help you start:
</h2>


<ul>


<li>


<h2>
<a
href="https://angular.io/tutorial"
target="_blank"
>
Tour of Heroes
</a>
</h2>


</li>


<li>


<h2>
<a
href="https://github.com/angular/angular-cli/wiki"
target="_blank"
>
CLI Documentation
</a>
</h2>


</li>


<li>


<h2>
<a
href="https://blog.angular.io//"
target="_blank"
>
Angular blog
</a>
</h2>


</li>


</ul>



</storybook-app-root>


</div>


</div>
</ng-component>
</storybook-dynamic-app-root>
`;

exports[`Storyshots Addon|Centered centered template 1`] = `
<storybook-dynamic-app-root
cfr={[Function CodegenComponentFactoryResolver]}
data={[Function Object]}
target={[Function ViewContainerRef_]}
>
<ng-component>


<div
ng-reflect-ng-style="[object Object]"
style="position: fixed; top: 0px; left: 0px; bottom: 0px; right: 0px; display: flex; overflow: auto;"
>


<div
ng-reflect-ng-style="[object Object]"
style="margin: auto;"
>


<storybook-button-component
_nghost-c5=""
ng-reflect-text="Hello Button"
>


<button
_ngcontent-c5=""
>
Hello Button
</button>


</storybook-button-component>


</div>


</div>
</ng-component>
</storybook-dynamic-app-root>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ exports[`Storyshots Addon|Links button with link to another story 1`] = `
target={[Function ViewContainerRef_]}
>
<storybook-button-component
_nghost-c5=""
_nghost-c6=""
>


<button
_ngcontent-c5=""
_ngcontent-c6=""
>
Go to Welcome Story
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ exports[`Storyshots Addon|Notes Note with HTML 1`] = `
target={[Function ViewContainerRef_]}
>
<storybook-button-component
_nghost-c7=""
_nghost-c8=""
>


<button
_ngcontent-c7=""
_ngcontent-c8=""
>
Notes with HTML
</button>
Expand All @@ -29,12 +29,12 @@ exports[`Storyshots Addon|Notes Simple note 1`] = `
target={[Function ViewContainerRef_]}
>
<storybook-button-component
_nghost-c6=""
_nghost-c7=""
>


<button
_ngcontent-c6=""
_ngcontent-c7=""
>
Notes on some Button
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ exports[`Storyshots Core|Parameters passed to story 1`] = `
target={[Function ViewContainerRef_]}
>
<storybook-button-component
_nghost-c8=""
_nghost-c9=""
>


<button
_ngcontent-c8=""
_ngcontent-c9=""
>
Parameters are {"globalParameter":"globalParameter","chapterParameter":"chapterParameter","storyParameter":"storyParameter"}
</button>
Expand Down
Loading

0 comments on commit 05d8797

Please sign in to comment.