Skip to content

Conversation

@aduth
Copy link
Contributor

@aduth aduth commented Aug 11, 2021

Description

This pull request seeks to maintain the existing visual appearance of the Checkbox and Radio Button components, while improving developer ergonomics regarding HTML whitespace sensitivity.

Context: 18F/identity-idp#5279 (comment)

Additional information

Because the current implementation styles the icon pseudo-element and label text as adjacent inline elements, it suffers from a common issue with unintended whitespace between the elements (see related blog post). For this reason, the code must be marked up very carefully to avoid additional whitespace within the label class (including newlines), as otherwise the text alignment between label and description is misplaced, seen in the screenshots below:

Before After
localhost_3000_components_preview_checkbox--tile (1) localhost_3000_components_preview_checkbox--tile

There are a few ways this could be addressed, including using Flexbox, though most of these alternative solutions would require backwards-incompatible changes to the component markup, such as the introduction of a new element to wrap the label text. A Flexbox approach can almost work with the existing markup through a combination of flex-wrap: wrap and flex-basis: 100% on the description element, but unfortunately this implementation fails when the label text is especially long.

Positioning absolutely is mostly straight-forward, with one exception: Because it is no longer in line with the label text, the checkmark icon must be manually positioned to restore the appearance of vertical alignment. The proposed implementation here uses some extensive math to achieve this, tested in the demo samples with default font size and with an adjusted $theme-body-font-size: "xl";. Ideally there'd be a simpler solution to this, though I could not come up with one.


Before you hit Submit, make sure you’ve done whichever of these applies to you:

  • Follow the 18F Front End Coding Style Guide and Accessibility Guide.
  • Run npm test and make sure the tests for the files you have changed have passed.
  • Run your code through HTML_CodeSniffer and make sure it’s error free.
  • Title your pull request using this format: [Website] - [UI component]: Brief statement describing what this pull request solves.

aduth added 2 commits August 11, 2021 10:17
Avoids issues where HTML spacing is sensitive between the checkmark pseudo-element and label text.
Since otherwise the end-result could possibly be an invalid size token
Comment on lines +228 to +229
line-height($theme-form-font-family, $theme-input-line-height) *
font-size($theme-form-font-family, $theme-body-font-size) -
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This may need a rethink, since it assumes font styles inherited from .usa-fieldset, which are likely not guaranteed.

.usa-fieldset,
.usa-hint {
@extend %block-input-general;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a situation where it would be nice to have the CSS4 lh unit, since essentially what we want is margin-top: calc((1lh - #{units($theme-input-select-size)}) / 2);.

A couple alternatives to consider:

  • Maybe the checkbox should explicitly set font expectations, which might be desirable anyways. i.e. Add the %block-input-general placeholder to usa-checkbox.
  • Another I'd hoped could work is to chop up the elements so that the natural height of the label is only the label text excluding the description, so that a standard vertical centering approach could work. I've got this close to working in a CodePen prototype, but positioning the description absolutely means that it won't "push down" content that occurs after the checkbox.

Copy link
Contributor

Choose a reason for hiding this comment

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

@aduth have you tried Flexbox? Seems similar to the media object pattern [solved by flexbox - philipwalton.github.io]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aduth have you tried Flexbox? Seems similar to the media object pattern [solved by flexbox - philipwalton.github.io]

Yeah, if we were starting fresh, flexbox could be a great option, but since most of the approaches I tried with flexbox would involve the addition of a new wrapper element (like the .Media-body element in those demos), I figured it best to find an alternative which remains backward-compatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • Maybe the checkbox should explicitly set font expectations, which might be desirable anyways. i.e. Add the %block-input-general placeholder to usa-checkbox.

I went this direction in 9e958ae, based on there being some precedent of this applying at the component level [1][2][3][4], where presumably it's expected that the checkbox and radio could (or should) inherit from the input-related typography settings.

@aduth aduth marked this pull request as draft August 13, 2021 16:28
So that checkbox positioning is accurate based on explicit font expectations
@thisisdano thisisdano merged commit c059eaa into uswds:develop Aug 17, 2021
@aduth aduth deleted the aduth-checkbox-position-absolute branch August 18, 2021 17:54
@thisisdano thisisdano mentioned this pull request Aug 18, 2021
aduth added a commit to 18F/identity-design-system that referenced this pull request Aug 25, 2021
**Why:** Avoid future maintenance overhead incurred by version drift, incorporate latest features and updates, including upstream contributions previously implemented as overrides.

Changelog:
- https://github.com/uswds/uswds/releases/tag/v2.12.1
- https://github.com/uswds/uswds/releases/tag/v2.12.0

Incorporated revisions:
- uswds/uswds#4199
- uswds/uswds#4286
aduth added a commit to 18F/identity-design-system that referenced this pull request Aug 27, 2021
**Why:** Avoid future maintenance overhead incurred by version drift, incorporate latest features and updates, including upstream contributions previously implemented as overrides.

Changelog:
- https://github.com/uswds/uswds/releases/tag/v2.12.1
- https://github.com/uswds/uswds/releases/tag/v2.12.0

Incorporated revisions:
- uswds/uswds#4199
- uswds/uswds#4286
aduth added a commit to 18F/identity-design-system that referenced this pull request Aug 27, 2021
**Why:** Avoid future maintenance overhead incurred by version drift, incorporate latest features and updates, including upstream contributions previously implemented as overrides.

Changelog:
- https://github.com/uswds/uswds/releases/tag/v2.12.1
- https://github.com/uswds/uswds/releases/tag/v2.12.0

Incorporated revisions:
- uswds/uswds#4199
- uswds/uswds#4286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants