Skip to content

DevMussab/Custom-Elements

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Custom Elements Guide 🎨✨📚

This guide offers a detailed exploration of how to effectively use and implement custom elements in your web projects. By leveraging these reusable components, developers can streamline their code and improve maintainability across various web applications. 💻🧑‍💻📖


Introduction 📌🔎💡

Custom elements empower developers to create unique HTML tags that encapsulate specific functionality. This modular approach allows for cleaner and more maintainable code. The provided elements.js file contains a sample custom element that can be reused across multiple web pages. 🛠️📜🔄


Description 📝📂🧩

The elements.js file defines a custom element that enhances code efficiency by eliminating the need to duplicate similar code across various HTML files. By using this file, you can easily implement reusable components that simplify your project structure. ⚙️🔧🗃️


HTML Example (index.php) 🖥️📄🖋️

Below is an example of an HTML file that incorporates the custom element defined in elements.js:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Elements | DevMussab</title>
</head>
<body>
    <!-- Using the custom element -->
    <custom-element></custom-element>

    <!-- Linking the custom elements script -->
    <script src="elements.js"></script>
</body>
</html>

Explanation 🖊️📘🧵

  1. <custom-element></custom-element>: This is the custom element created in elements.js. It displays the content defined within the JavaScript class.
  2. <script src="elements.js"></script>: The script is included at the end of the body to ensure that the DOM is fully loaded before the custom elements are initialized.

JavaScript Code (elements.js) 💻📜🖱️

The following JavaScript code defines the custom element:

// Custom Elements.JS | By: DevMussab
// Date: 13.01.24
// Version: 1.0.0
// Author: Mussab Qasim | https://github.com/DevMussab

// Element Class | ELEMENT
class ELEMENT extends HTMLElement {
    constructor() {
        super();
    }

    connectedCallback() {
        this.innerHTML = `
        <h1>1st Element Header</h1>
        <p>This is a paragraph inside the header element.</p>
        <button id="btn">Click Me</button>
        `;

        const btn = this.querySelector('#btn');
        btn.addEventListener('click', () => {
            alert('Button Clicked');
        });
    }
}

// Define the custom element
customElements.define('custom-element', ELEMENT);

Explanation 📚🔍🖥️

  1. Class Declaration (ELEMENT): The ELEMENT class extends the built-in HTMLElement interface to create a custom HTML element.
  2. connectedCallback() Method: This lifecycle method is triggered when the custom element is added to the DOM. It defines the HTML structure of the custom element and attaches an event listener to the button.
  3. Event Listener: The button within the custom element is programmed to display an alert message when clicked. 🎯📢🖱️

How to Use 🧭🔗📝

To implement the custom element in your project, follow these steps:

  1. Create an HTML file (e.g., index.php or index.html).
  2. Add the custom element (<custom-element>) to your HTML file where you want it to appear.
  3. Include the elements.js script at the end of your HTML file.
  4. Open the HTML file in a web browser to see the custom element in action. 🌐👨‍💻👩‍💻

Benefits of Using Custom Elements 🔁📈📚

  • Reusability: Define a custom element once and reuse it across multiple files.
  • Cleaner Code: Maintain organized HTML files by separating content structure from behavior.
  • Scalability: Easily introduce new custom elements as your project evolves. 🔧⚡🚀

Author ✍️👤📬

Mussab Qasim
GitHub: https://github.com/DevMussab 🔗💻📜


This guide covers essential concepts for utilizing custom elements in web projects. Feel free to modify the code examples to suit your specific requirements. Happy coding! 🎉💡🚀

About

An easy solution of Ctrl + C & Ctrl + V

Topics

Resources

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published