diff --git a/8-web-components/1-webcomponents-intro/article.md b/8-web-components/1-webcomponents-intro/article.md index 7162a5c3..1a277d66 100644 --- a/8-web-components/1-webcomponents-intro/article.md +++ b/8-web-components/1-webcomponents-intro/article.md @@ -4,10 +4,10 @@ This section describes a set of modern standards for "web components". As of now, these standards are under development. Some features are well-supported and integrated into the modern HTML/DOM standard, while others are yet in draft stage. You can try examples in any browser, Google Chrome is probably the most up to date with these features. Guess, that's because Google fellows are behind many of the related specifications. -The whole component idea is nothing new. It's used in many frameworks and elsewhere. - ## What's common between... +The whole component idea is nothing new. It's used in many frameworks and elsewhere. + Before we move to implementation details, take a look at this great achievement of humanity: ![](satellite.jpg) @@ -24,11 +24,11 @@ The International Space Station: - The components are very complex, much more complicated than most websites. - Components are developed internationally, by teams from different countries, speaking different languages. -...And this thing is flying, keeping humans alive in space! +...And this thing flies, keeps humans alive in space! How such complex devices are created? -Which principles we could borrow, to make our development same-level reliable and scalable? Or, at least, close to it. +Which principles we could borrow to make our development same-level reliable and scalable? Or, at least, close to it. ## Component architecture @@ -38,7 +38,7 @@ If something becomes complex -- split it into simpler parts and connect in the m **A good architect is the one who can make the complex simple.** -We can split a user interface into components -- visual entities, each of them has own place on the page, can "do" a well-described task, and is separate from the others. +We can split user interface into visual components: each of them has own place on the page, can "do" a well-described task, and is separate from the others. Let's take a look at a website, for example Twitter. @@ -50,27 +50,25 @@ It naturally splits into components: 2. User info. 3. Follow suggestions. 4. Submit form. -5. And also 6, 7 - messages. +5. (and also 6, 7) -- messages. Components may have subcomponents, e.g. messages may be parts of a higher-level "message list" component. A clickable user picture itself may be a component, and so on. -How do we decide, what is a component? That comes from intuition, experience and common sense. In the case above, the page has blocks, each of them plays its own role. - -So, what comprises a component? +How do we decide, what is a component? That comes from intuition, experience and common sense. Usually it's a separate visual entity that we can describe in terms of what it does and how it interacts with the page. In the case above, the page has blocks, each of them plays its own role, it's logical to make these components. - A component has its own JavaScript class. - DOM structure, managed solely by its class, outside code doesn't access it ("encapsulation" principle). - CSS styles, applied to the component. - API: events, class methods etc, to interact with other components. -"Web components" provide built-in browser capabilities for components: +Once again, the whole "component" thing is nothing special. + +There exist many frameworks and development methodologies to build them, each one with its own bells and whistles. Usually, special CSS classes and conventions are used to provide "component feel" -- CSS scoping and DOM encapsulation. + +"Web components" provide built-in browser capabilities for that, so we don't have to emulate them any more. - [Custom elements](https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements) -- to define custom HTML elements. - [Shadow DOM](https://dom.spec.whatwg.org/#shadow-trees) -- to create an internal DOM for the component, hidden from the others. - [CSS Scoping](https://drafts.csswg.org/css-scoping/) -- to declare styles that only apply inside the Shadow DOM of the component. -There exist many frameworks and development methodologies that aim to do the similar thing, each one with its own bells and whistles. Usually, special CSS classes and conventions are used to provide "component feel" -- CSS scoping and DOM encapsulation. - -Web components provide built-in browser capabilities for that, so we don't have to emulate them any more. - In the next chapter we'll go into details of "Custom Elements" -- the fundamental and well-supported feature of web components, good on its own. diff --git a/8-web-components/2-custom-elements/article.md b/8-web-components/2-custom-elements/article.md index b888e7ea..278defd2 100644 --- a/8-web-components/2-custom-elements/article.md +++ b/8-web-components/2-custom-elements/article.md @@ -1,22 +1,31 @@ # Custom elements -We can create our own class for a custom HTML element with its own methods and properties, events and so on. +We can create custom HTML elements, described by our class, with its own methods and properties, events and so on. + +Once an custom element is defined, we can use it on par with built-in HTML elements. + +That's great, as HTML dictionary is rich, but not infinite. There are no ``, ``, ``... Just think of any other tag we might need. + +We can define them with a special class, and then use as if they were always a part of HTML. There are two kinds of custom elements: 1. **Autonomous custom elements** -- "all-new" elements, extending the abstract `HTMLElement` class. 2. **Customized built-in elements** -- extending built-in elements, like customized `HTMLButtonElement` etc. -First we'll see how autonomous elements are made, and then the customized built-in ones. +First we'll create autonomous elements, and then customized built-in ones. -For a class to describe an element, it should support so-called "custom element reactions" -- methods that the browser calls when our element is created/added/removed from DOM. +To create a custom element, we need to tell the browser several details about it: how to show it, what to do when the element is added or removed to page, etc. -That's easy, as there are only few of them. Here's a sketch with the full list: +That's done by making a class with special methods. That's easy, as there are only few methods, and all of them are optional. + +Here's a sketch with the full list: ```js class MyElement extends HTMLElement { constructor() { + super(); // element created } @@ -42,17 +51,19 @@ class MyElement extends HTMLElement { // called when the element is moved to a new document // (happens in document.adoptNode, very rarely used) } + + // there can be other element methods and properties } ``` -Then we need to register the element: +After that, we need to register the element: ```js // let the browser know that is served by our new class customElements.define("my-element", MyElement); ``` -Now for any new elements with tag `my-element`, an instance of `MyElement` is created, and the aforementioned methods are called. +Now for any HTML elements with tag ``, an instance of `MyElement` is created, and the aforementioned methods are called. We also can `document.createElement('my-element')` in JavaScript. ```smart header="Custom element name must contain a hyphen `-`" Custom element name must have a hyphen `-`, e.g. `my-element` and `super-button` are valid names, but `myelement` is not. @@ -64,12 +75,14 @@ That's to ensure that there are no name conflicts between built-in and custom HT For example, there already exists `