Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

mahoneycm
Copy link
Contributor

@mahoneycm mahoneycm commented Feb 15, 2023

Alternative solution from #4962

Summary

Reverts usa-prose placeholders to use typeset mixins to resolve compilation errors.

Breaking change

⚠️ This is potentially a breaking change. This PR removes usa-prose- mixins and placeholders. Users should instead use typeset- mixins in their place.

Site changelog PR

uswds/uswds-site#2095

Related issue

Closes #4962

Preview link

Prose →

Problem statement

This work resolves two separate issues:

  1. Build errors while extending %usa-prose- placeholders.
  2. Compilation errors with mixins nested under the & > selector

1: Build errors with placeholder:

Sass can throw errors when we rely on @extend and placeholders in certain situations.
From #4962:

I’m trying to configure USWDS inside an @layer rule using the load-css function from sass:meta. When I try to @use "uswds-core" in my own Sass modules so that I can access the tokens for my styles, I get a target selector not found error for %usa-prose-p.

2: Compilation errors with mixins nested under the & > selector

This would result in the unexpected assignment of the & selector and in turn, incorrectly attach styles to classes

Solution

  1. Revert usa-prose placeholder to a mixin
  2. Manually paragraph and heading styles from the typeset mixins and directly into the usa-prose style definitions
  3. Removed redundant and unused usa-prose mixins in favor of typeset- mixins

