Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Utility function for using breakpoints with styled-components 💅.

NotificationsYou must be signed in to change notification settings

jameslnewell/styled-components-breakpoint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npmnpm bundle size (minified + gzip)npmBuild Status

Utility functions for creating breakpoints instyled-components 💅.

Change log

Have a look 👀 atstyled-components-spacing andstyled-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

importstyledfrom'styled-components';importbreakpointfrom'styled-components-breakpoint';constHeading=styled.h1`  color: #444;  font-family: sans-serif;  font-size: 12px;${breakpoint('tablet')`    font-size: 16px;  `}${breakpoint('desktop')`    font-size: 24px;  `}`;exportdefaultHeading;

./index.jsx

importReactfrom'react';importHeadingfrom'./Heading';<Heading>Hello World!</Heading>

Using custom breakpoints

Breakpoints may be customised usingThemeProvider. For example, to use the same breakpoints asBootstrap, you can do so like this:

./Heading.jsx

importstyledfrom'styled-components';importbreakpointfrom'styled-components-breakpoint';constHeading=styled.h1`  color: #444;  font-family: sans-serif;${breakpoint('sm')`    font-size: 12px;  `}${breakpoint('md')`    font-size: 16px;  `}${breakpoint('lg')`    font-size: 24px;  `}`;exportdefaultHeading;

./index.jsx

importReactfrom'react';import{ThemeProvider}from'styled-components';consttheme={breakpoints:{xs:0,sm:576,md:768,lg:992,xl:1200}};<ThemeProvidertheme={theme}><Heading>Hello World!</Heading></ThemeProvider>

API

breakpoint(gte)

breakpoint(gte, lt)

Wraps styles in a@media block.

Properties:

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

Returns:

The@media block.

Example:
importstyledfrom'styled-components';importbreakpointfrom'styled-components-breakpoint';constThing=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} orT. 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:
importstyledfrom'styled-components';import{map}from'styled-components-breakpoint';constThing=styled.div`${({size})=>map(size,val=>`width:${Math.round(val*100)}%;`)}`;<Thingsize={{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:

  • anobject containing the breakpoints, thebreakpoint andmap functions
Example:
importstyledfrom'styled-components';import{createStatic}from'styled-components-breakpoint';constbreakpoints=createStatic();constThing=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 devicestablet: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)}

About

Utility function for using breakpoints with styled-components 💅.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp