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

[Spike] Immediately hide the page when the Exit this Page button is interacted with #2949

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/govuk/components/hide-this-page/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@
display: none;
}
}

.govuk-hide-this-page__overlay {
position: fixed;
z-index: 9999;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: govuk-colour("white");
}
}
32 changes: 30 additions & 2 deletions src/govuk/components/hide-this-page/hide-this-page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function HideThisPage ($module) {
this.$button = $module.querySelector('.govuk-hide-this-page__button')
this.$updateSpan = null
this.$indicatorContainer = null
this.$overlay = null
this.escCounter = 0
this.escTimerActive = false
}
Expand Down Expand Up @@ -65,7 +66,16 @@ HideThisPage.prototype.updateIndicator = function () {
}

HideThisPage.prototype.exitPage = function (e) {
// we don't do anything with this just yet
if (typeof e !== 'undefined' && e.target) {
e.preventDefault()
}

// Blank the page
this.$overlay = document.createElement('div')
this.$overlay.className = 'govuk-hide-this-page__overlay'
document.body.appendChild(this.$overlay)

window.location.href = this.$button.href
}

HideThisPage.prototype.handleEscKeypress = function (e) {
Expand All @@ -78,7 +88,7 @@ HideThisPage.prototype.handleEscKeypress = function (e) {
if (this.escCounter >= 3) {
this.escCounter = 0
this.$updateSpan.innerText = 'Exit this page activated'
window.location.href = this.$button.href
this.exitPage()
} else {
this.$updateSpan.innerText = 'Exit this Page key press ' + this.escCounter + ' of 3'
}
Expand All @@ -100,6 +110,15 @@ HideThisPage.prototype.setEscTimer = function () {
}
}

HideThisPage.prototype.syncOverlayVisibility = function () {
// If there is no overlay, don't do anything
if (!this.$overlay) { return }

// If there is, remove the element and references to it
this.$overlay.remove()
this.$overlay = null
}

HideThisPage.prototype.init = function () {
this.buildIndicator()
this.initUpdateSpan()
Expand All @@ -110,6 +129,15 @@ HideThisPage.prototype.init = function () {
document.addEventListener('keyup', this.handleEscKeypress.bind(this), true)
document.body.dataset.govukFrontendHideThisPageEsc = true
}

// When the page is restored after navigating 'back' in some browsers the
// blank overlay remains present, rendering the page unusable. Here, we check
// to see if it's present on page (re)load, and remove it if so.
if ('onpageshow' in window) {
window.addEventListener('pageshow', this.syncOverlayVisibility.bind(this))
} else {
window.addEventListener('DOMContentLoaded', this.syncOverlayVisibility.bind(this))
}
}

export default HideThisPage