Skip to content
This repository was archived by the owner on Jul 30, 2018. It is now read-only.

Commit c1e8803

Browse files
rorticusagubler
authored andcommitted
Updating README to include more information on custom elements (#321)
1 parent 8f2641c commit c1e8803

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

README.md

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -565,31 +565,73 @@ const widget = createI18nWidget({
565565
566566
#### Web Components
567567
568-
Custom Widgets can be turned into [Custom Elements](https://www.w3.org/TR/2016/WD-custom-elements-20161013/) with
568+
Widgets can be turned into [Custom Elements](https://www.w3.org/TR/2016/WD-custom-elements-20161013/) with
569569
minimal extra effort.
570570
571571
Just create a `CustomElementDescriptor` factory and use the build tooling to do the rest of the work,
572572
573573
```ts
574574
import { CustomElementDescriptor } from '@dojo/widget-core/customElements';
575+
import createMyWidget from './path/to/createMyWidget';
575576

576577
export default function createCustomElement(): CustomElementDescriptor {
577578
return {
578-
attributes: [
579-
{
580-
attributeName: 'label'
581-
}
582-
],
583-
events: [
584-
{
585-
propertyName: 'onChange',
586-
name: 'change'
587-
}
588-
]
579+
tagName: 'my-widget',
580+
widgetFactory: createMyWidget,
581+
attributes: [
582+
{
583+
attributeName: 'label'
584+
}
585+
],
586+
events: [
587+
{
588+
propertyName: 'onChange',
589+
name: 'change'
590+
}
591+
]
589592
};
590593
};
591594
```
592595
596+
By convention, this file should be named `createMyWidgetElement.ts`.
597+
598+
To build your custom element, use [dojo cli](https://github.com/dojo/cli),
599+
600+
```bash
601+
$ dojo build --element=/path/to/createMyWidget.ts
602+
```
603+
604+
This will generate the following files,
605+
606+
* `dist/my-widget/my-widget.html` - HTML import file that includes all widget dependencies. This is the only file you need to import into your HTML page to use your widget.
607+
* `dist/my-widget/my-widget.js` - A compiled version of your widget.
608+
* `dist/my-widget/my-widget.css` - The CSS for your widget
609+
* `dist/my-widget/widget-core.js` - A shared base widget library. Keeping this separate means that you can include multiple HTML imports and the browser will not re-request this shared file.
610+
611+
Using your widget would be a simple matter of importing the HTML import,
612+
613+
```html
614+
<!DOCTYPE html>
615+
<html>
616+
<head>
617+
<!-- this will include all JS and CSS used by your widget -->
618+
<link rel="import" href="/path/to/my-widget.html" />
619+
</head>
620+
<body>
621+
<!-- this will actually create your widget -->
622+
<my-widget></my-widget>
623+
</body>
624+
</html>
625+
```
626+
627+
##### Tag Name
628+
629+
Your widget will be registered with the browser using the provided tag name. The tag name **must** have a `-` in it.
630+
631+
##### Widget Factory
632+
633+
A factory that will return the widget that you want wrapped as a custom element.
634+
593635
##### Attributes
594636
595637
You can explicitly map widget properties to DOM node attributes with the `attributes` array.

0 commit comments

Comments
 (0)