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

Fix flash of unstyled content with conditional reveals #885

Merged
merged 4 commits into from
Jul 24, 2018

Conversation

NickColley
Copy link
Contributor

@NickColley NickColley commented Jul 11, 2018

If the conditional reveal JavaScript is slow to execute it can result in showing the user their contents briefly which can be jarring.

See the original issue for an example: #787

Fixes #787

Tested in:

  • IE 8 ✅
  • IE 9 ✅
  • IE 10 ✅
  • IE 11 ✅
  • Edge 17 ✅
  • Firefox 61 ✅
  • Chrome 67 ✅
  • Safari 11.1 ✅

Assistive technologies:
I've tested this in VoiceOver and since display: none has the same behaviour as aria-hidden=true (styled with display: none), I've decided not to do a full test of assistive technologies.

Pages to review:

https://trello.com/c/o9krC0SR/1195-conditional-reveals-result-in-a-flash-of-unstyled-content-as-the-page-loads

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 11, 2018 11:02 Inactive
@@ -61,7 +61,7 @@
}) | indent(6) | trim }}
</div>
{% if item.conditional %}
<div class="govuk-radios__conditional" id="{{ conditionalId }}">
<div class="govuk-radios__conditional{% if not item.checked %} is-hidden{% endif %}" id="{{ conditionalId }}">
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For users that do not use macros, how do we document this?

Copy link
Member

Choose a reason for hiding this comment

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

Could we handle toggling this in JS somehow? If we add .js-hidden to all conditionals markup by default, no FOUC. When JS loads we check the preceeding .govuk-checkboxes__item sibling. If the input inside of it is checked, remove .js-hiddenfrom the conditional.

Copy link
Member

Choose a reason for hiding this comment

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

Actually I realised that this still wouldn't work because we need to we do .js-enabled .js-hidden in CSS so that style doesn't kick in until JS has loaded 😞 I'll keep on thinking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's currently how the JavaScript logic works. The logic in the template is for the initial render.

I think this might not be a big issue since the example we use in the Design System website has a pre-checked input which means you'd see it in the generated HTML.

@NickColley NickColley force-pushed the fix-fouc-with-conditional-reveals branch from 7a3f0e2 to 6e67929 Compare July 11, 2018 11:36
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 11, 2018 11:37 Inactive
@NickColley NickColley changed the title WIP: Fix fouc with conditional reveals Fix flash of unstyled content with conditional reveals Jul 11, 2018
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 11, 2018 11:40 Inactive
@@ -139,7 +139,7 @@
padding-left: $conditional-padding-left;
border-left: $conditional-border-width solid $govuk-border-colour;

&[aria-hidden="true"] {
.js-enabled &.is-hidden {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this class be called 'js-hidden'?

Could this be a common utility class?

#815

Copy link
Member

Choose a reason for hiding this comment

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

I think a common utility class would make sense.

(On a separate note, I think we should probably prefix our JS classes. But let's discuss that another time, I don't want to derail this PR 😁 )

Copy link
Contributor Author

@NickColley NickColley Jul 13, 2018

Choose a reason for hiding this comment

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

You thinking govuk-js-hidden?

It's OK to raise things like this as a blocker since classes are part of our public API, so you're not derailing things.

Copy link
Member

Choose a reason for hiding this comment

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

Yes I was thinking of govuk-js-hidden. I was even thinking of js-hidden-govuk as it's handy to recognise JS classes at a glance. But it just sounds too weird... 😅

But js-hidden just seems like it would very easily conflict with something else.

Copy link
Contributor Author

@NickColley NickColley Jul 13, 2018

Choose a reason for hiding this comment

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

Agreed - makes sense, looks like we already use js-hidden, so maybe raise an issue for us to think about it after all 😬

https://github.com/alphagov/govuk-frontend/search?q=js-hidden&unscoped_q=js-hidden

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'm going to change the 'is-hidden' to 'js-hidden' to be consistent with tabs.

@NickColley NickColley force-pushed the fix-fouc-with-conditional-reveals branch from feb5ddd to 4a3cb14 Compare July 13, 2018 15:44
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 13, 2018 15:44 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 13, 2018 16:09 Inactive
}
}

}

