Replies: 1 comment 1 reply
-
This is cool! Curious if this will still work in nested grid situations? What about at smaller breakpoints when the total column count changes? (4 columns at small, 8 at medium) I wouldn't think it would be worth back-porting to v10 seeing as support ends in September. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Problem/Motivation
tldr;
Provide utilities, in the form of Sass mixins, to more easily author container queries targetting Carbon grid columns.
--verbose
In our work on the ibm.com marketing site, we have encountered multiple instances of designs that define component style variations in terms of the Carbon grid. For instance, on the ibm.com marketing site, we have full width 16 column layouts, and also so called "table of contents" (TOC) layouts. In TOC layouts, the main content occupies 12 columns and the TOC is sticky in the remaining 4 columns. We see requirements from design like:
Carbon for AEM Promo Banner component at full width lg breakpoint
Carbon for AEM Promo Banner component TOC layout lg breakpoint
Notice that in both of the above screenshots, the viewport size is 1056px (lg breakpoint), however in the TOC layout the component is constrained to 12 columns, and thus doesn't have enough room for the image. You could achieve this using media queries, however you'd need to coordinate and target a modifier CSS class that would flag the TOC case. Container queries offer a cleaner alternative wherein the component can adapt to the inline-size of the container element to achieve the desired result. We can use the knowledge of the Carbon grid parameters (gutters, condensed, wide, narrow, grid margin, etc.) to compose a container query that targets a given number of columns.
In our specific use case, where components are developed by one team, and ingested and placed in the grid by another team, container queries allow us to encapsulate these types of requirements inside the component, and not have to coordinate with the component adopter.
Proposed resolution
Let's leverage container queries! While that's great, the details get tricky, so we're proposing additions / changes to Carbon to help make using container queries to target Carbon columns easier.
Set containment on
.cds--css-grid
First, anytime you want to make use of conatiner queries, you first have to set containment on a parent element. This is not that big a deal, especially if you own the page markup as well as specific components that want to leverage container queries. However to be able to reliably target Carbon grid columns, and all their variations, we propose setting the following on the Grid wrapping element (
.cds--css-grid
). This provides a reliable container query target:Provide a Carbon Grid Column Container Query mixin
What we're really after is this:
Of course the browser doesn't understand what a 'column' is. We're left with querying against the inline-size, which looks something like this:
That can work for this very specific use case of 12 / 16 columns, wide Carbon grid, but it become tedious to do the cqi math on your own, to subtract the right gutter amount, and to account for subpixel rendering issues (FF only, the
+/- 2px
offsets.We need a mixin! Here is proposed API for the mixin:
It's use would look like this:
You can also compose it with media queries to be able to say things like "if occupying 12 columns at lg breakpoint and above":
Proof of Concept
https://stackblitz.com/edit/vitejs-vite-wsvmto?file=index.html,style.scss,utilities.scss
This Stackblitz shows a sample implementation of the mixin we're proposing. The API is the same as noted above. There are copious inline comments attempting to explain each piece. The
index.html
file shows a simple Carbon grid with some content that we then target with media queries instyles.scss
.Worth noting that this could also be backported to v10, although it's slightly more complicated in v10 b/c of negative margin use.
Beta Was this translation helpful? Give feedback.
All reactions