• Stars
    star
    240
  • Rank 168,229 (Top 4 %)
  • Language
    JavaScript
  • Created almost 8 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Utility function for using breakpoints with styled-components πŸ’….

styled-components-breakpoint

npm npm bundle size (minified + gzip) npm Build Status

Utility functions for creating breakpoints in styled-components πŸ’….

Change log

Have a look πŸ‘€ at styled-components-spacing and styled-components-grid which both work well with this package.

Installation

yarn add styled-components styled-components-breakpoint

Usage

Examples

Using the default breakpoints

./Heading.jsx

import styled from 'styled-components';
import breakpoint from 'styled-components-breakpoint';

const Heading = styled.h1`

  color: #444;
  font-family: sans-serif;
  
  font-size: 12px;
  
  ${breakpoint('tablet')`
    font-size: 16px;
  `}
  
  ${breakpoint('desktop')`
    font-size: 24px;
  `}
  
`;

export default Heading;

./index.jsx

import React from 'react';
import Heading from './Heading';

<Heading>Hello World!</Heading>

Using custom breakpoints

Breakpoints may be customised using ThemeProvider. For example, to use the same breakpoints as Bootstrap, you can do so like this:

./Heading.jsx

import styled from 'styled-components';
import breakpoint from 'styled-components-breakpoint';

const Heading = styled.h1`

  color: #444;
  font-family: sans-serif;
  
  ${breakpoint('sm')`
    font-size: 12px;
  `}
  
  ${breakpoint('md')`
    font-size: 16px;
  `}
  
  ${breakpoint('lg')`
    font-size: 24px;
  `}
  
`;

export default Heading;

./index.jsx

import React from 'react';
import {ThemeProvider} from 'styled-components';

const theme = {
  breakpoints: { 
    xs: 0,
    sm: 576,
    md: 768,
    lg: 992,
    xl: 1200
  }
};

<ThemeProvider theme={theme}>
  <Heading>Hello World!</Heading>
</ThemeProvider>

API

breakpoint(gte)

breakpoint(gte, lt)

Wraps styles in a @media block.

Properties:

  • gte - Required. A string. The name of the breakpoint from which the styles will apply.
  • lt - Optional. A string. The name of the breakpoint at which the styles will no longer apply.

Returns:

The @media block.

Example:
import styled from 'styled-components';
import breakpoint from 'styled-components-breakpoint';

const Thing = styled.div`

  font-size: 12px;

  ${breakpoint('tablet')`
    font-size: 16px;
  `};

  ${breakpoint('desktop')`
    font-size: 24px;
  `};
  
`;

<Thing/>
Output:
.cESAFm {
  font-size: 12px;
}

@media (min-width: 46.0625em) {
  .cESAFm {
    font-size: 16px;
  }
}

@media (min-width: 64.0625em) {
  .cESAFm {
    font-size: 24px;
  }
}

map(value, mapValueToCSS)

Maps values to styles in @media blocks.

Properties:

  • value - Required. {[string]: T} or T. A map of values to breakpoint names.
  • mapValueToCSS - Required. T => string. A function to map a value to styles at the specified breakpoint.

Returns:

The @media blocks.

Example:
import styled from 'styled-components';
import {map} from 'styled-components-breakpoint';

const Thing = styled.div`
  ${({size}) => map(size, val => `width: ${Math.round(val * 100)}%;`)}
`;

<Thing size={{mobile: 1, tablet: 1/2, desktop: 1/4}}/>
Output:
.cESAFm {
  width: 100%;
}

@media (min-width: 46.0625em) {
  .cESAFm {
    width: 50%;
  }
}

@media (min-width: 64.0625em) {
  .cESAFm {
    width: 25%;
  }
}

createStatic()

createStatic(breakpoints)

Creates a static set of breakpoints which aren't themable.

Properties:

  • breakpoints - Optional. {[string]: number}. A map of breakpoint names and sizes.

Returns:

  • an object containing the breakpoints, the breakpoint and map functions
Example:
import styled from 'styled-components';
import {createStatic} from 'styled-components-breakpoint';

