Skip to content

Commit

Permalink
Merge pull request #22 from GavinJoyce/gj/support-capitalized-components
Browse files Browse the repository at this point in the history
support capitalized component names
  • Loading branch information
tomdale committed Oct 17, 2017
2 parents 0573f30 + c45dff9 commit 23f886a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -25,10 +25,13 @@ import initializeCustomElements from '@glimmer/web-component';
And then after `app.boot()`:

```ts
initializeCustomElements(app, /* array of component names */);
initializeCustomElements(app, {
'button-list': 'ButtonList',
'x-button': 'Button'
});
```

This will register custom elements for each of the component names you give to `initializeCustomElements` and will replace the custom element with your Glimmer component once the custom element connects. For example, if you provide the component name `'foo-bar'` you can now use the custom element `<foo-bar>` anywhere in the DOM and have your `foo-bar` component render in its place.
This will register custom elements for each of the items defined in the hash passed to `initializeCustomElements` and will replace the custom element with your Glimmer component once the custom element connects. For example, if you provide the hash `{ 'foo-bar': 'FooBar' }`, you can then use the custom element `<foo-bar>` anywhere in the DOM and have your `<FooBar>` Glimmer component render in its place.

## Browser Support

Expand Down
16 changes: 9 additions & 7 deletions src/initialize-custom-elements.ts
@@ -1,12 +1,14 @@
import Application from '@glimmer/application';

export default function initializeCustomElements(app: Application, customElementDefinitions: string[]): void {
customElementDefinitions.forEach((name) => {
initializeCustomElement(app, name);
});
export default function initializeCustomElements(app: Application, customElementDefinitions: { [key: string]: string; }): void {
for(let customElementName in customElementDefinitions) {
let glimmerComponentName = customElementDefinitions[customElementName];

initializeCustomElement(app, customElementName, glimmerComponentName);
}
}

function initializeCustomElement(app: Application, name: string): void {
function initializeCustomElement(app: Application, customElementName: string, glimmerComponentName: string): void {
function GlimmerElement() {
return Reflect.construct(HTMLElement, [], GlimmerElement);
}
Expand All @@ -20,7 +22,7 @@ function initializeCustomElement(app: Application, name: string): void {
parent.insertBefore(placeholder, this);
parent.removeChild(this);

app.renderComponent(name, parent, placeholder);
app.renderComponent(glimmerComponentName, parent, placeholder);

whenRendered(app, () => {
let customElement = this as Element;
Expand All @@ -33,7 +35,7 @@ function initializeCustomElement(app: Application, name: string): void {
}
});

window.customElements.define(name, GlimmerElement);
window.customElements.define(customElementName, GlimmerElement);
}

function assignAttributes(fromElement: Element, toElement: Element): void {
Expand Down
2 changes: 1 addition & 1 deletion test/initialize-custom-elements-test.ts
Expand Up @@ -10,7 +10,7 @@ let renderPromise: Promise<void>;
module('initializeCustomElements', {
before() {
let app = setupApp();
initializeCustomElements(app as Application, ['hello-world']);
initializeCustomElements(app as Application, { 'hello-world': 'HelloWorld' });
},

beforeEach() {
Expand Down

0 comments on commit 23f886a

Please sign in to comment.