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

@schlagerkhan/styled-components-media

v0.2.3

Published

Simple media query usage for styled-components

Downloads

7

Readme

Styled Components Media

A super simple library that makes it easier to handle media queries in styled-components.

OBS! This library injects the following properties into the styled-components default exported object:

  • minWidth, maxWidth
  • minHeight, maxHeight
  • minDevice, maxDevice

Installation

npm install @schlagerkhan/styled-components-media

or

yarn add @schlagerkhan/styled-components-media

Usage

Simple

/* App.js */
import styled from 'styled-components';
import { MediaThemeProvider } from '@schlagerkhan/styled-components-media';

export const App = () => (
	<MediaThemeProvider>
		<Comp />
	</MediaThemeProvider>
);

/* Comp.js */
import styled from 'styled-components';
// import { minWidth } from '@schlagerkhan/styled-components-media'; // if you want to

const Wrapper = styled.div`
	width: 100%;

	${styled.minWidth.laptop} {
		width: 50%;
	}
`;

export const Comp = () => <Wrapper>Text</Wrapper>;

Advanced

The following code exposes a component with the following properties:

  • The wrapper has a default width of 100% and 50% if the viewport if > 500px.
  • The text has a default font-size of 24px and 16px if the viewport is < 800px.
import styled from 'styled-components';
import { MediaThemeProvider, minWidth, maxWidth } from '@schlagerkhan/styled-components-media';

const WIDTH = { width1: 500, width2: 800 };
const THEME = {
	media: createMedia({ WIDTH }),
};

const Wrapper = styled.div`
	width: 100%;

	${minWidth.width1} {
		width: 50%;
	}
`;

const Text = styled.p`
	font-size: 24px;

	${maxWidth.width2} {
		font-size: 16px;
	}
`;

export const App = () => (
	<MediaThemeProvider theme={THEME}>
		<Wrapper>
			<Text>Hello</Text>
		</Wrapper>
	</MediaThemeProvider>
);

API

The package exposes the following modules/components:

MediaThemeProvider

A direct extension of styled-components's ThemeProvider but with a default set of media (see below). The media can be overridden (as seen in the example).

DEFAULT_MEDIA_WIDTH_SIZES

  • mobileS: 320
  • mobileM: 375
  • mobileL: 425
  • tablet: 600
  • laptop: 1024
  • laptopL: 1440
  • desktop: 2560

DEFAULT_MEDIA_HEIGHT_SIZES

  • mobileS: 568
  • mobileM: 667
  • mobileL: 812

DEFAULT_MEDIA_DEVICE_SIZES

Mind the case sensitivity

  • iPhoneSE: { width: 320, height: 568 }
  • iPhone678: { width: 375, height: 667 }
  • iPhone678Plus: { width: 414, height: 736 }
  • iPhoneX: { width: 375, height: 812 }

DEFAULT_MEDIA

  • WIDTH: DEFAULT_MEDIA_WIDTH_SIZES
  • HEIGHT: DEFAULT_MEDIA_HEIGHT_SIZES
  • DEVICE: DEFAULT_MEDIA_DEVICE_SIZES

createMedia

Helper function to create a custom media size schema to use with MediaThemeProvider.

const WIDTH = {
    width1: 500
};
const HEIGHT = {
    height1: 500
};
const DEVICE = {
    device1: {
        width: 200,
        height: 200
    }
}

const media = createMedia({ WIDTH, HEIGHT, DEVICE });
const theme = { media };

<MediaThemeProvider theme={theme}>
    {...}
</MediaThemeProvider>

minWidth, maxWidth

(also available as styled.minWidth and styled.maxWidth)

Uses the current theme to find out the value and translates it into a media query such as:

// CSS-in-JS
minWidth.laptop {
    background: red;
}

transpiles to

#CSS @media screen and (min-width: 1024px) {
	background: red;
}

minHeight, maxHeight

(also available as styled.minHeight and styled.maxHeight)

The same as min/maxWidth but is instead targetting the height dimension.

minDevice, maxDevice

(also available as styled.minDevice and styled.maxDevice)

In the same manner as above but with both the width and height dimension such as:

// CSS-in-JS
minDevice.iPhoneX {
	background: red;
}

transpiles to

#CSS @media screen and (min-width: 375px) and (min-height: 812px) {
	background: red;
}

Contribute

Pull requests are welcome.

License

MIT