Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit this page: Add attached keyboard help text #3268

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/govuk/components/exit-this-page/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

@include govuk-exports("govuk/component/exit-this-page") {
.govuk-exit-this-page {
@include govuk-responsive-padding(8, "bottom");
z-index: 1000;
top: 0;
left: 0;
width: 100%;
querkmachine marked this conversation as resolved.
Show resolved Hide resolved
@supports (position: sticky) {
@include govuk-responsive-padding(8, "bottom");
position: sticky;
}

@include govuk-media-query($from: tablet) {
display: inline-block;
right: 0;
left: auto;
width: auto;
float: right;
}

@supports (position: sticky) {
position: sticky;
}
}

.govuk-exit-this-page__button {
width: 100%;
margin-bottom: 0;
}

Expand Down Expand Up @@ -66,6 +67,30 @@
}
}

.govuk-exit-this-page__help {
@include govuk-responsive-padding(2);
@include govuk-font($size: 16);
display: none;
border: 1px solid $govuk-border-colour;
border-top: none;
background-color: govuk-colour("white");
text-align: center;

kbd {
@include govuk-font($size: 16);
display: inline-block;
querkmachine marked this conversation as resolved.
Show resolved Hide resolved
padding: 0 govuk-spacing(1);
border: 1px solid $govuk-border-colour;
border-radius: 4px;
background-color: govuk-colour("white");
box-shadow: 0 1px 0 govuk-colour("white"), 0 2px 0 $govuk-border-colour;
}

@include govuk-media-query($from: tablet) {
display: block;
}
}

@media only print {
.govuk-exit-this-page {
display: none;
Expand Down
75 changes: 70 additions & 5 deletions src/govuk/components/exit-this-page/exit-this-page.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */

import { nodeListForEach } from '../../common.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { nodeListForEach, mergeConfigs, extractConfigByNamespace } from '../../common.mjs'
import { I18n } from '../../i18n.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'
import '../../vendor/polyfills/Element/prototype/dataset.mjs'
import '../../vendor/polyfills/Function/prototype/bind.mjs'

/**
* @constant
* @type {ExitThisPageTranslations}
* @see Default value for {@link ExitThisPageConfig.i18n}
* @default
*/
var EXIT_THIS_PAGE_TRANSLATIONS = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general comment that even if we end up putting this work down for now, we should retain this as it's good for getting the final code up to scratch.

keypressProgress: 'Exit this Page key press %{press} of 3',
keypressComplete: 'Exit this page activated',
helpText: 'press <kbd>Shift</kbd> 3 times<span class="govuk-visually-hidden"> to exit the page</span>'
querkmachine marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* JavaScript functionality for the Exit this Page component
*
* @class
* @param {HTMLElement} $module - HTML element that wraps the EtP button
* @param {ExitThisPageConfig} [config] - Exit this Page config
*/
function ExitThisPage ($module) {
function ExitThisPage ($module, config) {
this.$module = $module

var defaultConfig = {
i18n: EXIT_THIS_PAGE_TRANSLATIONS
}

this.config = mergeConfigs(
defaultConfig,
config || {},
normaliseDataset($module.dataset)
)

this.i18n = new I18n(extractConfigByNamespace(this.config, 'i18n'))

this.$button = $module.querySelector('.govuk-exit-this-page__button')
this.$skiplinkButton = document.querySelector('.govuk-js-exit-this-page-skiplink')
this.$updateSpan = null
this.$helpTextSpan = null
this.$indicatorContainer = null
this.$overlay = null
this.escCounter = 0
Expand All @@ -29,12 +58,23 @@ function ExitThisPage ($module) {
*/
ExitThisPage.prototype.initUpdateSpan = function () {
this.$updateSpan = document.createElement('span')
this.$updateSpan.className = 'govuk-visually-hidden'
this.$updateSpan.setAttribute('aria-live', 'polite')
this.$updateSpan.setAttribute('class', 'govuk-visually-hidden')

this.$button.appendChild(this.$updateSpan)
}

/**
* Create the <span> and text advising the user of the keyboard shortcut.
*/
ExitThisPage.prototype.initHelpText = function () {
this.$helpTextSpan = document.createElement('div')
this.$helpTextSpan.className = 'govuk-exit-this-page__help'
this.$helpTextSpan.innerHTML = this.i18n.t('helpText')

this.$module.appendChild(this.$helpTextSpan)
}

/**
* Create button click handlers.
*/
Expand Down Expand Up @@ -146,10 +186,12 @@ ExitThisPage.prototype.handleEscKeypress = function (e) {

if (this.escCounter >= 3) {
this.escCounter = 0
this.$updateSpan.innerText = 'Exit this page activated'
this.$updateSpan.innerText = this.i18n.t('keypressComplete')
this.exitPage()
} else {
this.$updateSpan.innerText = 'Exit this Page key press ' + this.escCounter + ' of 3'
this.$updateSpan.innerText = this.i18n.t('keypressProgress', {
press: this.escCounter
})
}

this.setEscTimer()
Expand Down Expand Up @@ -211,6 +253,7 @@ ExitThisPage.prototype.syncOverlayVisibility = function () {
*/
ExitThisPage.prototype.init = function () {
this.buildIndicator()
this.initHelpText()
this.initUpdateSpan()
this.initButtonClickHandler()

Expand All @@ -231,3 +274,25 @@ ExitThisPage.prototype.init = function () {
}

export default ExitThisPage

/**
* Exit this Page config
*
* @typedef {object} ExitThisPageConfig
* @property {ExitThisPageTranslations} [i18n = EXIT_THIS_PAGE_TRANSLATIONS] - See constant {@link EXIT_THIS_PAGE_TRANSLATIONS}
*/

/**
* Exit this Page translations
*
* @typedef {object} ExitThisPageTranslations
*
* Messages used by the component programatically inserted text, including help
* text and screen reader announcements.
* @property {string} [helpText] - The text content for the keyboard help text
* displayed beneath the button.
* @property {string} [keypressProgress] - The text announced by screen readers
* when the user activates each step of the keyboard shortcut.
* @property {string} [keypressComplete] - The text announced by screen readers
* when the user completes the keyboard shortcut.
*/