From @amyleadem in #5043:
Sass seems to get confused when * + & is brought in from another file via a mixin (more details in #4576). Additionally, Sass can throw errors in limited circumstances when we rely on @extend and placeholders.

The key conclusion is that * + & does not currently seem to be a viable addition to mixins or placeholders. By moving all * + & out of mixins and placeholders and directly into the style definitions, we can remove the risk of Sass confusion.

Note: The downside of this solution is that it requires some code repetition, but that might be slightly mitigated by creating variables/settings for the heading and paragraph top margins.

Major changes

This PR removes usa-prose- mixins and placeholders. Users should instead use typeset- mixins in their place.

List of mixins removed:

  • usa-prose-link
  • usa-prose-display
  • usa-prose-p
  • usa-prose-heading
  • usa-prose-h1 through h6

New settings variables

  • $theme-heading-margin-top
  • $theme-paragraph-margin-top

Testing and review

  1. Confirm that Sass compiles without error
  2. Visit Prose Storybook
  3. Inspect usa-prose styles and make sure * + & selectors are correctly targetting headings and p tags
  4. Use npm link to create a symlink to the package
  5. Clone this test repo
  6. Use npm link @uswds/uswds to replace USWDS package with the symlink
  7. Run npm build and confirm no build errors
  • Run git pull origin [base branch] to pull in the most recent updates from your base and check for merge conflicts. (Often, the base branch is develop).
  • Run npm run prettier:scss to format any Sass updates.
  • Run npm test and confirm that all tests pass.

@mahoneycm mahoneycm requested review from amyleadem and mejiaj and removed request for amyleadem February 15, 2023 21:41
@mahoneycm
Copy link
Contributor Author

@mejiaj @amyleadem This is the alternative solution mentioned in James' comment

This maintains current develop styles but it does move them in the output CSS.

Here is an easel that shows the relevant difference between files and verifies all necessary prose styles are present.: usa prose refactor

I tried adding colored arrows to help connect the changes

@amyleadem amyleadem self-assigned this Feb 22, 2023
Copy link
Contributor

@amyleadem amyleadem left a comment

Choose a reason for hiding this comment

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

Hi @mahoneycm, I have started my review but need to step away for a bit. In the meantime, I found an error when $theme-global-content-styles is set to true because it is looking for the now missing usa-prose-link. I've also added some comments below with notes and a couple questions about code redundancy. I will cycle back later, but let me know if you have questions in the meantime.

So far the new solution appears to be on track, but there just a couple things to look at.

Comment on lines 10 to 19
p {
line-height: line-height($theme-body-font-family, $theme-body-line-height);
margin-bottom: 0;
margin-top: 0;
max-width: measure($theme-text-measure);

+ * {
margin-top: $theme-paragraph-margin-top;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious - this appears to be redundant with a good chunk of the typeset-p mixin. Is there a way to reduce redundancy without having a negative impact?

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 haven't been able to successfully use the typeset mixins without it incorrectly setting *+.usa-prose p as the top-margin selector, unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Scratch that, I'm going to use @mejiaj's typset-base recommendation to alleviate some of the redundancy!

Comment on lines 27 to 32
@include u-margin-y(0);
clear: both;

+ * {
margin-top: $theme-paragraph-margin-top;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here - feels redundant with the rules with typeset-heading. Can we reduce code redundancy here?

Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

This solution outputs what I would expect. If we go this route we should consider adding guidance to users (if any) that were using the removed extends.


Alternatively, we should consider simplifying in the future. By doing something like:

.usa-prose {
  & > {
    @include usa-list-styles;
    @include usa-table-styles;

    // Adds consistent margin to *all* child elements
    * + * {
      margin-top: $theme-paragraph-margin-top !important;
    }

    …H1-H6 styles

    // Only override headers
    * + {
      h1,
      h2,
      h3,
      h4,
      h5,
      h6 {
        margin-top: $theme-heading-margin-top !important;
      }
    }
  }
}

Comment on lines 11 to 14
line-height: line-height($theme-body-font-family, $theme-body-line-height);
margin-bottom: 0;
margin-top: 0;
max-width: measure($theme-text-measure);
Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid duplication, you could create a typeset-heading-base and typeset-p-base that include these styles in typeset.scss.

//
// typeset.scss
//
@mixin typeset-heading-base {
  @include u-margin-y(0);
  clear: both;
}

@mixin typeset-heading {
  @include typeset-heading-base;

  * + & {
    margin-top: $theme-heading-margin-top;
  }

  + * {
    margin-top: $theme-paragraph-margin-top;
  }
}

// typeset element mixins
@mixin typset-p-base {
  line-height: line-height($theme-body-font-family, $theme-body-line-height);
  margin-bottom: 0;
  margin-top: 0;
  max-width: measure($theme-text-measure);
}

@mixin typeset-p {
  @include typset-p-base;

  * + & {
    margin-top: $theme-paragraph-margin-top;
  }

  + * {
    margin-top: $theme-paragraph-margin-top;
  }
}

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 like this idea. I think it'll clean up the usa-prose class nicely!

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 was able to use the typset-p/heading-base mixin and I personally like the clean-up it offers!

Using * + * did not work as expected but I was able to use *:not(:first-child) to achieve the same result

@mahoneycm mahoneycm requested review from amyleadem and mejiaj March 1, 2023 18:02
@mahoneycm
Copy link
Contributor Author

@mejiaj @amyleadem ok! So I've completed some refactoring by a combination of creating typeset-heading-base and typeset-p-base to avoid some duplication and to correctly reset top and bottom margins. I also moved certain selectors around in order to have the styles apply correctly and used the owl selector * + * to correctly give all siblings of usa-prose children the correct top margin.

I've taken a stab at some basic SASS unit testing to make sure that our nested selectors are working as expected! I'd love your feedback whenever you have a chance to see if there are other enhancements I may be missing, or if we can test further with the unit tests!

Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

Looks good, minor comments!

@mahoneycm mahoneycm requested a review from mejiaj March 21, 2023 14:02
Copy link
Contributor

@amyleadem amyleadem left a comment

Choose a reason for hiding this comment

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

@mahoneycm This is looking good! It is feeling cleaner with each iteration. Thanks a ton for hanging in there with this one. Only thing I have to flag is the failed Sass test that is causing the build error.

  • Confirm that vertical margins look correct in the  usa-prose story
  • Confirm that vertical margins look correct when $theme-global-paragraph-styles and $theme-global-content-styles are set to true
  • Confirm that * + & references have been accounted for in the compiled CSS
  • Confirm that the error is resolved in the test repo
  • Confirm that updating values in $theme-heading-margin-top and $theme-paragraph-margin-top updates relevant values.
  • Confirm npm start runs without error
  • Confirm npm test runs without error
    • Receiving an error with the prose Sass test
  • Confirm gulp buildSass runs without error
  • Confirm no errors when $theme-global-content-styles is set to true
  • Check visual display on Safari, Firefox, Edge, Chrome

@mejiaj
Copy link
Contributor

mejiaj commented Mar 24, 2023

@mahoneycm can you please look into the failing test?

@mahoneycm mahoneycm requested a review from amyleadem March 27, 2023 13:45
@mahoneycm
Copy link
Contributor Author

@mejiaj @amyleadem Looks like when I broke it when I tried formatting the test scss. I believe it was only because of the order of the output not matching the expected order. Moving the expected results to match the actual outputs did the trick!

Copy link
Contributor

@amyleadem amyleadem left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the test!

@mahoneycm
Copy link
Contributor Author

@thisisdano Ready for final review!

@mahoneycm mahoneycm requested a review from thisisdano April 3, 2023 16:59
@thisisdano
Copy link
Contributor

thisisdano commented May 8, 2023

I'll draw up a changelog PR for this change.
Changelog PR: uswds/uswds-site#2096

@mahoneycm
Copy link
Contributor Author

@thisisdano I created a changelog of this one before I left yesterday as well, let me know if you want me to go ahead and close this ticket: uswds/uswds-site#2095

I also wanted to note I did mark it as a breaking change because it removes the usa-prose- mixin.

@thisisdano thisisdano merged commit c782687 into develop May 10, 2023
@thisisdano thisisdano deleted the cm-usa-prose-selector-fix branch May 10, 2023 21:35
@thisisdano thisisdano mentioned this pull request Jun 6, 2023
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.

USWDS - Bug: Unable to find %usa-prose-p selector when importing uswds-typography in one file and uswds-core in another

4 participants