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

Preserve the state of conditional reveals when navigating 'back' in the browser #1842

Merged
merged 5 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
We’ve made fixes to GOV.UK Frontend in the following pull requests:

- [#1838: Correctly camel case SVG attributes in the header and footer](https://github.com/alphagov/govuk-frontend/pull/1838)
- [#1842: Preserve the state of conditional reveals when navigating 'back' in the browser](https://github.com/alphagov/govuk-frontend/pull/1842)

## 3.7.0 (Feature release)

Expand Down
26 changes: 23 additions & 3 deletions src/govuk/components/checkboxes/checkboxes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
// addEventListener, event.target normalization and DOMContentLoaded
import '../../vendor/polyfills/Event'
import '../../vendor/polyfills/Element/prototype/classList'
import { nodeListForEach } from '../../common'

Expand Down Expand Up @@ -29,13 +30,32 @@ Checkboxes.prototype.init = function () {
// If we have content that is controlled, set attributes.
$input.setAttribute('aria-controls', controls)
$input.removeAttribute('data-aria-controls')
this.setAttributes($input)
}.bind(this))
})

if (document.readyState === 'loading') {
window.addEventListener('DOMContentLoaded', this.syncState.bind(this))
} else {
this.syncState()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I like how these cover asynchronous loading too 👏


// When the page is restored after navigating 'back' in some browsers the
// state of form controls is not restored until *after* the DOMContentLoaded
// event is fired, so we need to sync after the pageshow event is fired as
// well.
//
// (Older browsers don't have a pageshow event, so we do both.)
window.addEventListener('pageshow', this.syncState.bind(this))

// Handle events
$module.addEventListener('click', this.handleClick.bind(this))
}

Checkboxes.prototype.syncState = function () {
nodeListForEach(this.$inputs, function ($input) {
Copy link
Contributor

@NickColley NickColley Jun 24, 2020

Choose a reason for hiding this comment

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

I can't unsubscribe from all notifications so sometimes I see interesting things, excellent write up, super interesting...

You might be able to simplify this to something like:

nodeListForEach(this.$inputs, this.setAttributes($input).bind(this))

Maybe not though, see ya later

🏃‍♂️ 💨

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @NickColley! 👋

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it'd have to be:

nodeListForEach(this.$inputs, this.setAttributes.bind(this))

which is possibly a little less clear – any thoughts @hannalaakso?

this.setAttributes($input)
}.bind(this))
}

Checkboxes.prototype.setAttributes = function ($input) {
var inputIsChecked = $input.checked
$input.setAttribute('aria-expanded', inputIsChecked)
Expand Down
29 changes: 25 additions & 4 deletions src/govuk/components/radios/radios.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
// addEventListener, event.target normalization and DOMContentLoaded
import '../../vendor/polyfills/Event'
import '../../vendor/polyfills/Element/prototype/classList'
import { nodeListForEach } from '../../common'

function Radios ($module) {
this.$module = $module
this.$inputs = $module.querySelectorAll('input[type="radio"]')
}

Radios.prototype.init = function () {
var $module = this.$module
var $inputs = $module.querySelectorAll('input[type="radio"]')
var $inputs = this.$inputs

/**
* Loop over all items with [data-controls]
Expand All @@ -28,13 +30,32 @@ Radios.prototype.init = function () {
// If we have content that is controlled, set attributes.
$input.setAttribute('aria-controls', controls)
$input.removeAttribute('data-aria-controls')
this.setAttributes($input)
}.bind(this))
})

if (document.readyState === 'loading') {
window.addEventListener('DOMContentLoaded', this.syncState.bind(this))
} else {
this.syncState()
}

// When the page is restored after navigating 'back' in some browsers the
// state of form controls is not restored until *after* the DOMContentLoaded
// event is fired, so we need to sync after the pageshow event is fired as
// well.
//
// (Older browsers don't have a pageshow event, so we do both.)
window.addEventListener('pageshow', this.syncState.bind(this))

// Handle events
$module.addEventListener('click', this.handleClick.bind(this))
}

Radios.prototype.syncState = function () {
nodeListForEach(this.$inputs, function ($input) {
this.setAttributes($input)
}.bind(this))
}

Radios.prototype.setAttributes = function ($input) {
var $content = document.querySelector('#' + $input.getAttribute('aria-controls'))

Expand Down