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

Add checkbox mousedown event handler #2827

Closed
wants to merge 1 commit into from
Closed

Conversation

owenatgov
Copy link
Contributor

@owenatgov owenatgov commented Sep 5, 2022

Fixes #1906

Handles the visual bug mentioned in the issue using a mousedown event handler that targets checkbox labels. More details can be found in the commit message.

Thanks to @edwardhorsford for his investigation into this issue and original PR #1907

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2827 September 5, 2022 12:51 Inactive
@owenatgov owenatgov marked this pull request as ready for review September 5, 2022 12:51
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2827 September 5, 2022 12:52 Inactive
@owenatgov owenatgov requested a review from a team September 5, 2022 12:53
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2827 September 5, 2022 12:56 Inactive
querkmachine
querkmachine previously approved these changes Sep 5, 2022
Copy link
Member

@querkmachine querkmachine 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 all cushty to me.

The outcome is a little different in Safari. Previously Safari wouldn't show the yellow outline at all, as the focus state wasn't triggered by click events. With this change, it now shows the yellow outline during the mousedown/active state.

That's not a blocker as far as I'm concerned, but just in case someone raises that checkboxes are 'flashing' yellow on Safari, going forwards...

@querkmachine
Copy link
Member

Relatedly: Technically this is also an issue with radio buttons if you click the same one multiple times. That's not how radio buttons are intended to be used, however, so also not a big deal, IMO.

@owenatgov
Copy link
Contributor Author

@querkmachine Good spots. The safari note gives me pause as we're essentially recreating the issue but for safari only. We could consider it a safari bug but it's known and they've said they wont fix it. I'm gonna investigate if there's an alternative that could cover safari, including @edwardhorsford's original solution, although IMO we've reduced the scope of the problem and this feels more like a safari problem than a problem with the component now.

@edwardhorsford
Copy link
Contributor

@owenatgov PR works well for me I think. It would be nice if the same fix could be applied to radios too to make them consistent.

@36degrees
Copy link
Contributor

Although it's somewhat an edge case, because the active and focused elements can be different, you can end up with two elements that look focused.

(If you click a checkbox label, keep the mouse button held, and tab, you'll end up with both the checkbox you're clicking and the checkbox that are focused in the 'focus state')

Screenshot 2022-09-05 at 15 19 30

Generally, I'm a bit nervous about applying the focus state to something that isn't explicitly focused.

@edwardhorsford
Copy link
Contributor

@36degrees I think my original solution didn't have the same issue - as it made the parent thing get focus, and then styled based on that.

Otherwise I still think I'd go with this - as it's a visual glitch you get on all mouse-downs of labels (albeit not very noticeably) vs a much rarer behaviour of mouse-down and tab. But then looking more wrong.

When clicking the label of an associated checkbox, focus breifly moves away from the checkbox creating a small visual bug where the focus styles disappear from the checkbox. To fix this, we check for mousedowns on a given checkbox, check if the element being clicked is a label and stop default behaviour with zevent.preventDefault()`
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2827 September 8, 2022 13:23 Inactive
@owenatgov owenatgov changed the title Target label active state as well as input focus on checkbox focus styles Add checkbox mousedown event handler Sep 8, 2022
@owenatgov
Copy link
Contributor Author

An update on where we are with this.

  • Both the :active label solution and the original solution involved giving focus styles to things in instances where an element wasn't explicitly focused. Whilst this might not have immediate user consequences, it is a risky technical choice that may come back to us in unforeseen ways.
  • I've put together a new solution via js. In summary, it adds a mousedown event listener, checks if the part of the checkbox being clicked is the label and then prevents default behaviour. This seems to work well from testing. My concern is that I've been unable to work out a reliable way to write tests for it and am hoping my colleagues and beyond can help me on this. Based on that, I'm currently not comfortable with this being merged.
  • From a chat on slack with @36degrees there's a general concern that we might be doing a bit too much for an issue that has little to no impact on the user. Depending on the outcome of the testing quandary above, I may or may not recommend we formally reject this issue. That is probably going to be frustrating for those involved but we have to be tight about user impact vs risk of solutions.

Based on the above, @querkmachine I'm going to reject your review for now (thanks all the same!)

@owenatgov owenatgov dismissed querkmachine’s stale review September 8, 2022 13:36

See previous comment

@edwardhorsford
Copy link
Contributor

One thing I'll mention is where I first discovered this was because of the more obvious visual glitch you get if the checkboxes are contained in a focusable container.

@owenatgov
Copy link
Contributor Author

@edwardhorsford Correct me if I'm wrong but it looks like that instance you mentioned, specifically the wider issue around the focusable filter element mentioned in ministryofjustice/moj-design-system#39, is intended to give keyboard users a way to skip to the filters from another part of the page. In that case, we'd recommend using js to give that containing element focus programatically ($module.setAttribute('tabindex', '-1') and then add a blur event listener to remove it so that it's only focusable when it's required, as dictated by the js and as an enhancement to the keyboard experience. We've done something like this in the error summary component.

@owenatgov
Copy link
Contributor Author

Closing this PR along with the linked issue. Reasons detailed in comment on the issue #1906 (comment)

@owenatgov owenatgov closed this Sep 14, 2022
@owenatgov owenatgov deleted the checkbox-focus-bug branch June 28, 2024 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Visual focus glitch when clicking on checkbox and radio labels
5 participants