makeup-floating-label
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -7,9 +7,2 @@ "use strict"; | ||
exports.default = void 0; | ||
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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } | ||
var defaultOptions = { | ||
@@ -22,5 +15,6 @@ labelElementAnimateModifier: 'floating-label__label--animate', | ||
textboxElementBackgroundRGB: ['rgb(255, 255, 255)', 'rgb(247, 247, 247)', 'rgb(245, 245, 245)', 'rgb(230, 32, 72)', 'rgb(254, 245, 246)'] | ||
}; // Common getter. Will get either first option text (for select), | ||
}; | ||
// Common getter. Will get either first option text (for select), | ||
// or placeholder for textbox | ||
function getPlaceHolder(formControlEl) { | ||
@@ -33,6 +27,6 @@ if (isSelect(formControlEl)) { | ||
} | ||
} // Common setter. Will set either first option text (for select), | ||
} | ||
// Common setter. Will set either first option text (for select), | ||
// or placeholder for textbox | ||
function setPlaceholder(formControlEl, value) { | ||
@@ -43,3 +37,2 @@ if (isSelect(formControlEl)) { | ||
formControlEl.querySelector('option').text = value; | ||
if (!value && beforeWidth > formControlEl.offsetWidth) { | ||
@@ -53,9 +46,8 @@ formControlEl.style['min-width'] = "".concat(beforeWidth, "px"); | ||
} | ||
} // Called on mutatation. Sets placeholder for current state (focused or unfocused) | ||
} | ||
// Called on mutatation. Sets placeholder for current state (focused or unfocused) | ||
function checkForPlaceholder(formControlEl) { | ||
if (isSelect(formControlEl)) { | ||
var firstOption = formControlEl.querySelector('option'); | ||
if (!!firstOption.value) { | ||
@@ -65,9 +57,6 @@ // If first option has a value then it is not a placeholder | ||
} | ||
return !!firstOption.text; | ||
} | ||
return formControlEl.hasAttribute('placeholder'); | ||
} | ||
function onMutation() { | ||
@@ -77,3 +66,2 @@ var textboxFocus = isFocused(this.formControlEl); | ||
var placeholderCheck = checkForPlaceholder(this.formControlEl, this.placeholder); | ||
if (!!this.placeholder && textboxFocus && !placeholderCheck) { | ||
@@ -85,3 +73,2 @@ // Input has focus, make sure it has "placeholder" option | ||
} | ||
if (isInvalid(this.formControlEl)) { | ||
@@ -92,3 +79,2 @@ this.labelEl.classList.add(this.options.labelElementInvalidModifier); | ||
} | ||
if (isDisabled(this.formControlEl)) { | ||
@@ -100,23 +86,17 @@ this.labelEl.classList.add(this.options.labelElementDisabledModifier); | ||
} | ||
function isFocused(formControlEl) { | ||
return document.activeElement === formControlEl; | ||
} | ||
function isSelect(formControlEl) { | ||
return formControlEl.tagName === 'SELECT'; | ||
} | ||
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) { | ||
@@ -129,6 +109,4 @@ // check for computed background color because of Chrome autofill bug | ||
} | ||
return false; | ||
} | ||
function _onBlur() { | ||
@@ -138,12 +116,8 @@ if (!hasValue(this.formControlEl)) { | ||
} | ||
this.labelEl.classList.remove(this.options.labelElementFocusModifier); | ||
if (isInvalid(this.formControlEl)) { | ||
this.labelEl.classList.add(this.options.labelElementInvalidModifier); | ||
} | ||
setPlaceholder(this.formControlEl, ''); | ||
} | ||
function _onFocus() { | ||
@@ -154,3 +128,2 @@ this.labelEl.classList.add(this.options.labelElementAnimateModifier); | ||
this.labelEl.classList.remove(this.options.labelElementInvalidModifier); | ||
if (this.placeholder) { | ||
@@ -160,7 +133,4 @@ setPlaceholder(this.formControlEl, this.placeholder); | ||
} | ||
var _default = /*#__PURE__*/function () { | ||
function _default(el, userOptions) { | ||
_classCallCheck(this, _default); | ||
class _default { | ||
constructor(el, userOptions) { | ||
this.options = Object.assign({}, defaultOptions, userOptions); | ||
@@ -175,3 +145,2 @@ this._observer = new MutationObserver(onMutation.bind(this)); | ||
this.formControlEl.addEventListener('focus', this._onFocusListener); | ||
if (!hasValue(this.formControlEl) && !isAutofilled(this.formControlEl, this.options.textboxElementBackgroundRGB)) { | ||
@@ -182,3 +151,2 @@ this.labelEl.classList.add(this.options.labelElementInlineModifier); | ||
} | ||
if (isFocused(this.formControlEl)) { | ||
@@ -188,5 +156,3 @@ this.labelEl.classList.add(this.options.labelElementFocusModifier); | ||
} | ||
onMutation.call(this); | ||
this._observer.observe(this.formControlEl, { | ||
@@ -199,27 +165,17 @@ childList: isSelect(this.formControlEl), | ||
} | ||
_createClass(_default, [{ | ||
key: "destroy", | ||
value: function destroy() { | ||
this._observer.disconnect(); | ||
destroy() { | ||
this._observer.disconnect(); | ||
} | ||
refresh() { | ||
if (hasValue(this.formControlEl) || isSelect(this.formControlEl) || isAutofilled(this.formControlEl, this.options.textboxElementBackgroundRGB)) { | ||
this.labelEl.classList.remove(this.options.labelElementInlineModifier); | ||
} else { | ||
this.labelEl.classList.add(this.options.labelElementInlineModifier); | ||
} | ||
}, { | ||
key: "refresh", | ||
value: function refresh() { | ||
if (hasValue(this.formControlEl) || isSelect(this.formControlEl) || isAutofilled(this.formControlEl, this.options.textboxElementBackgroundRGB)) { | ||
this.labelEl.classList.remove(this.options.labelElementInlineModifier); | ||
} else { | ||
this.labelEl.classList.add(this.options.labelElementInlineModifier); | ||
} | ||
if (isFocused(this.formControlEl)) { | ||
this.labelEl.classList.add(this.options.labelElementFocusModifier); | ||
this.labelEl.classList.remove(this.options.labelElementInlineModifier); | ||
} | ||
if (isFocused(this.formControlEl)) { | ||
this.labelEl.classList.add(this.options.labelElementFocusModifier); | ||
this.labelEl.classList.remove(this.options.labelElementInlineModifier); | ||
} | ||
}]); | ||
return _default; | ||
}(); | ||
} | ||
} | ||
exports.default = _default; |
{ | ||
"name": "makeup-floating-label", | ||
"description": "Module for creating an accessible, progressively enhanced floating label", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"main": "./dist/cjs/index.js", | ||
@@ -6,0 +6,0 @@ "module": "./dist/mjs/index.js", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7
0
16802
322