Skip to content

Commit cfcab19

Browse files
committed
New: Added starting guide to web components
1 parent 26e0b14 commit cfcab19

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

docs/README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# API Console documentation
22

33
Select a topic
4-
- [API Console element documentation](api-console-element.md)
5-
- [API Console build tools](build-tools.md)
6-
- [API Console configuration options](configuring-api-console.md)
7-
- [Building API Console for GitHub pages on Travis](gh-pages.md)
8-
- [Handling CORS](cors.md)
9-
- [Passing RAML to the API Console](passing-raml-data.md)
10-
- [Rebuilding the api.json file in the CI process](rebuilding-api-json.md)
11-
- [Styling the API Console](theming.md)
4+
- [Starting guide to web components](web-components.md)
5+
- [API Console element documentation](api-console-element.md)
6+
- [API Console build tools](build-tools.md)
7+
- [API Console configuration options](configuring-api-console.md)
8+
- [Building API Console for GitHub pages on Travis](gh-pages.md)
9+
- [Handling CORS](cors.md)
10+
- [Passing RAML to the API Console](passing-raml-data.md)
11+
- [Rebuilding the api.json file in the CI process](rebuilding-api-json.md)
12+
- [Styling the API Console](theming.md)

docs/web-components.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Starting with web components
2+
3+
Web Components is relatively new specification for the web platform and therefore it is understandable that not all developers know how to use them.
4+
5+
Fortunately for the use of the API console you have to learn basics only but we are encouraging you to learn more about the spec(s) to maximize your efficiency while working with web components.
6+
7+
## Basic concepts
8+
9+
The web components is a combination of 4 specifications that browser vendors and developers community agreed on.
10+
11+
### Custom Elements
12+
13+
The specification allows to define new HTML elements. The HTML standard is limited to a set number of defined elements but new specification allows us to extend it to new elements with its own behaviors.
14+
15+
See the specification here: [Custom elements](https://w3c.github.io/webcomponents/spec/custom/)
16+
17+
### Shadow DOM
18+
19+
Allows us to encapsulate logic and view of the custom element. Nothing leaks out and nothing can interfere with your element until you allow this by exposing element's API or CSS variables / mixins to style the element. You can think of it as how current `<video>` element is constructed. You can see a video and controls to control the
20+
playback and volume. It expose its behavior (`play()`, `pause()` etc), properties (attributes) to control UI state (`controls`, `src`), events (`playing`, `loadstart`) and styling options. Element that declares shadow DOM can do the same thing and
21+
hide any properties that the hosting application shouldn't use.
22+
23+
See the specification here: [shadow dom](https://w3c.github.io/webcomponents/spec/shadow/)
24+
25+
### HTML imports
26+
27+
The spec defines how to import custom element to the web site or application. This specification is most controversial because some vendors (Microsoft, Mozilla) won't implement it in their browsers until they don't agree of how this specification relates to ES6 module imports. Therefore for now you have to use a polyfill library called
28+
[webcomponents.js](https://github.com/webcomponents/webcomponentsjs). This polyfill provides backward compatibility for most of specifications for web components.
29+
30+
Specification: [HTML imports](https://w3c.github.io/webcomponents/spec/imports/)
31+
32+
### HTML template
33+
34+
Allows to declare a HTML fragments that are not used during the page load but can be instantiated anytime later. This way you can include a lot of HTML structure without adding it to the DOM. This specification is heavily used in the Polymer library where you can create an element defining it's markup in the element definition but it is unused until the element is actually registered and attached to the DOM.
35+
36+
## Custom element lifecycle callbacks
37+
38+
Because your web component is not a part of the HTML tags spec it is undefined for the browser until you actually register the element. It doesn't mean that you can't use the custom element in the markup before its registration. On the contrary. When you use an element that is not yet registered it is recognized as the [unknown element](https://developer.mozilla.org/en-US/docs/Web/API/HTMLUnknownElement). Later, when the custom element is (possibly) imported and registered the instance of the unknown element is upgraded with the actual definition of the element.
39+
40+
**Custom elements can be used before their definition is registered.**
41+
42+
When the element is already registered and it is instantiated and inserted into the DOM few functions are called as a lifecycle methods.
43+
44+
**Constructor** is called when an instance of the element is created or upgraded. It is useful for initializing state, settings up event listeners, or creating shadow dom.
45+
46+
**connectedCallback** is called every time the element is inserted into the DOM. Remind that an element can be inserted more than once to the DOM, especially when it is used in dynamic applications.
47+
48+
**disconnectedCallback** is an inverse of `connectedCallback`. It is called when the element is removed from the DOM.
49+
50+
**attributeChangedCallback** is called when an attribute was added, removed, updated, or replaced. It is a method to use to expose an API of the element in a HTML way - by setting attributes. Like in the `<video>` example, the `controls` getter and setter can control whether the player controls are visible or not. This would be handled by this method call.
51+
52+
**adoptedCallback** is called when the custom element has been moved into a new document.
53+
54+
## So when an element is ready to be used?
55+
56+
If you include definition of your element into the source of the web page and register it before application code runs then you can use the element's API right away. When using API console build tools one of the options do exactly that. At the time when browser parser runs JavaScript code of the application (when it suppose to parse RAML or set JavaScript object on the console) all elements are already registered and full API is available.
57+
58+
However, even when using our build tools, you can include console elements definition from other file to defer the source code download. In this case you should call element's API differently.
59+
60+
Properties can be set whether the element is `HTMLUnknownElement` or defined element. Once the element is upgraded created the `attributeChangedCallback` is called for each set attribute.
61+
62+
Calling element's API methods exposed to external environment (outside shadow DOM) the methods will be `undefined` for `HTMLUnknownElement`. Therefore the application has to wait until the element is upgraded.
63+
64+
In the API console that still works on so called `v0` specification of the web components the application would wait until `WebComponentsReady` event is fired (it is provided by the Polymer library if not supported natively). After the event is fired all element should be registered and upgraded (if already in the DOM).
65+
66+
## Creating a web component
67+
68+
There are already a lot of nice tutorials about creating web components so it's redundant. Personally I can suggest Eric Bidelman's [Custom Elements v1: Reusable Web Components](https://developers.google.com/web/fundamentals/architecture/building-components/customelements). It is a good introduction into web components and explanation how to register and use them.
69+
70+
If you have any question use our issue tracker to contact me. I'll be happy to help you to use the API console.
71+
72+
[Pawel Psztyc](https://github.com/jarrodek)

0 commit comments

Comments
 (0)