-
Notifications
You must be signed in to change notification settings - Fork 1.1k
USWDS - Utilities: Custom breakpoints #6048
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
Conversation
This reverts commit e6c7682.
…om breakpoints are included in breakpoints-complete map
…uilder and at-media to handle custom breakpoints
Additional consideration: Custom breakpoints + layout gridThis work currently does not build Layout Grid utility classes for the new custom breakpoints, but it would be possible to update Is that something we want to provide to the users with this update? Alternatively, it could be an additional enhancement after this work is merged. |
|
@jamigibbs After some discussion, we've opted to only take custom breakpoints in I'm unable to tag you as a reviewer for this work, but if you could test this PR to see if it works with your custom breakpoints the way you'd like, it'd be greatly appreciated! All feedback is welcome 👍 |
mahoneycm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple notes on my approach here
| @function px-to-user-em($pixels) { | ||
| @if unit($pixels) != "px" { | ||
| @error 'This value must be in pixels'; | ||
| } | ||
| $px-to-user-em: px-to-rem($pixels); | ||
| $px-to-user-em: rem-to-user-em($px-to-user-em); | ||
|
|
||
| @return $px-to-user-em; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opted not to re-invent the wheel and use functions we already had to convert pixels to rem.
Alternatively, we could use the following sass math conversion if we don't want to rely on the other functions:
@if unit($pixels) != "px" {
@error 'This value must be in pixels';
}
$px-to-user-em: (math.div($pixels, root.$root-font-size-equiv)) * 1em;
$px-to-user-em: math.div(math.round($px-to-user-em * 100), 100);| @if unit($grid-in-rem) != "rem" { | ||
| @error 'This value must be in rem'; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: Added error to match other conversion functions
| $our-breakpoints: map-deep-get($system-properties, breakpoints, standard); | ||
| $custom-breakpoints: map-deep-get($system-properties, breakpoints, extended); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: Moved variable declaration out of mixin since they are used in both at-media and at-media-max
@mahoneycm Thanks Charlie! I also tested your branch locally with our implementation and it seems to be working great! The custom breakpoints were generating and an error message displayed if I tried to using something other than
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mahoneycm. I've been able to successfully test following your instructions. I've created a branch on uswds-sandbox test-uswds-breakpoints-6048, so others can test as well. Links to that testing environment:
- Branch:
test-uswds-breakpoints-6048. - Draft PR: uswds/uswds-sandbox#153
- Preview: Sandbox prose →
Confirming
- New utils are generated for custom breakpoint
- Breakpoint is available via
at-media - Warning on passing non-px values
- Additional utils are generated
- Overriding root font size sets media queries with
px
Non-pixel values
Confirming that passing non-px values give a warning.
Input
// _uswds-theme.scss
@use "uswds-core" with (
$theme-utility-breakpoints-custom: (
"desktop-custom-rem": 70rem // 1120px
)
);Output
Warning: Custom breakpoint `desktop-custom-rem` has the unit rem. Custom breakpoints must be set in px.
../../../@uswds/uswds/packages/uswds-core/src/styles/mixins/helpers/at-media.scss 35:7 at-media()
../../../../src/_styles/_uswds-theme-custom-styles.scss 11:1 @forward
../../../../src/_styles/styles.scss 3:1 Additional utils
Warning
Adding responsive variants has an impact on CSS compile size.
Confirming that additional utilities are output.
Input
// _uswds-theme.scss
@use "uswds-core" with (
$theme-utility-breakpoints-custom: (
"desktop-custom": 1100px,
),
$background-color-settings: (
responsive: true,
)
);Output
/* styles.css */
@media all and (min-width: 68.75em) {
.desktop-custom\:bg-transparent,
.desktop-custom\:hover\:bg-transparent:hover {
background-color: transparent;
}
.desktop-custom\:bg-black,
.desktop-custom\:hover\:bg-black:hover {
background-color: #000;
}
/* … */
}Overriding root font size
Input
// _uswds-theme.scss
@use "uswds-core" with (
$theme-utility-breakpoints-custom: (
"desktop-custom": 1100px,
),
$theme-respect-user-font-size: false,
$theme-root-font-size: 20px,
);Output
/* styles.css */
@media all and (min-width: 1100px) {
/* … */
}Output CSS size
Output CSS file size after adding a single custom breakpoint. This issue is compounded when we're adding responsive variants on top.
| File | Before | After |
|---|---|---|
| styles.css | 524KB | 572KB |
We should create an issue to support at-media without generating utils for performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good @mahoneycm! (And thanks for your original work on this @jamigibbs!)
Important
Before I approve, I wanted to highlight that this PR does not generate custom responsive breakpoints for layout grid or icon-list-size classes. I have added additional details in the notes below.
Are we comfortable with this moving forward without these responsive classes?
Everything else is looking good!
Testing details
I tested in uswds-sandbox using the following config:
@use "uswds-core" with (
$theme-show-compile-warnings: false,
$theme-show-notifications: false,
$theme-utility-breakpoints-custom: (
"new-breakpoint": 800px
),
$theme-utility-breakpoints: (
widescreen: true,
),
$left-settings: (
responsive: true
)
);- Confirm the
$theme-utility-breakpoints-customsetting generates new style rules for each new pixel-based value in the map- Note: There are some gaps in what
$theme-utility-breakpoints-customgenerates vs$theme-utility-breakpoints- Turning on
new-breakpointin$theme-utility-breakpoints-customgenerates 898 new references to.new-breakpointin the CSS. Turning onwidescreenin$theme-utility-breakpointsgenerates 1060 new references to.widescreen\: - After some digging, discovered that difference was that the custom breakpoint was not generated for:
- usa-icon-list sizes - for example, it did not generate
.new-breakpoint\:usa-icon-list—size-1, but did generate.widescreen\:usa-icon-list—size-1(In total, there were 90 references forusa-icon-list--sizegenerated forwidescreen) - layout grid classes - For example, it did not generate
.new-breakpoint\:grid-containerbut did generate.widescreen\:grid-container(In total, 72 grid references were generated forwidescreen)
- usa-icon-list sizes - for example, it did not generate
- For reference, here is the difference in CSS size:
- with default breakpoints: 524kb
- with only a new custom breakpoint: 572kb
- with only an additional standard breakpoint (
widescreen): 587kb
- Turning on
- Note: There are some gaps in what
- Confirm there is an appropriate warning in the console when a non-px unit is added to the
$theme-utility-breakpoints-customsetting map- Note: I added a non-breaking suggestion below to add a bit more clarity to the warning message
- Confirm that setting
$theme-respect-user-font-size: falseoutputs new breakpoint inpx - Confirm that setting
$theme-respect-user-font-size: trueoutputs new breakpoint inem - Confirm conversion from px to em is accurate (eg, 800px outputs as 50em)
- Confirm that turning on a responsive variant of a USWDS utility generates the custom breakpoint from
$theme-utility-breakpoints-customas well as the default breakpoints from$theme-utility-breakpoints(I tested$left-settings) - Confirm the code meets standards
|
Update: I have opened PR #6083 as a candidate to add support for layout grid utility classes to this PR. |
USWDS - Layout grid utility: Custom breakpoints
|
Update: I have merged in #6083 after team approval. This adds support for generating custom breakpoints for layout grid utilities. Will work on finalizing my review of this PR now. |
amyleadem
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I do have one suggestion to add detail to the warning message in the comment below, but I don't consider it to be a blocker
Co-authored-by: Amy Leadem <93996430+amyleadem@users.noreply.github.com>
thisisdano
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice feature. Thank you @jamigibbs and @mahoneycm!


Summary
Continuation of #5985. Thank you for your contribution @jamigibbs.
Added new
$theme-utility-breakpoints-customsetting. This setting generates responsive variants of USWDS utility classes at custom breakpoints.Being able to add a custom breakpoint to the utilities is useful for integrating USWDS with existing frameworks or other design requirements. Custom breakpoints will generate utilities alongside the default system breakpoints.
Sample implementation
To generate a custom breakpoint for your USWDS utilities, add custom breakpoint values to a Sass map for
$theme-utility-breakpoints-customin your USWDS settings configuration. Here is an example implementation:Important
Custom breakpoints must be defined with
pxvalues.Tip
Every breakpoint added to the system adds about 50kb to the compiled CSS. Be careful to only turn on the breakpoints you need for your project. You can turn off default USWDS breakpoints with the
$theme-utility-breakpointssetting. More details about$theme-utility-breakpointscan be found on the Settings page.Breaking change
This is not a breaking change.
Related issue
Closes #5984
Related pull requests
Continuation of #5985
Site documentation update →
Preview link
Preview link →
Problem statement
It is currently not possible for users to implement custom breakpoints and use them alongside our utility classes
Solution
Create new scss map for users to define their own breakpoints and build out utility classes with.
Major changes
$theme-utility-breakpoints-custommap for users to name and define their own breakpointspx-to-user-em()sass function_utility-builder.scssto take custom breakpointsat-mediamixin to create utility classes with custom breakpointspxunits.emconversation methods as standard USWDS breakpoints$theme-respect-user-font-sizeistrue, breakpoints are converted toem$theme-respect-user-font-sizeisfalse, breakpoints are output aspxTesting and review
Testing steps
Checkout feature branch
Visit
settings-utilities.scssand add custom breakpoints withpxandrem(or some other unit)$theme-utility-breakpoints-custom ( "bp-px": 64px; "bp-rem": 10rem; )Run
npx gulp compileSassConfirm the non
pxbreakpoint triggers Warning message in the terminal.View output USWDS css at
dist/css/uswds.css.Search for your custom breakpoint by name.
Confirm new media queries are built using values of your custom breakpoints.
Confirm value is correctly converted to
em.Confirm new utility classes are built using the name of your breakpoints.
Confirm non
pxbreakpoints were not built.Open
_settings-typographyand set$theme-respect-user-font-sizetofalse !defaultRecompile sass
npx gulp compileSassView output USWDS css at
dist/css/uswds.cssConfirm breakpoints are now output as
px.Update responsive settings test
Turn on a responsive utility class that is disabled by default and confirm that the custom breakpoints are created for the new utility.
_settings-utilities.scssbackground-color-settings-completechangeresponsive: falsetoresponsive: truenpx gulp compileSassdist/css/uswds.cssbg-(color)utility classes are created for your custom breakpointTesting checklist
extendedsystem propertypx-to-user-emsass function is valid.pxvalue is used.