• This repository has been archived on 07/Jun/2024
  • Stars
    star
    593
  • Rank 75,443 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

✨A super simple way to map props to styles with Styled Components ⚠️ See README.md

⚠️ As off 2022, I don't recomend using this as part of the foundation for a styling system for anything but small projects.

While this package is simple and lightweight and solves a common problem in a familiar way, I suggest you try styled-system or Shopify's Restyle (especially if you're using React Native) instead — or a component library inspired by (or built with) those tools.

There's a little more work upfront but it makes building performant UIs with React a lot faster and more consistent.

No more work will go into this package as of Febuary 2022. If you'd like an active fork to be linked here please contact me.




A better way to map props to styles

Simple CSS-like syntax, for Styled Components and Emotion


Total downloads GitHub Stars Bundle size MIT License


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.

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 with the pseudo-CSS syntax (new in v3.0). This functionality should arrive by v4.0. PRs welcome!:

Typings

StyledMap has TypeScript typings since version 3.2.0.

License

MIT Copyright 2017–2019

More Repositories

1

lyricist

Genius.com API client with lyrics scraping 🎶🎤👨‍🎤
JavaScript
101
star
2

PreTTI

Improving Text-to-Image Models with Large Language Models
20
star
3

OpenMusicLM

Update: Ignore this repo, check out @lucidrains' implementation https://github.com/lucidrains/musiclm-pytorch
15
star
4

callbaxx

[Satire]🔥 JS utility library to bring classic callback style programming to synchronous code — ES6? More like ES Sucks.
JavaScript
9
star
5

add-graphql-subscriptions

Helper function for use with subscription-transport-ws
JavaScript
6
star
6

banana-riffusion

Python
5
star
7

defuse

1
star
8

scf4

1
star
9

lipx

Image proxy and cache — ⚠️ Archived, do not use
TypeScript
1
star
10

async-styled-components-issue-example

JavaScript
1
star
11

bananaml-serverless-stable-diffusion-v2

test
1
star
12

scottinallca.ps

HTML
1
star
13

redis-dataloader-next

⚠️ Work in progress
1
star
14

misc-notebooks

Misc Colab/Jupyter notebooks
Jupyter Notebook
1
star
15

yikesify

Yikesify.com React App (Don't worry about it)
TypeScript
1
star
16

sonar.js-old

JS client for the private Sonar API [No longer functional]
TypeScript
1
star
17

sonar.js

Client for Sonar's private API (reverse engineered) — this codebase is a hacky mess // No longer functional due to API changes, no further work will be done on this project // Initially created in Jan 2021 for https://github.com/scf4/sonargram
TypeScript
1
star
18

sonargram

Bot to take snapshots of rooms on Sonar and give user a share link // Monorepo, contains bot/web app/api // Created early 2021/code is a mess // No longer functional // Examples: https://twitter.com/bnj/status/1397676636686798848
TypeScript
1
star
19

typescript-node-template

Simple starter template for a Node.js project with TypeScript
JavaScript
1
star