Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mg901/styled-breakpoints
Simple and powerful breakpoints for styled components and emotion.
https://github.com/mg901/styled-breakpoints
breakpoint breakpoints css-in-js css-in-react emotion media media-queries media-query preact react responsive-design responsive-layout styled-components styled-media-query typescript
Last synced: 4 days ago
JSON representation
Simple and powerful breakpoints for styled components and emotion.
- Host: GitHub
- URL: https://github.com/mg901/styled-breakpoints
- Owner: mg901
- License: mit
- Created: 2018-05-01T01:32:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-28T20:23:37.000Z (3 months ago)
- Last Synced: 2024-10-29T17:32:24.480Z (3 months ago)
- Topics: breakpoint, breakpoints, css-in-js, css-in-react, emotion, media, media-queries, media-query, preact, react, responsive-design, responsive-layout, styled-components, styled-media-query, typescript
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/styled-breakpoints
- Size: 12.7 MB
- Stars: 551
- Watchers: 8
- Forks: 24
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome - styled-breakpoints - Simple and powerful tool for creating breakpoints in styled components, emotion, linaria and astroturf. π (JavaScript)
README
styled-breakpointsSimple and powerful tool for creating media queries with SSR support.
## πΌ Preview
**Inside** components.
```tsx
const Box = styled.div`
background-color: pink;${({ theme }) => theme.breakpoints.up('sm')} {
background-color: hotpink;
}${({ theme }) => theme.breakpoints.up('md')} {
background-color: red;
}
`;
```
**Outside** components.
```tsx
import { useTheme } from 'styled-components'; // or '@emotion/react'const Layout = () => {
const { up } = useTheme().breakpoints;
const isMd = useMediaQuery(up('md'));return <>{isMd && }>;
};
```
## Examples
### ππ» Mobile First
From smallest to largest
### ππ» Desktop First
From largest to smallest
### ππ» Hooks API
## π Documentation
- [core concepts](#core-concepts)
- π© [getting started](#getting-started)
- [Media Queries API](#media-queries-api)- [min-width - up](#min-width---up)
- [max-width - down](#max-width---down)
- [single breakpoint - only](#single-breakpoint---only)
- [breakpoints range - between](#breakpoints-range---between)
- [customization](#customization)- [useMediaQuery hook](#usemediaquery-hook)
## π§ Core concepts
- **Breakpoints act as the fundamental elements of responsive design**. They enable you to control when your layout can adapt to a specific viewport or device size.
- **Utilize media queries to structure your CSS based on breakpoints**. Media queries are CSS features that allow you to selectively apply styles depending on a defined set of browser and operating system parameters. The most commonly used media query property is
min-width
.- **The objective is mobile-first, responsive design**. Styled Breakpoints aims to apply the essential styles required for a layout to function at the smallest breakpoint. Additional styles are then added to adjust the design for larger devices. This approach optimizes your CSS, enhances rendering speed, and delivers an excellent user experience.
## Getting Started
### π© Installation
```sh
npm install styled-breakpoints@latest# or
yarn add styled-breakpoints@latest
```
### Configuration
#### π© File Structure
```js
theme/
βββ index.ts
βββ styled.d.ts // or emotion.d.ts
app.tsx
```
#### π© Available breakpoints
Styled Breakpoints includes six default breakpoints, often referred to as grid tiers, for building responsive designs. These breakpoints can be [customized](#customization).
```tsx
const breakpoints = {
xs: '0px',
sm: '576px',
md: '768px',
lg: '992px',
xl: '1200px',
xxl: '1400px',
};
```Each breakpoint has been carefully selected to accommodate containers with widths that are multiples of 12. The breakpoints also represent a subset of common device sizes and viewport dimensions, although they do not specifically target every use case or device. Instead, they provide a robust and consistent foundation for building designs that cater to nearly any device.
#### π© Default Configuration
`theme/index.ts`
```tsx
import { createStyledBreakpointsTheme } from 'styled-breakpoints';export const theme = createStyledBreakpointsTheme();
```
#### Customization
##### π© Breakpoints
`theme/index.ts`
```tsx
import { createStyledBreakpointsTheme } from 'styled-breakpoints';export const theme = createStyledBreakpointsTheme({
breakpoints: {
small: '100px',
medium: '200px',
large: '300px',
xLarge: '400px',
xxLarge: '500px',
},
});
```
##### π¨ Merge with Another Theme
`theme/index.ts`
```tsx
import { createStyledBreakpointsTheme } from 'styled-breakpoints';export const primaryTheme = {
fonts: ['sans-serif', 'Roboto'],
fontSizes: {
small: '1em',
medium: '2em',
large: '3em',
},
} as const;export const theme = {
...primaryTheme,
...createStyledBreakpointsTheme(),
};
```
π Using with Styled Components
##### π© Installation
```sh
npm install styled-components# or
yarn add styled-components
```
`theme/styled.d.ts`
```ts
import 'styled-components';
import { theme } from './index';type ThemeConfig = typeof theme;
declare module 'styled-components' {
export interface DefaultTheme extends ThemeConfig {}
}
```
π©βπ€
Using with Emotion##### π© Installation
```sh
npm install @emotion/{styled,react}# or
yarn add @emotion/{styled,react}
```
`theme/emotion.d.ts`
```ts
import '@emotion/react';
import { theme } from './index';type ThemeConfig = typeof theme;
declare module '@emotion/react' {
export interface Theme extends ThemeConfig {}
}
```
### π Integration to Your App
`app.tsx`
```tsx
import styled { ThemeProvider } from 'styled-components'; // or '@emotion/react'
import { theme } from './theme';const Box = styled.div`
display: none;${({ theme }) => theme.breakpoints.up('sm')} {
display: block;
}
`;const App = () => (
);
```
## Media queries API
π Caching is implemented in all functions to optimize performance.
### ππ» Min-width - `up`
```tsx
const Box = styled.div`
display: none;${({ theme }) => theme.breakpoints.up('sm')} {
display: block;
}
`;
```
Will convert to vanilla css:```css
@media (min-width: 768px) {
display: block;
}
```
### ππ» Max-width - `down`
We occasionally use media queries that go in the other direction (the given screen size or smaller):
```tsx
const Box = styled.div`
display: block;${({ theme }) => theme.breakpoints.down('md')} {
display: none;
}
`;
```
Will convert to vanilla css:```css
@media (max-width: 767.98px) {
display: none;
}
```
> Why subtract .02px? Browsers donβt currently support [range context queries](https://www.w3.org/TR/mediaqueries-4/#range-context), so we work around the limitations of [min- and max- prefixes](https://www.w3.org/TR/mediaqueries-4/#mq-min-max) and viewports with fractional widths (which can occur under certain conditions on high-dpi devices, for instance) by using values with higher precision.
### ππ» Single breakpoint - `only`
There are also media queries and mixins for targeting a single segment of screen sizes using the minimum and maximum breakpoint widths.
```tsx
const Box = styled.div`
background-color: pink;${({ theme }) => theme.breakpoints.only('md')} {
background-color: rebeccapurple;
}
`;
```
Will convert to vanilla css:```css
@media (min-width: 768px) and (max-width: 991.98px) {
background-color: rebeccapurple;
}
```
### ππ» Breakpoints range - `between`
Similarly, media queries may span multiple breakpoint widths.
```tsx
const Box = styled.div`
background-color: gold;${({ theme }) => theme.breakpoints.between('md', 'xl')} {
background-color: rebeccapurple;
}
`;
```
Will convert to vanilla css:```css
@media (min-width: 768px) and (max-width: 1199.98px) {
background-color: rebeccapurple;
}
```
## ππ» `useMediaQuery` hook
features:
- π§ optimal performance by dynamically monitoring document changes in media queries.
- πͺπ» It supports SSR (server-side rendering).
- π¦ Minified and gzipped size 284b.
Type declaration
```ts
declare function useMediaQuery(query: string) => boolean
``````tsx
import { useTheme } from 'styled-components'; // or from '@emotion/react'
import { useMediaQuery } from 'styled-breakpoints/use-media-query';
import { Box } from 'third-party-library';const SomeComponent = () => {
const { only } = useTheme().breakpoints;
const isMd = useMediaQuery(only('md'));return {isMd && };
};
```
## License
MIT License
Copyright (c) 2018-2024 [Maxim Alyoshin](https://github.com/mg901).
This project is licensed under the MIT License - see the [LICENSE](https://github.com/mg901/styled-breakpoints/blob/master/LICENCE) file for details.
## Contributors
mg901
π¬ π» π¨ π π‘ π π§ π π£ π¬ π β οΈ π§ β
Abu Shamsutdinov
π π» π‘ π€ π π’
Sova
π» π‘ π€ π π’
Jussi Kinnula
π π»
RafaΕ Wyszomirski
π
Adrian CelczyΕski
π π»
Sam Holmes
π» π€
Ontopic
π€
Ryan Bell
π€
Bart Nagel
π π» π‘ π€
Greg McKelvey
π»
Buck DeFore
π€
Pierre Burel
π
Pawel Kochanek
π π»
Ian Christopher B. de Jesus
π
David de Lusenet
π π€
Dan Adler
π