Sha256: e6b6708be31004bdb71c6463ba881a76675b4e0a2d7d7c0ca7aa7c3724f4cf93

Contents?: true

Size: 1.37 KB

Versions: 28

Compression:

Stored size: 1.37 KB

Contents

import '../../vendor/polyfills/Event.mjs' // addEventListener

function NotificationBanner ($module) {
  this.$module = $module
}

/**
 * Initialise the component
 */
NotificationBanner.prototype.init = function () {
  var $module = this.$module
  // Check for module
  if (!$module) {
    return
  }

  this.setFocus()
}

/**
 * Focus the element
 *
 * If `role="alert"` is set, focus the element to help some assistive technologies
 * prioritise announcing it.
 *
 * You can turn off the auto-focus functionality by setting `data-disable-auto-focus="true"` in the
 * component HTML. You might wish to do this based on user research findings, or to avoid a clash
 * with another element which should be focused when the page loads.
 */
NotificationBanner.prototype.setFocus = function () {
  var $module = this.$module

  if ($module.getAttribute('data-disable-auto-focus') === 'true') {
    return
  }

  if ($module.getAttribute('role') !== 'alert') {
    return
  }

  // Set tabindex to -1 to make the element focusable with JavaScript.
  // Remove the tabindex on blur as the component doesn't need to be focusable after the page has
  // loaded.
  if (!$module.getAttribute('tabindex')) {
    $module.setAttribute('tabindex', '-1')

    $module.addEventListener('blur', function () {
      $module.removeAttribute('tabindex')
    })
  }

  $module.focus()
}

export default NotificationBanner

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
govuk_publishing_components-32.1.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-32.0.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-31.2.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-31.1.2 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-31.1.1 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-31.1.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-31.0.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.7.3 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.7.2 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.7.1 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.7.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.6.1 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.6.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.5.2 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.5.1 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.5.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.4.1 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.4.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.3.0 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs
govuk_publishing_components-30.2.1 node_modules/govuk-frontend/govuk-esm/components/notification-banner/notification-banner.mjs