Hướng dẫn javascript components without framework
Show
Everyone knows it: encapsulating and reusing UI components on the web is challenging. It is usually copy and paste of HTML, CSS, and JavaScript, often spread over one or more files. If you forget a part, it either does not look as desired, or the interaction will not work. Enough of that! Web Components open up new ways on the web to implement and (re-)use UI components in a standardized manner and without any framework. In this article:Patrick Jahr is architect at Thinktecture and focuses on Blazor WebAssembly, .NET Core and Angular.
What to expect
What are Web Components?Web Components are a collection of three technologies:
With these three technologies, it is possible to develop native Web Components. For the components part, this may lead us to a solution where we might not need to use full-blown frameworks like Angular, React, or Vue.js. Create your Web Component template with HTML TemplatesWith HTML Templates, you can create your
template markup. In order to use a template, you simply have to write your markup inside the But for our sample, we do not want to write the template in our HTML file. So we create a new JavaScript file Bring your Web Component to life with Custom ElementsWith ES6 classes it is possible to create a Custom Element Custom Elements. Now we create a new class Finally, you need to register your Custom Element. You can do this with one line of code: In the example, the After the Custom Element is defined, you can use it in your HTML file. To add this Custom Element and use your Web Component, you must import your JavaScript file. Relevant here is that the script file is added with the type The lifecycle of a Custom Element has a constructor for the element class and four methods to implement. If you use properties and attributes in your class, you must be careful, because property value is not the same as the attribute value. If you wish to sync them, then you will need to implement it. Let’s demonstrate that with our example: To encapsulate the HTML and CSS from the global scope, we must create our own shadow DOM. With the shadow DOM, we create
an isolated DOM, which is not accessible from the outside. CSS styles are scoped to the shadow DOM—no more ID or class conflicts. Also, conflicts with existing IDs or classes are history. The shadow DOM has the following concepts: So now, let us attach our HTML Template content to a shadow host. In our
sample, you see that the shadow DOM is attached to the Shadow Host, which is the parent element After we have defined a template, created a Custom Element, and attached a shadow DOM, we have all done everything needed to create a Web Component. Now we can have a look at how we bring content from outside, inside our Web Component. Let’s start with markup from outside. To allow markup from outside, we can use slots. Slots have the HTML tag In our sample, we must change a little bit. If we want to have the same count on stars as the Rating
What has changed in the HTML Template: The next step is to render all rating stars. What has changed in the Custom Element: The last step is that we have to register for the event So now, we can
change the rating stars from default to our rating star. If we want to overwrite more than one item in our template, we must give the Rating
New Rating Title What is striking here is that the title adopts the style from the outside and ignores the style of the
Web Component. In the next passage, we have to look at how we can fix this and how we can adapt the style in a specific context. Let us take a look at how we can have access to the styles inside the Web Component. To
style the host of the Web Component, we have four options (see here): To illustrate this, you will see a small example below, which shows how a Web Component looks in a specific context. If you want to style your Web Component from outside, you can set the CSS shadow Part In the sample above, you can see that the style for the title comes from outside and overwrites the style inside
the Web Component. The last step is to build your Web Component. Since there is no standardized approach to build it, you can use any tool you desire, like Webpack or Parcel, for instance. In my sample, I built the Web Component with Webpack. To build my
Web Component, I use the following To bundle the Web Component, I have set up an npm script At least I run In this article, we have looked into the world of Web Components based on a sample component. Web Components consist of the three specifications Custom Elements, shadow DOM, and HTML Templates. If we combine these specifications, it is possible to create own HTML elements that also hide their implementation details from their environment and encapsulate them.
This makes it possible to create components that can then be reused in other applications. When taking a look at the statistics of Can I Use on Custom Elements, shadow DOM and HTML Templates, it will become apparent that all three features already arrived in modern browsers like Chrome, Safari and Edge. To get a little bit more help to build Web Components and get a bit more browser compatibility, you can use LitElement. LitElement is a simple base class from the Polymer Project to create fast and lightweight Web Components. So try it out, fiddle around with them, and create your first small Web Components for your application. If you want to see all the code and try out the sample, you can find the demo here. Don’t
miss any content on Angular, .NET Core, Blazor, Azure, and Kubernetes and sign up for our free monthly dev newsletter.
The requirement to store additional fields, unknown at development time, in a
relational database is not new. Nonetheless, none of the projects I know of are willing to change the database structure at runtime. What if there is a project which needs dynamically created fields and doesn't want or cannot use entity–attribute–value model or switch to No-SQL databases?
The Roslyn Source Generator, implemented in the previous articles of the series, emits some C# code without looking at the dependencies of the current .NET (Core) project. In this article our
Making our Angular modules configurable is an important step in building a reusable architecture. Having used Angular for a while you might be
familiar with the commonly used |