-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular): add inline stories support in the docs addon
The configuration has to be set up in each angular project to avoid forcing dependencies to `@angular/elements` ans `@webcomponents/custom-elements`
- Loading branch information
Showing
15 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../dist/esm/frameworks/angular/prepareForInline'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import { IStory, StoryContext } from '@storybook/angular'; | ||
import { ElementRendererService } from '@storybook/angular/element-renderer'; | ||
import { StoryFn } from '@storybook/addons'; | ||
|
||
const customElementsVersions: Record<string, number> = {}; | ||
|
||
/** | ||
* Uses angular element to generate on-the-fly web components and reference it with `customElements` | ||
* then it is added into react | ||
*/ | ||
export const prepareForInline = (storyFn: StoryFn<IStory>, { id, parameters }: StoryContext) => { | ||
// Upgrade story version in order that the next defined component has a unique key | ||
customElementsVersions[id] = | ||
customElementsVersions[id] !== undefined ? customElementsVersions[id] + 1 : 0; | ||
|
||
const customElementsName = `${id}_${customElementsVersions[id]}`; | ||
|
||
const Story = class Story extends React.Component { | ||
wrapperRef: React.RefObject<unknown>; | ||
|
||
elementName: string; | ||
|
||
constructor(props: any) { | ||
super(props); | ||
this.wrapperRef = React.createRef(); | ||
} | ||
|
||
async componentDidMount() { | ||
// eslint-disable-next-line no-undef | ||
customElements.define( | ||
customElementsName, | ||
await new ElementRendererService().renderAngularElement({ | ||
storyFnAngular: storyFn(), | ||
parameters, | ||
}) | ||
); | ||
} | ||
|
||
render() { | ||
return React.createElement(customElementsName, { | ||
ref: this.wrapperRef, | ||
}); | ||
} | ||
}; | ||
return React.createElement(Story); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './dist/ts3.9/element-renderer.d'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./dist/ts3.9/element-renderer'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/angular/src/client/preview/angular-beta/ElementRendererService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Should be added first : | ||
// Custom Elements polyfill. Required for browsers that do not natively support Custom Elements. | ||
import '@webcomponents/custom-elements'; | ||
// Custom Elements ES5 shim. Required when using ES5 bundles on browsers that natively support | ||
// Custom Elements (either because the browser does not support ES2015 modules or because the app | ||
// is explicitly configured to generate ES5 only bundles). | ||
import '@webcomponents/custom-elements/src/native-shim'; | ||
|
||
import { Injector, NgModule, Type } from '@angular/core'; | ||
import { createCustomElement, NgElementConstructor } from '@angular/elements'; | ||
|
||
import { BehaviorSubject } from 'rxjs'; | ||
import { ICollection, StoryFnAngularReturnType } from '../types'; | ||
import { Parameters } from '../types-6-0'; | ||
import { getStorybookModuleMetadata } from './StorybookModule'; | ||
import { RendererService } from './RendererService'; | ||
|
||
/** | ||
* Bootstrap angular application to generate a web component with angular element | ||
*/ | ||
export class ElementRendererService { | ||
private platform = RendererService.getInstance().platform; | ||
|
||
/** | ||
* Returns a custom element generated by Angular elements | ||
*/ | ||
public async renderAngularElement({ | ||
storyFnAngular, | ||
parameters, | ||
}: { | ||
storyFnAngular: StoryFnAngularReturnType; | ||
parameters: Parameters; | ||
}): Promise<CustomElementConstructor> { | ||
const ngModule = getStorybookModuleMetadata( | ||
{ storyFnAngular, parameters }, | ||
new BehaviorSubject<ICollection>(storyFnAngular.props) | ||
); | ||
|
||
return this.platform | ||
.bootstrapModule(createElementsModule(ngModule)) | ||
.then((m) => m.instance.ngEl); | ||
} | ||
} | ||
|
||
const createElementsModule = (ngModule: NgModule): Type<{ ngEl: CustomElementConstructor }> => { | ||
@NgModule({ ...ngModule }) | ||
class ElementsModule { | ||
public ngEl: NgElementConstructor<unknown>; | ||
|
||
constructor(private injector: Injector) { | ||
this.ngEl = createCustomElement(ngModule.bootstrap[0] as Type<unknown>, { | ||
injector: this.injector, | ||
}); | ||
} | ||
|
||
ngDoBootstrap() {} | ||
} | ||
return ElementsModule; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ElementRendererService } from './client/preview/angular-beta/ElementRendererService'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters