• Stars
    star
    306
  • Rank 136,456 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 5 years ago

Reviews

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

Repository Details

Defines themes via flexible color selectors for use with styled-components

styled-components-theme


NPM Version NPM Downloads Build Status codecov.io styled with prettier

styled-components-theme generates selectors for colors in your styled-components theme that allows color manipulation, using the color library via calls on the selectors themselves.

A selector, in this context, is defined as a function that looks like (props) => props.theme.myColor that the styled-components library accepts as a template variable.


Why?

styled-components is an awesome library, and their React context-based theming scheme is great, but:

  1. Having ${(props) => props.theme.highlight} functions all over your template literals to use any of your theme colors is both hard to read and cumbersome to type.

  2. In migrating from SASS and CSS Modules, I missed the ability to lighten() or darken() or transparentize() a theme color at will to make subtle gradients or overlays.


Installation

Using npm:

  $ npm install --save styled-components-theme

Using yarn:

  $ yarn add styled-components-theme

Usage

1) Define your theme colors

// colors.js

const colors = {
  main:  '#393276',
  dark:  '#0D083B',
  light: '#837EB1'
}

export default colors

2) Apply your theme with ThemeProvider

import { ThemeProvider } from 'styled-components';
import colors from './colors' // from Step #1

const App = props =>
  <ThemeProvider theme={colors}>
    {props.children}
  </ThemeProvider>

3) Create an importable theme object using styled-components-theme

// theme.js

import createTheme from 'styled-components-theme';
import colors from './colors' // from Step #1

const theme = createTheme(...Object.keys(colors))
export default theme

4) Use the theme colors in your components

import styled from 'styled-components'
import theme from './theme' // from Step #3

const Header = styled.div`
  background: ${theme.dark};
  color: ${theme.light};
`

const Button = styled.button`
  background-image: linear-gradient(${theme.light}, ${theme.light.darken(0.3)});
  color: ${theme.dark};
  padding: 10px;
`

Available manipulation functions

This library uses the color manipulation provided by the color library.

theme.color.negate()         // rgb(0, 100, 255) -> rgb(255, 155, 0)

theme.color.lighten(0.5)     // hsl(100, 50%, 50%) -> hsl(100, 50%, 75%)
theme.color.darken(0.5)      // hsl(100, 50%, 50%) -> hsl(100, 50%, 25%)

theme.color.saturate(0.5)    // hsl(100, 50%, 50%) -> hsl(100, 75%, 50%)
theme.color.desaturate(0.5)  // hsl(100, 50%, 50%) -> hsl(100, 25%, 50%)
theme.color.greyscale()      // #5CBF54 -> #969696

theme.color.whiten(0.5)      // hwb(100, 50%, 50%) -> hwb(100, 75%, 50%)
theme.color.blacken(0.5)     // hwb(100, 50%, 50%) -> hwb(100, 50%, 75%)

theme.color.fade(0.5)        // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 0.4)
theme.color.opaquer(0.5)     // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 1.0)

theme.color.rotate(180)      // hsl(60, 20%, 20%) -> hsl(240, 20%, 20%)
theme.color.rotate(-90)      // hsl(60, 20%, 20%) -> hsl(330, 20%, 20%)

theme.color.mix(theme.otherColor, 0.2) // rgb(0, 0, 255) * 0.8 + rgb(0, 255, 0) * 0.2 -> rgb(0, 51, 204)

FAQ

Why use color? Why not [other color manipulation library]?

Because color's manipulation methods were so influenced by SASS, LESS and Stylus, they are already familiar to CSS coders.

Isn't Color mutable? Don't I need to clone()?

Yes, Color is mutable, but this library handles the cloning for you, so you can chain the manipulation methods together to your heart's content without mutating the original theme color. e.g. theme.primary.saturate(0.3).lighten(0.2).fade(0.4).

The manipulation methods in styled-components-theme are immutable.


Made with ❀️ in πŸ‡ͺπŸ‡Έ by @erikras.

More Repositories

1

react-redux-universal-hot-example

A starter boilerplate for a universal webapp using express, react, redux, webpack, and react-transform
JavaScript
12,008
star
2

ducks-modular-redux

A proposal for bundling reducers, action types and actions when using Redux
JavaScript
9,586
star
3

redux-form-material-ui

A set of wrapper components to facilitate using Material UI with Redux Form
JavaScript
831
star
4

multireducer

A utility to wrap many copies of a single Redux reducer into a single key-based reducer.
JavaScript
423
star
5

lru-memoize

A utility to provide LRU memoization for any js function
JavaScript
317
star
6

react-native-listener

A utility component to allow easy access to browser native events
JavaScript
136
star
7

remix-conf-2022

XState on the backend!
CSS
107
star
8

react-pdfjs

A React component to wrap PDF.js
JavaScript
81
star
9

react-redux-promise-listener

A React component and Redux middleware that allows actions to be converted into Promises
JavaScript
71
star
10

audiocard

⏯️ AudioCard - Opinionated, responsive, audio player compatible with Twitter Cards
TypeScript
66
star
11

map-props

A higher order React component and allows mapping prop values passed in to other prop values expected by the wrapped component
JavaScript
45
star
12

react-lazy-cache

A utility to lazily calculate and cache values in a react component based on props
JavaScript
45
star
13

redux-promise-listener

A Redux middleware that allows actions to be converted into Promises
JavaScript
44
star
14

reactalicante2018

Code from my talk at React Alicante 2018
CSS
29
star
15

redux-spy

A higher order component decorator to read from a Redux store without subscribing to all its changes
JavaScript
29
star
16

reactalicante2017

Code used during my Redux Form talk at React Alicante 2017
CSS
26
star
17

react-callbag-listener

πŸ‘‚ A React render-prop component that listens to values emitted by callbags
JavaScript
21
star
18

tic-tac-toe-finite-state-machine

An exercise to build a tic-tac-toe finite state machine in typescript and xstate
TypeScript
17
star
19

callbag-pausable

⏯️ Callbag Pausable is a callbag that will convert any callbag stream into one that can be paused and resumed via data or talkback events.
JavaScript
10
star
20

react-thug

Silly React "Thug Life" meme image
10
star
21

postmessage-client-server

A simple promise-based client and server to communicate between pages and iframes with postmessage.
JavaScript
9
star
22

happyhourfm

Happy Hour with Dennis and Erik
TypeScript
6
star
23

expect-predicate

An extension for expect that lets you test values against arbitrary predicates
JavaScript
6
star
24

reactathon2020

Modern Forms in React workshop for Reactathon 2020
JavaScript
5
star
25

potionocean

TypeScript
5
star
26

javascript-flickr-badge

Javascript Flickr Badge
4
star
27

lemoncoders-20180502

Material de una clase he dado para Lemon Coders
JavaScript
4
star
28

seekjusticefm

Seek Justice Podcast
TypeScript
4
star
29

primatrix

If x is prime, is x + 30n prime?
TypeScript
3
star
30

callbag-types

Types for πŸ‘œ Callbags
JavaScript
3
star
31

reactfinland2020

JavaScript
2
star
32

typescript-react-apollo-example

Just demonstrating a problem... move along...
1
star
33

datocms-virtual-event-starter-kit-demo-8411

TypeScript
1
star
34

react-hot-loader-problem

A demonstration of a problem (bug?) in react-hot-loader
JavaScript
1
star
35

rescript-gentype-bug

Reproduction of a problem with Rescript's genType
TypeScript
1
star