مكونات HTML القابلة لإعادة الاستخدام - كيفية إعادة استخدام رأس وتذييل على موقع ويب

تخيل أنك تبني موقعًا لعميل ، متجر صغير للأمهات والبوب ​​، يحتوي على صفحتين فقط.

ذلك ليس بالكثير. لذلك عند الانتهاء من العمل على الصفحة المقصودة والبدء في صفحة الاتصال ، ما عليك سوى إنشاء ملف HTML جديد ونسخ جميع التعليمات البرمجية من الصفحة الأولى.

يبدو رأس الصفحة وتذييلها جيدًا بالفعل ، وكل ما عليك فعله هو تغيير باقي المحتوى.

ولكن ماذا لو أراد عميلك 10 صفحات؟ أم 20؟ ويطلبون تغييرات طفيفة في الرأس والتذييل خلال التطوير.

فجأة أي تغيير ، مهما كان صغيرا ، يجب أن يتكرر عبر كل هذه الملفات.

هذه واحدة من المشكلات الرئيسية التي تحلها أشياء مثل React أو Handlebars.js: يمكن كتابة أي كود ، وخاصة الأشياء الهيكلية مثل رأس الصفحة أو تذييلها ، مرة واحدة ويمكن إعادة استخدامها بسهولة خلال المشروع.

حتى وقت قريب ، لم يكن من الممكن استخدام المكونات في Vanilla HTML و JavaScript. ولكن مع إدخال Web Components ، من الممكن إنشاء مكونات قابلة لإعادة الاستخدام دون استخدام أشياء مثل React.

ما هي مكونات الويب؟

مكونات الويب هي في الواقع مجموعة من بعض التقنيات المختلفة التي تسمح لك بإنشاء عناصر HTML مخصصة.

هذه التقنيات هي:

  • قوالب HTML : أجزاء من ترميز HTML باستخدام عناصر لن يتم عرضها حتى يتم إلحاقها بالصفحة باستخدام JavaScript.
  • العناصر المخصصة : واجهات برمجة تطبيقات جافا سكريبت المدعومة على نطاق واسع والتي تتيح لك إنشاء عناصر DOM جديدة. بمجرد إنشاء عنصر مخصص وتسجيله باستخدام واجهات برمجة التطبيقات هذه ، يمكنك استخدامه بشكل مشابه لمكون React.
  • Shadow DOM : عنصر DOM مغلف أصغر ، معزول عن DOM الرئيسي ويتم عرضه بشكل منفصل. أي أنماط ونصوص تقوم بإنشائها للمكونات المخصصة في Shadow DOM لن تؤثر على العناصر الأخرى في DOM الرئيسي.

سنغوص في كل منها أكثر قليلاً خلال البرنامج التعليمي.

كيفية استخدام قوالب HTML

الجزء الأول من اللغز هو تعلم كيفية استخدام قوالب HTML لإنشاء تخفيض سعر HTML قابل لإعادة الاستخدام.

لنلقِ نظرة على مثال بسيط لرسالة الترحيب:

Hello, World!

And all who inhabit it

إذا نظرت إلى الصفحة ، فلا يوجد

أو

To actually render the welcome message, you'll need to use a bit of JavaScript:

const template = document.getElementById('welcome-msg'); document.body.appendChild(template.content); 

Even though this is a pretty simple example, you can already see how using templates makes it easy to reuse code throughout a page.

The main issue is that, at least with the current example, the welcome message code is mixed in with the rest of the page's content. If you want to change the welcome message later, you'll need to change the code across multiple files.

Instead, you can pull the HTML template into the JavaScript file, so any page the JavaScript is included in will render the welcome message:

const template = document.createElement('template'); template.innerHTML = ` 

Hello, World!

And all who inhabit it

` document.body.appendChild(template.content);

Now that everything's in the JavaScript file, you don't need to create a element – you could just as easily create a or .

However, elements can be paired with a element, which allows you to do things like change the text for elements within the . It's a bit outside the scope of this tutorial, so you can read more about elements over on MDN.

