-
Notifications
You must be signed in to change notification settings - Fork 329
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
Changes from 2 commits
6d33191
aaff8f9
44ecae6
d29b200
82c5bf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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' | ||
|
||
|
@@ -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() | ||
} | ||
|
||
// 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Maybe not though, see ya later 🏃♂️ 💨 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, @NickColley! 👋 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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 👏