-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom-button.js
More file actions
58 lines (52 loc) · 2.02 KB
/
custom-button.js
File metadata and controls
58 lines (52 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// we create a style tag containing the CSS specific to our component
// cloning content from a <template> tag is faster than using .innerHTML() on the main element
const style = document.createElement("template");
style.innerHTML = `
<style>
/*:host references the shadowRoot of our custom element*/
:host > button{
background:#444857;
color:white;
font-size:1rem;
padding: 10px 16px;
line-height:1.2;
border-radius:4px;
border:3px solid transparent;
text-align:center;
user-select:none;
display:inline-block;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
:host > button:hover{
cursor:pointer;
background: #5a6074;
}
</style>
`;
//Here we put the HTML of our element
//inside the button we just add we put a special <slot> element
//it will contain what's between the <custom-button></custom-button>
const markup = document.createElement("template");
markup.innerHTML = `
<button>
<slot></slot>
</button>
`;
//the first half of the bare minimum is to create a class representing our element
//which inerhit from the HTMLElement class
class CustomButton extends HTMLElement {
//you have to specify a constructor method
constructor() {
//you have to call the super() function which calls the contructor of the parent
super();
//to our customElement referenced by the keyword "this" we attach a shadowRoot
//a shadow root is a scoped space which means that what's defined in there isn't definde outside
//also, interestingly, the outside CSS dosen't affect the CSS inside our shadow root
this.attachShadow({ mode: "open" });
//here we inject a copy of the CSS and HTML template tag present at the top of our file
this.shadowRoot.appendChild(markup.content.cloneNode(true));
this.shadowRoot.appendChild(style.content.cloneNode(true));
}
}
//the second half of the bare minimum is to add the definition of our element to the custom elements known by the browser
window.customElements.define("custom-button", CustomButton);