• Stars
    star
    183
  • Rank 203,761 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 6 years ago

Reviews

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

Repository Details

Extensible theming system for styled-components ๐Ÿ’…

styled-theme ๐Ÿ’…๐Ÿฟ

Generated with nod NPM version Build Status Coverage Status

Theming system for styled-components ๐Ÿ’…

Install

$ npm install --save styled-theme

Usage

Play with it on WebpackBin

import styled from 'styled-components'
import { font, palette } from 'styled-theme' 

const Text = styled.span`
  font-family: ${font('primary')};
  background-color: ${palette(1)};
  color: ${palette('grayscale', 0, true)};
`

Text.defaultProps = {
  palette: 'primary'
}
<Text>Hello</Text>

image

<Text reverse>Hello</Text>

image

<Text palette="secondary">Hello</Text>

image

Provide your own theme

import { ThemeProvider } from 'styled-components'

const xmasTheme = {
  fonts: {
    primary: 'Georgia, serif'
  },
  palette: {
    // red gradient
    primary: ['#D32F2F', '#F44336', '#F8877F', '#FFCDD2']
  }
}

<ThemeProvider theme={xmasTheme}>
  <Text>Hello</Text>
</ThemeProvider>

image

Default theme structure

This is the content of src/theme.js:

import coolorsToHex from 'coolors-to-hex'
import { reversePalette } from './composer'

const theme = {}

theme.palette = {
  primary: coolorsToHex('https://coolors.co/1976d2-2196f3-71bcf7-97cef9-c2e2fb'),
  secondary: coolorsToHex('https://coolors.co/c2185b-e91e63-f06292-f48caf-f8bbd0'),
  danger: coolorsToHex('https://coolors.co/d32f2f-f44336-f8877f-f9a7a1-ffcdd2'),
  alert: coolorsToHex('https://coolors.co/ffa000-ffc107-ffd761-ffecb3-fff2ce'),
  success: coolorsToHex('https://coolors.co/388e3c-4caf50-7cc47f-9fd4a1-c8e6c9'),
  grayscale: ['#212121', '#616161', '#9e9e9e', '#bdbdbd', '#e0e0e0', '#ffffff']
}

theme.reversePalette = reversePalette(theme.palette)

theme.fonts = {
  primary: 'Helvetica Neue, Helvetica, Roboto, sans-serif',
  pre: 'Consolas, Liberation Mono, Menlo, Courier, monospace',
  quote: 'Georgia, serif'
}

theme.sizes = {
  maxWidth: '1100px'
}

export default theme

reversePalette is a helper method. Import it from styled-theme/composer.

API

reversePalette

Revert the palette

Parameters

Examples

reversePalette({ primary: ['red', 'yellow', 'green'] })
// { primary: ['green', 'yellow', 'red'] }

Returns Palette

key

Returns the value of props.theme[path] or styledTheme[path]

Parameters

Examples

const Button = styled.button`
 font-family: ${key('fonts.primary')};
 color: ${key(['colors', 'primary', 0])};
`

Returns any

font

Shorthand to key(['fonts', path])

Parameters

  • path string
  • defaultValue any

Examples

const Button = styled.button`
 font-family: ${font('primary')};
`

Returns Font

size

Shorthand to key(['sizes', path])

Parameters

  • path string
  • defaultValue any

Examples

const Button = styled.button`
 padding: ${size('padding')};
`

Returns Size

palette

Returns the value of props.theme[palette || reversePalette][path][index] or styledTheme[palette || reversePalette][path][index] (default theme)

The arguments can be passed in any order, as long as types are kept.

Parameters

  • index number The index of tone in theme palette tones array
  • path string? The key of the tones in theme palette object (optional, default props.palette)
  • exceptions Object? An object with path as key and index as value
  • reverse boolean? Flag to return tone from reversePalette or palette
  • defaultValue string? Default value
  • args ...any

Examples

// index = 1
// exception = { grayscale: 0 }
// reverse = true
const Button = styled.button`
 background-color: ${palette({ grayscale: 0 }, 1, true)};
`

// renders props.theme.reversePalette.grayscale[0]
<Button palette="grayscale" />

// renders props.theme.palette.danger[1] (nullify reverse)
<Button palette="danger" reverse />

Returns Tones

Tone

Type: string

Tones

Type: Array<Tone>

Font

Type: string

Size

Type: string

Palette

Type: {}

Fonts

Type: {}

Sizes

Type: {}

Theme

Type: {palette: Palette?, reversePalette: Palette?, fonts: Fonts?, sizes: Sizes?}

