• This repository has been archived on 25/Nov/2019
  • Stars
    star
    241
  • Rank 167,643 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 5 years ago

Reviews

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

Repository Details

Simple lib that allows you to set styled props in your styled-components without stress.


npm version Greenkeeper badge Build Status Code Coverage downloads styled with prettier

Simple lib that allows you to set styled props in your styled-components without stress. Let's take Button component from styled-components web page. Here it is:

const Button = styled.button`
  font-size: 1em;
  margin: 1em;
  padding: 0.25 em 1em;
  border: 2px solid palevioletred;
  border radius: 3px;

  background: ${props => props.primary && 'palevioletred'}
  color: ${props => props.primary ? 'white' : 'palevioletred'}
`;

Now you can simply write

<Button>Hello</Button> or <Button primary>World!</Button>

But your application is probably much bigger than single button. And you want to keep your colors, sizes etc. in one place. So let's create simple styles.js file.

// styles.js

export const background = {
  primary: '#F5F5F5',
  danger: '#DD2C00',
  success: '#7CB342',
  info: '#BBDEFB',
};

export const color = {
  primary: '#263238',
  default: '#FAFAFA',
};

export const size = {
  padding: {
    small: 10,
    medium: 20,
    big: 30,
  },
  borderRadius: {
    small: 5,
    default: 10,
  },
};

styles.js file is cool because you can access them anywhere! You can also generate some style guides and of course keep all information in one place.

IMPORTANT It is better to use singular forms for keys. In bind mode keys are used to set fallbacks so color is better than colors as a prop.

So how can I help? styled-props package exports single function called styledProps. You can use it in all your components.

TL;DR; Examples

Installation

yarn add styled-props

// or

npm install styled-props

Basic usage

import styledProps from 'styled-props';
import styled from 'styled-components';
import {
  background,
  color,
  size,
} from './styles.js';

const Button = styled.button`
  background: ${styledProps(background)};
  color: ${styledProps(color)};
  padding: ${styledProps(size.padding)}px;
  border-radius: ${styledProps(size.borderRadius)}px;

  font-size: 1em;
  margin: 1em;
  border: 2px solid palevioletred;
`;

export default () => (
  <div>
    <Button primary small>This</Button>
    <Button info medium>is</Button>
    <Button danger big>so</Button>
    <Button success medium>cool!</Button>
  </div>
)

As you can see each prop can be mapped into specific value for selected css rule. If you need another combination, you just add it in styles.js.

Default values

Everything is based on props. As we know in React you can set defaultProps for each component. You can also use them to set default values for styles. For example:

const Button = styled.button`
  color: ${styledProps(color, 'color')}
`;
Button.defaultProps = {
  color: 'default',
};

If you will not provide primary or default property for Button component styledProps function will check value of color property and use it as a key in color map. In our case default color is color.white. This is quite cool because you can also set styles the old way:

<Button color="primary" size="big" />

Bind

When your component is full of dynamic styles you can ( from v0.1.0) use bind options to simplify things.

//styles.js
export default {
  color: {
    red: '#990000',
    white: '#ffffff',
    black: '#000000',
  },
  size: {
    small: 10,
    medium: 20,
    big: 30,
  }
}
import styles from './styles';
import { bindStyles } from 'styled-props';

// or alternatively

// import { bind } from 'styled-props';

s = bindStyles(styles);

export default styled.button`
  color: ${s.color};
  padding: ${s.size};
`;

As you can see bind is available as bind or bindStyles in case you're using lodash or any other library to for e.g bind functions context.

Each key in s provides styledProps function. Also default value is set automaticly with key from map.

s.color === styledProps(styles.color, 'color');

API

styledProps(stylesMap:Object, [fallbackKey]:string)

styledPropsBind(collectionsMap:Object)

More Repositories

1

colors-show

Present your application colors with style.
JavaScript
26
star
2

just-box

Just-Box helps you create universal layouts in your React and React-Native apps.
JavaScript
21
star
3

JavascriptRoutingServiceProvider

Access your Silex routes inside JavaScript.
PHP
16
star
4

jsxfromhtml

Universal HTML to JSX compiler.
JavaScript
13
star
5

ProfilerServiceProvider

Profiler Extension for Silex
PHP
11
star
6

ActiveRecordExtension

PHP-ActiveRecord Extension for Silex
PHP
8
star
7

MemcacheServiceProvider

Memcache extension for Silex
PHP
7
star
8

Spriter

Sprite Generator
PHP
6
star
9

trololo

Simple project wall for GitLab
JavaScript
5
star
10

Bookmarks-Manager

Simple bookmarks manager.
CoffeeScript
4
star
11

react-dates-range-picker

Small, stateless date / date range picker based on sweet DayPicker from react-dates by @airbnb.
JavaScript
4
star
12

nomad-ui

Command line nomad explorer.
JavaScript
3
star
13

lorem-graphql

Simple GraphQL example. Grab informations about single paragraph of lorem ipsum text.
JavaScript
2
star
14

sandbox

An empty boilerplate template to start a fresh project
1
star
15

remix-postgres-email-client

Remix version of the Next.js Email Client
TypeScript
1
star
16

rest-fsm

TypeScript
1
star
17

BuggyFetch

JavaScript
1
star
18

tailwind-o

TypeScript
1
star
19

openapi-union

TypeScript
1
star
20

next-spring-transitions

JavaScript
1
star
21

vanilla-o

TypeScript
1
star
22

notes

1
star
23

jobs

JavaScript
1
star
24

stitches-ts

JavaScript
1
star
25

stitches-pnpm-typescript

TypeScript
1
star
26

menu

TypeScript
1
star
27

nx-css-prop-bug

CSS
1
star
28

asdf

JavaScript
1
star
29

caddy-multizone

JavaScript
1
star
30

vanilla-o2

TypeScript
1
star
31

next-dynamic-routing-get-server-side-props

JavaScript
1
star
32

preconstruct-bug

TypeScript
1
star
33

act-bug

TypeScript
1
star
34

editor

Created with CodeSandbox
TypeScript
1
star
35

xstate-react-typestates-bug

TypeScript
1
star
36

next-auth-bug

TypeScript
1
star
37

rush-playground

JavaScript
1
star
38

jquery.tree

Tree plugin for jQuery
JavaScript
1
star
39

xstate-typestates-bug

TypeScript
1
star
40

wtf-faces

JavaScript
1
star
41

rafalfilipek.github.com

rafalfilipek.github.com
CSS
1
star