Skip to content

Commit 664ffa2

Browse files
adding external stylesheet shadow DOM example
1 parent 7ab43af commit 664ffa2

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed
6 KB
Binary file not shown.
4.57 KB
Loading
3.39 KB
Loading
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Pop-up info box — web components</title>
6+
<script src="main.js" defer></script>
7+
</head>
8+
<body>
9+
<h1>Pop-up info widget - web components</h1>
10+
11+
<form>
12+
<div>
13+
<label for="cvc">Enter your CVC <popup-info img="img/alt.png" data-text="Your card validation code (CVC) is an extra security feature — it is the last 3 or 4 numbers on the back of your card."></popup-info></label>
14+
<input type="text" id="cvc">
15+
</div>
16+
</form>
17+
18+
</body>
19+
</html>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Create a class for the element
2+
class PopUpInfo extends HTMLElement {
3+
constructor() {
4+
// Always call super first in constructor
5+
super();
6+
7+
// Create a shadow root
8+
const shadow = this.attachShadow({mode: 'open'});
9+
10+
// Create spans
11+
const wrapper = document.createElement('span');
12+
wrapper.setAttribute('class', 'wrapper');
13+
14+
const icon = document.createElement('span');
15+
icon.setAttribute('class', 'icon');
16+
icon.setAttribute('tabindex', 0);
17+
18+
const info = document.createElement('span');
19+
info.setAttribute('class', 'info');
20+
21+
// Take attribute content and put it inside the info span
22+
const text = this.getAttribute('data-text');
23+
info.textContent = text;
24+
25+
// Insert icon
26+
let imgUrl;
27+
if(this.hasAttribute('img')) {
28+
imgUrl = this.getAttribute('img');
29+
} else {
30+
imgUrl = 'img/default.png';
31+
}
32+
33+
const img = document.createElement('img');
34+
img.src = imgUrl;
35+
icon.appendChild(img);
36+
37+
// Apply external styles to the shadow dom
38+
const linkElem = document.createElement('link');
39+
linkElem.setAttribute('rel', 'stylesheet');
40+
linkElem.setAttribute('href', 'style.css');
41+
42+
// Attach the created elements to the shadow dom
43+
shadow.appendChild(linkElem);
44+
shadow.appendChild(wrapper);
45+
wrapper.appendChild(icon);
46+
wrapper.appendChild(info);
47+
}
48+
}
49+
50+
// Define the new element
51+
customElements.define('popup-info', PopUpInfo);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.wrapper {
2+
position: relative;
3+
}
4+
5+
.info {
6+
font-size: 0.8rem;
7+
width: 200px;
8+
display: inline-block;
9+
border: 1px solid black;
10+
padding: 10px;
11+
background: white;
12+
border-radius: 10px;
13+
opacity: 0;
14+
transition: 0.6s all;
15+
position: absolute;
16+
bottom: 20px;
17+
left: 10px;
18+
z-index: 3;
19+
}
20+
21+
img {
22+
width: 1.2rem;
23+
}
24+
25+
.icon:hover + .info, .icon:focus + .info {
26+
opacity: 1;
27+
}
6 KB
Binary file not shown.

0 commit comments

Comments
 (0)