14.1.3 β€’ Published 15 days ago

styled-breakpoints v14.1.3

Weekly downloads
12,953
License
MIT
Repository
github
Last release
15 days ago

🌼 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;
  }
`;

Outside components.

import { useTheme } from 'styled-components'; // or '@emotion/react'

const Layout = () => {
  const { breakpoints } = useTheme();
  const isMd = useMediaQuery(breakpoints.up('md'));

  return <>{isMd && <Box />}</>;
};

Examples

πŸ‘‰πŸ» Mobile First

From smallest to largest

πŸ‘‰πŸ» Desktop First

From largest to smallest

πŸ‘‰πŸ» Hooks API

πŸ“– Documentation

🧐 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

npm install styled-breakpoints@latest

# or

yarn add styled-breakpoints@latest

Configuration

🚩 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.

🚩 Default Configuration

theme/config.ts

import { createStyledBreakpointsTheme } from 'styled-breakpoints';

export const theme = createStyledBreakpointsTheme();

Customization

🚩 Breakpoints

theme/config.ts

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/config.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(),
};
🚩 Installation
npm install styled-components

# or

yarn add styled-components

theme/styled.d.ts

import 'styled-components';
import { theme } from './theme/config';

type MyTheme = typeof theme;

declare module 'styled-components' {
  export interface DefaultTheme extends MyTheme {}
}
🚩 Installation
npm install @emotion/{styled,react}

# or

yarn add @emotion/{styled,react}

theme/emotion.d.ts

import '@emotion/react';
import { theme } from './theme/config';

type MyTheme = typeof theme;

declare module '@emotion/react' {
  export interface Theme extends MyTheme {}
}

πŸš€ Integration to App

app.tsx

import styled { ThemeProvider } from 'styled-components'; // or '@emotion/react'
import { theme } from './theme/config';

const Box = styled.div`
  display: none;

  ${({ theme }) => theme.breakpoints.up('sm')} {
    display: block;
  }
`;

const App = () => (
  <ThemeProvider theme={theme}>
    <Box>πŸ₯³</Box>
  </ThemeProvider>
);

Media queries API

πŸš€ Caching is implemented in all functions to optimize performance.

Min-width - up

  declare function up(
    min: T,
    orientation?: 'portrait' | 'landscape'
  ) => string
const Box = styled.div`
  display: none;

  ${({ theme }) => theme.breakpoints.up('sm')} {
    display: block;
  }
`;
@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):

  declare function down(
    max: string,
    orientation?: 'portrait' | 'landscape'
  ) => string
const Box = styled.div`
  display: block;

  ${({ theme }) => theme.breakpoints.down('md')} {
    display: none;
  }
`;
@media (max-width: 767.98px) {
  display: none;
}

Why subtract .02px? 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.

  declare function only(
    name: string,
    orientation?: 'portrait' | 'landscape'
  ) => string
const Box = styled.div`
  background-color: pink;

  ${({ theme }) => theme.breakpoints.only('md')} {
    background-color: rebeccapurple;
  }
`;
@media (min-width: 768px) and (max-width: 991.98px) {
  background-color: rebeccapurple;
}

Breakpoints range - between

Similarly, media queries may span multiple breakpoint widths.

 declare function between(
    min: string,
    max: string,
    orientation?: 'portrait' | 'landscape'
  ) => string
const Box = styled.div`
  background-color: gold;

  ${({ theme }) => theme.breakpoints.between('md', 'xl')} {
    background-color: rebeccapurple;
  }
