-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun-input.js
124 lines (108 loc) · 3.47 KB
/
fun-input.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* The template that is used for the shadow root for every copy of your element,
* which houses the styles and layout for the element.
*/
const template = document.createElement("template");
template.innerHTML = `
<style>
:host {
display: inline-block;
}
input {
padding: 0.5em;
border-radius: 3px;
font-size: 0.8em;
border: none;
width: calc(100% - 1em) ;
}
input:focus {
outline: none;
}
div {
height: 2px;
width: 0px;
background-color: var( --fun-input-color ,var(--primary-color, #673AB7));
margin: 0px auto;
transition: ease width 0.3s;
z-index: 1;
}
input:focus + div {
width: 100%;
}
</style>
`;
/**
* This is the class that controls each instance of your custom element.
*/
class FunInput extends HTMLElement {
/**
* Part of the custom element spec. Returns an array of strings that are
* the names of attributes that this element observes/listens to.
*
* @returns {Array} an array of strings, each of which representing an
* attribute.
*/
static get observedAttributes() {
return ['type'];
};
constructor() {
super();
// create shadow root for any children context
this.attachShadow({mode: "open"});
this.shadowRoot.appendChild(template.content.cloneNode(true));
// add any initial variables here
}
/**
* Part of the custom element spec. Called after your element is attached to
* the DOM. Do anything related to the element or its children here in most
* cases.
*/
connectedCallback() {
let attributesToWatch = [
'type',
'placeholder',
'name',
'value'
];
this.input = document.createElement('input');
this.setAttributes(this.input, attributesToWatch);
this.shadowRoot.appendChild(this.input);
this.shadowRoot.appendChild(document.createElement('div'))
this.input.addEventListener('change', () => {
this.value = this.input.value;
this.setAttribute('value', this.input.value);
});
}
/**
* Part of the custom element spec. Called after your element is remove from
* the DOM. Disconnect any listeners or anything else here.
*/
disconnectedCallback() {
}
/**
* Part of the custom element spec. Called when one of the observed
* attributes changes, either via setAttribute() or with the attribute being
* manually set in the HTML.
*
* @param {String} name the name of the attribute that changed
* @param {Mixed} oldValue the previous value of the attribute
* @param {Mixed} newValue the new value of the attribute
*/
attributeChangedCallback(name, oldValue, newValue) {
// respond to a changed attribute here
}
/**
* Loop through attray of attributes and set them to an element
* from teh custom element
* @param {HTMLElement} element
* @param {string[]} attributes
*/
setAttributes(element, attributes) {
for(let i = 0; i < attributes.length; i++) {
if(this.hasAttribute(attributes[i])) {
element.setAttribute(attributes[i], this.getAttribute(attributes[i]));
}
}
}
}
customElements.define("fun-input", FunInput);