nhsuk-frontend icon indicating copy to clipboard operation
nhsuk-frontend copied to clipboard

Checkbox labels are full-width

Open 36degrees opened this issue 2 years ago • 0 comments

Bug Report

What is the issue?

Checkbox labels are full-width, which means the entire area to the right of the visible label is clickable:

https://user-images.githubusercontent.com/121939/189073255-067021af-b90d-4176-8df7-5d64fae5e741.mov

This is notably different to the way that labels for radios work:

https://user-images.githubusercontent.com/121939/189073234-1b0a603b-9dea-440e-9ad7-0f5c3f86f943.mov

I am pretty confident that this is an unintentional side effect of a change made after the components were copied from GOV.UK Frontend, affecting the order that the components appears in the outputted CSS – see below for details.

What steps are required to reproduce the issue?

  1. Go to https://service-manual.nhs.uk/design-system/components/checkboxes
  2. Click the area to the right of a check box label

Expected result: The checkbox should not be activated Expected result: The checkbox gets activated

What was the environment where this issue occurred?

  • Device: Macbook Pro
  • Operating System: macOS 12.5.1
  • Browser: Chrome
  • Browser version: 105.0.5195.102
  • NHS.UK frontend package version: Latest (as seen on https://service-manual.nhs.uk/design-system/components/checkboxes)
  • Node version: N/A
  • npm version: N/A

Is there anything else you think would be useful in recreating the issue?

In both GOV.UK Frontend and NHS Frontend, the label elements for checkboxes and radios have both the __label modifier class for the component and the label class from the label component.

The __label modifier class has display: inline-block:

https://github.com/nhsuk/nhsuk-frontend/blob/8ca299a690b9ff1f5180f4858bf6f9253d671955/packages/components/checkboxes/_checkboxes.scss#L41-L48

The label class has display: block:

https://github.com/nhsuk/nhsuk-frontend/blob/8ca299a690b9ff1f5180f4858bf6f9253d671955/packages/components/label/_label.scss#L5-L10

Importantly, both classes have the same specificity, and so whichever appears in the CSS last 'wins'.

Components in GOV.UK Frontend that rely on other components import them at the top of the file. For example, the checkboxes component relies on the label component, and so imports it (see https://github.com/alphagov/govuk-frontend/blob/3f264256fa01c8d5b5d4e8121756e50d866eda7d/src/govuk/components/checkboxes/_index.scss#L1-L6)

This means that the CSS for labels will appear before the CSS for checkboxes and radios in GOV.UK Frontend.

For whatever reason, NHS Frontend doesn't do this. Therefore the components appear in the CSS in the order they're imported, which is alphabetical.

This means the label component comes after the checkboxes, overriding the display property, but radios comes after label and so radios still get display: inline-block as intended.

36degrees avatar Sep 08 '22 08:09 36degrees