`;
@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.
 declare function useMediaQuery(query: string) => boolean
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 { breakpoints } = useTheme();
  const isMd = useMediaQuery(breakpoints.only('md'));

  return <AnotherComponent>{isMd && <Box />}</AnotherComponent>;
};

License

MIT License

Copyright (c) 2018-2019 Maxim Alyoshin.

This project is licensed under the MIT License - see the LICENSE file for details.

Contributors

14.1.3

15 days ago

14.1.2

1 month ago

14.1.1

1 month ago

14.1.0

2 months ago

13.1.3

3 months ago

13.1.4

3 months ago

14.0.0

3 months ago

13.0.0

3 months ago

13.1.1

3 months ago

13.1.2

3 months ago

13.1.0

3 months ago

12.1.10

3 months ago

12.1.6

6 months ago

12.1.7

6 months ago

12.1.8

6 months ago

12.1.9

6 months ago

12.0.3

11 months ago

12.0.4

11 months ago

12.0.0

11 months ago

12.0.1

11 months ago

12.0.2

11 months ago

12.0.0-beta.7

11 months ago

12.1.2

10 months ago

12.1.3

9 months ago

12.1.4

9 months ago

12.1.5

9 months ago

12.1.0

11 months ago

12.1.1

11 months ago

11.2.2

1 year ago

11.2.3

1 year ago

11.2.0

1 year ago

11.2.1

1 year ago

11.1.5

1 year ago

11.1.2

1 year ago

12.0.0-beta.4

12 months ago

12.0.0-beta.5

12 months ago

12.0.0-beta.2

12 months ago

12.0.0-beta.3

12 months ago

12.0.0-beta.1

12 months ago

12.0.0-beta.6

12 months ago

11.1.1

2 years ago

11.1.0

2 years ago

11.0.5

2 years ago

10.2.3

2 years ago

10.2.0

2 years ago

10.2.1

2 years ago

10.2.2

2 years ago

11.0.3-alpha.4

2 years ago

11.0.3-alpha.5

2 years ago

11.0.3-alpha.2

2 years ago

11.0.3-alpha.3

2 years ago

11.0.3-alpha.1

2 years ago

11.0.4

2 years ago

11.0.2

2 years ago

11.0.3

2 years ago

11.0.0

2 years ago

11.0.1

2 years ago

10.1.0

2 years ago

10.1.1

2 years ago

10.1.0-alpha.1

2 years ago

10.1.0-alpha.2

2 years ago

10.1.0-alpha.0

2 years ago

10.0.1

3 years ago

10.0.0

3 years ago

9.0.11-alpha.1

3 years ago

9.0.12

3 years ago

9.0.11-aplpha.1

3 years ago

9.0.11-alpha.0

3 years ago

9.0.10-alpha.11

3 years ago

9.0.10-alpha.10

3 years ago

9.0.11

3 years ago

9.0.10-alpha.13

3 years ago

9.0.10

3 years ago

9.0.10-alpha.12

3 years ago

9.0.10-alpha.0

3 years ago

9.0.10-alpha.1

3 years ago

9.0.10-alpha.2

3 years ago

9.0.10-alpha.3

3 years ago

9.0.10-alpha.4

3 years ago

9.0.10-alpha.5

3 years ago

9.0.10-alpha.6

3 years ago

9.0.10-alpha.7

3 years ago

9.0.10-alpha.8

3 years ago

9.0.10-alpha.9

3 years ago

9.0.9

3 years ago

9.0.8

3 years ago

9.0.7

3 years ago

9.0.6

3 years ago

9.0.5

3 years ago

9.0.4

3 years ago

9.0.3

3 years ago

9.0.2

3 years ago

9.0.1

3 years ago

9.0.0

3 years ago

9.0.0-alpha

3 years ago

8.2.3

3 years ago

8.2.2

3 years ago

8.2.1

3 years ago

8.2.0-alpha.2

3 years ago

8.2.0-alpha.1

3 years ago

8.2.0-alpha.4

3 years ago

8.1.3

3 years ago

8.2.0-alpha.3

3 years ago

8.2.0

3 years ago

8.2.0-rc

3 years ago

8.2.0-alpha

3 years ago

8.1.2

4 years ago

8.1.1

4 years ago

8.1.0

4 years ago

8.0.1

4 years ago

8.0.0

4 years ago

7.2.0

4 years ago

7.1.0

4 years ago

7.0.0

4 years ago

6.11.0

4 years ago

6.10.1

4 years ago

6.10.0

4 years ago

6.9.1

5 years ago

6.9.0

5 years ago

6.8.0

5 years ago

6.7.3

5 years ago

6.7.2

5 years ago

6.7.1

5 years ago

6.7.0

5 years ago

6.6.4

5 years ago

6.6.3

5 years ago

6.6.2

5 years ago

6.6.1

5 years ago

6.6.0

5 years ago

6.5.8

5 years ago

6.5.7

5 years ago

6.5.6

5 years ago

6.5.5

5 years ago

6.5.4

5 years ago

6.5.3

5 years ago

6.5.2

5 years ago

6.5.1

5 years ago

6.5.0

5 years ago

6.4.3

5 years ago

6.4.2

5 years ago

6.4.1

5 years ago

6.4.0

5 years ago

6.3.0

5 years ago

6.2.0

5 years ago

6.1.5

5 years ago

6.1.4

5 years ago

6.1.3

5 years ago

6.0.0

5 years ago

1.0.0

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.6

6 years ago

2.0.7

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.0-beta.1

6 years ago

0.0.1-alpha.16

6 years ago

0.0.1-alpha.15

6 years ago

0.0.1-alpha.14

6 years ago

0.0.1-alpha.13

6 years ago

0.0.1-alpha.12

6 years ago

0.0.1-alpha.11

6 years ago

0.0.1-alpha.10

6 years ago

0.0.1-alpha.9

6 years ago

0.0.1-alpha.8

6 years ago

0.0.1-alpha.7

6 years ago

0.0.1-alpha.6

6 years ago

0.0.1-alpha.5

6 years ago

0.0.1-alpha.4

6 years ago

0.0.1-alpha.3

6 years ago

0.0.1-alpha.2

6 years ago

0.0.1-alpha.1

6 years ago

0.0.1

6 years ago