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

styled-map

v3.3.0

Published

<br /><div align="center">

Downloads

7,815

Readme

A better way to map props to styles

Simple CSS-like syntax, for Styled Components and Emotion

Example

Install

yarn add styled-map

or

npm install styled-map --save

Why use Styled Map?

Styled Map simplifies your components' CSS, making your code cleaner and clearer wherever you use props to alter styles.

Without Styled Map

With Styled Components alone, you'll often do something like this:

const Button = styled.button`
  color: ${props => props.primary ? '#0c0' : '#ccc'};
`;

but this quickly turns messy:

const Button = styled.button`
 color: ${props =>
   props.primary && '#0c0' ||
   props.warning && '#c00' ||
   props.info && '#0cc' ||
   '#ccc'
 };
 border: 2px solid ${props =>
   props.primary && '#0c0' ||
   props.warning && '#c00' ||
   props.info && '#0cc' ||
   '#ccc'
 };
 font-size: ${props =>
   props.small && '8px' ||
   props.medium && '18px' ||
   props.large && '32px' ||
   '16px'
 };
`;

<Button primary large>Submit</Button>

With Styled Map

Here's the same component using styled-map:

import styledMap from 'styled-map';

const buttonColor = styledMap`
  primary: #0c0;
  warning: #c00;
  info: #0cc;
  default: #ccc;
`;

const Button = styled.button`
  color: ${buttonColor};
  border: 2px solid ${buttonColor};
  font-size: ${styledMap`
    large: 32px;
    small: 8px;
    medium: 18px;
    default: 16px;
  `};
`;

<Button primary large>Submit</Button>

Much better!

Note: If there are no matching props, styled-map will look for a "default" item in your map. If it doesn't find one it will use the last item by default.

What's with the pseudo-CSS syntax?

Until v3.0, Styled Map used JavaScript objects like this. Read more about the decision to use a new syntax.

You can still use objects if you prefer, and there are currently no plans to deprecate this option.

Usage with themes

Styled Map makes mapping to themes incredibly easy with the mapToTheme function.

Simply set up your themes like this:

const myTheme = {
  buttonColor: {
    primary: 'orange',
    warning: 'red',
    info: 'blue',
    default: 'white',
  },
  ...
};

and now you can do this:

import styledMap, { mapToTheme as theme } from 'styled-map';

const Button = styled.button`
  color: ${theme('buttonColor')};
  border: 2px solid ${theme('buttonColor')};
`;

Note: importing as theme is optional, but it reads a lot better!

Nested theme objects

Nested objects can be refered to with dots, so you can write theme('colors.button') if your theme looks like this:

const theme = {
  colors: {
    button: {
      primary: '#b00',
      info: '#0b0',
      etc: '#00f',
    }
  }
}

Optionally mapping to prop values

Sometimes you'll want to map styles to the value of a prop instead of using prop keys. This is especially useful if you have something like a type variable to pass to your component and you don't want to do something like <Button {...{[type]:true}} />.

You can use styled-map in these situations by simply passing a prop name as the first argument.

const Button = styled.button`
  background: ${styledMap('type', {
    primary: '#c00',
    default: '#ddd',
  })};
`;

styled-map will then look at the Button's type prop for a matching value.

This also works in mapToTheme:

import styledMap, { mapToTheme as theme } from 'styled-map';

const myTheme = {
  buttonColor: {
    primary: 'orange',
    warning: 'red',
    info: 'blue',
    default: 'white',
  },
  ...
};

const Button = styled.button`
  color: ${theme('buttonColor', 'kind')};
`;

<Button kind='warning'>Click</Button> // will be red

Note: This currently doesn't work doesn't work with the pseudo-CSS syntax. This functionality should arrive by v4.0. PRs welcome!:

Typings

We currently have TypeScript typings in release candidate stage @ 3.2.0-rc.1. Please upgrade specifically to [email protected] if you want typings now.

License

MIT Copyright 2017–2018