-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
165 lines (136 loc) · 5.39 KB
/
index.js
File metadata and controls
165 lines (136 loc) · 5.39 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"use strict";
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
var defaultOptions = {
labelElementAnimateModifier: "floating-label__label--animate",
labelElementInlineModifier: "floating-label__label--inline",
labelElementFocusModifier: "floating-label__label--focus",
labelElementInvalidModifier: "floating-label__label--invalid",
labelElementDisabledModifier: "floating-label__label--disabled",
textboxElementBackgroundRGB: ["rgb(255, 255, 255)", "rgb(245, 245, 245)", "rgb(230, 32, 72)", "rgb(254, 245, 246)"],
};
function onMutation() {
var textboxFocus = isFocused(this.textboxEl);
if (this.textboxEl.hasAttribute("placeholder")) {
this.placeholder = this.textboxEl.getAttribute("placeholder");
}
if (!!this.placeholder && textboxFocus && !this.textboxEl.hasAttribute("placeholder")) {
// Input has focus, make sure it has placeholder
this.textboxEl.setAttribute("placeholder", this.placeholder);
} else if (!textboxFocus && this.textboxEl.hasAttribute("placeholder")) {
this.textboxEl.removeAttribute("placeholder");
}
if (isInvalid(this.textboxEl)) {
this.labelEl.classList.add(this.options.labelElementInvalidModifier);
} else {
this.labelEl.classList.remove(this.options.labelElementInvalidModifier);
}
if (isDisabled(this.textboxEl)) {
this.labelEl.classList.add(this.options.labelElementDisabledModifier);
} else {
this.labelEl.classList.remove(this.options.labelElementDisabledModifier);
}
}
function isFocused(textboxEl) {
return document.activeElement === textboxEl;
}
function hasValue(input) {
return input.value.length > 0;
}
function isDisabled(input) {
return input.hasAttribute("disabled");
}
function isInvalid(input) {
return input.hasAttribute("aria-invalid") && input.getAttribute("aria-invalid") === "true";
}
function isAutofilled(input, color) {
// check for computed background color because of Chrome autofill bug
// https://stackoverflow.com/questions/35049555/chrome-autofill-autocomplete-no-value-for-password/35783761#35783761
var bgColor = getComputedStyle(input).backgroundColor;
return Array.isArray(color) ? !color.includes(bgColor) : bgColor !== color;
}
function _onBlur() {
if (!hasValue(this.textboxEl)) {
this.labelEl.classList.add(this.options.labelElementInlineModifier);
}
this.labelEl.classList.remove(this.options.labelElementFocusModifier);
if (isInvalid(this.textboxEl)) {
this.labelEl.classList.add(this.options.labelElementInvalidModifier);
}
this.textboxEl.removeAttribute("placeholder");
}
function _onFocus() {
this.labelEl.classList.add(this.options.labelElementAnimateModifier);
this.labelEl.classList.add(this.options.labelElementFocusModifier);
this.labelEl.classList.remove(this.options.labelElementInlineModifier);
this.labelEl.classList.remove(this.options.labelElementInvalidModifier);
if (this.placeholder) {
this.textboxEl.setAttribute("placeholder", this.placeholder);
}
}
module.exports = /*#__PURE__*/ (function () {
function _class(el, userOptions) {
_classCallCheck(this, _class);
this.options = Object.assign({}, defaultOptions, userOptions);
this._observer = new MutationObserver(onMutation.bind(this));
this.rootEl = el;
this.labelEl = this.rootEl.querySelector("label");
this.textboxEl = this.rootEl.querySelector("input,textarea,select");
this._onBlurListener = _onBlur.bind(this);
this._onFocusListener = _onFocus.bind(this);
this.textboxEl.addEventListener("blur", this._onBlurListener);
this.textboxEl.addEventListener("focus", this._onFocusListener);
if (!hasValue(this.textboxEl) && !isAutofilled(this.textboxEl, this.options.textboxElementBackgroundRGB)) {
this.labelEl.classList.add(this.options.labelElementInlineModifier);
}
if (isFocused(this.textboxEl)) {
this.labelEl.classList.add(this.options.labelElementFocusModifier);
}
onMutation.call(this);
this._observer.observe(this.textboxEl, {
childList: false,
subtree: false,
attributeFilter: ["disabled", "aria-invalid", "placeholder", "value"],
attributes: true,
});
}
_createClass(_class, [
{
key: "destroy",
value: function destroy() {
this._observer.disconnect();
},
},
{
key: "refresh",
value: function refresh() {
if (hasValue(this.textboxEl) || isAutofilled(this.textboxEl, this.options.textboxElementBackgroundRGB)) {
this.labelEl.classList.remove(this.options.labelElementInlineModifier);
} else {
this.labelEl.classList.add(this.options.labelElementInlineModifier);
}
if (isFocused(this.textboxEl)) {
this.labelEl.classList.add(this.options.labelElementFocusModifier);
}
},
},
]);
return _class;
})();