Awesome
<div align="center"> <h1> <br> styled-breakpoints <br> <a href="https://github.com/mg901/styled-breakpoints/actions?query=workflow%3Arelease"> <img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/mg901/styled-breakpoints/release.yml?branch=master&style=flat-square"> </a> <a href="https://coveralls.io/github/mg901/styled-breakpoints?branch=master"> <img alt="coverage status" src="https://img.shields.io/coverallsCoverage/github/mg901/styled-breakpoints?style=flat-square"> </a> <a href="https://bundlephobia.com/package/styled-breakpoints"> <img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/styled-breakpoints?style=flat-square"> </a> <a href="https://img.shields.io/npm/dm/styled-breakpoints?style=flat-square"> <img alt="npm downloads" src="https://img.shields.io/npm/dm/styled-breakpoints?style=flat-square"> </a> <a href="https://www.npmjs.com/package/styled-breakpoints"> <img alt="npm version" src="https://img.shields.io/npm/v/styled-breakpoints.svg?style=flat-square"> </a> </h1> <p>Simple and powerful tool for creating media queries with SSR support.</p> <p> <a href="https://www.styled-components.com" rel="nofollow"> <img src="https://raw.githubusercontent.com/styled-components/brand/master/styled-components.png" alt="Styled Components Logo" width="80"> </a> <strong>OR</strong> <a href="https://emotion.sh/docs/introduction" rel="nofollow"><img src="https://raw.githubusercontent.com/emotion-js/emotion/main/emotion.png" alt="Emotion logo" width="80"></a> <p> </div> <br > <br >πΌ Preview
Inside components.
const Box = styled.div`
background-color: pink;
${({ theme }) => theme.breakpoints.up('sm')} {
background-color: hotpink;
}
${({ theme }) => theme.breakpoints.up('md')} {
background-color: red;
}
`;
<br>
Outside components.
import { useTheme } from 'styled-components'; // or '@emotion/react'
const Layout = () => {
const { up } = useTheme().breakpoints;
const isMd = useMediaQuery(up('md'));
return <>{isMd && <Box />}</>;
};
<br>
Examples
ππ» Mobile First
From smallest to largest
<div> <a href="https://codesandbox.io/s/rough-wave-u0uuu?fontsize=14&hidenavigation=1&module=%2Fsrc%2Fapp.tsx&theme=dark"> <img alt="Edit mobile-first" src="https://codesandbox.io/static/img/play-codesandbox.svg"> </a> </div> <br>ππ» Desktop First
From largest to smallest
<div> <a href="https://codesandbox.io/s/desktop-first-example-0plsg?fontsize=14&hidenavigation=1&module=%2Fsrc%2Fapp.tsx&theme=dark"> <img alt="Edit desktop first example" src="https://codesandbox.io/static/img/play-codesandbox.svg"> </a> </div> <br>ππ» Hooks API
<div> <a href="https://codesandbox.io/s/styled-components-hooks-api-6q6w8?fontsize=14&hidenavigation=1&module=%2Fsrc%2Fapp.tsx&theme=dark"> <img alt="Hooks api (styled-components)" src="https://codesandbox.io/static/img/play-codesandbox.svg"> </a> </div> <br> <br>π Documentation
-
π© getting started
π§ 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 <code>min-width</code>.
-
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
npm install styled-breakpoints@latest
# or
yarn add styled-breakpoints@latest
<br >
Configuration
π© File Structure
theme/
βββ index.ts
βββ styled.d.ts // or emotion.d.ts
app.tsx
<br>
π© Available breakpoints
Styled Breakpoints includes six default breakpoints, often referred to as grid tiers, for building responsive designs. These breakpoints can be customized.
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.
<br>π© Default Configuration
theme/index.ts
import { createStyledBreakpointsTheme } from 'styled-breakpoints';
export const theme = createStyledBreakpointsTheme();
<hr/>
<br>
Customization
π© Breakpoints
theme/index.ts
import { createStyledBreakpointsTheme } from 'styled-breakpoints';
export const theme = createStyledBreakpointsTheme({
breakpoints: {
small: '100px',
medium: '200px',
large: '300px',
xLarge: '400px',
xxLarge: '500px',
},
});
<br>
π¨ Merge with Another Theme
theme/index.ts
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(),
};
<hr>
<br>
<h4>π
Using with Styled Components</h4>
π© Installation
npm install styled-components
# or
yarn add styled-components
<br>
theme/styled.d.ts
import 'styled-components';
import { theme } from './index';
type ThemeConfig = typeof theme;
declare module 'styled-components' {
export interface DefaultTheme extends ThemeConfig {}
}
</details>
<hr>
<br>
<h4>
<g-emoji class="g-emoji" alias="woman_singer" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f469-1f3a4.png">π©‍π€</g-emoji>
Using with Emotion</h4>
π© Installation
npm install @emotion/{styled,react}
# or
yarn add @emotion/{styled,react}
<br >
theme/emotion.d.ts
import '@emotion/react';
import { theme } from './index';
type ThemeConfig = typeof theme;
declare module '@emotion/react' {
export interface Theme extends ThemeConfig {}
}
<hr/>
<br>
π Integration to Your App
<br>app.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 = () => (
<ThemeProvider theme={theme}>
<Box />
</ThemeProvider>
);
<br>
Media queries API
π Caching is implemented in all functions to optimize performance.
<br>ππ» Min-width - up
const Box = styled.div`
display: none;
${({ theme }) => theme.breakpoints.up('sm')} {
display: block;
}
`;
<br>
<strong>Will convert to vanilla css: </strong>
@media (min-width: 768px) {
display: block;
}
<hr/>
<br>
ππ» Max-width - down
We occasionally use media queries that go in the other direction (the given screen size or smaller):
<br>const Box = styled.div`
display: block;
${({ theme }) => theme.breakpoints.down('md')} {
display: none;
}
`;
<br>
<strong>Will convert to vanilla css: </strong>
@media (max-width: 767.98px) {
display: none;
}
<br>
<hr/> <br><strong>Why subtract .02px?</strong> Browsers donβt currently support range context queries, so we work around the limitations of min- and max- prefixes 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.
<br>const Box = styled.div`
background-color: pink;
${({ theme }) => theme.breakpoints.only('md')} {
background-color: rebeccapurple;
}
`;
<br>
<strong>Will convert to vanilla css: </strong>
@media (min-width: 768px) and (max-width: 991.98px) {
background-color: rebeccapurple;
}
<hr/>
<br>
ππ» Breakpoints range - between
Similarly, media queries may span multiple breakpoint widths.
<br>const Box = styled.div`
background-color: gold;
${({ theme }) => theme.breakpoints.between('md', 'xl')} {
background-color: rebeccapurple;
}
`;
<br>
<strong>Will convert to vanilla css: </strong>
@media (min-width: 768px) and (max-width: 1199.98px) {
background-color: rebeccapurple;
}
<hr/>
<br>
ππ» useMediaQuery
hook
features:
- π§ optimal performance by dynamically monitoring document changes in media queries.
- πͺπ» It supports SSR (server-side rendering).
- π¦ Minified and gzipped size 284b.
declare function useMediaQuery(query: string) => boolean
</details>
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 <AnotherComponent>{isMd && <Box />}</AnotherComponent>;
};
<hr/>
<br>
License
MIT License
Copyright (c) 2018-2024 Maxim Alyoshin.
This project is licensed under the MIT License - see the LICENSE file for details.
<hr> <br>