All Projects → scf4 → Styled Map

scf4 / Styled Map

Licence: mit
A super simple way to map props to styles with Styled Components ✨

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Styled Map

Geometry2d
Unity3D: A set of helper classes for 2D geometric calculations.
Stars: ✭ 40 (-93.13%)
Mutual labels:  helper, helpers
Logging Helpers
Basic template helpers for printing messages out to the console. Useful for debugging context in templates. Should work with any template engine.
Stars: ✭ 5 (-99.14%)
Mutual labels:  helper, helpers
React Native Helpers
All helpers in one; iPhone series support, dimensions helper, hasNotch helper, normalize text helper and text helpers for React Native with very easy useEasy to use & awesome helpers for React Native.
Stars: ✭ 31 (-94.67%)
Mutual labels:  helper, helpers
Vivify
Vivify is free CSS animation library.
Stars: ✭ 1,651 (+183.68%)
Mutual labels:  helper, helpers
Handlebars Helpers
Related projects
Stars: ✭ 2,024 (+247.77%)
Mutual labels:  helper, helpers
ideas
Идеи по улучшению языка C++ для обсуждения
Stars: ✭ 65 (-88.83%)
Mutual labels:  helper, helpers
SQLServerTools
This repo is the home of various SQL-Server-Tools
Stars: ✭ 28 (-95.19%)
Mutual labels:  helper, helpers
ComoFazerUmaPerguntaPT
🤔 Farto de fazer perguntas e não ser respondido? Aprenda agora a melhor forma de fazer uma pergunta 🔥
Stars: ✭ 28 (-95.19%)
Mutual labels:  helper, helpers
React Firebase Starter
Boilerplate (seed) project for creating web apps with React.js, GraphQL.js and Relay
Stars: ✭ 4,366 (+650.17%)
Mutual labels:  styled-components
Brian Lovin Next
My personal site
Stars: ✭ 522 (-10.31%)
Mutual labels:  styled-components
Simple Trello
📋
Stars: ✭ 431 (-25.95%)
Mutual labels:  styled-components
Design System Utils
👩‍🎨 Access your design tokens with ease
Stars: ✭ 465 (-20.1%)
Mutual labels:  styled-components
React Redux Saga Boilerplate
Starter kit with react-router, react-helmet, redux, redux-saga and styled-components
Stars: ✭ 535 (-8.08%)
Mutual labels:  styled-components
React95
🌈🕹 Refreshed Windows 95 style UI components for your React app
Stars: ✭ 4,877 (+737.97%)
Mutual labels:  styled-components
Nest Access Control
Role and Attribute based Access Control for Nestjs 🔐
Stars: ✭ 562 (-3.44%)
Mutual labels:  helper
Styled Breakpoints
Simple and powerful tool for creating breakpoints in styled components and emotion. 💅
Stars: ✭ 428 (-26.46%)
Mutual labels:  styled-components
Laravel Helpers
An extensive set of Laravel framework helper functions and collection macros.
Stars: ✭ 407 (-30.07%)
Mutual labels:  helpers
Pup
The Ultimate Boilerplate for Products.
Stars: ✭ 563 (-3.26%)
Mutual labels:  styled-components
Gatsby Portfolio Dev
A portfolio for developers
Stars: ✭ 556 (-4.47%)
Mutual labels:  styled-components
Styled Components Website
The styled-components website and documentation
Stars: ✭ 513 (-11.86%)
Mutual labels:  styled-components




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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].