Related

  • styled-tools - Utilities for styled-components (like lodash)

License

MIT ยฉ Diego Haz

More Repositories

1

constate

React Context + State
TypeScript
3,916
star
2

arc

React starter kit based on Atomic Design
JavaScript
2,910
star
3

rest

REST API generator with Node.js, Express and Mongoose
JavaScript
1,778
star
4

generact

Generate React components by replicating your own
JavaScript
1,481
star
5

awesome-react-context

๐Ÿ˜Ž A curated list of stuff related to the new React Context API
JavaScript
915
star
6

styled-tools

Useful interpolated functions for CSS-in-JS
JavaScript
805
star
7

schm

Composable schemas for JavaScript and Node.js
JavaScript
513
star
8

reuse

โ™ป๏ธ Reuse React components to create new ones
TypeScript
496
star
9

singel

Single Element Pattern
JavaScript
409
star
10

nod

Node.js module generator/boilerplate with Babel, Jest, Flow, Documentation and more
JavaScript
360
star
11

redux-saga-thunk

Dispatching an action handled by redux-saga returns promise
JavaScript
221
star
12

querymen

Querystring parser middleware for MongoDB, Express and Nodejs (MEN)
JavaScript
129
star
13

parse-prop-types

Parses React prop-types into a readable object
JavaScript
67
star
14

bodymen

Body parser middleware for MongoDB, Express and Nodejs (MEN)
JavaScript
48
star
15

redux-saga-social-login

Facebook/Google login implementation with redux-saga
JavaScript
25
star
16

list-react-files

List React component files inside a directory
JavaScript
24
star
17

mongoose-keywords

Mongoose plugin that generates a keywords path combining other paths values
JavaScript
22
star
18

redux-modules

A modular approach to better organize redux stuff (not another library)
JavaScript
21
star
19

generator-rest-example

A fully commented RESTful API example generated with generator-rest
JavaScript
18
star
20

styled-selector

Get static CSS(-in-JS) selectors from React components
TypeScript
17
star
21

coolors-to-hex

Get hexadecimal values from a coolors url
JavaScript
16
star
22

webpack-blocks-split-vendor

A webpack block that splits vendor javascript into separated bundle
JavaScript
15
star
23

webpack-blocks-happypack

A webpack block that adds happypack support to your webpack config
JavaScript
12
star
24

redux-form-submit

Adds an async submit action creator to redux-form
JavaScript
9
star
25

webpack-spawn-plugin

A webpack plugin that runs child_process.spawn within compilation
JavaScript
7
star
26

waterfall-grid

A Polymer wrapper element for waterfall.js, a 1KB Javascript library for Pinterest-like grids.
HTML
7
star
27

webpack-blocks-server-source-map

A webpack block that adds source map support to server bundle
JavaScript
5
star
28

yo

A Yeoman generator for generating next generation of Yeoman generators
JavaScript
5
star
29

webpack-assets-by-type-plugin

A webpack plugin that save assets by type
JavaScript
5
star
30

arc-universal-redux

Universal Redux version of ARc boilerplate
JavaScript
5
star
31

webpack-sort-chunks

Sorts webpack chunks by dependency
JavaScript
4
star
32

webpack-child-config-plugin

A webpack plugin that runs/watches another config
JavaScript
4
star
33

rest-api

REST API Boilerplate with Mongoose, Express and Nodejs
JavaScript
4
star
34

rich-param

An object with name and value which accepts pluggable methods as formatters or validators
JavaScript
3
star
35

reakit-code-lab

TypeScript
3
star
36

arc-fullstack

Fullstack version of ARc boilerplate
JavaScript
3
star
37

mongoose-create-unique

Mongoose plugin to create a document or return the existing one based on the unique index
JavaScript
3
star
38

divisa

A game about war and peace.
JavaScript
2
star
39

eyeport

Collection of useful methods to deal with element and viewports in DOM
JavaScript
2
star
40

alpha-bank

JavaScript
2
star
41

guild

Just a social network prototype made with jQuery
HTML
2
star
42

is-git-rev

Verify if a string is a git-rev hash
JavaScript
2
star
43

clone

JavaScript
1
star
44

radiotoca

A PHP web radio with Twitter integration
PHP
1
star
45

adjacente

Blog para disciplina Expansรฃo dos Sentidos
JavaScript
1
star
46

diegohaz

1
star
47

hear-parse

JavaScript
1
star
48

hear-api

JavaScript
1
star
49

diegohaz.com

JavaScript
1
star