npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@humblebee/styled-components-breakpoint

v2.1.4

Published

Utility functions for creating breakpoints in styled-components.

Downloads

754

Readme

Commitizen friendly codecov semantic-release Travis npm npm

Styled Components Breakpoint 💅

This library provides utility functions for dealing with media queries, to make them reusable and easier to read. It can be used as an alternative to SASS/LESS mixins.

More on mixins and Styled Components in this article.


Installation

yarn install @humblebee/styled-components-breakpoint
# or
npm install @humblebee/styled-components-breakpoint

Usage and setup

The default export of styled-components-breakpoint is a function that accepts a config object of breakpoints. This will return an object with three main utility methods/mixins: up (min-width), down (max-width) and only (a range between two media queries), all described in detail below.

import styledBreakpoint from '@humblebee/styled-components-breakpoint';

// Creates an object with breakpoint utility methods.
export const breakpoint = styledBreakpoint({
  xxs: 0,
  xs: 320,
  s: 576,
  m: 768,
  l: 992,
  xl: 1200,
});

Up

breakpoint.up('m')
// Will return a media query with a min-width of 768
// @media only screen and (min-width: 768px)

Down

breakpoint.down('m')
// Will return a media query with a max-width of 768
// @media only screen and (max-width: 768px)

Only

breakpoint.only('m')
// Will return a range media query between "m" and the next upper breakpoint "l"
// @media only screen and (min-width: 768px) and (max-width: 1200px)
breakpoint.only('m', 'xl')
// Will return a range media query between "m" and the breakpoint passed as the second argument, "xl"
// @media only screen and (min-width: 768px) and (max-width: 1200px)

Shorthand

There is also a shorthand for mobile first media queries (min-width). Calling breakpoint.m is the same as breakpoint.up('m').

breakpoint.m
// Will return a media query with a min-width of 768
// @media only screen and (min-width: 768px)

Usage with styled components

In the following example we create a styled button component.

This is the folder structure we'll be working with.

.
+--components
|  +--Button.js
+--themes
|  +--mixins.js

themes/mixins.js

import styledBreakpoint from '@humblebee/styled-components-breakpoint';

// Create an instance of styled-components-breakpoint and pass it an object of breakpoints.
export const breakpoint = styledBreakpoint({
  xs: 320,
  s: 576,
  m: 768,
  l: 992,
  xl: 1200,
});

components/Button.js

// Styled Components
import styled from 'styled-components';
// Our breakpoint instance/mixin
import { breakpoint } from '../../theme/mixins';

const Button = styled.button`
    background: white;
    font-size: 18px;
    ${breakpoint.down('s')}`
      font-size: 12px;
    `
    ${breakpoint.m}`
      background: palevioletred;
    `
  `
});

The first mixin breakpoint.down('s'), will give the styled button component a font size of 12px, at a breakpoint bellow "s", i.e. max-width(320px).

The second mixin breakpoint.m, uses the short hand version of breakpoint.up.('m'), and will give the button a background of palevioletred, at a breakpoint above "m", i.e. min-width(768).


Happy coding!

/ The bees at Humblebee 🐝