/* WEPA this is miggymofongo's contribution to underBBS! I'm practicing web components in this bishhh this template contains the html code of the side bar */ const template = document.createElement("template") template.innerHTML = `
` /* setting up a web component will extend the set of available HTML elements for you to use in your project and keep your codebase organized and modular. Set up a web component in a separate file and link it directly in your html document head. creating a custom web component is just like creating a class. after setting up the constructor function, you need to create some methods that tell the browser more about it. you can add whatever methods you want, but they at least need some of the following: 1) connectedCallback() => call this method when the element is added to the document, 2) disconnectedCallback() => call this method when the element is removed from the document 3) static get observedAttributes() => this returns an array of attribute names to track for changes 4) attributeChangedCallback(name, oldValue, newValue) => this one is called when one of the attributes 5) adoptedCallback() => called when the element is moved to a new document */ class ProfileSideBar extends HTMLElement { // constructor() { // the constructor function initiates the new side bar super() this.appendChild(template.content.cloneNode(true)) this.sidenav = this.querySelector("#mySidenav"); this.openBtn = this.querySelector("#openBtn"); this.closeBtn = this.querySelector(".closeBtn"); this.mainContent = document.getElementById("main"); } /* this method adds event listeners to the openBtn and closeBtn classes that call the openNav closeNav function to open and close the sidebar*/ connectedCallback() { this.openBtn.addEventListener("click", this.openNav.bind(this)); this.closeBtn.addEventListener("click", this.closeNav.bind(this)); } disconnectedCallback() { this.openBtn.removeEventListener("click", this.openNav.bind(this)); this.closeBtn.removeEventListener("click", this.closeNav.bind(this)); } /* these two methods will open and close the profile side menu*/ openNav() { this.sidenav.style.width = "450px"; if (this.mainContent) { this.mainContent.style.marginLeft = "250px"; } } closeNav() { this.sidenav.style.width = "0"; if (this.mainContent) { this.mainContent.style.marginLeft = "0" } } } /*lastly, we need to define the element tag itself and attach it to the webcomponent */ customElements.define("profile-side-bar", ProfileSideBar)