How to create custom elements

One thing you might have noticed with HTML templates is that it can be tricky to insert your code in the right place. The earlier welcome message example was just appended to the page.

If there was content already on the page, say, a banner image, the welcome message would appear below it.

As a custom element, your welcome message might look like this:

And you can put it wherever you want on the page.

With that in mind, let's take a look at custom elements and create our own React-like header and footer elements.

Setup

For a portfolio site, you might have some boilerplate code that looks like this:

* { margin: 0; padding: 0; box-sizing: border-box; } html, body { height: 100%; } body { color: #333; font-family: sans-serif; display: flex; flex-direction: column; } main { flex: 1 0 auto; } 

Each page will have the same header and footer, so it makes sense to create a custom element for each of those.

Let's start with the header.

Define a custom element

First, create a directory called components and inside that directory, create a new file called header.js with the following code:

class Header extends HTMLElement { constructor() { super(); } } 

It's just a simple ES5 Class declaring your custom Header component, with the constructor method and special super keyword. You can read more about those on MDN.

By extending the generic HTMLElement class, you can create any kind of element you want. It's also possible to extend specific elements like HTMLParagraphElement.

Register your custom element

Before you can start using your custom element, you'll need to register it with the customElements.define() method:

class Header extends HTMLElement { constructor() { super(); } } customElements.define('header-component', Header); 

This method takes at least two arguments.

The first is a DOMString you'll use when adding the component to the page, in this case, .

The next is the component's class that you created earlier, here, the Header class.

The optional third argument describes which existing HTML element your custom element inherits properties from, for example, {extends: 'p'}. But we won't be using this feature in this tutorial.

Use lifecycle callbacks to add the header to the page

There are four special lifecycle callbacks for custom elements that we can use to append header markdown to the page: connectedCallback, attributeChangeCallback, disconnectedCallback, and adoptedCallback.

Of these callbacks, connectedCallback is one of the most commonly used. connectedCallback runs each time your custom element is inserted into the DOM.

You can read more about the other callbacks here.

For our simple example, connectedCallback is enough to add a header to the page:

class Header extends HTMLElement { constructor() { super(); } connectedCallback() { this.innerHTML = `  nav { height: 40px; display: flex; align-items: center; justify-content: center; background-color: #0a0a23; } ul li { list-style: none; display: inline; } a { font-weight: 700; margin: 0 25px; color: #fff; text-decoration: none; } a:hover { padding-bottom: 5px; box-shadow: inset 0 -2px 0 0 #fff; }    
      
  • About
  • Work
  • Contact
`; } } customElements.define('header-component', Header);

Then in index.html, add the components/header.js script and just above the element:

And your reusable header component should be rendered to the page:

Now adding a header to the page is as easy as adding a tag pointing to components/header.js, and adding wherever you want.

Note that, since the header and its styling are being inserted into the main DOM directly, it's possible to style it in the styles.css file.

But if you look at the header styles included in connectedCallback, they're quite general, and can affect other styling on the page.

For example, if we add the following footer component:

class Footer extends HTMLElement { constructor() { super(); } connectedCallback() { this.innerHTML = `  footer { height: 60px; padding: 0 10px; list-style: none; display: flex; justify-content: space-between; align-items: center; background-color: #dfdfe2; } ul li { list-style: none; display: inline; } a { margin: 0 15px; color: inherit; text-decoration: none; } a:hover { padding-bottom: 5px; box-shadow: inset 0 -2px 0 0 #333; } .social-row { font-size: 20px; } .social-row li a { margin: 0 15px; }   
      
  • About
  • Work
  • Contact
`; } } customElements.define('footer-component', Footer);

Here's what the page would look like:

The styling from the footer component overrides the styling for the header, changing the color of the links. That's expected behavior for CSS, but it would be nice if each component's styling was scoped to that component, and wouldn't affect other things on the page.

Well, that's exactly where the Shadow DOM shines. Or shades? Anyway, the Shadow DOM can do that.

How to use the Shadow DOM with custom elements

The Shadow DOM acts as a separate, smaller instance of the main DOM. Rather than act as a copy of the main DOM, the Shadow DOM is more like a subtree just for your custom element. Anything added to a Shadow DOM, especially styles, are scoped that particular custom element.

In a way, it's like using const and let rather than var.

Let's start by refactoring the header component:

const headerTemplate = document.createElement('template'); headerTemplate.innerHTML = `  nav { height: 40px; display: flex; align-items: center; justify-content: center; background-color: #0a0a23; } ul li { list-style: none; display: inline; } a { font-weight: 700; margin: 0 25px; color: #fff; text-decoration: none; } a:hover { padding-bottom: 5px; box-shadow: inset 0 -2px 0 0 #fff; }    
      
  • About
  • Work
  • Contact
`; class Header extends HTMLElement { constructor() { super(); } connectedCallback() { } } customElements.define('header-component', Header);

The first thing you need to do is to use the .attachShadow() method to attach a shadow root to your custom header component element. In connectedCallback, add the following code:

... class Header extends HTMLElement { constructor() { super(); } connectedCallback() { const shadowRoot = this.attachShadow({ mode: 'closed' }); } } customElements.define('header-component', Header); 

Notice that we're passing an object to .attachShadow() with an option, mode: 'closed'. This just means that the header component's shadow DOM is inaccessible from external JavaScript.

If you'd like to manipulate the header component's shadow DOM later with JavaScript outside the components/header.js file, just change the option to mode: 'open'.

Finally, append shadowRoot to the page with the .appendChild() method:

... class Header extends HTMLElement { constructor() { super(); } connectedCallback() { const shadowRoot = this.attachShadow({ mode: 'closed' }); shadowRoot.appendChild(headerTemplate.content); } } customElements.define('header-component', Header); 

And now, since the header component's styles are encapsulated in its Shadow DOM, the page should look like this:

Here's what the final code looks like across all files after refactoring the footer component:

* { margin: 0; padding: 0; box-sizing: border-box; } html, body { height: 100%; } body { color: #333; font-family: sans-serif; display: flex; flex-direction: column; } main { flex: 1 0 auto; } 
const headerTemplate = document.createElement('template'); headerTemplate.innerHTML = `  nav { height: 40px; display: flex; align-items: center; justify-content: center; background-color: #0a0a23; } ul li { list-style: none; display: inline; } a { font-weight: 700; margin: 0 25px; color: #fff; text-decoration: none; } a:hover { padding-bottom: 5px; box-shadow: inset 0 -2px 0 0 #fff; }    
      
  • About
  • Work
  • Contact
`; class Header extends HTMLElement { constructor() { super(); } connectedCallback() { const shadowRoot = this.attachShadow({ mode: 'closed' }); shadowRoot.appendChild(headerTemplate.content); } } customElements.define('header-component', Header);
const footerTemplate = document.createElement('template'); footerTemplate.innerHTML = `  footer { height: 60px; padding: 0 10px; list-style: none; display: flex; flex-shrink: 0; justify-content: space-between; align-items: center; background-color: #dfdfe2; } ul li { list-style: none; display: inline; } a { margin: 0 15px; color: inherit; text-decoration: none; } a:hover { padding-bottom: 5px; box-shadow: inset 0 -2px 0 0 #333; } .social-row { font-size: 20px; } .social-row li a { margin: 0 15px; }   
      
  • About
  • Work
  • Contact
`; class Footer extends HTMLElement { constructor() { super(); } connectedCallback() { const shadowRoot = this.attachShadow({ mode: 'closed' }); shadowRoot.appendChild(footerTemplate.content); } } customElements.define('footer-component', Footer);

In closing

We've covered a lot here, and you might have already decided just to use React or Handlebars.js instead.

Those are both great options!

Still, for a smaller project where you'll only need a few reusable components, a whole library or templating language might be overkill.

Hopefully now you have the confidence to create your own reusable HTML components. Now get out there and create something great (and reusable).