-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
[system] Add container queries utility #41674
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
05bc425
add container queries
siriwatknp ca0d779
add containerQueries
siriwatknp 6682098
pnpm extract-error-codes
siriwatknp 35e2a07
reexport cssContainerQueries
siriwatknp c66c031
add demo
siriwatknp 235a5c3
remove cq prefix
siriwatknp 1ae42b4
make container queries as default
siriwatknp 4aa9b01
Merge branch 'next' of https://github.com/mui/material-ui into system…
siriwatknp 2602d28
simplify logic
siriwatknp 7a88670
add containerQueries to Joy theme
siriwatknp e77155d
add more tests
siriwatknp af3b7a1
Merge branch 'next' of https://github.com/mui/material-ui into system…
siriwatknp 4493ee9
add cq field to test
siriwatknp a1aa6d6
Merge branch 'next' of https://github.com/mui/material-ui into system…
siriwatknp 07907de
pnpm extract-error-codes
siriwatknp 7677ad0
change `cq` to `containerQueries`
siriwatknp 885caa5
Merge branch 'next' of https://github.com/mui/material-ui into system…
siriwatknp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default class MuiError { | ||
constructor(message: string); | ||
constructor(message: string, ...args: string[]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/mui-system/src/cssContainerQueries/cssContainerQueries.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { expect } from 'chai'; | ||
|
||
import createTheme from '@mui/system/createTheme'; | ||
import cssContainerQueries, { | ||
sortContainerQueries, | ||
getContainerQuery, | ||
} from '@mui/system/cssContainerQueries'; | ||
|
||
describe('cssContainerQueries', () => { | ||
it('should have `up`, `down`, `between`, `only`, and `not` functions', () => { | ||
const theme = cssContainerQueries(createTheme()); | ||
|
||
expect(theme.cq.up('sm')).to.equal('@container (min-width:600px)'); | ||
expect(theme.cq.down('sm')).to.equal('@container (max-width:599.95px)'); | ||
expect(theme.cq.between('sm', 'md')).to.equal( | ||
'@container (min-width:600px) and (max-width:899.95px)', | ||
); | ||
expect(theme.cq.only('sm')).to.equal('@container (min-width:600px) and (max-width:899.95px)'); | ||
expect(theme.cq.not('xs')).to.equal('@container (min-width:600px)'); | ||
expect(theme.cq.not('xl')).to.equal('@container (max-width:1535.95px)'); | ||
expect(theme.cq.not('md')).to.equal('@container (width<900px) and (width>1199.95px)'); | ||
}); | ||
|
||
it('should be able to create named containment context', () => { | ||
const theme = cssContainerQueries(createTheme()); | ||
|
||
expect(theme.cq('sidebar').up('sm')).to.equal('@container sidebar (min-width:600px)'); | ||
expect(theme.cq('sidebar').down('sm')).to.equal('@container sidebar (max-width:599.95px)'); | ||
expect(theme.cq('sidebar').between('sm', 'md')).to.equal( | ||
'@container sidebar (min-width:600px) and (max-width:899.95px)', | ||
); | ||
expect(theme.cq('sidebar').only('sm')).to.equal( | ||
'@container sidebar (min-width:600px) and (max-width:899.95px)', | ||
); | ||
expect(theme.cq('sidebar').not('xs')).to.equal('@container sidebar (min-width:600px)'); | ||
expect(theme.cq('sidebar').not('xl')).to.equal('@container sidebar (max-width:1535.95px)'); | ||
expect(theme.cq('sidebar').not('sm')).to.equal( | ||
'@container sidebar (width<600px) and (width>899.95px)', | ||
); | ||
}); | ||
|
||
it('should sort container queries', () => { | ||
const theme = cssContainerQueries(createTheme()); | ||
|
||
const css = { | ||
'@container (min-width:960px)': {}, | ||
'@container (min-width:1280px)': {}, | ||
'@container (min-width:0px)': {}, | ||
'@container (min-width:600px)': {}, | ||
}; | ||
|
||
const sorted = sortContainerQueries(theme, css); | ||
|
||
expect(Object.keys(sorted)).to.deep.equal([ | ||
'@container (min-width:0px)', | ||
'@container (min-width:600px)', | ||
'@container (min-width:960px)', | ||
'@container (min-width:1280px)', | ||
]); | ||
}); | ||
|
||
it('should sort container queries with other unit', () => { | ||
const theme = cssContainerQueries(createTheme()); | ||
|
||
const css = { | ||
'@container (min-width:30.5rem)': {}, | ||
'@container (min-width:20rem)': {}, | ||
'@container (min-width:50.5rem)': {}, | ||
'@container (min-width:40rem)': {}, | ||
}; | ||
|
||
const sorted = sortContainerQueries(theme, css); | ||
|
||
expect(Object.keys(sorted)).to.deep.equal([ | ||
'@container (min-width:20rem)', | ||
'@container (min-width:30.5rem)', | ||
'@container (min-width:40rem)', | ||
'@container (min-width:50.5rem)', | ||
]); | ||
}); | ||
|
||
it('should throw an error if shorthand is invalid', () => { | ||
expect(() => { | ||
const theme = cssContainerQueries(createTheme()); | ||
getContainerQuery(theme, 'cq0'); | ||
}).to.throw( | ||
'MUI: The provided shorthand (cq0) is invalid. The format should be `cq@<breakpoint | number>` or `cq@<breakpoint | number>/<container>`.\n' + | ||
'For example, `cq@sm` or `cq@600` or `cq@40rem/sidebar`.', | ||
); | ||
}); | ||
}); |
108 changes: 108 additions & 0 deletions
108
packages/mui-system/src/cssContainerQueries/cssContainerQueries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import MuiError from '@mui/internal-babel-macros/MuiError.macro'; | ||
import { Breakpoints, Breakpoint } from '../createTheme/createBreakpoints'; | ||
|
||
type Fn = 'up' | 'down' | 'between' | 'only' | 'not'; | ||
|
||
interface CssContainerQueries { | ||
cq: ((name: string) => Pick<Breakpoints, Fn>) & Pick<Breakpoints, Fn>; | ||
} | ||
|
||
function createBreakpointToCQ<T extends { breakpoints: Partial<Breakpoints> }>(themeInput: T) { | ||
return function toContainerQuery(key: Fn, name?: string) { | ||
return (...args: Array<Breakpoint | number>) => { | ||
// @ts-ignore | ||
const result = themeInput.breakpoints[key](...args).replace( | ||
'@media', | ||
name ? `@container ${name}` : '@container', | ||
); | ||
if (key === 'not' && result.includes('not all and')) { | ||
return result | ||
.replace('not all and ', '') | ||
.replace('min-width:', 'width<') | ||
.replace('max-width:', 'width>'); | ||
} | ||
return result; | ||
}; | ||
}; | ||
} | ||
|
||
export function sortContainerQueries( | ||
theme: Partial<CssContainerQueries>, | ||
css: Record<string, any>, | ||
) { | ||
if (!theme.cq) { | ||
return css; | ||
} | ||
const sorted = Object.keys(css) | ||
.filter((key) => key.startsWith('@container')) | ||
.sort((a, b) => { | ||
const regex = /min-width:\s*([0-9.]+)/; | ||
return +(a.match(regex)?.[1] || 0) - +(b.match(regex)?.[1] || 0); | ||
}); | ||
if (!sorted.length) { | ||
return css; | ||
} | ||
return sorted.reduce( | ||
(acc, key) => { | ||
const value = css[key]; | ||
delete acc[key]; | ||
acc[key] = value; | ||
return acc; | ||
}, | ||
{ ...css }, | ||
); | ||
} | ||
|
||
export function getContainerQuery( | ||
theme: Partial<CssContainerQueries> & { breakpoints: Pick<Breakpoints, 'up'> }, | ||
shorthand: string, | ||
) { | ||
if (shorthand.startsWith('cq')) { | ||
const matches = shorthand.match(/@([^/\n]+)\/?(.+)?/); | ||
if (!matches) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new MuiError( | ||
'MUI: The provided shorthand %s is invalid. The format should be `cq@<breakpoint | number>` or `cq@<breakpoint | number>/<container>`.\n' + | ||
'For example, `cq@sm` or `cq@600` or `cq@40rem/sidebar`.', | ||
`(${shorthand})`, | ||
); | ||
} | ||
return null; | ||
} | ||
const [, containerQuery, containerName] = matches; | ||
const value = (Number.isNaN(+containerQuery) ? containerQuery : +containerQuery) as | ||
| Breakpoint | ||
| number; | ||
if (theme.cq) { | ||
return containerName ? theme.cq(containerName).up(value) : theme.cq.up(value); | ||
} | ||
if (theme.breakpoints) { | ||
return createBreakpointToCQ(theme)('up', containerName)(value); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
export default function cssContainerQueries<T extends { breakpoints: Breakpoints }>( | ||
themeInput: T, | ||
): T & CssContainerQueries { | ||
const toContainerQuery = createBreakpointToCQ(themeInput); | ||
function cq(name: string) { | ||
return { | ||
up: toContainerQuery('up', name), | ||
down: toContainerQuery('down', name), | ||
between: toContainerQuery('between', name), | ||
only: toContainerQuery('only', name), | ||
not: toContainerQuery('not', name), | ||
}; | ||
} | ||
cq.up = toContainerQuery('up'); | ||
cq.down = toContainerQuery('down'); | ||
cq.between = toContainerQuery('between'); | ||
cq.only = toContainerQuery('only'); | ||
cq.not = toContainerQuery('not'); | ||
return { | ||
...themeInput, | ||
cq, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './cssContainerQueries'; | ||
export { getContainerQuery, sortContainerQueries } from './cssContainerQueries'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Come across this since my test is the first
.ts
that usesMuiError
with multiple parameters.