{"version":3,"file":"button.mjs","sources":["../../../../src/govuk/components/button/button.mjs"],"sourcesContent":["import { ConfigurableComponent } from '../../common/configuration.mjs'\n\nconst DEBOUNCE_TIMEOUT_IN_SECONDS = 1\n\n/**\n * JavaScript enhancements for the Button component\n *\n * @preserve\n * @augments ConfigurableComponent<ButtonConfig>\n */\nexport class Button extends ConfigurableComponent {\n  /**\n   * @private\n   * @type {number | null}\n   */\n  debounceFormSubmitTimer = null\n\n  /**\n   * @param {Element | null} $root - HTML element to use for button\n   * @param {ButtonConfig} [config] - Button config\n   */\n  constructor($root, config = {}) {\n    super($root, config)\n\n    this.$root.addEventListener('keydown', (event) => this.handleKeyDown(event))\n    this.$root.addEventListener('click', (event) => this.debounce(event))\n  }\n\n  /**\n   * Trigger a click event when the space key is pressed\n   *\n   * Some screen readers tell users they can use the space bar to activate\n   * things with the 'button' role, so we need to match the functionality of\n   * native HTML buttons.\n   *\n   * See https://github.com/alphagov/govuk_elements/pull/272#issuecomment-233028270\n   *\n   * @private\n   * @param {KeyboardEvent} event - Keydown event\n   */\n  handleKeyDown(event) {\n    const $target = event.target\n\n    // Handle space bar only\n    if (event.key !== ' ') {\n      return\n    }\n\n    // Handle elements with [role=\"button\"] only\n    if (\n      $target instanceof HTMLElement &&\n      $target.getAttribute('role') === 'button'\n    ) {\n      event.preventDefault() // prevent the page from scrolling\n      $target.click()\n    }\n  }\n\n  /**\n   * Debounce double-clicks\n   *\n   * If the click quickly succeeds a previous click then nothing will happen.\n   * This stops people accidentally causing multiple form submissions by double\n   * clicking buttons.\n   *\n   * @private\n   * @param {MouseEvent} event - Mouse click event\n   * @returns {undefined | false} Returns undefined, or false when debounced\n   */\n  debounce(event) {\n    // Check the button that was clicked has preventDoubleClick enabled\n    if (!this.config.preventDoubleClick) {\n      return\n    }\n\n    // If the timer is still running, prevent the click from submitting the form\n    if (this.debounceFormSubmitTimer) {\n      event.preventDefault()\n      return false\n    }\n\n    this.debounceFormSubmitTimer = window.setTimeout(() => {\n      this.debounceFormSubmitTimer = null\n    }, DEBOUNCE_TIMEOUT_IN_SECONDS * 1000)\n  }\n\n  /**\n   * Name for the component used when initialising using data-module attributes.\n   */\n  static moduleName = 'govuk-button'\n\n  /**\n   * Button default config\n   *\n   * @see {@link ButtonConfig}\n   * @constant\n   * @type {ButtonConfig}\n   */\n  static defaults = Object.freeze({\n    preventDoubleClick: false\n  })\n\n  /**\n   * Button config schema\n   *\n   * @constant\n   * @satisfies {Schema<ButtonConfig>}\n   */\n  static schema = Object.freeze({\n    properties: {\n      preventDoubleClick: { type: 'boolean' }\n    }\n  })\n}\n\n/**\n * Button config\n *\n * @typedef {object} ButtonConfig\n * @property {boolean} [preventDoubleClick=false] - Prevent accidental double\n *   clicks on submit buttons from submitting forms multiple times.\n */\n\n/**\n * @import { Schema } from '../../common/configuration.mjs'\n */\n"],"names":["DEBOUNCE_TIMEOUT_IN_SECONDS","Button","ConfigurableComponent","constructor","$root","config","debounceFormSubmitTimer","addEventListener","event","handleKeyDown","debounce","$target","target","key","HTMLElement","getAttribute","preventDefault","click","preventDoubleClick","window","setTimeout","moduleName","defaults","Object","freeze","schema","properties","type"],"mappings":";;AAEA,MAAMA,2BAA2B,GAAG,CAAC;;AAErC;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,MAAM,SAASC,qBAAqB,CAAC;AAOhD;AACF;AACA;AACA;AACEC,EAAAA,WAAWA,CAACC,KAAK,EAAEC,MAAM,GAAG,EAAE,EAAE;AAC9B,IAAA,KAAK,CAACD,KAAK,EAAEC,MAAM,CAAC;IAAA,IAAA,CAPtBC,uBAAuB,GAAG,IAAI;AAS5B,IAAA,IAAI,CAACF,KAAK,CAACG,gBAAgB,CAAC,SAAS,EAAGC,KAAK,IAAK,IAAI,CAACC,aAAa,CAACD,KAAK,CAAC,CAAC;AAC5E,IAAA,IAAI,CAACJ,KAAK,CAACG,gBAAgB,CAAC,OAAO,EAAGC,KAAK,IAAK,IAAI,CAACE,QAAQ,CAACF,KAAK,CAAC,C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