Skip to content
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

feat: Have breakpoints accept custom media queries #1653

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## UNRELEASED

- Set `gatsby` peerDependency more explicit to `^2.0.0 || ^3.0.0`. [#1640](https://github.com/system-ui/theme-ui/pull/1640) ([@LekoArts](https://github.com/LekoArts))
- Have `breakpoints` accept custom media queries [#1653](https://github.com/system-ui/theme-ui/pull/1653) [@carolinmaisenbacher](https://github.com/carolinmaisenbacher)

## v0.6.2 (Mon Apr 05 2021)

Expand Down
4 changes: 3 additions & 1 deletion packages/css/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ const responsive = (
(theme && (theme.breakpoints as string[])) || defaultBreakpoints
const mediaQueries = [
null,
...breakpoints.map((n) => `@media screen and (min-width: ${n})`),
...breakpoints.map((n) =>
n.includes('@media') ? n : `@media screen and (min-width: ${n})`
),
]

for (const k in styles) {
Expand Down
33 changes: 32 additions & 1 deletion packages/css/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,38 @@ test('returns correct media query order 2', () => {
])
})

test('returns custom media queries', () => {
const result = css({
fontSize: [2, 3, 4],
color: 'primary',
})({
theme: {
...theme,
breakpoints: [
'32em',
'@media screen and (orientation: landscape) and (min-width: 40rem)',
],
},
})
const keys = Object.keys(result)
expect(keys).toEqual([
'fontSize',
'@media screen and (min-width: 32em)',
'@media screen and (orientation: landscape) and (min-width: 40rem)',
'color',
])
expect(result).toEqual({
fontSize: 16,
'@media screen and (min-width: 32em)': {
fontSize: 24,
},
'@media screen and (orientation: landscape) and (min-width: 40rem)': {
fontSize: 36,
},
color: 'tomato',
})
})

test('supports vendor properties', () => {
expect(css({ WebkitOverflowScrolling: 'touch' })(theme)).toStrictEqual({
WebkitOverflowScrolling: 'touch',
Expand All @@ -677,7 +709,6 @@ test('omits empty values', () => {
).toStrictEqual({ border: '1px solid black' })
})


test('borderTopWidth accepts number', () => {
expect(css({
borderTopWidth: 7,
Expand Down
7 changes: 4 additions & 3 deletions packages/docs/src/pages/theme-spec.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,15 @@ With Theme UI, _variants_ are the mechanism to use for such abstractions.
## Breakpoints

To configure the default breakpoints used in responsive array values, add a `breakpoints` array to your theme.
Each breakpoint should be a string with a CSS length unit included.
These values will be used to generate mobile-first (i.e. `min-width`) media queries, which can then be used to apply [responsive styles](/getting-started/#responsive-styles).
Each breakpoint should be a string with a CSS length unit included or a string including a CSS media query.
String values with a CSS length unit will be used to generate a mobile-first (i.e. `min-width`) media query.
The breakpoints can then be used to apply [responsive styles](/sx-prop/#responsive-values).

```js
// example custom breakpoints
{
breakpoints: [
'40em', '56em', '64em',
'40em', '@media (min-width: 56em) and (orientation: landscape)', '64em',
],
}
```
Expand Down
7 changes: 4 additions & 3 deletions packages/docs/src/pages/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,15 @@ To add base, top-level styles to the `<body>` element, use `theme.styles.root`.
## Breakpoints

To configure the default breakpoints used in responsive array values, add a `breakpoints` array to your theme.
Each breakpoint should be a string with a CSS length unit included.
These values will be used to generate mobile-first (i.e. `min-width`) media queries, which can then be used to apply [responsive styles](/sx-prop/#responsive-values).
Each breakpoint should be a string with a CSS length unit included or a string including a CSS media query.
String values with a CSS length unit will be used to generate a mobile-first (i.e. `min-width`) media query.
The breakpoints can then be used to apply [responsive styles](/sx-prop/#responsive-values).

```js
// example custom breakpoints
{
breakpoints: [
'40em', '56em', '64em',
'40em', '@media (min-width: 56em) and (orientation: landscape)', '64em',
],
}
```
Expand Down