-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs for media queries and reusable media queries (#439)
* add doc for reusable media queries * fix syntax highlighting
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Media Queries | ||
|
||
```jsx | ||
const Avatar = styled('img')` | ||
width: 32px; | ||
height: 32px; | ||
border-radius: 50%; | ||
@media (min-width: 420px) { | ||
width: 96px; | ||
height: 96px; | ||
} | ||
` | ||
|
||
render(<Avatar src={logoUrl} rounded />) | ||
``` | ||
|
||
## Reusable Media Queries | ||
|
||
[Demo](https://stackblitz.com/edit/react-wudbyn) | ||
|
||
```jsx | ||
const breakpoints = { | ||
// Numerical values will result in a min-width query | ||
small: 576, | ||
medium: 768, | ||
large: 992, | ||
xLarge: 1200, | ||
// String values will be used as is | ||
tallPhone: '(max-width: 360px) and (min-height: 740px)' | ||
}; | ||
|
||
export const queries = Object.keys(breakpoints).reduce((accumulator, label) => { | ||
if (typeof breakpoints[label] === 'string') { | ||
accumulator[label] = (...args) => | ||
css` | ||
@media (${breakpoints[label]}) { | ||
${css(...args)}; | ||
} | ||
`; | ||
} else { | ||
accumulator[label] = (...args) => | ||
css` | ||
@media (min-width: ${breakpoints[label]}px) { | ||
${css(...args)}; | ||
} | ||
`; | ||
} | ||
|
||
return accumulator; | ||
}, {}); | ||
``` | ||
|
||
```jsx | ||
import { queries } from './mediaQueries.js`; | ||
const paragraph = css` | ||
font-size: 12px; | ||
${queries.medium` | ||
font-size: 14px; | ||
`} | ||
${queries.large` | ||
font-size: 16px; | ||
`} | ||
`; | ||
``` |