.js-hidden {
Copy link
Contributor Author

@NickColley NickColley Jul 13, 2018

Choose a reason for hiding this comment

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

good news is the tests found this selector being defined globally

i imagine I should fix this behaviour in a different PR first...?

Copy link
Contributor Author

@NickColley NickColley Jul 13, 2018

Choose a reason for hiding this comment

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

Makes me think having this class used with slightly different behaviours is indeed dangerous.

So we should either

  1. Continue doing js-hidden for specific components but namespacing with their component names: govuk-tabs-js-hidden or .govuk-tabs .js-hidden?
  2. Pull js-hidden out into a utility class that is used by tabs and conditional reveals

I feel 2. is probably the best way, since others can use this class too.

js-hidden is something from the old world too, so it'll always be a 'helper' that's defined globally in gov.uk template.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed up some scrappy working to see what that might look like, will do something clean next week.

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 13, 2018 16:24 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 13, 2018 16:25 Inactive
@NickColley NickColley changed the title Fix flash of unstyled content with conditional reveals [Do not merge] Fix flash of unstyled content with conditional reveals Jul 13, 2018
@NickColley NickColley added the 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) label Jul 16, 2018
@NickColley NickColley force-pushed the fix-fouc-with-conditional-reveals branch from b76f36d to 4a3cb14 Compare July 17, 2018 16:27
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 17, 2018 16:27 Inactive
@NickColley NickColley force-pushed the fix-fouc-with-conditional-reveals branch from 4a3cb14 to 08d21f7 Compare July 17, 2018 16:35
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-885 July 17, 2018 16:35 Inactive
@NickColley NickColley changed the title [Do not merge] Fix flash of unstyled content with conditional reveals Fix flash of unstyled content with conditional reveals Jul 17, 2018
Uses the presence of the `js-enabled` class to know when to hide or show content.
Uses the presence of the `js-enabled` class to know when to hide or show content.
@NickColley NickColley force-pushed the fix-fouc-with-conditional-reveals branch from 08d21f7 to 745c008 Compare July 17, 2018 16:37
@NickColley NickColley changed the title Fix flash of unstyled content with conditional reveals [Do not merge] Fix flash of unstyled content with conditional reveals Jul 18, 2018
@@ -40,7 +41,7 @@ Checkboxes.prototype.setAttributes = function ($input) {
$input.setAttribute('aria-expanded', inputIsChecked)

var $content = document.querySelector('#' + $input.getAttribute('aria-controls'))
$content.setAttribute('aria-hidden', !inputIsChecked)
$content.classList.toggle('govuk-checkboxes__conditional--hidden', !inputIsChecked)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the case that CSS fails but JS succeeds, should we also include 'aria-hidden'? Or is this a bit too over the top?

Copy link
Contributor

Choose a reason for hiding this comment

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

I reckon this is fine as is.

@NickColley NickColley changed the title [Do not merge] Fix flash of unstyled content with conditional reveals Fix flash of unstyled content with conditional reveals Jul 19, 2018
Copy link
Member

@hannalaakso hannalaakso left a comment

Choose a reason for hiding this comment

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

This looks good to me 👏

@kr8n3r kr8n3r added this to the [NEXT] milestone Jul 20, 2018
@36degrees
Copy link
Contributor

@NickColley is there anything blocking this from being merged?

@NickColley
Copy link
Contributor Author

@36degrees just wanted to get more eyes on it since it's a big change

@NickColley NickColley merged commit e41a9b3 into master Jul 24, 2018
@NickColley NickColley deleted the fix-fouc-with-conditional-reveals branch July 24, 2018 08:54
Copy link
Contributor

@36degrees 36degrees left a comment

Choose a reason for hiding this comment

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

LGTM, though tests could be more focussed.

const $component = $('.govuk-checkboxes')

const $firstConditional = $component.find('.govuk-checkboxes__conditional').first()
expect($firstConditional.html()).toContain('Conditional content that should be hidden')
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be good to split this out into its own test and just have the one assertion per test.

@@ -40,7 +41,7 @@ Checkboxes.prototype.setAttributes = function ($input) {
$input.setAttribute('aria-expanded', inputIsChecked)

var $content = document.querySelector('#' + $input.getAttribute('aria-controls'))
$content.setAttribute('aria-hidden', !inputIsChecked)
$content.classList.toggle('govuk-checkboxes__conditional--hidden', !inputIsChecked)
Copy link
Contributor

Choose a reason for hiding this comment

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

I reckon this is fine as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working the way it should (including incorrect wording in documentation)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants