Coder Survival Guide

Coder Survival Guide

5675 bookmarks
Custom sorting
A Web Component Intro with Example
A Web Component Intro with Example
I will demonstrate writing a web component by implementing tabbed panels. The finished tabs will look like below. You can find the source code in this repository. Web Component is a standard built into the browser. At the time of writing every major browser supports this feature. It is an underrated feature and often shadowed by popular SPA frameworks like React and Angular. I say this feature is underrated because WC (Web Component) predates React and it does not require importing any external libraries. Enough of history lets see how to write a component. A WC needs two steps. A class that extends HTMLElement. Registering the component as a custom element. <!DOCTYPE html> <html> <head> <script> class WCTab extends HTMLElement { } //Step 1 customElements.define("wc-tab", WCTab) //Step 2 </script> </head> </html> That's it. A Web Component is ready to use. In registering the WC, the name must always contain a hyphen that is the reason it is wc-tab instead of wctab. This name is what needed to use this WC. We can use it just be creating a tag with same name as below. <body> <wc-tab></wc-tab> </body> Opening the html in browser doesn't show anything. It is not any better than an empty div at this point. Lets write something in between the opening and close tag. <wc-tab> <p>Hello world!</p> </wc-tab> This actually prints Hello world! in the browser! Shadow Root You almost always should enable shadow root in your WC. Shadow root provides scoped DOM tree with the web component as its root element. This enables us to import css styles without polluting the global scope. That means we can use css stylesheets and those styles will apply only within this custom element. Any tag with matching css selectors outside the custom component are unaffected. This can be enabled in our constructor as below. class WCTab extends HTMLElement { constructor() { super(); this.shadow = this.attachShadow({ mode: "open" }); } } As soon as this change is made, the hello world printed in the browser has disappeared. When shadow DOM is attached, it replaces our existing children. WC has few lifecycle callbacks, one of them is connectedCallback. It is called as soon as the WC is attached to dom. Lets add it! class WCTab extends HTMLElement { constructor() { super(); this.shadow = this.attachShadow({ mode: "open" }); } connectedCallback(){ console.log("connected!"); } } This prints connected! in console when the page is refreshed. Tab - Example Lets define how our tab component is going to be designed. Our WC will have each tab as div. The WC should define tab and its content as shown below. <wc-tab> <div name="Tab 1">Tab 1 content</div> <div name="Tab 2">Tab 2 content</div> <div name="Tab 3">Tab 3 content</div> </wc-tab> We are going to read the provided children as input and generate a UI to show them as tabs. it is possible to make each tab as its own custom element instead of div tag. We will stick with div for this example. Let's see how to access the children in our component. We are going to do this in our lifecycle method connectedCallback connectedCallback(){ let tabs = this.querySelectorAll("div"); console.log(tabs); } This is how we read the children. Unfortunately this does not work. connectedCallback is called before the children are attached to DOM. There is no simple way to read them as soon as they are attached. We go with MutationObserver. This observes changes for children and calls the given callback. connectedCallback() { let thisNode = this; let observer = new MutationObserver(function () { let tabs = thisNode.querySelectorAll("div"); console.log(tabs); }); // We are only interested in the children of // this component observer.observe(this, { childList: true }); }
·blog.rasvi.io·
A Web Component Intro with Example
EcoSonar | Home
EcoSonar | Home
EcoSonar the ecodesign and accessibility audit tool to minimize web carbon footprint easily
·ecosonar.org·
EcoSonar | Home
Note d'analyse : Planifier la décarbonation du système numérique en France - The Shift Project
Note d'analyse : Planifier la décarbonation du système numérique en France - The Shift Project
Share:Nous avons le plaisir de vous présenter notre nouvelle note d’analyse : « Planifier la décarbonation du système numérique en France : cahier des charges ». Elle fait suite à la participation du Shift Project aux groupes de travail lancés par le Haut Comité du Numérique Ecoresponsable (HCNE) pour proposer une feuille de route de décarbonation du […]
·theshiftproject.org·
Note d'analyse : Planifier la décarbonation du système numérique en France - The Shift Project
[:fr]Bilan des gaz à effet de serre : Infomaniak publie son rapport 2021[:de]Treibhausgasbilanz : Infomaniak veröffentlicht Bericht 2021[:en]Greenhouse gas balance : Infomaniak publishes its 2021 report[:it]Bilancio dei gas a effetto serra : Infomaniak pubblica il suo rapporto 2021[:es]Bilancio dei gas a effetto serra : Infomaniak pubblica il suo rapporto 2021[:] • Infomaniak
[:fr]Bilan des gaz à effet de serre : Infomaniak publie son rapport 2021[:de]Treibhausgasbilanz : Infomaniak veröffentlicht Bericht 2021[:en]Greenhouse gas balance : Infomaniak publishes its 2021 report[:it]Bilancio dei gas a effetto serra : Infomaniak pubblica il suo rapporto 2021[:es]Bilancio dei gas a effetto serra : Infomaniak pubblica il suo rapporto 2021[:] • Infomaniak
[:fr]Avec 31% de croissance, nos émissions de CO2 ont baissé de 1.9% en 2021. Voici en toute transparence le bilan 2021 des gaz à effet de serre d'Infomaniak.[:de]Bei einem Wachstum von 31% sind unsere CO2-Emissionen bis 2021 um 1,9% gesunken. Nachfolgend in völliger Transparenz die Treibhausgasbilanz von Infomaniak für 2021.[:en]With 31% growth, our CO2 emissions have decreased by 1.9% in 2021. Here is the Infomaniak greenhouse gas balance 2021, published in a spirit of complete transparency.[:it]Con una crescita del 31%, le nostre emissioni di CO2 sono diminuite dell'1,9% nel 2021. All'insegna della trasparenza, ecco il bilancio 2021 dei gas a effetto serra emessi da Infomaniak.[:es]Con un crecimiento del 31%, nuestras emisiones de CO2 han disminuido un 1,9% en 2021. Aquí está, con total transparencia, el informe de gases de efecto invernadero de Infomaniak 2021.[:]
·news.infomaniak.com·
[:fr]Bilan des gaz à effet de serre : Infomaniak publie son rapport 2021[:de]Treibhausgasbilanz : Infomaniak veröffentlicht Bericht 2021[:en]Greenhouse gas balance : Infomaniak publishes its 2021 report[:it]Bilancio dei gas a effetto serra : Infomaniak pubblica il suo rapporto 2021[:es]Bilancio dei gas a effetto serra : Infomaniak pubblica il suo rapporto 2021[:] • Infomaniak
Zenika/pagiel: PAGIEL est un outil de monitoring d'indicateurs environnementaux d'un projet web, fait pour être intégré dans des pipelines de déploiement continu.
Zenika/pagiel: PAGIEL est un outil de monitoring d'indicateurs environnementaux d'un projet web, fait pour être intégré dans des pipelines de déploiement continu.
PAGIEL est un outil de monitoring d'indicateurs environnementaux d'un projet web, fait pour être intégré dans des pipelines de déploiement continu. - Zenika/pagiel: PAGIEL est un outil de m...
·github.com·
Zenika/pagiel: PAGIEL est un outil de monitoring d'indicateurs environnementaux d'un projet web, fait pour être intégré dans des pipelines de déploiement continu.
Atos et Greenspector livrent à la Délégation ministérielle au Numérique en Santé et à l’Agence du Numérique en Santé un service de mesure de l'impact environnemental des applications de santé web et mobiles
Atos et Greenspector livrent à la Délégation ministérielle au Numérique en Santé et à l’Agence du Numérique en Santé un service de mesure de l'impact environnemental des applications de santé web et mobiles
Atos et Greenspector, alumni de Scaler, l’accélérateur de start-ups d'Atos, annoncent avoir livré à la Délégation ministérielle au Numérique en Santé (DNS) et à l’Agence du Numérique en Santé (ANS) un service de calcul d’un écoscore des applications de santé web et mobiles, accessible en ligne
·atos.net·
Atos et Greenspector livrent à la Délégation ministérielle au Numérique en Santé et à l’Agence du Numérique en Santé un service de mesure de l'impact environnemental des applications de santé web et mobiles
How to measure the energy consumption of your apps - Sustainable Software
How to measure the energy consumption of your apps - Sustainable Software
The second principle of Sustainable Software Engineering is to build energy efficient applications. The very first step in that direction is to measure the energy cost of your application. This post will answer the question "what is the best way to measure the energy consumption of your apps?".
·devblogs.microsoft.com·
How to measure the energy consumption of your apps - Sustainable Software
Pourquoi quitter la tech (Sonia PRÉVOST MERCIER)
Pourquoi quitter la tech (Sonia PRÉVOST MERCIER)
🔥 Pour rester informé sur l'actualité de Devoxx France, suivez nous sur linkedIn : https://www.linkedin.com/in/devoxxfrance/, twitter : https://twitter.com/...
·youtube.com·
Pourquoi quitter la tech (Sonia PRÉVOST MERCIER)
Femmes sous algorithmes - Culture et pop | ARTE
Femmes sous algorithmes - Culture et pop | ARTE
Sur YouTube, des milliers de femmes se filment en train de faire le ménage, de se maquiller ou de déballer leurs courses, sous les yeux de millions d’abonnés. Que nous racontent ces vidéos sur les femmes d’aujourd’hui ? Comment l’Algorithme finit-il par les conditionner ? Au fil de la série, notre héroïne prend de plus en plus conscience des carcans qui l’enferment.
·arte.tv·
Femmes sous algorithmes - Culture et pop | ARTE