All Projects → RafalFilipek → Styled Props

RafalFilipek / Styled Props

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Styled Props

Next Dark Mode
🌑 Enable dark mode for Next.js apps
Stars: ✭ 133 (-46.15%)
Mutual labels:  styled-components, css-in-js
Awesome Styled Components
A curated list of awesome styled-components resources 💅
Stars: ✭ 2,869 (+1061.54%)
Mutual labels:  styled-components, css-in-js
Goober
🥜 goober, a less than 1KB 🎉 css-in-js alternative with a familiar API
Stars: ✭ 2,317 (+838.06%)
Mutual labels:  styled-components, css-in-js
Xstyled
A utility-first CSS-in-JS framework built for React. 💅👩‍🎤⚡️
Stars: ✭ 1,835 (+642.91%)
Mutual labels:  styled-components, css-in-js
Css In Js
Autocomplete React Native / JS Styles and converting plain CSS to JS styles
Stars: ✭ 192 (-22.27%)
Mutual labels:  styled-components, css-in-js
Scoped Style
A tiny css in js library 🚀
Stars: ✭ 129 (-47.77%)
Mutual labels:  styled-components, css-in-js
Gatsby Theme Superstylin
💅 A Gatsby Theme with styled-components
Stars: ✭ 165 (-33.2%)
Mutual labels:  styled-components, css-in-js
Css In React
🍭 CSS in React - Learn the best CSS in JS frameworks by example
Stars: ✭ 101 (-59.11%)
Mutual labels:  styled-components, css-in-js
Styled Theme
Extensible theming system for styled-components 💅
Stars: ✭ 183 (-25.91%)
Mutual labels:  styled-components, css-in-js
Grid
This package has moved and renamed
Stars: ✭ 2,079 (+741.7%)
Mutual labels:  styled-components, css-in-js
Styled By
Simple and powerful lib to handle styled props in your components
Stars: ✭ 122 (-50.61%)
Mutual labels:  styled-components, css-in-js
Styled Email Components
💌 styled-components for emails
Stars: ✭ 231 (-6.48%)
Mutual labels:  styled-components, css-in-js
Styled Typography
Typograpy components for react and styled-components
Stars: ✭ 113 (-54.25%)
Mutual labels:  styled-components, css-in-js
React Next Boilerplate
🚀 A basis for reducing the configuration of your projects with nextJS, best development practices and popular libraries in the developer community.
Stars: ✭ 129 (-47.77%)
Mutual labels:  styled-components, css-in-js
Nanostyled
A <1kB library for styling React components as if with CSS-in-JS, without CSS-in-JS
Stars: ✭ 104 (-57.89%)
Mutual labels:  styled-components, css-in-js
Styled Bootstrap
💅🏻 A styled-component implementation of Bootstrap
Stars: ✭ 154 (-37.65%)
Mutual labels:  styled-components, css-in-js
React Image Smooth Loading
[not maintained] Images which just flick to appear aren't cool. Images which appear smoothly with a fade like Instagram are cool
Stars: ✭ 84 (-65.99%)
Mutual labels:  styled-components, css-in-js
Onno
Responsive style props for building themed design systems
Stars: ✭ 95 (-61.54%)
Mutual labels:  styled-components, css-in-js
Filbert Js
A lightweight(~1kb) css-in-js framework
Stars: ✭ 167 (-32.39%)
Mutual labels:  styled-components, css-in-js
Styled Components Vs Emotion
a short doc comparing the popular CSS-in-JS libraries styled-components and emotion
Stars: ✭ 204 (-17.41%)
Mutual labels:  styled-components, css-in-js


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)
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].