Sha256: f6522f8a73cc5231e78e530c383207196e73a7d967480644c0391f7a85367cf4

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

//= require ../vendor/polyfills-govuk-frontend-v4/Element/prototype/classList.js
/*
  Toggle the class 'focus' on input boxes on element focus/blur
  Used by the search component but generic enough for reuse
*/
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
  function GemToggleInputClassOnFocus ($module) {
    this.$module = $module
  }

  GemToggleInputClassOnFocus.prototype.init = function () {
    this.toggleTarget = this.$module.querySelector('.js-class-toggle')
    this.$module.addFocusClass = this.addFocusClass.bind(this)
    this.$module.removeFocusClassFromEmptyInput = this.removeFocusClassFromEmptyInput.bind(this)

    if (!this.inputIsEmpty()) {
      this.addFocusClass()
    }

    this.toggleTarget.addEventListener('focus', this.$module.addFocusClass)
    this.toggleTarget.addEventListener('blur', this.$module.removeFocusClassFromEmptyInput)
  }

  GemToggleInputClassOnFocus.prototype.inputIsEmpty = function () {
    return this.toggleTarget.value === ''
  }

  GemToggleInputClassOnFocus.prototype.addFocusClass = function () {
    this.toggleTarget.classList.add('focus')
  }

  GemToggleInputClassOnFocus.prototype.removeFocusClassFromEmptyInput = function () {
    if (this.inputIsEmpty()) {
      this.toggleTarget.classList.remove('focus')
    }
  }

  Modules.GemToggleInputClassOnFocus = GemToggleInputClassOnFocus
})(window.GOVUK.Modules)

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
govuk_publishing_components-41.1.2 app/assets/javascripts/govuk_publishing_components/lib/toggle-input-class-on-focus.js
govuk_publishing_components-41.1.1 app/assets/javascripts/govuk_publishing_components/lib/toggle-input-class-on-focus.js
govuk_publishing_components-41.1.0 app/assets/javascripts/govuk_publishing_components/lib/toggle-input-class-on-focus.js
govuk_publishing_components-41.0.0 app/assets/javascripts/govuk_publishing_components/lib/toggle-input-class-on-focus.js
govuk_publishing_components-40.1.0 app/assets/javascripts/govuk_publishing_components/lib/toggle-input-class-on-focus.js
govuk_publishing_components-40.0.0 app/assets/javascripts/govuk_publishing_components/lib/toggle-input-class-on-focus.js