Skip to content

Commit e749771

Browse files
committed
add web components
1 parent 48a33d7 commit e749771

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

playground/web-components/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Web Components
2+
3+
A quick intro to web components.
4+
5+
<p align="center">
6+
<img src="screenshot.png">
7+
</p>
8+
9+
## Features
10+
11+
- creating a custom web component.
12+
- setting up a shadow DOM.
13+
- generating an HTML template.
14+
- displaying slots.
15+
- handling events.
16+
17+
Based on [Web Components Crash Course](https://www.youtube.com/watch?v=PCWaFLy3VUo) by Brad Traversy (2020).

playground/web-components/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Web Components Example</title>
8+
<style>
9+
body {
10+
display: flex;
11+
flex-direction: column;
12+
align-items: center;
13+
justify-content: center;
14+
min-height: 100vh;
15+
}
16+
/* h3 {
17+
color: purple;
18+
} */
19+
</style>
20+
</head>
21+
<body>
22+
<!-- <h3>Hello World</h3> -->
23+
<user-card
24+
name="John Doe"
25+
avatar="https://randomuser.me/api/portraits/men/1.jpg"
26+
>
27+
<div slot="email">johndoe@gmail.com</div>
28+
<div slot="phone">555-555-5555</div>
29+
</user-card>
30+
<user-card
31+
name="Jane Doe"
32+
avatar="https://randomuser.me/api/portraits/women/1.jpg"
33+
>
34+
<div slot="email">janedoe@gmail.com</div>
35+
<div slot="phone">333-333-3333</div>
36+
</user-card>
37+
<script src="userCard.js"></script>
38+
</body>
39+
</html>
35.7 KB
Loading

playground/web-components/userCard.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const template = document.createElement("template");
2+
template.innerHTML = `
3+
<style>
4+
.user-card {
5+
font-family: "Arial", sans-serif;
6+
background-color: #f4f4f4;
7+
width: 500px;
8+
display: grid;
9+
grid-template-columns: 1fr 2fr;
10+
gap: 10px;
11+
margin-bottom: 15px;
12+
border-bottom: 5px solid darkorchid;
13+
}
14+
.user-card img {
15+
width: 100%;
16+
}
17+
.user-card button {
18+
cursor: pointer;
19+
background-color: darkorchid;
20+
color: #fff;
21+
border: 0;
22+
border-radius: 5px;
23+
padding: 5px 10px;
24+
}
25+
</style>
26+
<div class="user-card">
27+
<img />
28+
<div>
29+
<h3></h3>
30+
<div class="info">
31+
<p><slot name="email" /></p>
32+
<p><slot name="phone" /></p>
33+
</div>
34+
<button id="toggle-info">Hide Info</button>
35+
</div>
36+
</div>
37+
`;
38+
39+
class UserCard extends HTMLElement {
40+
constructor() {
41+
super();
42+
43+
this.showInfo = true;
44+
45+
this.attachShadow({ mode: "open" });
46+
this.shadowRoot.appendChild(template.content.cloneNode(true));
47+
this.shadowRoot.querySelector("h3").innerText = this.getAttribute("name");
48+
this.shadowRoot.querySelector("img").src = this.getAttribute("avatar");
49+
// this.innerHTML = `<style>h3 {color: coral}</style><h3>${this.getAttribute(
50+
// "name"
51+
// )}</h3>`;
52+
}
53+
54+
toggleInfo() {
55+
this.showInfo = !this.showInfo;
56+
const info = this.shadowRoot.querySelector(".info");
57+
const toggleButton = this.shadowRoot.querySelector("#toggle-info");
58+
if (this.showInfo) {
59+
info.style.display = "block";
60+
toggleButton.innerText = "Hide Info";
61+
} else {
62+
info.style.display = "none";
63+
toggleButton.innerText = "Show Info";
64+
}
65+
}
66+
67+
connectedCallback() {
68+
this.shadowRoot
69+
.querySelector("#toggle-info")
70+
.addEventListener("click", () => this.toggleInfo());
71+
}
72+
73+
disconnectedCallback() {
74+
this.shadowRoot.querySelector("#toggle-info").removeEventListener();
75+
}
76+
}
77+
78+
window.customElements.define("user-card", UserCard);

0 commit comments

Comments
 (0)