forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elements): add support for creating custom elements (angular#22413)
PR Close angular#22413
- Loading branch information
1 parent
cedc04c
commit 22b96b9
Showing
34 changed files
with
1,823 additions
and
17 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
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,67 @@ | ||
# Elements | ||
|
||
## Release Status | ||
|
||
**Angular Labs Project** - experimental and unstable. **Breaking Changes Possible** | ||
|
||
Targeted to land in the [6.x release cycle](https://github.com/angular/angular/blob/master/docs/RELEASE_SCHEDULE.md) of Angular - subject to change | ||
|
||
## Overview | ||
|
||
Elements provides an API that allows developers to register Angular Components as Custom Elements | ||
("Web Components"), and bridges the built-in DOM API to Angular's component interface and change | ||
detection APIs. | ||
|
||
```ts | ||
//hello-world.ts | ||
import { Component, Input, NgModule } from '@angular/core'; | ||
import { createNgElementConstructor, getConfigFromComponentFactory } from '@angular/elements'; | ||
|
||
@Component({ | ||
selector: 'hello-world', | ||
template: `<h1>Hello {{name}}</h1>` | ||
}) | ||
export class HelloWorld { | ||
@Input() name: string = 'World!'; | ||
} | ||
|
||
@NgModule({ | ||
declarations: [ HelloWorld ], | ||
entryComponents: [ HelloWorld ] | ||
}) | ||
export class HelloWorldModule {} | ||
``` | ||
|
||
```ts | ||
//app.component.ts | ||
import { Component, NgModuleRef } from '@angular/core'; | ||
import { createNgElementConstructor } from '@angular/elements'; | ||
|
||
import { HelloWorld } from './hello-world.ngfactory'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css'] | ||
}) | ||
export class AppComponent { | ||
constructor(ngModuleRef: NgModuleRef) { | ||
const ngElementConfig = getConfigFromComponentFactory(HelloWorld, injector); | ||
const NgElementConstructor = createNgElementConstructor(ngElementConfig); | ||
customElements.register('hello-world', NgElementConstructor); | ||
} | ||
} | ||
|
||
``` | ||
Once registered, these components can be used just like built-in HTML elements, because they *are* | ||
HTML Elements! | ||
|
||
They can be used in any HTML page: | ||
|
||
```html | ||
<hello-world name="Angular"></hello-world> | ||
<hello-world name="Typescript"></hello-world> | ||
``` | ||
|
||
Custom Elements are "self-bootstrapping" - they are automatically started when they are added to the | ||
DOM, and automatically destroyed when removed from the DOM. |
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
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,19 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("//tools:defaults.bzl", "ng_module") | ||
|
||
ng_module( | ||
name = "elements", | ||
srcs = glob( | ||
[ | ||
"*.ts", | ||
"src/**/*.ts", | ||
], | ||
), | ||
module_name = "@angular/elements", | ||
deps = [ | ||
"//packages/core", | ||
"//packages/platform-browser", | ||
"@rxjs", | ||
], | ||
) |
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,14 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
// This file is not used to build this module. It is only used during editing | ||
// by the TypeScript language service and during build for verification. `ngc` | ||
// replaces this file with production index.ts when it rewrites private symbol | ||
// names. | ||
|
||
export * from './public_api'; |
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,22 @@ | ||
{ | ||
"name": "@angular/elements", | ||
"version": "0.0.0-PLACEHOLDER", | ||
"description": "Angular - library for using Angular Components as Custom Elements", | ||
"main": "./bundles/elements.umd.js", | ||
"module": "./esm5/elements.js", | ||
"es2015": "./esm2015/elements.js", | ||
"typings": "./elements.d.ts", | ||
"author": "angular", | ||
"license": "MIT", | ||
"dependencies": { | ||
"tslib": "^1.7.1" | ||
}, | ||
"peerDependencies": { | ||
"@angular/core": "0.0.0-PLACEHOLDER", | ||
"@angular/platform-browser": "0.0.0-PLACEHOLDER" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/angular/angular.git" | ||
} | ||
} |
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,19 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/** | ||
* @module | ||
* @description | ||
* Entry point for all public APIs of the `elements` package. | ||
*/ | ||
export {ComponentFactoryNgElementStrategy, ComponentFactoryNgElementStrategyFactory, getConfigFromComponentFactory} from './src/component-factory-strategy'; | ||
export {NgElementStrategy, NgElementStrategyEvent, NgElementStrategyFactory} from './src/element-strategy'; | ||
export {NgElement, NgElementConfig, NgElementConstructor, createNgElementConstructor} from './src/ng-element-constructor'; | ||
export {VERSION} from './src/version'; | ||
|
||
// This file only reexports content of the `src` folder. Keep it that way. |
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,31 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
const resolve = require('rollup-plugin-node-resolve'); | ||
const sourcemaps = require('rollup-plugin-sourcemaps'); | ||
|
||
const globals = { | ||
'@angular/core': 'ng.core', | ||
'@angular/platform-browser': 'ng.platformBrowser', | ||
'rxjs/Subscription': 'Rx', | ||
'rxjs/Observable': 'Rx', | ||
'rxjs/observable/merge': 'Rx.Observable', | ||
'rxjs/operator/map': 'Rx.Observable.prototype' | ||
}; | ||
|
||
module.exports = { | ||
entry: '../../dist/packages-dist/elements/esm5/elements.js', | ||
dest: '../../dist/packages-dist/elements/bundles/elements.umd.js', | ||
format: 'umd', | ||
exports: 'named', | ||
amd: {id: '@angular/elements'}, | ||
moduleName: 'ng.elements', | ||
plugins: [resolve(), sourcemaps()], | ||
external: Object.keys(globals), | ||
globals: globals | ||
}; |
Oops, something went wrong.