Visitar URL original
USWDS - Fieldset: Resolve legend margin error by mahoneycm · Pull Request #5344 · uswds/uswds · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/usa-fieldset/src/styles/_usa-fieldset.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.usa-fieldset {
@extend %block-input-general;
border: none;
display: contents;
margin: 0;
padding: 0;
Comment on lines +6 to 8

@amyleadem amyleadem Sep 19, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Applying display:contents to the fieldset element appears to cause all other fieldset styles to be ignored. This can be ok on a base level because the USWDS styles are basically declaring a reset to 0. However, I get concerned that this will cause confusion with users who apply utility classes or styles to the fieldset element. It also looks like anchor ids aren't recognized.

Wanted to share this explanation for display:contents because I found the explanation helpful:

display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. This can be useful when a wrapper element should be ignored when using CSS grid or similar layout techniques. 1

I do think that prioritizing usa-legend styles over usa-fieldset make sense because they are more substantive. I just wanted to check if this was the only path forward (does it make sense to apply the base style top margin to usa-fieldset instead of usa-legend?).

Regardless, we should find a way to document this very clearly.

Footnotes

  1. https://caniuse.com/css-display-contents ↩

@mahoneycm mahoneycm Sep 20, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One issue with moving the margin to usa-fieldset is that we offer two different legend options; usa-legend and usa-legend--large, which features larger text and a smaller margin.

@use "uswds-core" as *;
.usa-legend {
@include typeset(
$theme-form-font-family,
$theme-body-font-size,
$theme-input-line-height
);
display: block;
font-weight: font-weight("normal");
margin-top: units(3);
max-width: units($theme-input-max-width);
}
.usa-legend--large {
font-size: font-size($theme-form-font-family, "xl");
font-weight: font-weight("bold");
margin-top: units(2);
}

I wonder if we could take a similar approach to prose and and set the margin using something like

.usa-fieldset {
   ...

   & > .usa-legend {
      margin-top: units(3);
   }
    
   & > + .usa-legend--large {
       margin-top: units(2);
   }
}

What do you think of that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ooh sorry I forgot there are limitations selecting a parent based on children. I'll look into using has() or other newer selectors to see if it's possible to set the margin on fieldset based on it's child element but we may be stuck there

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Found my old notes on this issue and postcss rendering breaks when we attempt to target classes using :has().

Alternatively, we could consider creating classes for fieldset to handle the different legend sizes, but that would be a breaking change for users as well.

To me, this was the path of least resistance for users to utlize the margins set via usa-legend classes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As far as documenting this change, do you thing we need to add guidance to our forms page or form elements that use fieldset such as checkbox / radio that users should be mindful when adding styles to fieldset/legend?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ooh sorry I forgot there are limitations selecting a parent based on children. I'll look into using has() or other newer selectors to see if it's possible to set the margin on fieldset based on it's child element but we may be stuck there

This quirk is limited (or should be) to SASS mixins/functions.

@mejiaj mejiaj Oct 30, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@amyleadem @mahoneycm caniuse lists a resource 1 for partial support. It has an important accessibility warning:

The display property has no effect on an element’s semantics: these are defined by the document language and are not affected by CSS. Aside from the none value, which also affects the aural/speech output and interactivity of an element and its descendants, the display property only affects visual layout: its purpose is to allow designers freedom to change the layout behavior of an element without affecting the underlying document semantics.

Looking at our sponsor list example, it means that the item is no longer seen as a list, but as something else (test case in CodePen).

It seems mostly resolved, but safari 2 still has issues. Is there a quick alternative or should we bump from 3.7.0?

Footnotes

  1. https://hidde.blog/more-accessible-markup-with-display-contents/#heading-4 ↩

  2. https://adrianroselli.com/2022/07/its-mid-2022-and-browsers-mostly-safari-still-break-accessibility-via-display-properties.html ↩

}
4 changes: 2 additions & 2 deletions packages/usa-form/src/test/test-pattern/test-usa-form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</div>
</form>

<fieldset class="usa-fieldset margin-top-2">
<fieldset class="usa-fieldset">
<legend class="usa-legend">Checkbox</legend usa-label>

<div class="usa-checkbox">
Expand Down Expand Up @@ -50,7 +50,7 @@
</fieldset>


<fieldset class="usa-fieldset margin-top-2">
<fieldset class="usa-fieldset">
Comment thread
mahoneycm marked this conversation as resolved.
Resolved
<legend class="usa-legend">Radio</legend>
<div class="usa-radio">
<input
Expand Down