Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add demo
  • Loading branch information
siriwatknp committed Mar 27, 2024
commit c66c031b7f9954e7dc260667c5f7003c94d14b7c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Read more on the [Typography page](/system/typography/).

## Responsive values

All properties associated with the `sx` prop also support responsive values for specific breakpoints.
All properties associated with the `sx` prop also support responsive values for specific breakpoints and container queries.

Read more on the [Usage page—Responsive values](/system/getting-started/usage/#responsive-values).

Expand Down
88 changes: 88 additions & 0 deletions docs/data/system/getting-started/usage/ContainerQueries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as React from 'react';
import Box from '@mui/material/Box';

export default function ContainerQueries() {
return (
<Box
sx={{
overflow: 'auto',
resize: 'horizontal',
width: 400,
maxWidth: '80%',
containerType: 'inline-size',
}}
>
<Box
sx={{
display: 'flex',
flexDirection: { xs: 'column', 'cq@350': 'row' },
bgcolor: 'background.default',
border: '1px solid',
borderColor: 'divider',
borderRadius: 2,
overflow: 'clip',
}}
>
<Box
component="img"
sx={{
alignSelf: 'stretch',
aspectRatio: '16 / 9',
objectFit: 'cover',
width: '100%',
maxWidth: { 'cq@350': '36%', 'cq@500': 240 },
}}
alt="The house from the offer."
src="https://images.unsplash.com/photo-1512917774080-9991f1c4c750?auto=format&w=350&dpr=2"
/>
<Box
sx={{
p: { xs: 2, 'cq@500': 3 },
display: 'flex',
flexDirection: 'column',
gap: 1,
}}
>
<div>
<Box
component="span"
sx={{ fontSize: '0.875rem', color: 'text.secondary' }}
>
123 Main St, Phoenix AZ
</Box>
<Box
sx={{
color: 'primary.main',
fontSize: '1.125rem',
fontWeight: 'bold',
}}
>
$280,000 — $310,000
</Box>
</div>
<Box
sx={{
width: 'fit-content',
py: 0.5,
px: 1,
backgroundColor: 'rgba(46, 125, 50, 0.1)',
borderRadius: 10,
display: 'flex',
alignItems: 'center',
gap: 0.5,
border: '1px solid',
borderColor: 'rgba(46, 125, 50, 0.1)',
fontSize: '0.7rem',
fontWeight: 'bold',
letterSpacing: '.05rem',
textTransform: 'uppercase',
color: 'success.main',
}}
>
Confidence score: 85%
</Box>
</Box>
</Box>
</Box>
);
}
88 changes: 88 additions & 0 deletions docs/data/system/getting-started/usage/ContainerQueries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as React from 'react';
import Box from '@mui/material/Box';

export default function ContainerQueries() {
return (
<Box
sx={{
overflow: 'auto',
resize: 'horizontal',
width: 400,
maxWidth: '80%',
containerType: 'inline-size',
}}
>
<Box
sx={{
display: 'flex',
flexDirection: { xs: 'column', 'cq@350': 'row' },
bgcolor: 'background.default',
border: '1px solid',
borderColor: 'divider',
borderRadius: 2,
overflow: 'clip',
}}
>
<Box
component="img"
sx={{
alignSelf: 'stretch',
aspectRatio: '16 / 9',
objectFit: 'cover',
width: '100%',
maxWidth: { 'cq@350': '36%', 'cq@500': 240 },
}}
alt="The house from the offer."
src="https://images.unsplash.com/photo-1512917774080-9991f1c4c750?auto=format&w=350&dpr=2"
/>
<Box
sx={{
p: { xs: 2, 'cq@500': 3 },
display: 'flex',
flexDirection: 'column',
gap: 1,
}}
>
<div>
<Box
component="span"
sx={{ fontSize: '0.875rem', color: 'text.secondary' }}
>
123 Main St, Phoenix AZ
</Box>
<Box
sx={{
color: 'primary.main',
fontSize: '1.125rem',
fontWeight: 'bold',
}}
>
$280,000 — $310,000
</Box>
</div>
<Box
sx={{
width: 'fit-content',
py: 0.5,
px: 1,
backgroundColor: 'rgba(46, 125, 50, 0.1)',
borderRadius: 10,
display: 'flex',
alignItems: 'center',
gap: 0.5,
border: '1px solid',
borderColor: 'rgba(46, 125, 50, 0.1)',
fontSize: '0.7rem',
fontWeight: 'bold',
letterSpacing: '.05rem',
textTransform: 'uppercase',
color: 'success.main',
}}
>
Confidence score: 85%
</Box>
</Box>
</Box>
</Box>
);
}
13 changes: 13 additions & 0 deletions docs/data/system/getting-started/usage/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,19 @@ The following demo shows how to define a set of breakpoints using the object syn

{{"demo": "BreakpointsAsObject.js"}}

:::info
📣 Starting from v6, the object structure supports [container queries](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_containment/Container_queries) shorthand with `cq`.

We recommend you to check the [browser support](https://caniuse.com/?search=container%20que) before using CSS container queries.
:::

The shorthand syntax is `cq@{breakpoint}/{container}`:

- **breakpoint**: a number for `px` unit or a breakpoint key (e.g. `sm`, `md`, `lg`, `xl` for default breakpoints) or a valid CSS value (e.g. `40em`).
- **container** (optional): the name of the [containment context](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_containment/Container_queries#naming_containment_contexts).

{{"demo": "ContainerQueries.js"}}

#### Breakpoints as an array

The second option is to define your breakpoints as an array, from smallest to largest.
Expand Down
10 changes: 10 additions & 0 deletions packages/mui-system/src/cssContainerQueries/cssContainerQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ interface CssContainerQueries {
cq: ((name: string) => Pick<Breakpoints, Fn>) & Pick<Breakpoints, Fn>;
}

/**
* A wrapper of the `breakpoints`'s utilities to create container queries.
*/
function createBreakpointToCQ<T extends { breakpoints: Partial<Breakpoints> }>(themeInput: T) {
return function toContainerQuery(key: Fn, name?: string) {
return (...args: Array<Breakpoint | number>) => {
Expand All @@ -16,6 +19,7 @@ function createBreakpointToCQ<T extends { breakpoints: Partial<Breakpoints> }>(t
name ? `@container ${name}` : '@container',
);
if (key === 'not' && result.includes('not all and')) {
// `@container` does not work with `not all and`, so need to invert the logic
return result
.replace('not all and ', '')
.replace('min-width:', 'width<')
Expand All @@ -26,6 +30,12 @@ function createBreakpointToCQ<T extends { breakpoints: Partial<Breakpoints> }>(t
};
}

/**
* For using in `sx` prop to sort the breakpoint from low to high.
Copy link
Member

Choose a reason for hiding this comment

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

Is this function only used for the xs, sm, md, lg, and xl queries?

I'm wondering as it only supports min-width declarations.

Copy link
Member Author

@siriwatknp siriwatknp Apr 11, 2024

Choose a reason for hiding this comment

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

No, it supports valid CSS units like 40em or 300px but it requires all fields to use the same unit.

sx={{
  flexDirection: {
    '@300px': 'column',
    '@500px': 'row',
  }
}}

Copy link
Member

Choose a reason for hiding this comment

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

But it only supports min-width ones, right? So, for example, theme.cq.down, which outputs @container (max-width:...) won't be sorted correctly.

I want to better understand this function's use case. Is it internal or also for users?

Copy link
Member Author

Choose a reason for hiding this comment

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

The sorting only happens for the sx prop (happens internally, the function isn't meant for users) similar to theme breakpoints shorthand.

* Note: this function does not work and will not support multiple units.
* e.g. input: { '@container (min-width:300px)': '1rem', '@container (min-width:40rem)': '2rem' }
* output: { '@container (min-width:40rem)': '2rem', '@container (min-width:300px)': '1rem' } // since 40 < 300 eventhough 40rem > 300px
*/
export function sortContainerQueries(
theme: Partial<CssContainerQueries>,
css: Record<string, any>,
Expand Down