const breakpoints = createStatic();

const Thing = styled.div`

  font-size: 12px;

  ${breakpoints.tablet`
    font-size: 16px;
  `};

  ${breakpoints.desktop`
    font-size: 24px;
  `};
  
`;

<Thing/>
Output:
.cESAFm {
  font-size: 12px;
}

@media (min-width: 46.0625em) {
  .cESAFm {
    font-size: 16px;
  }
}

@media (min-width: 64.0625em) {
  .cESAFm {
    font-size: 24px;
  }
}

Default breakpoints

The default breakpoints are:

{
    mobile: 0,      // targeting all devices
    tablet: 737,    // targeting devices that are larger than the iPhone 6 Plus (which is 736px in landscape mode)
    desktop: 1025   // targeting devices that are larger than the iPad (which is 1024px in landscape mode)
}

More Repositories

1

styled-components-grid

A responsive grid components for styled-components.
JavaScript
220
star
2

styled-components-spacing

Responsive margin and padding components for styled-components πŸ’….
JavaScript
213
star
3

xhr-mock

Utility for mocking XMLHttpRequest.
TypeScript
197
star
4

react-lazily-render

Delay mounting expensive components until a placeholder component has been scrolled into view.
JavaScript
22
star
5

react-create-component-from-tag-prop

Create a `react` component from a `tag` prop.
JavaScript
18
star
6

redux-when

Redux middleware for delaying dispatch of an action until a condition evaluates to true.
JavaScript
18
star
7

redux-formo

An alternate forms framework for Redux+React.
JavaScript
9
star
8

satay

A utility for uploading static sites to AWS S3.
TypeScript
9
star
9

react-render-image

Render an image in React.
TypeScript
8
star
10

wait-for-event

Wait for events to be triggered.
TypeScript
6
star
11

react-testutils-render

A lite wrapper for shallow rendering React components.
JavaScript
6
star
12

substance

βš—οΈPrimitives for building components.
TypeScript
5
star
13

publish-if-not-published

Publish a package if the current version isn't already published.
TypeScript
4
star
14

sass-triangle

Mixin for creating CSS triangles.
CSS
4
star
15

tradie-v4

A framework for building tools like `create-react-app` tailored to your use case and tech stack.
TypeScript
4
star
16

webpack-resolve-short-path-plugin

A plugin for `enhanced-resolve` (Webpack v2) that resolves paths like `~module` to `%rootPath%/module` so you don't have to write relative paths like `../../../module`.
JavaScript
4
star
17

react-firebase

React hooks for working with firebase.
TypeScript
3
star
18

koa

A number of packages made to work with koa.
TypeScript
3
star
19

tradie

A semi-opinionated build tool for frontend projects. Use it to lint, bundle and test your script and style files.
JavaScript
3
star
20

run-if-diff

Run a command if files have changed.
JavaScript
3
star
21

react-app-example

JavaScript
1
star
22

react

πŸ“¦ βš›οΈ A collection of packages for building React applications.
TypeScript
1
star
23

react-testutils-query

Utility functions for retrieving elements from within a React virtual-dom tree.
JavaScript
1
star
24

buildkite-pipelines

Generate Buildkite pipelines from code.
TypeScript
1
star
25

now-deploy-action

Deploy a project using ZEIT Now.
JavaScript
1
star
26

rev-manifest-path

Get the cache busted path of an asset from a `gulp-rev` or `gulp-rev-all` manifest.
JavaScript
1
star
27

cancelable-promise

A cancelable promise implementation.
TypeScript
1
star
28

tradie-template-react-static-site

Tradie template for building static sites with React.
JavaScript
1
star
29

generator-fed

Yeoman generator for Frontend Development with Browserify and SASS Composer.
JavaScript
1
star
30

react-static-site-example

react-static-site-example
JavaScript
1
star
31

design-system-example

A demonstration of the tooling setup for a React based Design System.
TypeScript
1
star
32

koa-reload-middleware

Reload middleware if it changes during development.
TypeScript
1
star
33

workspace-metadata-webpack-plugin

Display workspace metadata on your website.
TypeScript
1
star